Skip to content

Commit 88d8901

Browse files
authored
Merge pull request #352 from APIParkLab/feature/liujian-1.9
Feature/liujian 1.9
2 parents 3df303a + c1c5eaa commit 88d8901

3 files changed

Lines changed: 70 additions & 19 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.23.4
55
toolchain go1.23.6
66

77
require (
8-
github.com/eolinker/ap-account v1.0.16
8+
github.com/eolinker/ap-account v1.0.17
99
github.com/eolinker/eosc v0.18.3
1010
github.com/eolinker/go-common v1.1.7
1111
github.com/gabriel-vasile/mimetype v1.4.4

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/r
2828
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
2929
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
3030
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
31-
github.com/eolinker/ap-account v1.0.16 h1:v1VvSeQ2AvxAvkYT4n4APqZdWS8d1CbA/1O0LYEyNM4=
32-
github.com/eolinker/ap-account v1.0.16/go.mod h1:zm/Ivs6waJ/M/nEszhpPmM6g50y/MKO+5eABFAdeD0g=
31+
github.com/eolinker/ap-account v1.0.17 h1:tziqAv6cB+oH1dpXDXg6Sp8lZko5r4Q2rbkbZ2wkPFU=
32+
github.com/eolinker/ap-account v1.0.17/go.mod h1:zm/Ivs6waJ/M/nEszhpPmM6g50y/MKO+5eABFAdeD0g=
3333
github.com/eolinker/eosc v0.18.3 h1:3IK5HkAPnJRfLbQ0FR7kWsZr6Y/OiqqGazvN1q2BL5A=
3434
github.com/eolinker/eosc v0.18.3/go.mod h1:O9PQQXFCpB6fjHf+oFt/LN6EOAv779ItbMixMKCfTfk=
3535
github.com/eolinker/go-common v1.1.7 h1:bi7wDmlCYQGjS3k8Bz/o+Mo9aMJAzmPsBLXWurxPfwk=

login_driver/feishu/feishu.go

Lines changed: 67 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package feishu
22

33
import (
44
"context"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"net/http"
89
"net/url"
910

10-
"github.com/eolinker/eosc/common/bean"
11+
"github.com/eolinker/go-common/autowire"
1112

13+
"github.com/eolinker/ap-account/service/role"
1214
"github.com/eolinker/ap-account/service/user"
1315

1416
"github.com/eolinker/go-common/utils"
@@ -33,14 +35,27 @@ var _ auth_driver.IDriver = (*Driver)(nil)
3335

3436
func init() {
3537
d := &Driver{}
36-
bean.Autowired(&d.accountService)
37-
bean.Autowired(&d.userService)
38+
3839
auth_driver.Register(name, d)
3940
}
4041

4142
type Driver struct {
42-
accountService account.IAccountService `autowired:""`
43-
userService user.IUserService `autowired:""`
43+
isInit bool
44+
accountService account.IAccountService `autowired:""`
45+
userService user.IUserService `autowired:""`
46+
roleService role.IRoleService `autowired:""`
47+
roleMemberService role.IRoleMemberService `autowired:""`
48+
}
49+
50+
func (d *Driver) Init() {
51+
if d.isInit {
52+
return
53+
}
54+
autowire.Autowired(&d.accountService)
55+
autowire.Autowired(&d.userService)
56+
autowire.Autowired(&d.roleService)
57+
autowire.Autowired(&d.roleMemberService)
58+
d.isInit = true
4459
}
4560

4661
func (d *Driver) FilterConfig(config map[string]string) {
@@ -68,7 +83,21 @@ func (d *Driver) ThirdLogin(ctx context.Context, args map[string]string) (string
6883
if !ok {
6984
return "", fmt.Errorf("missing client_secret parameter")
7085
}
71-
tokenResp, err := getUserToken(code, clientId, clientSecret)
86+
redirectUri, ok := args["redirect_uri"]
87+
if !ok {
88+
return "", fmt.Errorf("missing redirect_uri parameter")
89+
}
90+
u, err := url.Parse(redirectUri)
91+
if err != nil {
92+
return "", fmt.Errorf("invalid redirect_uri parameter")
93+
}
94+
query := u.Query()
95+
query.Del("code")
96+
redirectUri = fmt.Sprintf("%s://%s%s", u.Scheme, u.Host, u.Path)
97+
if len(query) > 0 {
98+
redirectUri = fmt.Sprintf("%s?%s", redirectUri, query.Encode())
99+
}
100+
tokenResp, err := getUserToken(code, redirectUri, clientId, clientSecret)
72101
if err != nil {
73102
return "", err
74103
}
@@ -86,33 +115,55 @@ func (d *Driver) ThirdLogin(ctx context.Context, args map[string]string) (string
86115
return "", err
87116
}
88117
uId := uuid.NewString()
118+
89119
err = d.accountService.Save(ctx, name, uId, userId, utils.Md5(fmt.Sprintf("%s%s", uId, userId)))
90120
if err != nil {
91121
return "", err
92122
}
93-
_, err = d.userService.Create(ctx, uId, username, email, mobile, "")
123+
_, err = d.userService.Create(ctx, uId, username, email, mobile, name)
124+
if err != nil {
125+
return "", err
126+
}
127+
r, err := d.roleService.GetDefaultRole(ctx, role.SystemTarget())
94128
if err != nil {
95129
return "", err
96130
}
97-
return userId, nil
131+
err = d.roleMemberService.Add(ctx, &role.AddMember{
132+
Role: r.Id,
133+
User: uId,
134+
Target: role.SystemTarget(),
135+
})
136+
if err != nil {
137+
return "", err
138+
}
139+
return uId, nil
98140
}
99141
_, err = d.userService.Update(ctx, info.Uid, &username, &email, &mobile)
100142
if err != nil {
101143
return "", err
102144
}
103145

104-
return userId, nil
146+
return info.Uid, nil
105147
}
106148

107-
func getUserToken(code string, clientId string, clientSecret string) (*UserTokenResponse, error) {
149+
func getUserToken(code string, redirectUri, clientId string, clientSecret string) (*UserTokenResponse, error) {
108150
headers := http.Header{}
109151
headers.Set("Content-Type", "application/json")
110-
body := url.Values{}
111-
body.Set("grant_type", "authorization_code")
112-
body.Set("code", code)
113-
body.Set("client_id", clientId)
114-
body.Set("client_secret", clientSecret)
115-
resp, err := SendRequest[UserTokenResponse](getTokenUri, http.MethodPost, headers, nil, []byte(body.Encode()))
152+
//body := url.Values{}
153+
//body.Set("grant_type", "authorization_code")
154+
//body.Set("code", code)
155+
//body.Set("client_id", clientId)
156+
//body.Set("client_secret", clientSecret)
157+
//body.Set("redirect_uri", redirectUri)
158+
body := map[string]string{
159+
"grant_type": "authorization_code",
160+
"code": code,
161+
"client_id": clientId,
162+
"client_secret": clientSecret,
163+
"redirect_uri": redirectUri,
164+
}
165+
bodyByte, _ := json.Marshal(body)
166+
resp, err := SendRequest[UserTokenResponse](getTokenUri, http.MethodPost, headers, nil, bodyByte)
116167
if err != nil {
117168
return nil, fmt.Errorf("failed to get user token: %w", err)
118169
}

0 commit comments

Comments
 (0)