-
Notifications
You must be signed in to change notification settings - Fork 3
/
cognito.go
163 lines (137 loc) · 3.52 KB
/
cognito.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
package cognito
import (
"crypto/rsa"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"math/big"
"net/http"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
)
var (
ErrInvalidParam = errors.New("invalid param")
)
//go:generate mockgen -source=cognito.go -package=cognito -destination=mocks/cognito.go
type Client interface {
VerifyToken(tokenStr string) (*jwt.Token, error)
Authorize(c *gin.Context)
}
type Cognito struct {
// AWS App Client ID
ClientId string
// AWS Cognito Issuer
Iss string
// Map of JWKs from AWS Cognito
PublicKeys PublicKeys
}
type PublicKey struct {
Alg string `json:"alg"`
E string `json:"e"`
Kid string `json:"kid"`
Kty string `json:"kty"`
N string `json:"n"`
Use string `json:"use"`
PEM *rsa.PublicKey
}
type PublicKeys map[string]PublicKey
func NewCognitoClient(region, usePoolId, clientId string) (Client, error) {
// validate region and usePoolId, make sure they are present
if region == "" || usePoolId == "" {
return nil, fmt.Errorf("invalid region or use pool id: %w", ErrInvalidParam)
}
iss := fmt.Sprintf("https://cognito-idp.%s.amazonaws.com/%s", region, usePoolId)
pkUrl := fmt.Sprintf("%s/.well-known/jwks.json", iss)
publicKeys, err := getPublicKeys(pkUrl)
if err != nil {
return nil, err
}
return &Cognito{
ClientId: clientId,
Iss: iss,
PublicKeys: publicKeys,
}, nil
}
func (c *Cognito) VerifyToken(tokenStr string) (*jwt.Token, error) {
// parse token and verify signature
token, err := jwt.Parse(tokenStr, func(token *jwt.Token) (interface{}, error) {
// validate token signing method
if alg := token.Method.Alg(); alg != "RS256" {
return nil, fmt.Errorf("invalid signing method %s. signing method must be RS256", alg)
}
return c.getCert(token)
})
if err != nil {
return nil, err
}
// verify claims
// verify audience claim
if !token.Claims.(jwt.MapClaims).VerifyAudience(c.ClientId, false) {
return token, errors.New("audience is invalid")
}
// verify expire time
if !token.Claims.(jwt.MapClaims).VerifyExpiresAt(time.Now().Unix(), true) {
return token, errors.New("token expired")
}
// verify issuer
if !token.Claims.(jwt.MapClaims).VerifyIssuer(c.Iss, true) {
return token, errors.New("iss is invalid")
}
return token, nil
}
func (c *Cognito) getCert(token *jwt.Token) (*rsa.PublicKey, error) {
kid := token.Header["kid"].(string)
key, ok := c.PublicKeys[kid]
if !ok {
return nil, fmt.Errorf("invalid kid %s", kid)
}
return key.PEM, nil
}
func getPublicKeys(iss string) (PublicKeys, error) {
client := &http.Client{
Timeout: time.Second * time.Duration(10),
}
resp, err := client.Get(iss)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respJson := struct {
Keys []PublicKey `json:"keys"`
}{}
if err := json.NewDecoder(resp.Body).Decode(&respJson); err != nil {
return nil, err
}
// iterate through list of keys and assign them to key map
publicKeys := make(map[string]PublicKey)
for _, key := range respJson.Keys {
if pem, err := parsePEM(key); err != nil {
return nil, err
} else {
key.PEM = pem
}
publicKeys[key.Kid] = key
}
return publicKeys, nil
}
func parsePEM(k PublicKey) (*rsa.PublicKey, error) {
if k.Kty != "RSA" {
return nil, fmt.Errorf("KTY %s must be RSA", k.Kty)
}
n, err := base64.RawURLEncoding.DecodeString(k.N)
if err != nil {
return nil, err
}
e := 0
if k.E == "AQAB" || k.E == "AAEAAQ" {
e = 65537
} else {
return nil, fmt.Errorf("E %s is invalid", k.E)
}
return &rsa.PublicKey{
N: new(big.Int).SetBytes(n),
E: e,
}, nil
}