-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathjwk_aws_cognito.go
45 lines (37 loc) · 1.23 KB
/
jwk_aws_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
package jwt
import (
"encoding/json"
"errors"
"fmt"
)
// |=========================================================================|
// | Amazon's AWS Cognito integration for token validation and verification. |
// |=========================================================================|
// AWSCognitoError represents an error response from AWS Cognito.
// It implements the error interface.
type AWSCognitoError struct {
StatusCode int
Message string `json:"message"`
}
// Error returns the error message.
func (e AWSCognitoError) Error() string {
return e.Message
}
// FetchAWSCognitoPublicKeys fetches the JSON Web Key Set (JWKS) from the AWS Cognito endpoint
// and returns the public keys as Keys map.
// It returns an error if the request fails or the JWKS is invalid.
func FetchAWSCognitoPublicKeys(region, userPoolID string) (Keys, error) {
url := fmt.Sprintf("https://cognito-idp.%s.amazonaws.com/%s/.well-known/jwks.json", region, userPoolID)
keys, err := FetchPublicKeys(url)
if err != nil {
var httpErr httpError
if errors.As(err, &httpErr) {
var awsErr AWSCognitoError
if jsonErr := json.Unmarshal(httpErr.Body, &awsErr); jsonErr == nil {
return nil, awsErr
}
}
return nil, err
}
return keys, nil
}