-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauthentication.go
338 lines (276 loc) · 8.39 KB
/
authentication.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
// Copyright 2015-2025 Bleemeo
//
// bleemeo.com an infrastructure monitoring solution in the Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bleemeo
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"sync"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)
const tokenPath = "/o/token/"
type (
tokenProvider func(ctx context.Context) (*oauth2.Token, error)
tokenRefresher func(ctx context.Context, refreshToken string) (*oauth2.Token, error)
)
type userAgentTransporter struct {
userAgentHeader string
http.RoundTripper
}
func (t userAgentTransporter) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("User-Agent", t.userAgentHeader)
return t.RoundTripper.RoundTrip(req) //nolint:wrapcheck
}
func wrapTransportWithUserAgent(client *http.Client, userAgentHeader string) *http.Client {
c := *client // Avoid mutating the given client
initialTransport := client.Transport
if initialTransport == nil {
initialTransport = http.DefaultTransport
}
c.Transport = userAgentTransporter{
userAgentHeader: userAgentHeader,
RoundTripper: initialTransport,
}
return &c
}
func newRefresher(endpointURL *url.URL, clientID, clientSecret string, client *http.Client) tokenRefresher {
cfg := oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
Endpoint: oauth2.Endpoint{
TokenURL: endpointURL.JoinPath(tokenPath).String(),
AuthStyle: oauth2.AuthStyleInParams,
},
}
return func(ctx context.Context, refreshToken string) (*oauth2.Token, error) {
ctx = context.WithValue(ctx, oauth2.HTTPClient, client)
refTk := oauth2.Token{
RefreshToken: refreshToken,
}
return cfg.TokenSource(ctx, &refTk).Token()
}
}
type authenticationProvider struct {
l sync.Mutex
// Whether this provider only supports token refresh or not.
refreshOnly bool
clientID, clientSecret string
newOAuthTokenCallback func(token *oauth2.Token)
httpClient *http.Client
newToken tokenProvider
refreshToken tokenRefresher
token *oauth2.Token
}
func newAuthenticationProvider(
endpointURL *url.URL, username, password, initialRefreshToken, clientID, clientSecret string, client *http.Client,
) *authenticationProvider {
client = wrapTransportWithUserAgent(client, defaultUserAgent)
authProvider := authenticationProvider{
clientID: clientID,
clientSecret: clientSecret,
httpClient: client,
refreshToken: newRefresher(endpointURL, clientID, clientSecret, client),
}
if username != "" {
authProvider.refreshOnly = false
authProvider.newToken = credentialsTokenProvider(endpointURL, username, password, clientID, clientSecret, client)
} else {
authProvider.refreshOnly = true
}
if initialRefreshToken != "" {
authProvider.token = &oauth2.Token{
RefreshToken: initialRefreshToken,
}
}
return &authProvider
}
// newCredentialsAuthProvider makes a new token source based on the given credentials.
// New tokens will be fetched with the "password" grant type.
func credentialsTokenProvider(
endpointURL *url.URL, username, password, clientID, clientSecret string, client *http.Client,
) tokenProvider {
cfg := clientcredentials.Config{
ClientID: clientID,
ClientSecret: clientSecret,
TokenURL: endpointURL.JoinPath(tokenPath).String(),
EndpointParams: url.Values{
"grant_type": {"password"},
"username": {username},
"password": {password},
},
AuthStyle: oauth2.AuthStyleInParams,
}
client = wrapTransportWithUserAgent(client, defaultUserAgent)
return func(ctx context.Context) (*oauth2.Token, error) {
return cfg.TokenSource(context.WithValue(ctx, oauth2.HTTPClient, client)).Token()
}
}
func (ap *authenticationProvider) Token(ctx context.Context) (*oauth2.Token, error) {
ap.l.Lock()
defer ap.l.Unlock()
var err error
switch {
case ap.token == nil:
if ap.newToken == nil {
return nil, ErrNoAuthMeanProvided
}
ap.token, err = ap.newToken(ctx)
case !ap.token.Valid():
if ap.token.RefreshToken == "" {
return nil, errTokenHasNoRefresh
}
ap.token, err = ap.refreshToken(ctx, ap.token.RefreshToken)
if err != nil {
if !ap.refreshOnly {
ap.token, err = ap.newToken(ctx)
}
}
default:
return ap.token, nil
}
// A new token has been retrieved (if no error occurred)
if ap.newOAuthTokenCallback != nil && err == nil {
ap.newOAuthTokenCallback(ap.token)
}
return ap.token, err
}
func (ap *authenticationProvider) refetchToken(ctx context.Context) error {
if ap.refreshOnly {
return ErrTokenIsRefreshOnly
}
ap.l.Lock()
defer ap.l.Unlock()
tk, err := ap.newToken(ctx)
if err != nil {
if retErr := new(oauth2.RetrieveError); errors.As(err, &retErr) {
return buildAuthError(tokenPath, retErr)
}
return err
}
ap.token = tk
if ap.newOAuthTokenCallback != nil {
ap.newOAuthTokenCallback(ap.token)
}
return nil
}
func (ap *authenticationProvider) injectHeader(ctx context.Context, req *http.Request) error {
tk, err := ap.Token(ctx)
if err != nil {
if retErr := new(oauth2.RetrieveError); errors.As(err, &retErr) {
return buildAuthError(req.URL.Path, retErr)
}
return fmt.Errorf("failed to retrieve authentication token: %w", err)
}
tk.SetAuthHeader(req)
return nil
}
func (ap *authenticationProvider) logout(ctx context.Context, endpoint string) error {
ap.l.Lock()
defer ap.l.Unlock()
if ap.token == nil || !ap.token.Valid() {
return nil // No need to perform a logout
}
endpointURL, err := url.Parse(endpoint)
if err != nil {
return fmt.Errorf("can't parse endpoint URL: %w", err)
}
reqURL, err := endpointURL.Parse("/o/revoke_token/")
if err != nil {
return fmt.Errorf("can't parse logout URL: %w", err)
}
values := url.Values{
"client_id": {ap.clientID},
"token_type_hint": {"refresh_token"},
"token": {ap.token.RefreshToken},
}
if ap.clientSecret != "" {
values.Set("client_secret", ap.clientSecret)
}
// Revoking the refresh token will also revoke the related access token
body := strings.NewReader(values.Encode())
req, err := http.NewRequestWithContext(ctx, http.MethodPost, reqURL.String(), body)
if err != nil {
return fmt.Errorf("failed to parse logout request: %w", err)
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := ap.httpClient.Do(req)
if err != nil {
// Multiple error verbs are only possible since Go1.20
return fmt.Errorf("%s: %w", ErrTokenRevoke.Error(), err)
}
defer cleanupResponse(resp)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("%w: server replyed with status code %d", ErrTokenRevoke, resp.StatusCode)
}
ap.token = nil
return nil
}
func buildAuthError(reqPath string, retErr *oauth2.RetrieveError) *AuthError {
return &AuthError{
APIError: &APIError{
ReqPath: reqPath,
StatusCode: retErr.Response.StatusCode,
ContentType: retErr.Response.Header.Get("Content-Type"),
Message: retErr.ErrorDescription,
Err: retErr,
Response: retErr.Body,
},
ErrorCode: retErr.ErrorCode,
}
}
func buildAuthErrorFromBody(apiErr *APIError) error {
authErr := AuthError{
APIError: apiErr,
}
var respData struct {
// OAuth response
Error string `json:"error"`
ErrorDescription string `json:"error_description"`
// API response
Detail string `json:"detail"`
Code string `json:"code"`
Messages []struct {
Message string `json:"message"`
} `json:"messages"`
}
if err := json.Unmarshal(apiErr.Response, &respData); err != nil {
authErr.Err = &JSONUnmarshalError{
&jsonError{
Err: err,
DataKind: JsonErrorDataKind_401Details,
Data: apiErr.Response,
},
}
return &authErr
}
switch {
case respData.Error != "":
authErr.ErrorCode = respData.Error
authErr.Message = respData.ErrorDescription
case len(respData.Messages) > 0:
authErr.Message = respData.Messages[0].Message
case respData.Detail != "":
authErr.Message = respData.Detail
default:
authErr.Message = string(apiErr.Response)
}
return &authErr
}