-
Notifications
You must be signed in to change notification settings - Fork 0
/
authClient.go
154 lines (144 loc) · 4.78 KB
/
authClient.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
package ylyOpenApi
import (
"encoding/json"
"github.com/google/uuid"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
)
type OAuthClient struct {
config Config
}
func NewAuthClient(conf Config) OAuthClient {
auth := OAuthClient{}
auth.SetConfig(conf)
return auth
}
func (oauth *OAuthClient) SetConfig(conf Config) {
oauth.config = conf
}
//获取授权url API
//redirectUri 回调地址
//state 用于保持请求和回调的状态,在回调时,会回传该参数。开发者可以用这个参数验证请求有效性,也可以记录用户请求授权页前的位置。可防止CSRF攻击
func (oauth *OAuthClient) GetAuthUrl(redirectUri string, state string) string{
params := url.Values{}
responseType := "code"
params.Set("client_id", oauth.config.clientId)
params.Set("response_type", responseType)
params.Set("redirect_uri", redirectUri)
params.Set("state", state)
oauth.config.info("GetAuthUrl[req]:" + string(params.Encode()))
host := oauth.config.GetHost()
authUrl := host + "/oauth/authorize?" + params.Encode()
return authUrl
}
//自有应用获取调用凭证API
func (oauth *OAuthClient) GetAccessToken() Token {
params := url.Values{}
t := time.Now().Unix()
timestamp := strconv.FormatInt(t, 10)
sign := oauth.config.GetSign(timestamp)
params.Set("client_id", oauth.config.clientId)
params.Set("grant_type", "client_credentials")
params.Set("scope","all")
params.Set("timestamp", timestamp)
params.Set("sign", sign)
params.Set("id", oauth.GetUUID4())
oauth.config.info("GetAccessToken[req]:" + string(params.Encode()))
host := oauth.config.GetHost()
reqUrl := host + "/oauth/oauth"
resp, _ := http.PostForm(reqUrl, params)
body, _ := ioutil.ReadAll(resp.Body)
oauth.config.info("GetAccessToken[res]:" + string(body))
defer resp.Body.Close()
var token Token
_ = json.Unmarshal(body, &token)
return token
}
//开放型应用获取调用凭API
//code 授权码
func (oauth *OAuthClient) GetAccessTokenByAuthCode(code string) Token {
params := url.Values{}
t := time.Now().Unix()
timestamp := strconv.FormatInt(t, 10)
sign := oauth.config.GetSign(timestamp)
params.Set("client_id", oauth.config.clientId)
params.Set("grant_type", "authorization_code")
params.Set("code", code)
params.Set("scope","all")
params.Set("timestamp", timestamp)
params.Set("sign", sign)
params.Set("id", oauth.GetUUID4())
oauth.config.info("GetAccessTokenByAuthCode[req]:" + string(params.Encode()))
host := oauth.config.GetHost()
reqUrl := host + "/oauth/oauth"
resp, _ := http.PostForm(reqUrl, params)
body, _ := ioutil.ReadAll(resp.Body)
oauth.config.info("GetAccessTokenByAuthCode[res]:" + string(body))
defer resp.Body.Close()
var token Token
_ = json.Unmarshal(body, &token)
return token
}
//自有应用&开放型应用刷新调用凭证API
//需刷新令牌refresh_token
func (oauth *OAuthClient) GetAccessTokenByRefreshToken(refreshToken string) Token {
params := url.Values{}
t := time.Now().Unix()
timestamp := strconv.FormatInt(t, 10)
sign := oauth.config.GetSign(timestamp)
params.Set("client_id", oauth.config.clientId)
params.Set("grant_type", "refresh_token")
params.Set("refresh_token", refreshToken)
params.Set("scope","all")
params.Set("timestamp", timestamp)
params.Set("sign", sign)
params.Set("id", oauth.GetUUID4())
oauth.config.info("GetAccessTokenByRefreshToken[req]:" + string(params.Encode()))
host := oauth.config.GetHost()
reqUrl := host + "/oauth/oauth"
resp, _ := http.PostForm(reqUrl, params)
body, _ := ioutil.ReadAll(resp.Body)
oauth.config.info("GetAccessTokenByRefreshToken[res]:" + string(body))
defer resp.Body.Close()
var token Token
_ = json.Unmarshal(body, &token)
return token
}
//极速授权获取调用凭证API 注意:仅支持开放型应用
//machineCode 终端号
//secret 密钥(可能是msign或qr_key)
func (oauth *OAuthClient) GetAccessTokenBySecret(machineCode string, secret string, secretType int) Token {
params := url.Values{}
t := time.Now().Unix()
timestamp := strconv.FormatInt(t, 10)
sign := oauth.config.GetSign(timestamp)
params.Set("client_id", oauth.config.clientId)
params.Set("machine_code", machineCode)
if secretType == 1 {
params.Set("qr_key", secret)
}else {
params.Set("msign", secret)
}
params.Set("scope", "all")
params.Set("timestamp", timestamp)
params.Set("sign", sign)
params.Set("id", oauth.GetUUID4())
oauth.config.info("GetAccessTokenByQrKey[req]:" + string(params.Encode()))
host := oauth.config.GetHost()
reqUrl := host + "/oauth/scancodemodel"
resp, _ := http.PostForm(reqUrl, params)
body, _ := ioutil.ReadAll(resp.Body)
oauth.config.info("GetAccessTokenByQrKey[res]:" + string(body))
defer resp.Body.Close()
var token Token
_ = json.Unmarshal(body, &token)
return token
}
//获取UUID4
func (oauth *OAuthClient) GetUUID4() string {
u4 := uuid.New()
return u4.String()
}