-
Notifications
You must be signed in to change notification settings - Fork 26
/
jwt.go
147 lines (125 loc) · 3.96 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2016 The Serviced Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package auth
import (
"time"
jwt "github.com/dgrijalva/jwt-go"
)
var (
// Verify JWTIdentity implements the Identity interface
_ Identity = &jwtIdentity{}
_ jwt.Claims = &jwtIdentity{}
)
// At sets a fake time, executes the provided
// function, then restores the default time getter,
// making it possible to test time-sensitive stuff
func At(t time.Time, f func()) {
defer func() {
jwt.TimeFunc = time.Now
}()
jwt.TimeFunc = func() time.Time {
return t
}
f()
}
// jwtIdentity is an implementation of the Identity interface based on a JSON
// web token.
type jwtIdentity struct {
Host string `json:"hid,omitempty"`
Pool string `json:"pid,omitempty"`
ExpiresAt int64 `json:"exp,omitempty"`
IssuedAt int64 `json:"iat,omitempty"`
AdminAccess bool `json:"adm,omitempty"`
DFSAccess bool `json:"dfs,omitempty"`
PubKey string `json:"key,omitempty"`
}
// ParseJWTIdentity parses a JSON Web Token string, verifying that it was signed by the master.
func ParseJWTIdentity(token string) (Identity, error) {
claims := &jwtIdentity{}
parsed, err := jwt.ParseWithClaims(token, claims, func(token *jwt.Token) (interface{}, error) {
// Validate the algorithm matches the key
if _, ok := token.Method.(*jwt.SigningMethodRSAPSS); !ok {
return nil, ErrInvalidSigningMethod
}
return GetMasterPublicKey()
})
if err != nil {
if verr, ok := err.(*jwt.ValidationError); ok {
if verr.Errors&jwt.ValidationErrorExpired != 0 {
return nil, ErrIdentityTokenExpired
}
if verr.Errors&(jwt.ValidationErrorNotValidYet|jwt.ValidationErrorIssuedAt) != 0 {
return nil, ErrIdentityTokenNotValidYet
}
if verr.Errors&(jwt.ValidationErrorSignatureInvalid|jwt.ValidationErrorUnverifiable) != 0 {
return nil, ErrIdentityTokenBadSig
}
return nil, verr.Inner
}
return nil, err
}
if claims, ok := parsed.Claims.(*jwtIdentity); ok && parsed.Valid {
return claims, nil
}
return nil, ErrIdentityTokenInvalid
}
// CreateJWTIdentity returns a signed string
func CreateJWTIdentity(hostID, poolID string, admin, dfs bool, pubKeyPEM []byte, expiration time.Duration) (string, int64, error) {
now := jwt.TimeFunc().UTC()
claims := &jwtIdentity{
Host: hostID,
Pool: poolID,
ExpiresAt: now.Add(expiration).Unix(),
IssuedAt: now.Unix(),
AdminAccess: admin,
DFSAccess: dfs,
PubKey: string(pubKeyPEM),
}
token := jwt.NewWithClaims(jwt.SigningMethodPS256, claims)
masterPrivKey, err := getMasterPrivateKey()
if err != nil {
return "", 0, err
}
signed, err := token.SignedString(masterPrivKey)
return signed, claims.ExpiresAt, err
}
func (id *jwtIdentity) Valid() error {
if id.Expired() {
return ErrIdentityTokenExpired
}
now := jwt.TimeFunc().UTC().Unix()
// provide tolerance for clockdrifting slower
if now < (id.IssuedAt - int64(ClockDriftDelta.Seconds())) {
return ErrIdentityTokenNotValidYet
}
return nil
}
func (id *jwtIdentity) Expired() bool {
now := jwt.TimeFunc().UTC().Unix()
return now >= (id.ExpiresAt + int64(ClockDriftDelta.Seconds()))
}
func (id *jwtIdentity) HostID() string {
return id.Host
}
func (id *jwtIdentity) PoolID() string {
return id.Pool
}
func (id *jwtIdentity) HasAdminAccess() bool {
return id.AdminAccess
}
func (id *jwtIdentity) HasDFSAccess() bool {
return id.DFSAccess
}
func (id *jwtIdentity) Verifier() (Verifier, error) {
return RSAVerifierFromPEM([]byte(id.PubKey))
}