Skip to content

Commit

Permalink
Merge pull request #263 from kobayashi/236
Browse files Browse the repository at this point in the history
add fields options
  • Loading branch information
bentranter authored Jan 4, 2019
2 parents 36164d2 + 928a7c0 commit 157987f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 29 deletions.
42 changes: 20 additions & 22 deletions providers/facebook/facebook.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func New(clientKey, secret, callbackURL string, scopes ...string) *Provider {
providerName: "facebook",
}
p.config = newConfig(p, scopes)
p.Fields = "email,first_name,last_name,link,about,id,name,picture,location"
return p
}

Expand All @@ -46,6 +47,7 @@ type Provider struct {
Secret string
CallbackURL string
HTTPClient *http.Client
Fields string
config *oauth2.Config
providerName string
}
Expand All @@ -60,6 +62,16 @@ func (p *Provider) SetName(name string) {
p.providerName = name
}

// SetCustomFields sets the fields used to return information
// for a user.
//
// A list of available field values can be found at
// https://developers.facebook.com/docs/graph-api/reference/user
func (p *Provider) SetCustomFields(fields []string) *Provider {
p.Fields = strings.Join(fields, ",")
return p
}

func (p *Provider) Client() *http.Client {
return goth.HTTPClientWithFallBack(p.HTTPClient)
}
Expand Down Expand Up @@ -99,7 +111,7 @@ func (p *Provider) FetchUser(session goth.Session) (goth.User, error) {

reqUrl := fmt.Sprint(
endpointProfile,
strings.Join(p.config.Scopes, ","),
p.Fields,
"&access_token=",
url.QueryEscape(sess.AccessToken),
"&appsecret_proof=",
Expand Down Expand Up @@ -177,31 +189,17 @@ func newConfig(provider *Provider, scopes []string) *oauth2.Config {
},
Scopes: []string{
"email",
"first_name",
"last_name",
"link",
"about",
"id",
"name",
"picture",
"location",
},
}

// creates possibility to invoke field method like 'picture.type(large)'
var found bool
for _, sc := range scopes {
sc := sc
for i, defScope := range c.Scopes {
if defScope == strings.Split(sc, ".")[0] {
c.Scopes[i] = sc
found = true
}
}
if !found {
c.Scopes = append(c.Scopes, sc)
defaultScopes := map[string]struct{}{
"email": {},
}

for _, scope := range scopes {
if _, exists := defaultScopes[scope]; !exists {
c.Scopes = append(c.Scopes, scope)
}
found = false
}

return c
Expand Down
21 changes: 14 additions & 7 deletions providers/facebook/facebook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package facebook_test
import (
"fmt"
"os"
"strings"
"testing"

"github.com/markbates/goth"
Expand Down Expand Up @@ -54,12 +55,18 @@ func Test_SessionFromJSON(t *testing.T) {
a.Equal(session.AccessToken, "1234567890")
}

func Test_SetCustomFields(t *testing.T) {
t.Parallel()
defaultFields := "email,first_name,last_name,link,about,id,name,picture,location"
cf := []string{"email", "picture.type(large)"}
a := assert.New(t)

provider := facebookProvider()
a.Equal(provider.Fields, defaultFields)
provider.SetCustomFields(cf)
a.Equal(provider.Fields, strings.Join(cf, ","))
}

func facebookProvider() *facebook.Provider {
scopes := []string{"picture.type(large)", "email"}
return facebook.New(
os.Getenv("FACEBOOK_KEY"),
os.Getenv("FACEBOOK_SECRET"),
"/foo",
scopes...,
)
return facebook.New(os.Getenv("FACEBOOK_KEY"), os.Getenv("FACEBOOK_SECRET"), "/foo", "email")
}

0 comments on commit 157987f

Please sign in to comment.