generated from mythrnr/template-lib-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt.go
63 lines (53 loc) · 1.61 KB
/
jwt.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
package paypayopa
import (
"errors"
"fmt"
"github.com/golang-jwt/jwt/v5"
)
type AuthorizationResponseToken struct {
Audience string
Issuer string
ExpiresAt int64
Result UserAuthorizeResult
ProfileIdentifier string
Nonce string
UserAuthorizationID string
ReferenceID string
}
var ErrJWTAudienceNotMatch = errors.New("jwt: audience not match")
type rawToken struct {
Result UserAuthorizeResult `json:"result"`
ProfileIdentifier string `json:"profileIdentifier"`
Nonce string `json:"nonce"`
UserAuthorizationID string `json:"userAuthorizationId"`
ReferenceID string `json:"referenceId"`
Audience string `json:"aud"`
jwt.RegisteredClaims
}
func decodeAuthorizationResponseToken(
creds *Credentials,
token string,
) (*AuthorizationResponseToken, error) {
claims := rawToken{}
if _, err := jwt.ParseWithClaims(
token, &claims,
func(_ *jwt.Token) (interface{}, error) {
return []byte(creds.apiKeySecret), nil
},
); err != nil {
return nil, fmt.Errorf("jwt: invalid: %w", err)
}
if creds.merchantID != claims.Audience {
return nil, ErrJWTAudienceNotMatch
}
return &AuthorizationResponseToken{
Audience: claims.Audience,
Issuer: claims.Issuer,
ExpiresAt: claims.ExpiresAt.Unix(),
Result: claims.Result,
ProfileIdentifier: claims.ProfileIdentifier,
Nonce: claims.Nonce,
UserAuthorizationID: claims.UserAuthorizationID,
ReferenceID: claims.ReferenceID,
}, nil
}