/
user.go
279 lines (260 loc) · 8.25 KB
/
user.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
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package ucauth
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"github.com/pkg/errors"
"github.com/erda-project/erda/pkg/http/httpclient"
)
type USERID string
// maybe int or string, unmarshal them to string(USERID)
func (u *USERID) UnmarshalJSON(b []byte) error {
var intid int
if err := json.Unmarshal(b, &intid); err != nil {
var stringid string
if err := json.Unmarshal(b, &stringid); err != nil {
return err
}
*u = USERID(stringid)
return nil
}
*u = USERID(strconv.Itoa(intid))
return nil
}
type UserInfo struct {
ID USERID `json:"id"`
Token string `json:"token"`
Email string `json:"email"`
EmailExist bool `json:"emailExist"`
PasswordExist bool `json:"passwordExist"`
PhoneExist bool `json:"phoneExist"`
Birthday string `json:"birthday"`
PasswordStrength int `json:"passwordStrength"`
Phone string `json:"phone"`
AvatarUrl string `json:"avatarUrl"`
UserName string `json:"username"`
NickName string `json:"nickName"`
Enabled bool `json:"enabled"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
LastLoginAt string `json:"lastLoginAt"`
}
type OAuthToken struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
RefreshToken string `json:"refresh_token"`
ExpiresIn int `json:"expires_in"`
Scope string `json:"scope"`
Jti string `json:"jti"`
}
type UCUserAuth struct {
UCHostFront string
UCHost string
RedirectURI string
ClientID string
ClientSecret string
}
const OryCompatibleClientId = "kratos"
func (a *UCUserAuth) oryEnabled() bool {
// TODO: it's a hack
return a.ClientID == OryCompatibleClientId
}
func (a *UCUserAuth) oryKratosAddr() string {
return a.UCHost
}
func NewUCUserAuth(UCHostFront, UCHost, RedirectURI, ClientID, ClientSecret string) *UCUserAuth {
return &UCUserAuth{UCHostFront, UCHost, RedirectURI, ClientID, ClientSecret}
}
// 返回用户中心的登陆URL, 也就是浏览器请求的地址
// http://uc.terminus.io/oauth/authorize?response_type=code&client_id=dice&redirect_uri=http%3A%2F%2Fopenapi.test.terminus.io%2Flogincb&scope=public_profile
func (a *UCUserAuth) LoginURL(https bool) string {
proto := "http"
if https {
proto = "https"
}
return fmt.Sprintf("%s://%s/oauth/authorize?response_type=code&client_id=%s&redirect_uri=%s&scope=public_profile", proto, a.UCHostFront, a.ClientID, url.QueryEscape(a.RedirectURI))
}
// (登陆) 从uc回调回来,会有uccode,用于得到token
func (a *UCUserAuth) Login(uccode string) (OAuthToken, error) {
if a.oryEnabled() {
// TODO:
return OAuthToken{}, fmt.Errorf("not supported Login")
}
basic := "Basic " + base64.StdEncoding.EncodeToString([]byte(a.ClientID+":"+a.ClientSecret))
formBody := make(url.Values)
formBody.Set("grant_type", "authorization_code")
formBody.Set("code", uccode)
formBody.Set("redirect_uri", a.RedirectURI)
var body bytes.Buffer
r, err := httpclient.New(httpclient.WithCompleteRedirect()).
Post(a.UCHost).
Path("/oauth/token").
Header("Authorization", basic).
FormBody(formBody).Do().Body(&body)
if err != nil {
err := errors.Wrap(err, "login: post /oauth/token fail")
return OAuthToken{}, err
}
if !r.IsOK() {
err := fmt.Errorf("login: /oauth/token statuscode: %d, body: %v", r.StatusCode(), body.String())
return OAuthToken{}, err
}
var oauthToken OAuthToken
if err := json.NewDecoder(&body).Decode(&oauthToken); err != nil {
err := errors.Wrap(err, "login: decode fail")
return OAuthToken{}, err
}
return oauthToken, nil
}
func (a *UCUserAuth) PwdAuth(username, password string) (OAuthToken, error) {
if a.oryEnabled() {
// TODO:
return OAuthToken{}, fmt.Errorf("not supported PwdAuth")
}
basic := "Basic " + base64.StdEncoding.EncodeToString([]byte(a.ClientID+":"+a.ClientSecret))
formBody := make(url.Values)
formBody.Set("grant_type", "password")
formBody.Set("username", username)
formBody.Set("password", password)
formBody.Set("scope", "public_profile")
var body bytes.Buffer
r, err := httpclient.New(httpclient.WithCompleteRedirect()).
Post(a.UCHost).
Path("/oauth/token").
Header("Authorization", basic).
FormBody(formBody).Do().Body(&body)
if err != nil {
err := errors.Wrap(err, "pwdAuth: post /oauth/token fail")
return OAuthToken{}, err
}
if !r.IsOK() {
err := fmt.Errorf("pwdAuth: /oauth/token statuscode: %d, body: %v", r.StatusCode(), body.String())
return OAuthToken{}, err
}
var oauthToken OAuthToken
if err := json.NewDecoder(&body).Decode(&oauthToken); err != nil {
err := errors.Wrap(err, "pwdAuth: decode fail")
return OAuthToken{}, err
}
return oauthToken, nil
}
//
func (a *UCUserAuth) GetUserInfo(oauthToken OAuthToken) (UserInfo, error) {
if a.oryEnabled() {
// sessionID as token
return whoami(a.oryKratosAddr(), oauthToken.AccessToken)
}
bearer := "Bearer " + oauthToken.AccessToken
var me bytes.Buffer
r, err := httpclient.New(httpclient.WithCompleteRedirect()).
Get(a.UCHost).
Path("/api/oauth/me").
Header("Authorization", bearer).Do().Body(&me)
if err != nil {
err := errors.Wrap(err, "GetUserInfo: /api/oauth/me fail")
return UserInfo{}, err
}
if !r.IsOK() {
err := fmt.Errorf("GetUserInfo: /api/oauth/me statuscode: %d, body: %v", r.StatusCode(), me.String())
return UserInfo{}, err
}
var info UserInfo
if err := json.NewDecoder(&me).Decode(&info); err != nil {
err := errors.Wrap(err, "GetUserInfo: decode fail")
return UserInfo{}, err
}
return info, nil
}
// {
// "success": true,
// "result": {
// "id": 1000530,
// "tenantId": 1,
// "username": "u191-1019703192",
// "nickname": "",
// "avatar": "",
// "prefix": "86",
// "mobile": "15950552810",
// "email": "",
// "pwdExpireAt": null,
// "passwordExist": true,
// "enabled": true,
// "locked": false,
// "channel": "",
// "channelType": "",
// "source": "",
// "sourceType": "",
// "tag": "",
// "extra": null,
// "userDetail": null,
// "createdAt": "2020-09-21T09:11:26.000+0000",
// "updatedAt": "2020-12-15T04:01:02.000+0000",
// "lastLoginAt": "2020-12-15T04:01:02.000+0000",
// "pk": 1307970680503390208
// },
// "code": null,
// "args": null,
// "error": null,
// "sourceIp": null,
// "sourceStack": null
// }
type CurrentUser struct {
Success bool `json:"success"`
Result struct {
ID USERID `json:"id"`
Email string `json:"email"`
Mobile string `json:"mobile"`
Username string `json:"username"`
Nickname string `json:"nickname"`
} `json:"result"`
Error interface{} `json:"error"`
}
func (a *UCUserAuth) GetCurrentUser(headers http.Header) (UserInfo, error) {
var me bytes.Buffer
r, err := httpclient.New(httpclient.WithCompleteRedirect()).
Get(a.UCHost).
Path("/api/user/web/current-user").
Headers(headers).Do().Body(&me)
if err != nil {
err := errors.Wrapf(err, "GetCurrentUser: /api/user/web/current-user: %+v", err)
return UserInfo{}, err
}
if !r.IsOK() {
err := fmt.Errorf("GetCurrentUser: /api/user/web/current-user statuscode: %d, body: %v", r.StatusCode(), me.String())
return UserInfo{}, err
}
var info CurrentUser
d := json.NewDecoder(&me)
if err := d.Decode(&info); err != nil {
buffered, _ := ioutil.ReadAll(d.Buffered())
err := errors.Wrapf(err, "GetCurrentUser: decode fail: %v", string(buffered))
return UserInfo{}, err
}
if info.Result.ID == "" {
return UserInfo{}, fmt.Errorf("not login")
}
return UserInfo{
ID: info.Result.ID,
Email: info.Result.Email,
Phone: info.Result.Mobile,
UserName: info.Result.Username,
NickName: info.Result.Nickname,
}, nil
}