-
Notifications
You must be signed in to change notification settings - Fork 0
/
facebook.go
207 lines (171 loc) · 5.29 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package facebook
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"reflect"
"github.com/qor/auth"
"github.com/qor/auth/auth_identity"
"github.com/qor/auth/claims"
"github.com/qor/qor/utils"
"golang.org/x/oauth2"
"golang.org/x/oauth2/facebook"
)
var UserInfoURL = "https://graph.facebook.com/me?access_token="
// FacebookProvider provide login with facebook method
type FacebookProvider struct {
*Config
}
// Config facebook Config
type Config struct {
ClientID string
ClientSecret string
AuthorizeURL string
RedirectURL string
TokenURL string
Scopes []string
AuthorizeHandler func(context *auth.Context) (*claims.Claims, error)
}
func New(config *Config) *FacebookProvider {
if config == nil {
config = &Config{}
}
provider := &FacebookProvider{Config: config}
if config.ClientID == "" {
panic(errors.New("Facebook's ClientID can't be blank"))
}
if config.ClientSecret == "" {
panic(errors.New("Facebook's ClientSecret can't be blank"))
}
if config.AuthorizeURL == "" {
config.AuthorizeURL = facebook.Endpoint.AuthURL
}
if config.TokenURL == "" {
config.TokenURL = facebook.Endpoint.TokenURL
}
if len(config.Scopes) == 0 {
config.Scopes = []string{"public_profile", "email"}
}
if config.AuthorizeHandler == nil {
config.AuthorizeHandler = func(context *auth.Context) (*claims.Claims, error) {
var (
req = context.Request
schema auth.Schema
authInfo auth_identity.Basic
tx = context.Auth.GetDB(req)
authIdentity = reflect.New(utils.ModelType(context.Auth.Config.AuthIdentityModel)).Interface()
)
state := req.URL.Query().Get("state")
claims, err := context.Auth.SessionStorer.ValidateClaims(state)
if err != nil || claims.Valid() != nil || claims.Subject != "state" {
return nil, auth.ErrUnauthorized
}
if err == nil {
oauthCfg := provider.OAuthConfig(context)
tkn, err := oauthCfg.Exchange(oauth2.NoContext, req.URL.Query().Get("code"))
if err != nil {
return nil, err
}
{
resp, err := http.Get("https://graph.facebook.com/me?access_token=" + tkn.AccessToken)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
userInfo := UserInfo{}
json.Unmarshal(body, &userInfo)
schema.Provider = provider.GetName()
schema.UID = userInfo.ID
schema.Email = userInfo.Email
schema.FirstName = userInfo.GivenName
schema.LastName = userInfo.FamilyName
schema.Image = userInfo.Picture
schema.Name = userInfo.Name
schema.RawInfo = userInfo
}
authInfo.Provider = provider.GetName()
authInfo.UID = schema.UID
if !tx.Model(authIdentity).Where(authInfo).Scan(&authInfo).RecordNotFound() {
return authInfo.ToClaims(), nil
}
if _, userID, err := context.Auth.UserStorer.Save(&schema, context); err == nil {
if userID != "" {
authInfo.UserID = userID
}
} else {
return nil, err
}
if err = tx.Where(authInfo).FirstOrCreate(authIdentity).Error; err == nil {
return authInfo.ToClaims(), nil
}
}
return nil, err
}
}
return provider
}
// GetName return provider name
func (FacebookProvider) GetName() string {
return "facebook"
}
// ConfigAuth config auth
func (provider FacebookProvider) ConfigAuth(auth *auth.Auth) {
auth.Render.RegisterViewPath("github.com/qor/auth/providers/facebook/views")
}
// OAuthConfig return oauth config based on configuration
func (provider FacebookProvider) OAuthConfig(context *auth.Context) *oauth2.Config {
var (
config = provider.Config
scheme = context.Request.URL.Scheme
)
if scheme == "" {
scheme = "http://"
}
return &oauth2.Config{
ClientID: config.ClientID,
ClientSecret: config.ClientSecret,
Endpoint: oauth2.Endpoint{
AuthURL: config.AuthorizeURL,
TokenURL: config.TokenURL,
},
RedirectURL: scheme + context.Request.Host + context.Auth.AuthURL("facebook/callback"),
Scopes: config.Scopes,
}
}
// Login implemented login with facebook provider
func (provider FacebookProvider) Login(context *auth.Context) {
claims := claims.Claims{}
claims.Subject = "state"
signedToken := context.Auth.SessionStorer.SignedToken(&claims)
url := provider.OAuthConfig(context).AuthCodeURL(signedToken)
http.Redirect(context.Writer, context.Request, url, http.StatusFound)
}
// Logout implemented logout with facebook provider
func (FacebookProvider) Logout(context *auth.Context) {
}
// Register implemented register with facebook provider
func (provider FacebookProvider) Register(context *auth.Context) {
provider.Login(context)
}
// Callback implement Callback with facebook provider
func (provider FacebookProvider) Callback(context *auth.Context) {
context.Auth.LoginHandler(context, provider.AuthorizeHandler)
}
// ServeHTTP implement ServeHTTP with facebook provider
func (FacebookProvider) ServeHTTP(*auth.Context) {
}
// UserInfo facebook user info structure
type UserInfo struct {
ID string `json:"id"`
Name string `json:"name"`
GivenName string `json:"first_name"`
FamilyName string `json:"last_name"`
Picture string `json:"picture"`
Profile string `json:"link"`
Email string `json:"email"`
Gender string `json:"gender"`
Locale string `json:"locale"`
Verified bool `json:"verified"`
}