-
-
Notifications
You must be signed in to change notification settings - Fork 201
/
client.go
316 lines (295 loc) · 8.4 KB
/
client.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package oauth2client
import (
"github.com/admpub/goth"
"github.com/admpub/log"
"github.com/admpub/nging/v5/application/handler"
"github.com/admpub/nging/v5/application/initialize/backend"
"github.com/admpub/nging/v5/application/library/backend/oauth2nging"
"github.com/admpub/nging/v5/application/library/common"
"github.com/admpub/nging/v5/application/library/config"
"github.com/admpub/nging/v5/application/model"
"github.com/admpub/nging/v5/application/registry/route"
"github.com/coscms/oauth2s/client/goth/providers"
"github.com/webx-top/db"
"github.com/webx-top/echo"
"github.com/webx-top/echo/code"
"github.com/webx-top/echo/handler/oauth2"
"github.com/webx-top/echo/middleware/session"
"github.com/webx-top/echo/subdomains"
)
var (
defaultOAuth *oauth2.OAuth
SuccessHandler interface{} = successHandler
BeginAuthHandler echo.Handler = echo.HandlerFunc(oauth2.BeginAuthHandler)
AfterLoginSuccess []func(ctx echo.Context, ouser *goth.User) (end bool, err error)
)
func Default() *oauth2.OAuth {
return defaultOAuth
}
func OnChangeBackendURL(d config.Diff) error {
if defaultOAuth == nil || !d.IsDiff {
return nil
}
host := d.String()
if len(host) == 0 {
host = subdomains.Default.URL(``, `backend`)
}
if len(host) == 0 {
return nil
}
defaultOAuth.HostURL = host
defaultOAuth.Config.RangeAccounts(func(account *oauth2.Account) bool {
// 清空生成的网址,以便于在后面的 GenerateProviders() 函数中重新生成新的网址
account.CallbackURL = ``
account.LoginURL = ``
return true
})
defaultOAuth.Config.GenerateProviders()
return nil
}
func OnAfterLoginSuccess(hooks ...func(ctx echo.Context, ouser *goth.User) (end bool, err error)) {
AfterLoginSuccess = append(AfterLoginSuccess, hooks...)
}
func FireAfterLoginSuccess(ctx echo.Context, ouser *goth.User) (end bool, err error) {
for _, hook := range AfterLoginSuccess {
end, err = hook(ctx, ouser)
if err != nil || end {
return
}
}
return
}
type OAuth2Config struct {
On bool `json:"on" xml:"on"` // on / off
Accounts []OAuthAccount `json:"accounts" xml:"accounts"`
}
func (c *OAuth2Config) ToAccounts() []*oauth2.Account {
if !c.On {
return []*oauth2.Account{}
}
accounts := make([]*oauth2.Account, 0, len(c.Accounts))
isProduction := config.FromFile().Sys.IsEnv(`prod`)
for _, account := range c.Accounts {
if !account.On {
continue
}
acc := account.ToAccount()
var provider func(account *oauth2.Account) goth.Provider
if !isProduction {
provider = providers.Get(acc.Name + `_dev`)
}
if provider == nil {
provider = providers.Get(acc.Name)
}
if provider != nil {
log.Infof(`backend oauth2 account: %s`, acc.Name)
acc.SetConstructor(provider)
} else {
log.Errorf(`no provider for %q exists`, acc.Name)
}
accounts = append(accounts, acc)
}
return accounts
}
func (c *OAuth2Config) Reload() error {
if c == nil || defaultOAuth == nil {
return nil
}
accounts := c.ToAccounts()
defaultOAuth.Config.ClearAccounts()
if len(accounts) > 0 {
defaultOAuth.Config.AddAccount(accounts...)
defaultOAuth.Config.GenerateProviders()
}
log.Debugf(`reload backend oauth configuration information`)
return nil
}
type OAuthAccount struct {
On bool `json:"on" xml:"on"` // on / off
Name string `json:"name" xml:"name"`
AppID string `json:"appID" xml:"appID"`
Secret string `json:"secret" xml:"secret"`
Extra echo.H `json:"extra" xml:"extra"`
}
func (c *OAuthAccount) ToAccount() *oauth2.Account {
a := &oauth2.Account{
On: c.On,
Name: c.Name,
Secret: c.Secret,
Key: c.AppID,
Extra: c.Extra,
}
if c.Extra == nil {
a.Extra = map[string]interface{}{}
}
return a
}
// InitOauth 第三方登录
func InitOauth(e *echo.Echo) {
oCfg, _ := common.ExtendConfig().Get(`oauth2backend`).(*OAuth2Config)
host := common.BackendURL(nil)
if len(host) == 0 {
host = subdomains.Default.URL(``, `backend`)
}
if len(host) == 0 {
backendDomain := config.FromCLI().BackendDomain
if len(backendDomain) > 0 {
host = backend.MakeSubdomains(backendDomain, backend.DefaultLocalHostNames)[0]
}
if len(host) == 0 {
host = `127.0.0.1:28081`
}
host = `http://` + host
}
log.Warnf(`backend oauth host: %s`, host)
oauth2Config := oauth2.NewConfig()
RegisterProvider(oauth2Config)
if oCfg != nil {
oauth2Config.AddAccount(oCfg.ToAccounts()...)
}
defaultOAuth = oauth2.New(host, oauth2Config)
defaultOAuth.SetSuccessHandler(SuccessHandler)
defaultOAuth.SetBeginAuthHandler(BeginAuthHandler)
e.Group(defaultOAuth.Config.Path).SetMetaKV(route.PermGuestKV())
defaultOAuth.Wrapper(e, session.Middleware(config.SessionOptions))
}
// RegisterProvider 注册Provider
func RegisterProvider(c *oauth2.Config) {
providers.Register(`nging`, func(account *oauth2.Account) goth.Provider {
hostURL := account.Extra.String(`hostURL`)
if len(account.CallbackURL) == 0 {
account.CallbackURL = hostURL + "/oauth/callback/" + account.Name
}
m := oauth2nging.New(account.Key, account.Secret, account.CallbackURL, hostURL, `profile`)
m.SetName(`nging`)
return m
})
}
func DefaultOAuth() *oauth2.OAuth {
doa := defaultOAuth
return doa
}
// 通过oauth登录第三方网站成功之后的处理
func successHandler(ctx echo.Context) error {
doa := DefaultOAuth()
var ouser *goth.User
if user := doa.User(ctx); len(user.Provider) > 0 {
ouser = &user
}
if len(ouser.UserID) == 0 {
return ctx.NewError(code.InvalidParameter, `oauth2登录后获取UserID无效`)
}
end, err := FireAfterLoginSuccess(ctx, ouser)
if err != nil || end {
return err
}
var next string
oauthM := model.NewUserOAuth(ctx)
user := handler.User(ctx)
err = oauthM.GetByOutUser(ouser)
if err != nil {
if err == db.ErrNoMoreRows { // 没有绑定过
if user != nil { // 用户已经登录时,自动绑定
oauthM.CopyFrom(ouser)
oauthM.Uid = user.Id
_, err = oauthM.Add()
} else {
err = ctx.NewError(code.DataNotFound, `请先绑定账号`)
}
}
if err != nil {
return err
}
} else {
if user != nil && oauthM.Uid != user.Id {
err = ctx.NewError(code.DataUnavailable, `此外部账号已经被其他用户绑定`)
return err
}
}
oauthSet := echo.H{}
if ouser.AccessToken != oauthM.AccessToken {
oauthSet[`access_token`] = ouser.AccessToken
}
if ouser.RefreshToken != oauthM.RefreshToken {
oauthSet[`refresh_token`] = ouser.RefreshToken
}
if !ouser.ExpiresAt.IsZero() {
oauthSet[`expired`] = ouser.ExpiresAt.Unix()
}
// 直接登录
userM := model.NewUser(ctx)
err = userM.Get(nil, `id`, oauthM.Uid)
if err != nil {
if err != db.ErrNoMoreRows {
return err
}
oauthM.Delete(nil, `id`, oauthM.Id) // 删除垃圾数据
return ctx.NewError(code.UserNotFound, `用户不存在`)
}
// 更新用户的旧资料
if len(ouser.AvatarURL) > 0 && ouser.AvatarURL != oauthM.Avatar {
oauthSet[`avatar`] = ouser.AvatarURL
if oauthM.Avatar == userM.Avatar {
err = userM.NgingUser.UpdateField(nil, `avatar`, ouser.AvatarURL, `id`, userM.Id)
if err != nil {
log.Error(ctx.T(`更新本地用户头像为oauth2用户头像时失败`), `: `, err.Error())
}
}
}
if len(oauthSet) > 0 {
err = oauthM.UpdateFields(nil, oauthSet, `id`, oauthM.Id)
if err != nil {
log.Error(ctx.T(`更新用户oauth2的数据(%s)失败`, echo.Dump(oauthSet, false)), `: `, err.Error())
}
}
authType := model.AuthTypeOauth2
// 未登录时设置登录状态
if user == nil {
err = userM.FireLoginSuccess(authType)
}
if err != nil {
userM.FireLoginFailure(authType, ``, err)
return err
}
if len(next) == 0 {
next, _ = ctx.Session().Get(`next`).(string)
if len(next) == 0 {
next = `/`
}
}
return ctx.Redirect(next)
}
// UpdateOAuthAccount 第三方登录平台账号
func UpdateOAuthAccount() error {
if defaultOAuth == nil {
return nil
}
oCfg, _ := common.ExtendConfig().Get(`oauth2backend`).(*OAuth2Config)
if oCfg == nil {
return nil
}
accounts := oCfg.ToAccounts()
defaultOAuth.Config.ClearAccounts()
defaultOAuth.Config.AddAccount(accounts...)
defaultOAuth.Config.GenerateProviders()
log.Debug(`update backend oauth configuration information`)
return nil
}
func GetOAuthAccounts() []oauth2.Account {
var accounts []oauth2.Account
if defaultOAuth == nil {
return accounts
}
defaultOAuth.Config.RangeAccounts(func(a *oauth2.Account) bool {
if !a.On {
return true
}
account := *a
accounts = append(accounts, account)
return true
})
return accounts
}
func OnInstalled(ctx echo.Context) error {
return UpdateOAuthAccount()
}