forked from futurenda/google-auth-id-token-verifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jwt.go
116 lines (102 loc) · 2.31 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
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
package googleAuthIDTokenVerifier
import (
"encoding/base64"
"encoding/json"
"errors"
"strings"
"time"
"bytes"
"fmt"
"golang.org/x/oauth2/jws"
)
var (
nowFn = time.Now
)
func parseJWT(token string) (*jws.Header, *ClaimSet, error) {
s := strings.Split(token, ".")
if len(s) != 3 {
return nil, nil, errors.New("Invalid token received")
}
decodedHeader, err := base64.RawURLEncoding.DecodeString(s[0])
if err != nil {
return nil, nil, err
}
header := &jws.Header{}
err = json.NewDecoder(bytes.NewBuffer(decodedHeader)).Decode(header)
if err != nil {
return nil, nil, err
}
claimSet, err := Decode(token)
if err != nil {
return nil, nil, err
}
return header, claimSet, nil
}
// Decode returns ClaimSet
func Decode(token string) (*ClaimSet, error) {
s := strings.Split(token, ".")
if len(s) != 3 {
return nil, ErrInvalidToken
}
decoded, err := base64.RawURLEncoding.DecodeString(s[1])
if err != nil {
return nil, err
}
c := &ClaimSet{}
err = json.NewDecoder(bytes.NewBuffer(decoded)).Decode(c)
return c, err
}
// VerifySignedJWTWithCerts is golang port of OAuth2Client.prototype.verifySignedJwtWithCerts
func VerifySignedJWTWithCerts(token string, certs *Certs, allowedAuds []string, issuers []string, maxExpiry time.Duration) error {
header, claimSet, err := parseJWT(token)
if err != nil {
return err
}
key := certs.Keys[header.KeyID]
if key == nil {
return ErrPublicKeyNotFound
}
err = jws.Verify(token, key)
if err != nil {
return ErrWrongSignature
}
if claimSet.Iat < 1 {
return ErrNoIssueTimeInToken
}
if claimSet.Exp < 1 {
return ErrNoExpirationTimeInToken
}
now := nowFn()
if claimSet.Exp > now.Unix()+int64(maxExpiry.Seconds()) {
return ErrExpirationTimeTooFarInFuture
}
earliest := claimSet.Iat - int64(ClockSkew.Seconds())
latest := claimSet.Exp + int64(ClockSkew.Seconds())
if now.Unix() < earliest {
return ErrTokenUsedTooEarly
}
if now.Unix() > latest {
return ErrTokenUsedTooLate
}
found := false
for _, issuer := range issuers {
if issuer == claimSet.Iss {
found = true
break
}
}
if !found {
return fmt.Errorf("Wrong issuer: %s", claimSet.Iss)
}
audFound := false
for _, aud := range allowedAuds {
if aud == claimSet.Aud {
audFound = true
break
}
}
if !audFound {
return fmt.Errorf("Wrong aud: %s", claimSet.Aud)
}
return nil
}