forked from futurenda/google-auth-id-token-verifier
-
Notifications
You must be signed in to change notification settings - Fork 1
/
jwt.go
53 lines (47 loc) · 1.01 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
package googleIDVerifier
import (
"encoding/base64"
"encoding/json"
"errors"
"strings"
"time"
"bytes"
"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
}