-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathauthing.go
708 lines (579 loc) · 20.3 KB
/
authing.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
// Package authing provides a Graphql client for `Authing`` which is an IDaaS provider
package authing
import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
"log"
"github.com/kelvinji2009/graphql"
"golang.org/x/oauth2"
)
const (
// Development env
userEndpointDevURL = "http://users.authing.dodora.cn/graphql"
oauthEndpointDevURL = "http://oauth.authing.dodora.cn/graphql"
// Production env
userEndpointProdURL = "https://users.authing.cn/graphql"
oauthEndpointProdURL = "https://oauth.authing.cn/graphql"
)
const pubPEM = `
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4xKeUgQ+Aoz7TLfAfs9+paePb
5KIofVthEopwrXFkp8OCeocaTHt9ICjTT2QeJh6cZaDaArfZ873GPUn00eOIZ7Ae
+TiA2BKHbCvloW3w5Lnqm70iSsUi5Fmu9/2+68GZRH9L7Mlh8cFksCicW2Y2W2uM
GKl64GDcIq3au+aqJQIDAQAB
-----END PUBLIC KEY-----`
// Client is a client for interacting with the GraphQL API of `Authing`
type Client struct {
Client *graphql.Client
clientID string
// Log is called with various debug information.
// To log to standard out, use:
// client.Log = func(s string) { log.Println(s) }
Log func(s string)
}
// NewClient creates a new Authing user endpoint GraphQL API client
func NewClient(clientID string, appSecret string, isDev bool) *Client {
c := &Client{
clientID: clientID,
}
if c.Client == nil {
var endpointURL string
if isDev {
endpointURL = userEndpointDevURL
} else {
endpointURL = userEndpointProdURL
}
client := graphql.NewClient(endpointURL, nil)
accessToken, err := getAccessTokenByAppSecret(client, clientID, appSecret)
if err != nil {
log.Println(err)
return nil
}
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: accessToken},
)
httpClient := oauth2.NewClient(context.Background(), src)
c.Client = graphql.NewClient(endpointURL, httpClient)
}
return c
}
// NewOauthClient creates a new Authing oauth endpoint GraphQL API client
func NewOauthClient(clientID string, appSecret string, isDev bool) *Client {
c := &Client{
clientID: clientID,
}
if c.Client == nil {
var endpointURL string
if isDev {
endpointURL = userEndpointDevURL
} else {
endpointURL = userEndpointProdURL
}
client := graphql.NewClient(endpointURL, nil)
accessToken, err := getAccessTokenByAppSecret(client, clientID, appSecret)
if err != nil {
log.Println(err)
return nil
}
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: accessToken},
)
httpClient := oauth2.NewClient(context.Background(), src)
if isDev {
endpointURL = oauthEndpointDevURL
} else {
endpointURL = oauthEndpointProdURL
}
c.Client = graphql.NewClient(endpointURL, httpClient)
}
return c
}
func (c *Client) logf(format string, args ...interface{}) {
c.Log(fmt.Sprintf(format, args...))
}
// Get access token by appSeceret
func getAccessTokenByAppSecret(client *graphql.Client, clientID string, appSecret string) (string, error) {
var q struct {
GetAccessTokenByAppSecret graphql.String `graphql:"getAccessTokenByAppSecret(secret: $secret, clientId: $id)"`
}
variables := map[string]interface{}{
"id": graphql.String(clientID),
"secret": graphql.String(appSecret),
}
err := client.Query(context.Background(), &q, variables)
if err != nil {
return "", err
}
accessToken := string(q.GetAccessTokenByAppSecret)
return accessToken, err
}
// Encrypt password with PKCS1v15 and encode the encrypted password by base64
func encryptPassword(password []byte) string {
block, _ := pem.Decode([]byte(pubPEM))
if block == nil {
panic("failed to parse PEM block containing the public key")
}
pub, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
panic("failed to parse DER encoded public key: " + err.Error())
}
pubKey := pub.(*rsa.PublicKey)
cipherPassword, err := rsa.EncryptPKCS1v15(rand.Reader, pubKey, password)
if err != nil {
panic("failed to encrypt the password with PKCS1v15" + err.Error())
}
base64Password := base64.StdEncoding.EncodeToString(cipherPassword)
return base64Password
}
//------------------------------------------------------------------------------------
// UserRegisterInput user register mutation parameters needed to fill
type UserRegisterInput struct {
Email graphql.String `json:"email"`
Phone graphql.String `json:"phone"`
Password graphql.String `json:"password"`
RegisterInClient graphql.String `json:"registerInClient"` // FIXME: Mandotory
}
// UserRegisterMutation user register mutation
type UserRegisterMutation struct {
Register struct {
Unionid graphql.String
Email graphql.String
Phone graphql.String
EmailVerified graphql.Boolean
PhoneVerified graphql.Boolean
RegisterInClient graphql.String
RegisterMethod graphql.String
Token graphql.String
TokenExpiredAt graphql.String
// TODO: more needed fields from `ExtendUser`
} `graphql:"register(userInfo: $userInfo)"`
}
// Register new user in your app
func (c *Client) Register(input *UserRegisterInput) (UserRegisterMutation, error) {
var m UserRegisterMutation
password := graphql.String(encryptPassword([]byte(string(input.Password))))
variables := map[string]interface{}{
"userInfo": UserRegisterInput{
Email: input.Email,
Phone: input.Phone,
Password: password,
RegisterInClient: input.RegisterInClient,
},
}
err := c.Client.Mutate(context.Background(), &m, variables)
if err != nil {
// log.Println("Register failed: " + err.Error())
return m, err
}
return m, nil
}
//------------------------------------------------------------------------------------
// UserLoginInput user login mutation parameters needed to fill
type UserLoginInput struct {
Unionid graphql.String `json:"unionid"`
Email graphql.String `json:"email"`
Phone graphql.String `json:"phone"`
Password graphql.String `json:"password"`
LastIP graphql.String `json:"lastIP,omitempty"` // FIXME: Mandotory in struct
RegisterInClient graphql.String `json:"registerInClient"`
VerifyCode graphql.String `json:"verifyCode,omitempty"`
}
// UserLoginMutation user login mutation
type UserLoginMutation struct {
Login struct {
ID graphql.String `graphql:"_id"`
Email graphql.String
EmailVerified graphql.Boolean
Username graphql.String
Nickname graphql.String
Company graphql.String
Photo graphql.String
Browser graphql.String
Token graphql.String
TokenExpiredAt graphql.String
LoginsCount graphql.Int
LastLogin graphql.String
// LastIP graphql.String //FIXME: it may cause `mutation` failed
SignedUp graphql.String
Blocked graphql.Boolean
IsDeleted graphql.Boolean
// TODO: more needed fields from `ExtendUser`
} `graphql:"login(unionid: $unionid, email: $email, phone: $phone, password: $password, lastIP: $lastIP, registerInClient: $registerInClient, verifyCode: $verifyCode)"`
}
// Login your app
func (c *Client) Login(input *UserLoginInput) (UserLoginMutation, error) {
var m UserLoginMutation
password := graphql.String(encryptPassword([]byte(string(input.Password))))
variables := map[string]interface{}{
"unionid": input.Unionid,
"email": input.Email,
"phone": input.Phone,
"password": password,
"lastIP": input.LastIP,
"registerInClient": input.RegisterInClient,
"verifyCode": input.VerifyCode,
}
err := c.Client.Mutate(context.Background(), &m, variables)
if err != nil {
// log.Println("Login failed: " + err.Error())
return m, err
}
return m, nil
}
//------------------------------------------------------------------------------------
// CheckLoginStatusQuery check the login status query
type CheckLoginStatusQuery struct {
CheckLoginStatus struct {
Message graphql.String
Code graphql.Int
Status graphql.Boolean
} `graphql:"checkLoginStatus"`
}
// CheckLoginStatus check the user login status
func (c *Client) CheckLoginStatus() (CheckLoginStatusQuery, error) {
var q CheckLoginStatusQuery
err := c.Client.Query(context.Background(), &q, nil)
if err != nil {
// log.Println("Check login status failed: " + err.Error())
return q, err
}
return q, nil
}
//------------------------------------------------------------------------------------
// UserQueryParameter user query parameters needed to fill
type UserQueryParameter struct {
ID graphql.String `graphql:"_id"`
RegisterInClient graphql.String `json:"registerInClient"`
// Token graphql.String `json:"token,omitempty"` // TODO:
// Auth graphql.Boolean `json:"auth,omitempty"`
// UserLoginHistoryPage graphql.Int `json:"userLoginHistoryPage,omitempty"`
// UserLoginHistoryCount graphql.Int `json:"userLoginHistoryCount,omitempty"`
}
// UserQuery user query
type UserQuery struct {
User struct {
ID graphql.String `graphql:"_id"`
Email graphql.String
Unionid graphql.String
EmailVerified graphql.Boolean
Phone graphql.String
PhoneVerified graphql.Boolean
Username graphql.String
Nickname graphql.String
Photo graphql.String
Company graphql.String
Token graphql.String
TokenExpiredAt graphql.String
LoginsCount graphql.Int
LastLogin graphql.String
SignedUp graphql.String
Blocked graphql.Boolean
IsDeleted graphql.Boolean
// TODO: more fields from `ExtendUser`
} `graphql:"user(id: $id, registerInClient: $registerInClient)"` // TODO: more parameters according to schema
}
// User get the user information by user ID
func (c *Client) User(parameter *UserQueryParameter) (UserQuery, error) {
var q UserQuery
variables := map[string]interface{}{
"id": parameter.ID,
"registerInClient": parameter.RegisterInClient,
}
err := c.Client.Query(context.Background(), &q, variables)
if err != nil {
return q, err
}
return q, nil
}
//------------------------------------------------------------------------------------
// UsersQueryParameter users query parmeters needed to fill
type UsersQueryParameter struct {
RegisterInClient graphql.String `json:"registerInClient,omitempty"`
Page graphql.Int `json:"page,omitempty"`
Count graphql.Int `json:"count,omitempty"`
}
// UsersQuery users query
type UsersQuery struct {
Users struct {
List []struct {
ID graphql.String `graphql:"_id"`
Email graphql.String
Unionid graphql.String
EmailVerified graphql.Boolean
Phone graphql.String
PhoneVerified graphql.Boolean
Username graphql.String
Nickname graphql.String
// TODO: more fields from `ExtendUser`
}
TotalCount graphql.Int
} `graphql:"users(registerInClient: $registerInClient, page: $page, count: $count)"`
}
// Users get all of the users information by page and count/page
func (c *Client) Users(parameter *UsersQueryParameter) (UsersQuery, error) {
var q UsersQuery
variables := map[string]interface{}{
"registerInClient": parameter.RegisterInClient,
"page": parameter.Page, // default: 1
"count": parameter.Count, // default: 10
}
err := c.Client.Query(context.Background(), &q, variables)
if err != nil {
return q, err
}
return q, nil
}
//------------------------------------------------------------------------------------
// RemoveUsersInput remove users input parameters needed to fill
type RemoveUsersInput struct {
IDs []graphql.String `json:"ids"`
RegisterInClient graphql.String `json:"registerInClient"`
// Operator should be your `Authing.cn` account ID
Operator graphql.String `json:"operator"` // no more Mandatory in the latest version
}
// RemoveUsersMutation remove users mutation
type RemoveUsersMutation struct {
RemoveUsers []struct {
ID graphql.String `graphql:"_id"`
Email graphql.String
Unionid graphql.String
} `graphql:"removeUsers(ids: $ids, registerInClient: $registerInClient, operator: $operator)"`
}
// RemoveUsers remove users by user ID
// TODO: need to tune the graphql error response json unmarshal
func (c *Client) RemoveUsers(input *RemoveUsersInput) (RemoveUsersMutation, error) {
var m RemoveUsersMutation
variables := map[string]interface{}{
"ids": input.IDs,
"registerInClient": input.RegisterInClient,
"operator": input.Operator,
}
err := c.Client.Mutate(context.Background(), &m, variables)
if err != nil {
return m, err
}
return m, nil
}
//------------------------------------------------------------------------------------
// UserUpdateInput user update input parameters needed to fill
// TODO: no need all fields
type UserUpdateInput struct {
ID graphql.String `json:"_id"` // Mandotory in struct
Email graphql.String `json:"email,omitempty"`
Unionid graphql.String `json:"unionid,omitempty"`
EmailVerified graphql.Boolean `json:"emailVerified,omitempty"`
Phone graphql.String `json:"phone,omitempty"`
PhoneVerified graphql.Boolean `json:"phoneVerified,omitempty"`
Username graphql.String `json:"username,omitempty"`
Nickname graphql.String `json:"nickname,omitempty"`
Company graphql.String `json:"company,omitempty"`
Photo graphql.String `json:"photo,omitempty"`
Browser graphql.String `json:"browser,omitempty"`
// Password graphql.String `json:"password,omitempty"`
RegisterInClient graphql.String `json:"registerInClient"`
RegisterMethod graphql.String `json:"registerMethod,omitempty"`
// Oauth graphql.String `json:"oauth,omitempty"`
}
// UpdateUserMutation update user mutation
// TODO: no need all fields
type UpdateUserMutation struct {
UpdateUser struct {
Email graphql.String
Unionid graphql.String
EmailVerified graphql.Boolean
Phone graphql.String
PhoneVerified graphql.Boolean
Username graphql.String
Nickname graphql.String
Company graphql.String
Photo graphql.String
Browser graphql.String
Password graphql.String
RegisterInClient graphql.String
RegisterMethod graphql.String
Oauth graphql.String
Token graphql.String
TokenExpiredAt graphql.String
LoginsCount graphql.Int
LastLogin graphql.String
// LastIP graphql.String //FIXME: it may cause `mutation` failed
SignedUp graphql.String
Blocked graphql.Boolean
IsDeleted graphql.Boolean
// OldPassword graphql.String
} `graphql:"updateUser(options: $options)"`
}
// UpdateUser update the user information
func (c *Client) UpdateUser(input *UserUpdateInput) (UpdateUserMutation, error) {
var m UpdateUserMutation
variables := map[string]interface{}{
"options": UserUpdateInput{
ID: input.ID,
Username: input.Username,
Nickname: input.Nickname,
Phone: input.Phone,
RegisterInClient: input.RegisterInClient,
// TODO: more fileds from `UserUpdateInput`
},
}
err := c.Client.Mutate(context.Background(), &m, variables)
if err != nil {
return m, err
}
return m, nil
}
//------------------------------------------------------------------------------------
// SendVerifyEmailInput sendVerifyEmail input parameters needed to fill
type SendVerifyEmailInput struct {
Email graphql.String `json:"email"`
Client graphql.String `json:"client"`
Token graphql.String `json:"token,omitempty"`
}
// SendVerifyEmailMutation sendVerifyEmail mutation
type SendVerifyEmailMutation struct {
SendVerifyEmail struct {
Message graphql.String
Code graphql.Int
Status graphql.Boolean
} `graphql:"sendVerifyEmail(email: $email, client: $client, token: $token)"`
}
// SendVerifyEmail send verify email to user
// TODO: no need to send verify email if EmailVerified is true
func (c *Client) SendVerifyEmail(input *SendVerifyEmailInput) error {
var m SendVerifyEmailMutation
variables := map[string]interface{}{
"email": input.Email,
"client": input.Client,
"token": input.Token,
}
err := c.Client.Mutate(context.Background(), &m, variables)
if err != nil {
return err
}
return nil
}
//------------------------------------------------------------------------------------
// SendResetPasswordEmailInput sendResetPasswordEmail input parameter needed to fill
type SendResetPasswordEmailInput struct {
Client graphql.String
Email graphql.String
}
// SendResetPasswordEmailMutation sendResetPasswordEmail mutation
type SendResetPasswordEmailMutation struct {
SendResetPasswordEmail struct {
Message graphql.String
Code graphql.Int
Status graphql.Boolean
} `graphql:"sendResetPasswordEmail(client: $client, email: $email)"`
}
// SendResetPasswordEmail send reset password email to user
func (c *Client) SendResetPasswordEmail(input *SendResetPasswordEmailInput) error {
var m SendResetPasswordEmailMutation
variables := map[string]interface{}{
"email": input.Email,
"client": input.Client,
}
err := c.Client.Mutate(context.Background(), &m, variables)
if err != nil {
return err
}
return nil
}
//------------------------------------------------------------------------------------
// VerifyResetPasswordVerifyCodeInput verifyResetPaaswordVerifyCode input parameter needed to fill
type VerifyResetPasswordVerifyCodeInput struct {
Client graphql.String
Email graphql.String
VerifyCode graphql.String
}
// VerifyResetPasswordVerifyCodeMutation VerifyResetPasswordVerifyCode mutation
type VerifyResetPasswordVerifyCodeMutation struct {
VerifyResetPasswordVerifyCode struct {
Message graphql.String
Code graphql.Int
Status graphql.Boolean
} `graphql:"verifyResetPasswordVerifyCode(client: $client, email: $email, verifyCode: $verifyCode)"`
}
// VerifyResetPasswordVerifyCode verify reset_password_verify_code
func (c *Client) VerifyResetPasswordVerifyCode(input *VerifyResetPasswordVerifyCodeInput) error {
var m VerifyResetPasswordVerifyCodeMutation
variables := map[string]interface{}{
"email": input.Email,
"client": input.Client,
"verifyCode": input.VerifyCode,
}
err := c.Client.Mutate(context.Background(), &m, variables)
if err != nil {
return err
}
return nil
}
//------------------------------------------------------------------------------------
// ChangePasswordInput changePassword input parameters needed to fill
type ChangePasswordInput struct {
Email graphql.String
Client graphql.String
Password graphql.String
VerifyCode graphql.String
}
// ChangePasswordInputMutation changePassword mutation
type ChangePasswordInputMutation struct {
ChangePassword struct {
Password graphql.String
} `graphql:"changePassword(email: $email,client: $client,password: $password,verifyCode: $verifyCode)"`
}
// ChangePassword change password
func (c *Client) ChangePassword(input *ChangePasswordInput) error {
var m ChangePasswordInputMutation
password := graphql.String(encryptPassword([]byte(string(input.Password))))
variables := map[string]interface{}{
"email": input.Email,
"client": input.Client,
"verifyCode": input.VerifyCode,
"password": password,
}
err := c.Client.Mutate(context.Background(), &m, variables)
if err != nil {
return err
}
return nil
}
//------------------------------------------------------------------------------------
// ReadOauthListQueryParameter ReadOauthList query parameters needed to fill
type ReadOauthListQueryParameter struct {
ClientID graphql.String
DontGetURL graphql.Boolean
}
// ReadOauthListQuery ReadOauthList query
type ReadOauthListQuery struct {
ReadOauthList []struct {
Name graphql.String
Alias graphql.String
Image graphql.String
Description graphql.String
Enabled graphql.Boolean
// Url graphql.String
Client graphql.String
User graphql.String
// TODO more fields from `OAuthList`
} `graphql:"ReadOauthList(clientId: $clientId, dontGetURL: $dontGetURL) "`
}
// ReadOauthList read the oauth list
func (c *Client) ReadOauthList(parameter *ReadOauthListQueryParameter) (ReadOauthListQuery, error) {
var q ReadOauthListQuery
variables := map[string]interface{}{
"clientId": parameter.ClientID,
"dontGetURL": parameter.DontGetURL,
}
err := c.Client.Query(context.Background(), &q, variables)
if err != nil {
return q, err
}
return q, nil
}
//------------------------------------------------------------------------------------