-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws_auth_service.go
156 lines (132 loc) · 3.63 KB
/
aws_auth_service.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
package awsx
import (
"context"
"fmt"
"github.com/lestrrat-go/jwx/v2/jwk"
"github.com/lestrrat-go/jwx/v2/jwt"
"time"
)
type Auth struct {
AuthService
}
type AuthService interface {
DecodeAccessToken(ctx context.Context, accessToken string, options ...AuthOption) (*JWTToken, error)
}
type authService struct {
auth *authClient
jwkCache *jwk.Cache
opts []AuthOption
}
const cognitoURL = "https://cognito-idp.%s.amazonaws.com/%s"
const cognitoPubKeyPath = "/.well-known/jwks.json"
var formattedCognitoURL string
const publicKeyRefreshIntervall = 2880 // minutes = 2 days
// NewAuthService creates a new auth authService.
// The options for the app client id and user pool id needs to be set.
// If additional options are given
// this options will be used for the upcoming requests to the aws client.
func NewAuthService(
opts ...AuthOption,
) (AuthService, error) {
options := applyAuthOptions(opts)
auth := initCognitoClient(options.appClientID, options.userPoolID)
jwkCache := jwk.NewCache(context.Background())
formattedCognitoURL = fmt.Sprintf(cognitoURL, options.awsDefaultRegion, options.userPoolID)
if err := jwkCache.Register(
formattedCognitoURL+cognitoPubKeyPath,
jwk.WithMinRefreshInterval(publicKeyRefreshIntervall*time.Minute),
); err != nil {
return nil, err
}
return &authService{
auth: auth,
jwkCache: jwkCache,
opts: opts,
}, nil
}
// DecodeAccessToken of given accessToken and verifies it against the given realm.
// It converts the JWT sub into the custom claim of the go sso type.
// Returns the jwt.Token and the SsoClaims representation if successful, otherwise an error.
func (s *authService) DecodeAccessToken(
ctx context.Context,
accessToken string,
options ...AuthOption,
) (
*JWTToken,
error,
) {
reqOptions := s.applyOptions(options)
keySet, err := s.jwkCache.Get(ctx, formattedCognitoURL+cognitoPubKeyPath)
if err != nil {
return nil, err
}
token, err := jwt.Parse(
[]byte(accessToken),
jwt.WithKeySet(keySet),
jwt.WithValidate(true),
)
if err != nil {
return nil, err
}
jwtToken := &JWTToken{
Issuer: token.Issuer(),
PrivateClaims: struct {
AuthTime float64
ClientId string
EventId string
OriginJti string
Scope string
TokenUse string
Username string
}{
AuthTime: (token.PrivateClaims()["auth_time"]).(float64),
ClientId: (token.PrivateClaims()["client_id"]).(string),
EventId: (token.PrivateClaims()["event_id"]).(string),
OriginJti: "",
Scope: (token.PrivateClaims()["scope"]).(string),
TokenUse: (token.PrivateClaims()["token_use"]).(string),
Username: (token.PrivateClaims()["username"]).(string),
},
Subject: token.Subject(),
}
err = verifyJWTClaims(jwtToken, reqOptions)
if err != nil {
return nil, err
}
return jwtToken, nil
}
func verifyJWTClaims(token *JWTToken, reqOptions *AuthRequestConfig) error {
if token.Issuer != formattedCognitoURL {
return fmt.Errorf(
"token issuer invalid: issuer %s <> pubKey URL %s",
token.Issuer,
formattedCognitoURL,
)
}
if token.PrivateClaims.TokenUse != "access" {
fmt.Errorf(
"token use invalid: token use %s <> access",
token.PrivateClaims.TokenUse,
)
}
if token.PrivateClaims.ClientId != reqOptions.appClientID {
fmt.Errorf(
"token client id invalid: token use %s <> %s",
token.PrivateClaims.ClientId,
reqOptions.appClientID,
)
}
return nil
}
func (s *authService) applyOptions(options []AuthOption) *AuthRequestConfig {
req := &AuthRequestConfig{}
// per client options apply first
for _, option := range s.opts {
option(req)
}
// per request options
for _, option := range options {
option(req)
}
return req
}