-
-
Notifications
You must be signed in to change notification settings - Fork 253
/
oauth_login_logic.go
68 lines (54 loc) · 1.71 KB
/
oauth_login_logic.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
package oauthprovider
import (
"context"
"strings"
"golang.org/x/oauth2"
"github.com/suyuan32/simple-admin-core/rpc/ent/oauthprovider"
"github.com/suyuan32/simple-admin-core/rpc/internal/svc"
"github.com/suyuan32/simple-admin-core/rpc/internal/utils/dberrorhandler"
"github.com/suyuan32/simple-admin-core/rpc/types/core"
"github.com/zeromicro/go-zero/core/logx"
)
var providerConfig = make(map[string]oauth2.Config)
// userInfoURL used to store infoURL in database | 用来存储获取用户信息网址数据
var userInfoURL = make(map[string]string)
type OauthLoginLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewOauthLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OauthLoginLogic {
return &OauthLoginLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *OauthLoginLogic) OauthLogin(in *core.OauthLoginReq) (*core.OauthRedirectResp, error) {
p, err := l.svcCtx.DB.OauthProvider.Query().Where(oauthprovider.NameEQ(in.Provider)).First(l.ctx)
if err != nil {
return nil, dberrorhandler.DefaultEntError(l.Logger, err, in)
}
var config oauth2.Config
if v, ok := providerConfig[p.Name]; ok {
config = v
} else {
providerConfig[p.Name] = oauth2.Config{
ClientID: p.ClientID,
ClientSecret: p.ClientSecret,
Endpoint: oauth2.Endpoint{
AuthURL: p.AuthURL,
TokenURL: p.TokenURL,
AuthStyle: oauth2.AuthStyle(p.AuthStyle),
},
RedirectURL: p.RedirectURL,
Scopes: strings.Split(p.Scopes, " "),
}
config = providerConfig[p.Name]
}
if _, ok := userInfoURL[p.Name]; !ok {
userInfoURL[p.Name] = p.InfoURL
}
url := config.AuthCodeURL(in.State)
return &core.OauthRedirectResp{Url: url}, nil
}