forked from vast-data/go-vast-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.go
More file actions
236 lines (213 loc) · 5.45 KB
/
auth.go
File metadata and controls
236 lines (213 loc) · 5.45 KB
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
package vast_client
import (
"bytes"
"crypto/tls"
"encoding/json"
"io"
"net/http"
"net/url"
"strconv"
"time"
)
var authenticators []Authenticator
type Authenticator interface {
authorize() error
setAuthHeader(headers *http.Header)
equal(other Authenticator) bool
setInitialized(bool)
}
// createAuthenticator creates a new Authenticator instance based on the provided VMSConfig.
// Each session gets its own authenticator instance to avoid global state issues.
func createAuthenticator(config *VMSConfig) (Authenticator, error) {
var authenticator Authenticator
if config.Username != "" && config.Password != "" {
authenticator = &JWTAuthenticator{
Host: config.Host,
Port: config.Port,
SslVerify: config.SslVerify,
Username: config.Username,
Password: config.Password,
Tenant: config.Tenant,
Token: &jwtToken{},
}
}
if config.ApiToken != "" {
authenticator = &ApiRTokenAuthenticator{
Host: config.Host,
Port: config.Port,
SslVerify: config.SslVerify,
Token: config.ApiToken,
Tenant: config.Tenant,
}
}
if authenticator != nil {
for _, existingAuthenticator := range authenticators {
if existingAuthenticator.equal(authenticator) {
return existingAuthenticator, nil
}
}
if err := authenticator.authorize(); err != nil {
return nil, err
}
authenticators = append(authenticators, authenticator)
return authenticator, nil
}
panic("CreateAuthenticator: neither username/password nor apiToken are provided")
}
type jwtToken struct {
Access string `json:"access"`
Refresh string `json:"refresh"`
}
type JWTAuthenticator struct {
Host string
Port uint64
SslVerify bool
Username string
Password string
Token *jwtToken
Tenant string
initialized bool
}
func parseToken(rsp *http.Response) (*jwtToken, error) {
var tokens jwtToken
out, e := io.ReadAll(rsp.Body)
if e != nil {
return nil, e
}
e = json.Unmarshal(out, &tokens)
if e != nil {
return nil, e
}
return &tokens, nil
}
func (auth *JWTAuthenticator) refreshToken(client *http.Client) (*http.Response, error) {
path := url.URL{
Scheme: "https",
Host: auth.Host,
Path: "api/token/refresh/",
}
body, err := json.Marshal(map[string]string{"refresh": auth.Token.Refresh})
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, path.String(), bytes.NewBuffer(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
if auth.Tenant != "" {
req.Header.Set("X-Tenant-Name", auth.Tenant)
}
return client.Do(req)
}
func (auth *JWTAuthenticator) acquireToken(client *http.Client) (*http.Response, error) {
// obtain new access & refresh tokens
userPass := map[string]string{"username": auth.Username, "password": auth.Password}
server := auth.Host + ":" + strconv.FormatUint(auth.Port, 10)
body, err := json.Marshal(userPass)
if err != nil {
return nil, err
}
// Generate URL to obtain token keys
path := url.URL{
Scheme: "https",
Host: server,
Path: "api/token/",
}
req, err := http.NewRequest(http.MethodPost, path.String(), bytes.NewBuffer(body))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
if auth.Tenant != "" {
req.Header.Set("X-Tenant-Name", auth.Tenant)
}
return client.Do(req)
}
func (auth *JWTAuthenticator) authorize() error {
var (
resp *http.Response
err error
)
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: !auth.SslVerify},
}
client := &http.Client{
Transport: tr,
Timeout: 20 * time.Second,
}
if auth.initialized {
resp, err = auth.refreshToken(client)
} else {
resp, err = auth.acquireToken(client)
auth.setInitialized(true)
}
if err != nil {
return err
}
if resp != nil {
defer resp.Body.Close()
}
if err = validateResponse(resp, auth.Host, auth.Port); err != nil {
return err
}
// Read response
token, err := parseToken(resp)
if err != nil {
return err
}
auth.Token = token
return nil
}
func (auth *JWTAuthenticator) setAuthHeader(headers *http.Header) {
headers.Add("Authorization", "Bearer "+auth.Token.Access)
if auth.Tenant != "" {
headers.Add("X-Tenant-Name", auth.Tenant)
}
}
func (auth *JWTAuthenticator) equal(other Authenticator) bool {
otherAuth, ok := other.(*JWTAuthenticator)
if !ok {
return false
}
return auth.Username == otherAuth.Username &&
auth.Password == otherAuth.Password &&
auth.Host == otherAuth.Host &&
auth.Port == otherAuth.Port &&
auth.Tenant == otherAuth.Tenant &&
auth.SslVerify == otherAuth.SslVerify
}
func (auth *JWTAuthenticator) setInitialized(state bool) {
auth.initialized = state
}
type ApiRTokenAuthenticator struct {
Host string
Port uint64
SslVerify bool
Token string
Tenant string
}
func (auth *ApiRTokenAuthenticator) authorize() error {
// No-op for ApiRTokenAuthenticator
return nil
}
func (auth *ApiRTokenAuthenticator) setAuthHeader(headers *http.Header) {
headers.Add("Authorization", "Api-Token "+auth.Token)
if auth.Tenant != "" {
headers.Add("X-Tenant-Name", auth.Tenant)
}
}
func (auth *ApiRTokenAuthenticator) equal(other Authenticator) bool {
otherAuth, ok := other.(*ApiRTokenAuthenticator)
if !ok {
return false
}
return auth.Token == otherAuth.Token &&
auth.Host == otherAuth.Host &&
auth.Port == otherAuth.Port &&
auth.Tenant == otherAuth.Tenant &&
auth.SslVerify == otherAuth.SslVerify
}
func (auth *ApiRTokenAuthenticator) setInitialized(_ bool) {
// No-op
}