-
Notifications
You must be signed in to change notification settings - Fork 12
/
api_credentials.go
56 lines (44 loc) · 940 Bytes
/
api_credentials.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
package cmd
import (
"encoding/json"
"strings"
"github.com/dvsekhvalnov/jose2go/base64url"
)
type APICredentials struct {
APIKey string
APIToken string
}
func apiCredentialsFromAuthResult(ares *authResult) *APICredentials {
return &APICredentials{
APIKey: ares.APIKey,
APIToken: ares.Token,
}
}
func (c *APICredentials) getOperatorID() string {
if c == nil {
return ""
}
return extractOperatorIDFromAPIToken(c.APIToken)
}
type jwtPayload struct {
Operator jwtPayloadOperator `json:"operator"`
}
type jwtPayloadOperator struct {
OperatorID string `json:"operatorId"`
}
func extractOperatorIDFromAPIToken(apiToken string) string {
parts := strings.Split(apiToken, ".")
if len(parts) < 2 {
return ""
}
b64Decoded, err := base64url.Decode(parts[1])
if err != nil {
return ""
}
var jp jwtPayload
err = json.Unmarshal(b64Decoded, &jp)
if err != nil {
return ""
}
return jp.Operator.OperatorID
}