-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
381 lines (311 loc) · 10.2 KB
/
client.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
package authutils
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/go-playground/validator"
"github.com/savannahghi/firebasetools"
"github.com/savannahghi/serverutils"
"moul.io/http2curl"
)
// Client bundles data needed by methods in order to interact with the slade360 auth server API
type Client struct {
client *http.Client
configurations Config
}
// Config holds the necessary authentication configurations for interacting with the slade360 auth server service
type Config struct {
AuthServerEndpoint string `json:"authServerEndpoint"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
GrantType string `json:"grant_type"`
Username string `json:"username"`
Password string `json:"password"`
}
// Validate checks if all required configuration variables are present
func (c *Config) Validate() error {
v := validator.New()
err := v.Struct(c)
return err
}
// NewClient creates a new authutils client
func NewClient(config Config) (*Client, error) {
err := config.Validate()
if err != nil {
fields := ""
for _, i := range err.(validator.ValidationErrors) {
fields += fmt.Sprintf("%s, ", i.Field())
}
err := fmt.Errorf("expected %s to be defined", fields)
return nil, err
}
client := Client{
client: &http.Client{
Timeout: time.Second * 60 * 30,
},
configurations: Config{
AuthServerEndpoint: config.AuthServerEndpoint,
ClientID: config.ClientID,
ClientSecret: config.ClientSecret,
GrantType: config.GrantType,
Username: config.Username,
Password: config.Password,
},
}
_, err = client.Authenticate()
if err != nil {
return nil, fmt.Errorf("unable to initialize server client: %w", err)
}
return &client, nil
}
// Authenticate uses client credentials to log in to a slade360 authentication server
func (c *Client) Authenticate() (*OAUTHResponse, error) {
apiTokenURL := fmt.Sprintf("%s/oauth2/token/", c.configurations.AuthServerEndpoint)
credentials := url.Values{}
credentials.Set("client_id", c.configurations.ClientID)
credentials.Set("client_secret", c.configurations.ClientSecret)
credentials.Set("grant_type", c.configurations.GrantType)
credentials.Set("username", c.configurations.Username)
credentials.Set("password", c.configurations.Password)
encodedCredentials := strings.NewReader(credentials.Encode())
response, err := c.client.Post(apiTokenURL, "application/x-www-form-urlencoded", encodedCredentials)
if err != nil {
return nil, err
}
responseData, err := decodeOauthResponse(response)
if err != nil {
return nil, err
}
return responseData, nil
}
// CreateUser creates a user on slade360 auth server
func (c *Client) CreateUser(ctx context.Context, input *CreateUserPayload) (*CreateUserResponse, error) {
createUserEndpoint := fmt.Sprintf("%s/v1/user/user_roles/", c.configurations.AuthServerEndpoint)
response, err := c.makeRequest(ctx, http.MethodPost, createUserEndpoint, input, "application/json", true, nil, nil)
if err != nil {
return nil, err
}
data, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
if response.StatusCode >= 300 || response.StatusCode < 200 {
msg := fmt.Sprintf(
"error from create user endpoint, status %d and error: %s",
response.StatusCode, string(data),
)
return nil, fmt.Errorf(msg)
}
var dataResponse *CreateUserResponse
err = json.Unmarshal(data, &dataResponse)
if err != nil {
return nil, err
}
return dataResponse, nil
}
// RefreshToken uses the refresh token to obtain a fresh access token
func (c *Client) RefreshToken(ctx context.Context, refreshToken string) (*OAUTHResponse, error) {
if refreshToken == "" {
return nil, fmt.Errorf("unable to get access token from the input")
}
apiTokenURL := fmt.Sprintf("%s/oauth2/token/", c.configurations.AuthServerEndpoint)
credentials := url.Values{}
credentials.Set("client_id", c.configurations.ClientID)
credentials.Set("client_secret", c.configurations.ClientSecret)
credentials.Set("grant_type", "refresh_token")
credentials.Set("refresh_token", refreshToken)
encodedCredentials := strings.NewReader(credentials.Encode())
response, err := c.client.Post(apiTokenURL, "application/x-www-form-urlencoded", encodedCredentials)
if err != nil {
return nil, err
}
responseData, err := decodeOauthResponse(response)
if err != nil {
return nil, err
}
return responseData, nil
}
// LoginUser logs in a user on slade360 auth server using their email and password
func (c *Client) LoginUser(ctx context.Context, input *LoginUserPayload) (*OAUTHResponse, error) {
err := input.Validate()
if err != nil {
return nil, err
}
apiTokenURL := fmt.Sprintf("%s/oauth2/token/", c.configurations.AuthServerEndpoint)
credentials := url.Values{}
credentials.Set("client_id", c.configurations.ClientID)
credentials.Set("client_secret", c.configurations.ClientSecret)
credentials.Set("grant_type", c.configurations.GrantType)
credentials.Set("username", input.Email)
credentials.Set("password", input.Password)
encodedCredentials := strings.NewReader(credentials.Encode())
response, err := c.client.Post(apiTokenURL, "application/x-www-form-urlencoded", encodedCredentials)
if err != nil {
return nil, err
}
responseData, err := decodeOauthResponse(response)
if err != nil {
return nil, err
}
return responseData, nil
}
// ResetPassword is used to reset a user's password on AuthServer
func (c *Client) ResetPassword(ctx context.Context, payload *PasswordResetPayload) (*PasswordResetResponse, error) {
err := payload.Validate()
if err != nil {
return nil, err
}
url := fmt.Sprintf("%s/accounts/password/reset/", c.configurations.AuthServerEndpoint)
extraHeaders := map[string]string{
"origin": payload.Origin,
"X-Variant": payload.Variant,
}
email := EmailResetPayload{
Email: payload.Email,
}
resp, err := c.makeRequest(ctx, http.MethodPost, url, email, "application/json", false, nil, extraHeaders)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respData, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
msg := fmt.Sprintf(
"unable to send password reset instructions. Details: %v",
string(respData),
)
return nil, fmt.Errorf(msg)
}
var message PasswordResetResponse
err = json.Unmarshal(respData, &message)
if err != nil {
return nil, err
}
return &message, nil
}
// ValidateUser validates whether a user exists on the authserver
func (c *Client) ValidateUser(ctx context.Context, authTokens *OAUTHResponse) (*MeResponse, error) {
meURL := fmt.Sprintf("%s/v1/user/me/", c.configurations.AuthServerEndpoint)
resp, err := c.makeRequest(ctx, http.MethodGet, meURL, nil, "application/json", true, authTokens, nil)
if err != nil {
return nil, err
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
msg := fmt.Sprintf(
"an error occurred while processing your request. detail: %v",
string(data),
)
return nil, fmt.Errorf(msg)
}
var responseData MeResponse
err = json.Unmarshal(data, &responseData)
if err != nil {
return nil, err
}
return &responseData, nil
}
// verifyAccessToken is used to introspect a token to determine the active state of the
// OAuth 2.0 access token and to determine meta-information about this token.
func (c *Client) verifyAccessToken(ctx context.Context, accessToken string) (*TokenIntrospectionResponse, error) {
if accessToken == "" {
return nil, fmt.Errorf("unable to get access token from the input")
}
if len(accessToken) > 256 {
return nil, fmt.Errorf("ensure the token has no more than 255 characters")
}
introspectionURL := fmt.Sprintf("%s/v1/app/introspect/", c.configurations.AuthServerEndpoint)
payload := TokenIntrospectionPayload{
TokenType: "access_token",
Token: accessToken,
}
response, err := c.makeRequest(ctx, http.MethodPost, introspectionURL, payload, "application/json", false, nil, nil)
if err != nil {
return nil, err
}
resp, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
var introspectionResponse *TokenIntrospectionResponse
err = json.Unmarshal(resp, &introspectionResponse)
if err != nil {
return nil, err
}
if !introspectionResponse.IsValid {
return nil, fmt.Errorf("the supplied access token is invalid")
}
return introspectionResponse, nil
}
// HasValidSlade360BearerToken returns true with no errors if the request has a valid bearer token in the authorization header.
// Otherwise, it returns false and the error in a map with the key "error"
func (c *Client) HasValidSlade360BearerToken(ctx context.Context, r *http.Request) (bool, map[string]string, *TokenIntrospectionResponse) {
bearerToken, err := firebasetools.ExtractBearerToken(r)
if err != nil {
// this error here will only be returned to the user if all the verification functions in the chain fail
return false, serverutils.ErrorMap(err), nil
}
validToken, err := c.verifyAccessToken(ctx, bearerToken)
if err != nil {
return false, serverutils.ErrorMap(err), nil
}
return true, nil, validToken
}
// makeRequest is a helper function for making http requests
func (c *Client) makeRequest(
ctx context.Context,
method string,
path string,
body interface{},
contentType string,
isAuthenticated bool,
loginCreds *OAUTHResponse,
extraHeaders map[string]string,
) (*http.Response, error) {
client := http.Client{}
encoded, err := json.Marshal(body)
if err != nil {
return nil, err
}
payload := bytes.NewBuffer(encoded)
req, err := http.NewRequestWithContext(ctx, method, path, payload)
if err != nil {
return nil, err
}
if isAuthenticated {
if loginCreds == nil {
loginCreds, err = c.Authenticate()
if err != nil {
return nil, err
}
}
token := fmt.Sprintf("Bearer %s", loginCreds.AccessToken)
req.Header.Set("Authorization", token)
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", contentType)
if extraHeaders != nil {
for header, value := range extraHeaders {
req.Header.Set(header, value)
}
}
command, _ := http2curl.GetCurlCommand(req)
fmt.Println(command)
response, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("an error occurred while sending a HTTP request: %w", err)
}
return response, nil
}