1
- // + build aws
1
+ //go: build aws
2
2
3
3
package main
4
4
@@ -15,7 +15,7 @@ import (
15
15
"github.com/aws/aws-sdk-go-v2/service/marketplacemetering"
16
16
"github.com/aws/aws-sdk-go-v2/service/marketplacemetering/types"
17
17
18
- "github.com/dgrijalva /jwt-go /v4"
18
+ "github.com/golang-jwt /jwt/v4"
19
19
)
20
20
21
21
var (
@@ -61,49 +61,52 @@ func checkAWSEntitlement() error {
61
61
return err
62
62
}
63
63
64
- pk , err := base64 .StdEncoding .DecodeString (pubKeyString )
65
- if err != nil {
66
- return fmt .Errorf ("error decoding Public Key string: %w" , err )
67
- }
68
- pubKey , err := jwt .ParseRSAPublicKeyFromPEM (pk )
69
- if err != nil {
70
- return fmt .Errorf ("error parsing Public Key: %w" , err )
71
- }
64
+ token , err := jwt .ParseWithClaims (* out .Signature , & claims {}, func (token * jwt.Token ) (interface {}, error ) {
65
+ if _ , ok := token .Method .(* jwt.SigningMethodRSAPSS ); ! ok {
66
+ return nil , fmt .Errorf ("unexpected signing method: %v" , token .Header ["alg" ])
67
+ }
72
68
73
- token , err := jwt .ParseWithClaims (* out .Signature , & claims {}, jwt .KnownKeyfunc (jwt .SigningMethodPS256 , pubKey ))
74
- if err != nil {
75
- return fmt .Errorf ("error parsing the JWT token: %w" , err )
76
- }
69
+ pk , err := base64 .StdEncoding .DecodeString (pubKeyString )
70
+ if err != nil {
71
+ return nil , fmt .Errorf ("error decoding Public Key string: %w" , err )
72
+ }
73
+ pubKey , err := jwt .ParseRSAPublicKeyFromPEM (pk )
74
+ if err != nil {
75
+ return nil , fmt .Errorf ("error parsing Public Key: %w" , err )
76
+ }
77
+
78
+ return pubKey , nil
79
+ })
77
80
78
81
if claims , ok := token .Claims .(* claims ); ok && token .Valid {
79
82
if claims .ProductCode != productCode || claims .PublicKeyVersion != pubKeyVersion || claims .Nonce != nonce {
80
83
return fmt .Errorf ("the claims in the JWT token don't match the request" )
81
84
}
82
85
} else {
83
- return fmt .Errorf ("something is wrong with the JWT token" )
86
+ return fmt .Errorf ("something is wrong with the JWT token: %w" , err )
84
87
}
85
-
86
88
return nil
87
89
}
88
90
89
91
type claims struct {
90
- ProductCode string `json:"productCode,omitempty"`
91
- PublicKeyVersion int32 `json:"publicKeyVersion,omitempty"`
92
- IssuedAt * jwt. Time `json:"iat ,omitempty"`
93
- Nonce string `json:"nonce,omitempty"`
92
+ ProductCode string `json:"productCode,omitempty"`
93
+ PublicKeyVersion int32 `json:"publicKeyVersion,omitempty"`
94
+ Nonce string `json:"nonce ,omitempty"`
95
+ jwt. RegisteredClaims
94
96
}
95
97
96
- func (c claims ) Valid (h * jwt. ValidationHelper ) error {
98
+ func (c claims ) Valid () error {
97
99
if c .Nonce == "" {
98
- return & jwt.InvalidClaimsError { Message : "the JWT token doesn't include the Nonce"}
100
+ return jwt .NewValidationError ( " token doesn't include the Nonce", jwt . ValidationErrorClaimsInvalid )
99
101
}
100
102
if c .ProductCode == "" {
101
- return & jwt.InvalidClaimsError { Message : "the JWT token doesn't include the ProductCode"}
103
+ return jwt .NewValidationError ( " token doesn't include the ProductCode", jwt . ValidationErrorClaimsInvalid )
102
104
}
103
105
if c .PublicKeyVersion == 0 {
104
- return & jwt.InvalidClaimsError { Message : "the JWT token doesn't include the PublicKeyVersion"}
106
+ return jwt .NewValidationError ( " token doesn't include the PublicKeyVersion", jwt . ValidationErrorClaimsInvalid )
105
107
}
106
- if err := h .ValidateNotBefore (c .IssuedAt ); err != nil {
108
+
109
+ if err := c .RegisteredClaims .Valid (); err != nil {
107
110
return err
108
111
}
109
112
0 commit comments