Skip to content

Commit

Permalink
refactor test
Browse files Browse the repository at this point in the history
  • Loading branch information
thaitanloi365 committed Jun 26, 2021
1 parent ff67de2 commit dfe94b5
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 52 deletions.
13 changes: 7 additions & 6 deletions apple/apple.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}
}

Expand Down
2 changes: 1 addition & 1 deletion apple/apple_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package appleauth
package apple

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion errs.go → errs/errs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package auth
package errs

// Error err
type Error struct {
Expand Down
2 changes: 1 addition & 1 deletion facebook/err.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package facebookauth
package facebook

// Err err
type Err struct {
Expand Down
60 changes: 47 additions & 13 deletions facebook/facebook.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package facebookauth
package facebook

import (
"encoding/json"
Expand All @@ -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"`
Expand All @@ -20,43 +20,77 @@ 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",
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) 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))
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down
18 changes: 8 additions & 10 deletions facebook/facebook_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
14 changes: 11 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
13 changes: 7 additions & 6 deletions google/google.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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
}
}

Expand Down
16 changes: 8 additions & 8 deletions google/google_test.go
Original file line number Diff line number Diff line change
@@ -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_kCjC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5MiwiZXhwIjoxNjI0NzI5MjkyLCJqdGkiOiI3OWVlOGM3YzRiN2RmMDc2MTJhM2IxMGZkN2RlNzc1NDk4NmQ3M2IwIn0.NmBqBEvybQtoUWwtRw0X29iYOsbqvuO5uC18CJERXsWhKpHSce5Thw52SnKkH31gYM3zN7v8IVgZVodGg-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)
}
15 changes: 12 additions & 3 deletions decoder.go → utils/decoder.go
Original file line number Diff line number Diff line change
@@ -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",
Expand Down

0 comments on commit dfe94b5

Please sign in to comment.