Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Relia committed Jun 24, 2020
1 parent 32a8e31 commit 87d4cd7
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 62 deletions.
60 changes: 40 additions & 20 deletions apple.go → apple/apple.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package auth
package appleauth

import (
"time"

"github.com/dgrijalva/jwt-go"
"github.com/mitchellh/mapstructure"
auth "github.com/thaitanloi365/go-social-auth"
)

// AppleLoginResponse response
type AppleLoginResponse struct {
// TokenResponse response
type TokenResponse struct {
Aud string `json:"aud"`
AuthTime int64 `json:"auth_time"`
CHash string `json:"c_hash"`
Expand All @@ -21,24 +22,42 @@ type AppleLoginResponse struct {
Sub string `json:"sub"`
}

// AppleLogin provider
type AppleLogin struct {
// Config config
type Config struct {
Iss string `json:"iss"`
Aud string `json:"aud"`
SkipExpiry bool `json:"skip_expiry"`
}

// NewAppleLogin new
func NewAppleLogin() *AppleLogin {
return &AppleLogin{
// New new
func New() *Config {
return &Config{
Iss: "https://appleid.apple.com",
Aud: "",
}
}

// WithIssuer override issuer
func (c *Config) WithIssuer(iss string) *Config {
c.Iss = iss
return c
}

// WithAudience override audience
func (c *Config) WithAudience(aud string) *Config {
c.Aud = aud
return c
}

// WithExpiry override expiry
func (c *Config) WithExpiry(skipExpiry bool) *Config {
c.SkipExpiry = skipExpiry
return c
}

// Login login
func (a *AppleLogin) Login(token string) (*AppleLoginResponse, error) {
var result AppleLoginResponse
func (c *Config) Login(token string) (*TokenResponse, error) {
var result TokenResponse
var claims = jwt.MapClaims{}
_, err := jwt.ParseWithClaims(token, claims, nil)
if err != nil {
Expand All @@ -55,8 +74,9 @@ func (a *AppleLogin) Login(token string) (*AppleLoginResponse, error) {
}

decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
TagName: "json",
Result: &result,
TagName: "json",
Result: &result,
WeaklyTypedInput: true,
})
if err != nil {
return nil, err
Expand All @@ -67,21 +87,21 @@ func (a *AppleLogin) Login(token string) (*AppleLoginResponse, error) {
return nil, err
}

if a.Iss != "" {
if result.Iss != a.Iss {
return nil, ErrIssuerInvalid
if c.Iss != "" {
if result.Iss != c.Iss {
return nil, auth.ErrIssuerInvalid
}
}

if a.Aud != "" {
if result.Aud != a.Aud {
return nil, ErrAudienceInvalid
if c.Aud != "" {
if result.Aud != c.Aud {
return nil, auth.ErrAudienceInvalid
}
}

if !a.SkipExpiry {
if !c.SkipExpiry {
if result.Exp < time.Now().Unix() {
return nil, ErrTokenExpired
return nil, auth.ErrTokenExpired
}
}

Expand Down
18 changes: 18 additions & 0 deletions apple/apple_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package appleauth

import (
"fmt"
"testing"
)

func TestLoginApple(t *testing.T) {
var token = "eyJraWQiOiJlWGF1bm1MIiwiYWxnIjoiUlMyNTYifQ.eyJpc3MiOiJodHRwczovL2FwcGxlaWQuYXBwbGUuY29tIiwiYXVkIjoiY29tLm5vcnRoZ2F0ZS5oYXBsby5kZXYiLCJleHAiOjE1ODk0MjQ0NDMsImlhdCI6MTU4OTQyMzg0Mywic3ViIjoiMDAwMjQ2LjNkZjBhYWQ5ZTc2NzQwOWFiM2Q1YjhiN2JmMThkODlmLjA5NTYiLCJjX2hhc2giOiJHSkltY3ZBSXFOTG1FQnpTRlhYYWJ3IiwiZW1haWwiOiJodXlsYWlwaHVvY0BnbWFpbC5jb20iLCJlbWFpbF92ZXJpZmllZCI6InRydWUiLCJhdXRoX3RpbWUiOjE1ODk0MjM4NDMsIm5vbmNlX3N1cHBvcnRlZCI6dHJ1ZX0.BydXOomLMe36nc7ENZMErQGmsc9R_1_ZqKWdgjoGXAAZB0CgTvT48naJLhawJC_N183Q2BHlukMakiJ_46S7iEnJID7UntAKhl3lJs0Rf2_etGZIZtOxl7eMaeM3Ey2h5FEPslIEKFY2ybjuhMut-csfxsmpo9FVluHi5qDAL6ld7xRCAyFKvRmoK-YkHD3_-uTihJffaqK0dc6R33UK7BG_RbkvzvmTvZKejg4OSG4MxVAPoyhPqKf7uW0Q_FVPUgCbqp2S_Tp81FpWkYnwz8PAm9WzxBRSWxfswvfjzNHCxyw3uZJEgKpAMJvh9K1WfFZvm1QCR_IrpPRrwzwTqw"
var appleauth = New().WithExpiry(true)
result, err := appleauth.Login(token)
if err != nil {
panic(err)
}

fmt.Println("result", result)

}
47 changes: 30 additions & 17 deletions facebook.go → facebook/facebook.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package auth
package facebookauth

import (
"encoding/json"
Expand All @@ -9,45 +9,58 @@ import (
"strings"

"github.com/mitchellh/mapstructure"
auth "github.com/thaitanloi365/go-social-auth"
)

// FacebookLoginResponse response
type FacebookLoginResponse struct {
// TokenResponse response
type TokenResponse struct {
ID string `json:"id"`
Email string `json:"email"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Name string `json:"name"`
}

// FacebookLogin provider
type FacebookLogin struct {
Scope []string `json:"scope"`
URL string `json:"url"`
// Config config
type Config struct {
Scopes []string `json:"scopes"`
URL string `json:"url"`
}

// NewFacebookLogin new
func NewFacebookLogin() *FacebookLogin {
return &FacebookLogin{
URL: "https://graph.facebook.com",
Scope: []string{"id", "email", "first_name", "last_name", "name"},
// New new
func New() *Config {
return &Config{
URL: "https://graph.facebook.com",
Scopes: []string{"id", "email", "first_name", "last_name", "name"},
}
}

// WithURL override url
func (c *Config) WithURL(url string) *Config {
c.URL = url
return c
}

// WithScopes override scopes
func (c *Config) WithScopes(scopes []string) *Config {
c.Scopes = scopes
return c
}

func isValidFacebookToken(accessToken string) bool {
// TODO fixed me https://developers.facebook.com/tools/explorer/?method=GET&path=debug_token%3Finput_token%3D%257Binput-token%257D&version=v6.0

return true
}

// Login login
func (f *FacebookLogin) Login(accessToken string) (*FacebookLoginResponse, error) {
var result FacebookLoginResponse
func (c *Config) Login(accessToken string) (*TokenResponse, error) {
var result TokenResponse
if !isValidFacebookToken(accessToken) {
return nil, ErrTokenInvalid
return nil, auth.ErrTokenInvalid
}
var scope = strings.Join(f.Scope, ",")
var url = fmt.Sprintf("%s/me?fields=%s&access_token=%s", scope, f.URL, url.QueryEscape(accessToken))
var scopes = strings.Join(c.Scopes, ",")
var url = fmt.Sprintf("%s/me?fields=%s&access_token=%s", c.URL, scopes, url.QueryEscape(accessToken))
resp, err := http.Get(url)
if err != nil {
return nil, err
Expand Down
17 changes: 17 additions & 0 deletions facebook/facebook_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package facebookauth

import (
"fmt"
"testing"
)

func TestLoginFacebook(t *testing.T) {
var token = "EAACejEGlyekBAKoV7PZAIo3qj8e7VcQZCihlW0E9GnjlioIcIt8vEnAb9KNJcpi8zSCZA0H42ZAa7YvGz3ctgqMj78fZARYCBiPikLZBZCsAiGakXA2iXUT8JPVIoGlI0ot2fWlgk6ZAxzbZB1j5qWNden5pwN27GyHwh5XSizffDxVAl1PZCZAVfquLE6MfGcs2iP0ZC5UbNumE7NM50obI0HkPQS90pt0LYSdbsKf2qtBIZBAZDZD"
var facebookauth = New()
result, err := facebookauth.Login(token)
if err != nil {
panic(err)
}

fmt.Println("result", result)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ go 1.14

require (
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/mitchellh/mapstructure v1.3.1
github.com/mitchellh/mapstructure v1.3.2
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumC
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/mitchellh/mapstructure v1.3.1 h1:cCBH2gTD2K0OtLlv/Y5H01VQCqmlDxz30kS5Y5bqfLA=
github.com/mitchellh/mapstructure v1.3.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.3.2 h1:mRS76wmkOn3KkKAyXDu42V+6ebnXWIztFSYGN7GeoRg=
github.com/mitchellh/mapstructure v1.3.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
57 changes: 38 additions & 19 deletions firebase.go → google/google.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package auth
package googleauth

import (
"time"

"github.com/dgrijalva/jwt-go"
"github.com/mitchellh/mapstructure"
auth "github.com/thaitanloi365/go-social-auth"
)

// SignInProvider provider
type SignInProvider string

// Providers
var (
PhoneSignInProvider SignInProvider = "phone"
EmailSignInProvider SignInProvider = "email"
Phone SignInProvider = "phone"
Email SignInProvider = "email"
)

// FirebaseLoginResponse response
type FirebaseLoginResponse struct {
// TokenResponse response
type TokenResponse struct {
Aud string `json:"aud"`
AuthTime int64 `json:"auth_time"`
CHash string `json:"c_hash"`
Expand All @@ -33,7 +34,7 @@ type FirebaseLoginResponse struct {
UserID string `json:"user_id"`
}

type firebaseLoginResponse struct {
type tokenResponse struct {
Iss string `json:"iss"`
Aud string `json:"aud"`
AuthTime int64 `json:"auth_time"`
Expand All @@ -54,24 +55,42 @@ type firebaseLoginResponse struct {
} `json:"firebase"`
}

// FirebaseLogin provider
type FirebaseLogin struct {
// Config provider
type Config struct {
Iss string `json:"iss"`
Aud string `json:"aud"`
SkipExpiry bool `json:"skip_expiry"`
}

// NewFirebaseLogin new
func NewFirebaseLogin() *FirebaseLogin {
return &FirebaseLogin{
// New new
func New() *Config {
return &Config{
Iss: "https://accounts.google.com",
Aud: "",
}
}

// WithIssuer override issuer
func (c *Config) WithIssuer(iss string) *Config {
c.Iss = iss
return c
}

// WithAudience override audience
func (c *Config) WithAudience(aud string) *Config {
c.Aud = aud
return c
}

// WithExpiry override expiry
func (c *Config) WithExpiry(skipExpiry bool) *Config {
c.SkipExpiry = skipExpiry
return c
}

// Login login
func (a *FirebaseLogin) Login(token string) (*FirebaseLoginResponse, error) {
var result firebaseLoginResponse
func (c *Config) Login(token string) (*TokenResponse, error) {
var result tokenResponse
var claims = jwt.MapClaims{}
_, err := jwt.ParseWithClaims(token, claims, nil)
if err != nil {
Expand Down Expand Up @@ -102,17 +121,17 @@ func (a *FirebaseLogin) Login(token string) (*FirebaseLoginResponse, error) {

if a.Iss != "" {
if result.Iss != a.Iss {
return nil, ErrIssuerInvalid
return nil, auth.ErrIssuerInvalid
}
}

if a.Aud != "" {
if result.Aud != a.Aud {
return nil, ErrAudienceInvalid
return nil, auth.ErrAudienceInvalid
}
}

var response = FirebaseLoginResponse{
var response = TokenResponse{
Aud: result.Aud,
Sub: result.Sub,
Iss: result.Iss,
Expand All @@ -123,17 +142,17 @@ func (a *FirebaseLogin) Login(token string) (*FirebaseLoginResponse, error) {
EmailVerified: result.EmailVerified,
Exp: result.Exp, Iat: result.Iat, NonceSupported: result.NonceSupported,
UserID: result.UserID,
SignInProvider: EmailSignInProvider,
SignInProvider: Email,
}

if result.Firebase.SignInProvider == "phone" {
response.SignInProvider = PhoneSignInProvider
response.SignInProvider = Phone

}

if !a.SkipExpiry {
if response.Exp < time.Now().Unix() {
return nil, ErrTokenExpired
return nil, auth.ErrTokenExpired
}
}

Expand Down
9 changes: 4 additions & 5 deletions firebase_test.go → google/google_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package auth
package googleauth

import (
"fmt"
"testing"
)

func TestLoginFirebase(t *testing.T) {
func TestLoginGoogle(t *testing.T) {
var token = "eyJhbGciOiJSUzI1NiIsImtpZCI6IjgyMmM1NDk4YTcwYjc0MjQ5NzI2ZDhmYjYxODlkZWI3NGMzNWM4MGEiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL3NlY3VyZXRva2VuLmdvb2dsZS5jb20vbm90aWZpY2F0aW9uLWRlbW8tMjVjMTEiLCJhdWQiOiJub3RpZmljYXRpb24tZGVtby0yNWMxMSIsImF1dGhfdGltZSI6MTU5MDQyODAzMiwidXNlcl9pZCI6ImdidWZ6eHdTOTBYOHpsS2FmeWtQWnlxZU1BaDEiLCJzdWIiOiJnYnVmenh3UzkwWDh6bEthZnlrUFp5cWVNQWgxIiwiaWF0IjoxNTkwNDI4MDMyLCJleHAiOjE1OTA0MzE2MzIsInBob25lX251bWJlciI6Iis4NDM3ODMwNjg5MyIsImZpcmViYXNlIjp7ImlkZW50aXRpZXMiOnsicGhvbmUiOlsiKzg0Mzc4MzA2ODkzIl19LCJzaWduX2luX3Byb3ZpZGVyIjoicGhvbmUifX0.YnB0CruLFP5ZhLVI_FqoewI_7HjO6UZuy8bO1KR2aSzbYArFUZKY_o3_hw2Za6MVjprW8nOFWGmk0wWyQW-7SZ1OUn1QNyWideDnwxQ4blaNIO_84gdgmpwdfx99n-hNRG1fvNAr3tVPFiMa5rSwopIfmJzCBP9j-aoZbqumw3HJwtDxNvZvha_Gj_Nu97Jk0Y87oJelD6AFiHVXJzlZ3qStluMR1gujzdo8jrf4eGOWW3H9Vvp_jZlYh6iQ8ddvnj-muP_kCjCZbRwo0bMKQvzVqvNpSqf0ewaHu1pgRYhUNTo8MAR4wt2wJcecEiauXSZru2CFCqICG_1QpdFiSg"
var firebaseAuth = NewFirebaseLogin()
firebaseAuth.Iss = ""
result, err := firebaseAuth.Login(token)
var googleauth = New().WithIssuer("").WithExpiry(true)
result, err := googleauth.Login(token)
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 87d4cd7

Please sign in to comment.