-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
78 lines (67 loc) · 1.46 KB
/
auth.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
/**
* @Package brickset
* @Time: 2022/2/8 5:35 PM
* @Author: wuhb
* @File: auth.go
*/
package brickset
import (
"context"
"net/url"
)
type auth struct {
apiKey string
username string
password string
client IClient
}
func (a auth) CheckKey(ctx context.Context, key string) (bool, error) {
if key == "" {
key = a.apiKey
}
resp := &CommonResponse{}
req := url.Values{}
req.Set("apiKey", key)
err := a.client.GetJSON(ctx, checkKeyURL, req, resp)
if err != nil {
return false, err
}
return resp.IsSuccess(), resp.Error()
}
func (a auth) Login(ctx context.Context) (string, error) {
response := &CommonResponse{}
req := url.Values{}
req.Add("apiKey", a.apiKey)
req.Add("username", a.username)
req.Add("password", a.password)
err := a.client.GetJSON(ctx, loginURL, req, response)
if err != nil {
return "", err
}
if response.IsSuccess() {
return response.Hash, nil
}
return "", response.Error()
}
func (a *auth) CheckUserHash(ctx context.Context, hash string) (bool, error) {
response := &CommonResponse{}
req := url.Values{}
req.Add("apiKey", a.apiKey)
req.Add("userHash", hash)
err := a.client.GetJSON(ctx, checkUserHashURL, req, response)
if err != nil {
return false, err
}
if response.IsSuccess() {
return true, nil
}
return false, response.Error()
}
func NewAuth(apiKey, username, password string, client IClient) IBrickAuth {
return &auth{
apiKey: apiKey,
username: username,
password: password,
client: client,
}
}