-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
66 lines (55 loc) · 1.7 KB
/
utils.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
package authutils
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
)
// ContextKey is used as a type for the UID key for the auth server token on context.Context.
type ContextKey string
const (
// AuthTokenContextKey is used to add/retrieve the Firebase UID on the context
AuthTokenContextKey = ContextKey("UID")
)
// GetLoggedInUserUID returns user information as part of OIDC protocol.
func GetLoggedInUserUID(ctx context.Context) (string, error) {
authToken, err := GetUserTokenFromContext(ctx)
if err != nil {
return "", err
}
return authToken.UserGUID, nil
}
// GetUserTokenFromContext retrieves a slade360 token from the supplied context
func GetUserTokenFromContext(ctx context.Context) (*TokenIntrospectionResponse, error) {
val := ctx.Value(AuthTokenContextKey)
if val == nil {
return nil, fmt.Errorf(
"unable to get auth token from context with key %#v", AuthTokenContextKey)
}
token, ok := val.(*TokenIntrospectionResponse)
if !ok {
return nil, fmt.Errorf("wrong auth token type, got %#v, expected a slade 360 auth token", val)
}
return token, nil
}
// decodeOauthResponse extracts the OAUTH data from the passed response body. It is used when generating or refreshing an access token
func decodeOauthResponse(response *http.Response) (*OAUTHResponse, error) {
data, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
if response.StatusCode >= 300 || response.StatusCode < 200 {
msg := fmt.Sprintf(
"an error occurred while processing your request. detail: %v",
string(data),
)
return nil, fmt.Errorf(msg)
}
var responseData OAUTHResponse
err = json.Unmarshal(data, &responseData)
if err != nil {
return nil, err
}
return &responseData, nil
}