-
Notifications
You must be signed in to change notification settings - Fork 53
/
tokens.go
89 lines (79 loc) · 1.65 KB
/
tokens.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
package openid
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/lestrrat-go/jwx/jwk"
"github.com/lestrrat-go/jwx/jwt"
"github.com/lestrrat-go/jwx/jwt/openid"
)
type TokenType string
const (
Opaque TokenType = "opaque"
IDToken TokenType = "id_token"
)
// Claims required for valid ID tokens
var requiredClaims = []string{
"iss",
"sub",
"aud",
"exp",
"iat",
}
func GetTokenType(token string) TokenType {
if isIDToken(token) {
return IDToken
}
return Opaque
}
func isIDToken(token string) bool {
j, err := jwt.ParseString(token,
jwt.WithToken(openid.New()),
jwt.WithValidate(true),
)
if err != nil {
return false
}
for _, claim := range requiredClaims {
if _, ok := j.Get(claim); !ok {
return false
}
}
return true
}
func ValidateIDToken(token string, keySet jwk.Set) (openid.Token, error) {
j, err := jwt.ParseString(token,
jwt.WithKeySet(keySet),
jwt.WithValidate(true),
jwt.WithToken(openid.New()),
)
if err != nil {
return nil, err
}
return j.(openid.Token), nil
}
func FetchUserInfo(endpoint, token string, opts ...ClientOption) (map[string]interface{}, error) {
options := &ClientOptions{
client: http.DefaultClient,
}
options.apply(opts...)
req, err := http.NewRequest("POST", endpoint, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
resp, err := options.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, errors.New(resp.Status)
}
var data map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, err
}
return data, nil
}