diff --git a/apple/apple.go b/apple/apple.go index 2b159e3..64558f4 100644 --- a/apple/apple.go +++ b/apple/apple.go @@ -1,10 +1,11 @@ -package appleauth +package apple import ( "time" "github.com/dgrijalva/jwt-go" - auth "github.com/thaitanloi365/go-social-auth" + "github.com/thaitanloi365/go-social-auth/errs" + "github.com/thaitanloi365/go-social-auth/utils" ) // TokenResponse response @@ -72,26 +73,26 @@ func (c *Config) Login(token string) (*TokenResponse, error) { } - err = auth.DecodeTypedWeakly(&claims, &result) + err = utils.DecodeTypedWeakly(&claims, &result) if err != nil { return nil, err } if c.Iss != "" { if result.Iss != c.Iss { - return nil, auth.ErrIssuerInvalid + return nil, errs.ErrIssuerInvalid } } if c.Aud != "" { if result.Aud != c.Aud { - return nil, auth.ErrAudienceInvalid + return nil, errs.ErrAudienceInvalid } } if !c.SkipExpiry { if result.Exp < time.Now().Unix() { - return nil, auth.ErrTokenExpired + return nil, errs.ErrTokenExpired } } diff --git a/apple/apple_test.go b/apple/apple_test.go index 29ac0fc..71284d1 100644 --- a/apple/apple_test.go +++ b/apple/apple_test.go @@ -1,4 +1,4 @@ -package appleauth +package apple import ( "fmt" diff --git a/errs.go b/errs/errs.go similarity index 97% rename from errs.go rename to errs/errs.go index db07f6f..562113d 100644 --- a/errs.go +++ b/errs/errs.go @@ -1,4 +1,4 @@ -package auth +package errs // Error err type Error struct { diff --git a/facebook/err.go b/facebook/err.go index 659ec0f..5e0e199 100644 --- a/facebook/err.go +++ b/facebook/err.go @@ -1,4 +1,4 @@ -package facebookauth +package facebook // Err err type Err struct { diff --git a/facebook/facebook.go b/facebook/facebook.go index ebd650b..7b346c9 100644 --- a/facebook/facebook.go +++ b/facebook/facebook.go @@ -1,4 +1,4 @@ -package facebookauth +package facebook import ( "encoding/json" @@ -8,10 +8,10 @@ import ( "net/url" "strings" - auth "github.com/thaitanloi365/go-social-auth" + "github.com/thaitanloi365/go-social-auth/errs" + "github.com/thaitanloi365/go-social-auth/utils" ) -// TokenResponse response type TokenResponse struct { ID string `json:"id"` Email string `json:"email"` @@ -20,13 +20,28 @@ type TokenResponse struct { Name string `json:"name"` } -// Config config +type debugTokenResponse struct { + Data struct { + AppID string `json:"app_id"` + Type string `json:"type"` + Application string `json:"application"` + DataAccessExpiresAt int `json:"data_access_expires_at"` + ExpiresAt int `json:"expires_at"` + IsValid bool `json:"is_valid"` + Metadata struct { + AuthType string `json:"auth_type"` + } `json:"metadata"` + Scopes []string `json:"scopes"` + UserID string `json:"user_id"` + } `json:"data"` +} + type Config struct { Scopes []string `json:"scopes"` URL string `json:"url"` + AppID string `json:"app_id"` } -// New new func New() *Config { return &Config{ URL: "https://graph.facebook.com", @@ -34,29 +49,48 @@ func New() *Config { } } -// WithURL override url func (c *Config) WithURL(url string) *Config { c.URL = url return c } -// WithScopes override scopes +func (c *Config) WithAppID(id string) *Config { + c.AppID = id + return c +} 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 +func (c *Config) isValidFacebookToken(accessToken string) bool { + if c.AppID != "" { + var url = fmt.Sprintf("%s/debug_token?input_token=%s&access_token=%s", c.URL, url.QueryEscape(accessToken), url.QueryEscape(accessToken)) + resp, err := http.Get(url) + if err != nil { + return false + } + defer resp.Body.Close() + + responseData, err := ioutil.ReadAll(resp.Body) + if err != nil { + return false + } + + var debugToken debugTokenResponse + if err = json.Unmarshal(responseData, &debugToken); err == nil { + return debugToken.Data.AppID == c.AppID + } + } return true } // Login login func (c *Config) Login(accessToken string) (*TokenResponse, error) { var result TokenResponse - if !isValidFacebookToken(accessToken) { - return nil, auth.ErrTokenInvalid + if !c.isValidFacebookToken(accessToken) { + return nil, errs.ErrTokenInvalid } var scopes = strings.Join(c.Scopes, ",") var url = fmt.Sprintf("%s/me?fields=%s&access_token=%s", c.URL, scopes, url.QueryEscape(accessToken)) @@ -79,7 +113,7 @@ func (c *Config) Login(accessToken string) (*TokenResponse, error) { if value, ok := responseMap["error"].(map[string]interface{}); ok { var e Err - err = auth.DecodeTypedWeakly(&value, &e) + err = utils.DecodeTypedWeakly(&value, &e) if err != nil { return nil, err } @@ -91,7 +125,7 @@ func (c *Config) Login(accessToken string) (*TokenResponse, error) { return nil, fmt.Errorf("%s", value.(string)) } - err = auth.DecodeTypedWeakly(&responseMap, &result) + err = utils.DecodeTypedWeakly(&responseMap, &result) if err != nil { return nil, err } diff --git a/facebook/facebook_test.go b/facebook/facebook_test.go index 47d4234..f33c038 100644 --- a/facebook/facebook_test.go +++ b/facebook/facebook_test.go @@ -1,19 +1,17 @@ -package facebookauth +package facebook import ( - "fmt" "testing" + + "github.com/stretchr/testify/assert" + "github.com/thaitanloi365/go-social-auth/utils" ) func TestLoginFacebook(t *testing.T) { - // var token = "EAACejEGlyekBAKoV7PZAIo3qj8e7VcQZCihlW0E9GnjlioIcIt8vEnAb9KNJcpi8zSCZA0H42ZAa7YvGz3ctgqMj78fZARYCBiPikLZBZCsAiGakXA2iXUT8JPVIoGlI0ot2fWlgk6ZAxzbZB1j5qWNden5pwN27GyHwh5XSizffDxVAl1PZCZAVfquLE6MfGcs2iP0ZC5UbNumE7NM50obI0HkPQS90pt0LYSdbsKf2qtBIZBAZDZD" - var token = "asdf" - var facebookauth = New() + var token = "EAAC2I0ITd0MBAARDwFhCZCY9WI7vTgkiQ5jjiRFyxKiO2vPeYvD2AzIR9TQoHoSZAkLWCttZCEtjV9bd4jWZBMvvFhkniPyYrG8HTSCIUPdcuo9JBpqVY2dOM4nskUK1vFeVPx5fTrtmoQOd3j0qZA98pzW1tT1584MUDuBkCtJQBBQhjtVgH3WZAde0BZCZANQjAbdp7WvIxhZCoV94rvUXz" + var facebookauth = New().WithAppID("200262548682563") result, err := facebookauth.Login(token) - if err != nil { - fmt.Println("eeee", err) - panic(err.Error()) - } + assert.NoError(t, err) - fmt.Println("result", result) + utils.PrintJSON(result) } diff --git a/go.mod b/go.mod index b9fb8ac..111395f 100644 --- a/go.mod +++ b/go.mod @@ -5,4 +5,5 @@ go 1.14 require ( github.com/dgrijalva/jwt-go v3.2.0+incompatible github.com/mitchellh/mapstructure v1.3.2 + github.com/stretchr/testify v1.7.0 ) diff --git a/go.sum b/go.sum index b132409..2c92755 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,15 @@ -github.com/dgrijalva/jwt-go v1.0.2 h1:KPldsxuKGsS2FPWsNeg9ZO18aCrGKujPoWXn2yo+KQM= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= 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= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/google/google.go b/google/google.go index ede18c3..e487a5d 100644 --- a/google/google.go +++ b/google/google.go @@ -1,10 +1,11 @@ -package googleauth +package google import ( "time" "github.com/dgrijalva/jwt-go" - auth "github.com/thaitanloi365/go-social-auth" + "github.com/thaitanloi365/go-social-auth/errs" + "github.com/thaitanloi365/go-social-auth/utils" ) // SignInProvider provider @@ -112,20 +113,20 @@ func (c *Config) Login(token string) (*TokenResponse, error) { } - err = auth.DecodeTypedWeakly(&claims, &result) + err = utils.DecodeTypedWeakly(&claims, &result) if err != nil { return nil, err } if c.Iss != "" { if result.Iss != c.Iss { - return nil, auth.ErrIssuerInvalid + return nil, errs.ErrIssuerInvalid } } if c.Aud != "" { if result.Aud != c.Aud { - return nil, auth.ErrAudienceInvalid + return nil, errs.ErrAudienceInvalid } } @@ -154,7 +155,7 @@ func (c *Config) Login(token string) (*TokenResponse, error) { if !c.SkipExpiry { if response.Exp < time.Now().Unix() { - return nil, auth.ErrTokenExpired + return nil, errs.ErrTokenExpired } } diff --git a/google/google_test.go b/google/google_test.go index 947b8c8..938c78d 100644 --- a/google/google_test.go +++ b/google/google_test.go @@ -1,18 +1,18 @@ -package googleauth +package google import ( - "fmt" "testing" + + "github.com/stretchr/testify/assert" + "github.com/thaitanloi365/go-social-auth/utils" ) 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 token = "eyJhbGciOiJSUzI1NiI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mBqBEvybQtoUWwtRw0X29iYOsbqvuO5uC18CJERXsWhKpHSce5Thw52SnKkH31gYM3zN7v8IVgZVodGg-JdwYEi92O9djiQCWy5-AEZXQMi9SCM1NdrcZ4kdEWZ5MpA-b8RYFRQG3H5wBv1b3Mbo6hweb0V5nT9MIkoMpsEfXrbgziJpvLgsM_t_JJ1ZXqwMpbL6OOP5isp4okVakePBLOdZsCKgJyuHli3wMNZDdUSyTpg--ZQkRwoM8Ch3fdaPl6F9EeK5Dj5upaHljuyVA00tqJ1yNBO-4V6qg38NpEa0vW2ZlPdl33XZF_qIP0etqsU9JyqAN9EuzjgmY3ksA" var googleauth = New().WithIssuer("").WithExpiry(true) result, err := googleauth.Login(token) - if err != nil { - panic(err) - } - fmt.Println("result", result) + assert.NoError(t, err) + + utils.PrintJSON(result) } diff --git a/decoder.go b/utils/decoder.go similarity index 58% rename from decoder.go rename to utils/decoder.go index 66caec5..be2b466 100644 --- a/decoder.go +++ b/utils/decoder.go @@ -1,8 +1,17 @@ -package auth +package utils -import "github.com/mitchellh/mapstructure" +import ( + "encoding/json" + "fmt" + + "github.com/mitchellh/mapstructure" +) + +func PrintJSON(in interface{}) { + data, _ := json.MarshalIndent(in, "", " ") + fmt.Println(string(data)) +} -// DecodeTypedWeakly deacode typed weakly func DecodeTypedWeakly(in interface{}, out interface{}) error { decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{ TagName: "json",