/
facebook.go
147 lines (128 loc) · 4.22 KB
/
facebook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package sso
import (
"net/url"
"github.com/authgear/authgear-server/pkg/lib/authn/stdattrs"
"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/util/crypto"
)
const (
facebookAuthorizationURL string = "https://www.facebook.com/v11.0/dialog/oauth"
// nolint: gosec
facebookTokenURL string = "https://graph.facebook.com/v11.0/oauth/access_token"
facebookUserInfoURL string = "https://graph.facebook.com/v11.0/me?fields=id,email,first_name,last_name,middle_name,name,name_format,picture,short_name"
)
type FacebookImpl struct {
ProviderConfig config.OAuthSSOProviderConfig
Credentials config.OAuthSSOProviderCredentialsItem
StandardAttributesNormalizer StandardAttributesNormalizer
HTTPClient OAuthHTTPClient
}
func (*FacebookImpl) Type() config.OAuthSSOProviderType {
return config.OAuthSSOProviderTypeFacebook
}
func (f *FacebookImpl) Config() config.OAuthSSOProviderConfig {
return f.ProviderConfig
}
func (f *FacebookImpl) GetAuthURL(param GetAuthURLParam) (string, error) {
return MakeAuthorizationURL(facebookAuthorizationURL, AuthorizationURLParams{
ClientID: f.ProviderConfig.ClientID,
RedirectURI: param.RedirectURI,
Scope: f.ProviderConfig.Type.Scope(),
ResponseType: ResponseTypeCode,
// ResponseMode is unset
State: param.State,
Prompt: f.GetPrompt(param.Prompt),
// Nonce is unset
}.Query()), nil
}
func (f *FacebookImpl) GetAuthInfo(r OAuthAuthorizationResponse, param GetAuthInfoParam) (authInfo AuthInfo, err error) {
return f.NonOpenIDConnectGetAuthInfo(r, param)
}
func (f *FacebookImpl) NonOpenIDConnectGetAuthInfo(r OAuthAuthorizationResponse, param GetAuthInfoParam) (authInfo AuthInfo, err error) {
authInfo = AuthInfo{}
accessTokenResp, err := fetchAccessTokenResp(
f.HTTPClient,
r.Code,
facebookTokenURL,
param.RedirectURI,
f.ProviderConfig.ClientID,
f.Credentials.ClientSecret,
)
if err != nil {
return
}
userProfileURL, err := url.Parse(facebookUserInfoURL)
if err != nil {
return
}
q := userProfileURL.Query()
appSecretProof := crypto.HMACSHA256String([]byte(f.Credentials.ClientSecret), []byte(accessTokenResp.AccessToken()))
q.Set("appsecret_proof", appSecretProof)
userProfileURL.RawQuery = q.Encode()
// Here is the refacted user profile of Louis' facebook account.
// {
// "id": "redacted",
// "email": "redacted",
// "first_name": "Jonathan",
// "last_name": "Doe",
// "name": "Johnathan Doe",
// "name_format": "{first} {last}",
// "picture": {
// "data": {
// "height": 50,
// "is_silhouette": true,
// "url": "http://example.com",
// "width": 50
// }
// },
// "short_name": "John"
// }
userProfile, err := fetchUserProfile(f.HTTPClient, accessTokenResp, userProfileURL.String())
if err != nil {
return
}
authInfo.ProviderRawProfile = userProfile
id, _ := userProfile["id"].(string)
email, _ := userProfile["email"].(string)
firstName, _ := userProfile["first_name"].(string)
lastName, _ := userProfile["last_name"].(string)
name, _ := userProfile["name"].(string)
shortName, _ := userProfile["short_name"].(string)
var picture string
if pictureObj, ok := userProfile["picture"].(map[string]interface{}); ok {
if data, ok := pictureObj["data"].(map[string]interface{}); ok {
if url, ok := data["url"].(string); ok {
picture = url
}
}
}
authInfo.ProviderUserID = id
stdAttrs, err := stdattrs.Extract(map[string]interface{}{
stdattrs.Email: email,
stdattrs.GivenName: firstName,
stdattrs.FamilyName: lastName,
stdattrs.Name: name,
stdattrs.Nickname: shortName,
stdattrs.Picture: picture,
}, stdattrs.ExtractOptions{
EmailRequired: *f.ProviderConfig.Claims.Email.Required,
})
if err != nil {
return
}
authInfo.StandardAttributes = stdAttrs
err = f.StandardAttributesNormalizer.Normalize(authInfo.StandardAttributes)
if err != nil {
return
}
return
}
func (f *FacebookImpl) GetPrompt(prompt []string) []string {
// facebook doesn't support prompt parameter
// https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/
return []string{}
}
var (
_ OAuthProvider = &FacebookImpl{}
_ NonOpenIDConnectProvider = &FacebookImpl{}
)