diff --git a/apple.go b/apple/apple.go similarity index 55% rename from apple.go rename to apple/apple.go index 493d8cc..0de4f16 100644 --- a/apple.go +++ b/apple/apple.go @@ -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"` @@ -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 { @@ -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 @@ -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 } } diff --git a/apple/apple_test.go b/apple/apple_test.go new file mode 100644 index 0000000..29ac0fc --- /dev/null +++ b/apple/apple_test.go @@ -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) + +} diff --git a/facebook.go b/facebook/facebook.go similarity index 58% rename from facebook.go rename to facebook/facebook.go index 323e251..ac7b628 100644 --- a/facebook.go +++ b/facebook/facebook.go @@ -1,4 +1,4 @@ -package auth +package facebookauth import ( "encoding/json" @@ -9,10 +9,11 @@ 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"` @@ -20,20 +21,32 @@ type FacebookLoginResponse struct { 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 @@ -41,13 +54,13 @@ func isValidFacebookToken(accessToken string) bool { } // 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 diff --git a/facebook/facebook_test.go b/facebook/facebook_test.go new file mode 100644 index 0000000..0ca97e2 --- /dev/null +++ b/facebook/facebook_test.go @@ -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) +} diff --git a/go.mod b/go.mod index 8654ddd..b9fb8ac 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 9138f04..b132409 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/firebase.go b/google/google.go similarity index 74% rename from firebase.go rename to google/google.go index 80620de..89ee0f7 100644 --- a/firebase.go +++ b/google/google.go @@ -1,10 +1,11 @@ -package auth +package googleauth import ( "time" "github.com/dgrijalva/jwt-go" "github.com/mitchellh/mapstructure" + auth "github.com/thaitanloi365/go-social-auth" ) // SignInProvider provider @@ -12,12 +13,12 @@ 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"` @@ -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"` @@ -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 { @@ -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, @@ -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 } } diff --git a/firebase_test.go b/google/google_test.go similarity index 86% rename from firebase_test.go rename to google/google_test.go index 606ce2f..39c14b0 100644 --- a/firebase_test.go +++ b/google/google_test.go @@ -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) }