-
Notifications
You must be signed in to change notification settings - Fork 1
/
claims.go
97 lines (84 loc) · 2.42 KB
/
claims.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
package jwt
import "time"
// IValidateExp describes an implementation to check the
// claims 'exp' value against the given current time.
type IValidateExp interface {
ValidateExp(now time.Time) bool
}
// IValidateNbf describes an implementation to check the
// claims 'nbf' value against the given current time.
type IValidateNbf interface {
ValidateNbf(now time.Time) bool
}
// PublicClaims contains general public clains as
// specified in RFC7519, Section 4.1.
//
// This struct also implements IValidateExp and
// IValidateNbf to validate the timings of the
// claims.
//
// You can simply extend these claims by your custom
// ones by setting the PublicClaims as an anonymous
// field in your claims model.
// Example:
// type MyClains struct {
// PublicClaims
//
// UserID string `json:"uid"`
// }
//
// claims := new(MyClains)
// claims.UserID = "123"
// claims.SetExpDuration(15 * time.Minute)
//
// Reference:
// https://www.rfc-editor.org/rfc/rfc7519.html#section-4.1
type PublicClaims struct {
Iss string `json:"iss,omitempty"` // Issuer
Sub string `json:"sub,omitempty"` // Subject
Aud string `json:"aud,omitempty"` // Audience
Exp int64 `json:"exp,omitempty"` // UNIX Expiration Time
Nbf int64 `json:"nbf,omitempty"` // UNIX Not Before Time
Iat int64 `json:"iat,omitempty"` // UNIX Issued At Time
Jti string `json:"jti,omitempty"` // JWT ID
}
func (t PublicClaims) ValidateExp(now time.Time) bool {
if t.Exp == 0 {
return true
}
return now.Before(time.Unix(t.Exp, 0))
}
func (t PublicClaims) ValidateNbf(now time.Time) bool {
if t.Nbf == 0 {
return true
}
return now.After(time.Unix(t.Nbf, 0))
}
// SetExpTime sets 'exp' to the given time.
func (t *PublicClaims) SetExpTime(tm time.Time) {
t.Exp = tm.Unix()
}
// SetExpDuration sets 'exp' to the time in the given duration.
func (t *PublicClaims) SetExpDuration(duration time.Duration) {
t.SetExpTime(time.Now().Add(duration))
}
// SetNbfTime sets 'nbf' to the given time.
func (t *PublicClaims) SetNbfTime(tm time.Time) {
t.Nbf = tm.Unix()
}
// SetNbfDuration sets 'nbf' to the time in the given duration.
func (t *PublicClaims) SetNbfDuration(duration time.Duration) {
t.SetNbfTime(time.Now().Add(duration))
}
// SetIat sets 'iat' to the current time.
//
// You can also pass a custom time to be set.
func (t *PublicClaims) SetIat(tm ...time.Time) {
var st time.Time
if len(tm) != 0 {
st = tm[0]
} else {
st = time.Now()
}
t.Iat = st.Unix()
}