-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
claims_reg.go
166 lines (123 loc) · 3.99 KB
/
claims_reg.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package sjwt
import "time"
const (
// TokenID is a unique identifier for this token
TokenID = "jti"
// Issuer is the principal that issued the token
Issuer = "iss"
// Audience identifies the recipents the token is intended for
Audience = "aud"
// Subject is the subject of the token
Subject = "sub"
// IssuedAt is a timesatamp for when the token was issued
IssuedAt = "iat"
// ExpiresAt is a timestamp for when the token should expire
ExpiresAt = "exp"
// NotBeforeAt is a timestamp for which this token should not be excepted until
NotBeforeAt = "nbf"
)
// SetTokenID will set a random uuid v4 id
func (c Claims) SetTokenID() { c[TokenID] = UUID() }
// DeleteTokenID deletes token id
func (c Claims) DeleteTokenID() { delete(c, TokenID) }
// GetTokenID will get the id set on the Claims
func (c Claims) GetTokenID() (string, error) {
if !c.Has(TokenID) {
return "", ErrNotFound
}
switch val := c[TokenID].(type) {
case string:
return val, nil
}
return "", ErrClaimValueInvalid
}
// SetIssuer will set a string value for the issuer
func (c Claims) SetIssuer(issuer string) { c[Issuer] = issuer }
// DeleteIssuer deletes issuer
func (c Claims) DeleteIssuer() { delete(c, Issuer) }
// GetIssuer will get the issuer set on the Claims
func (c Claims) GetIssuer() (string, error) {
if !c.Has(Issuer) {
return "", ErrNotFound
}
switch val := c[Issuer].(type) {
case string:
return val, nil
}
return "", ErrClaimValueInvalid
}
// SetAudience will set a string value for the audience
func (c Claims) SetAudience(audience []string) { c[Audience] = audience }
// DeleteAudience deletes audience
func (c Claims) DeleteAudience() { delete(c, Audience) }
// GetAudience will get the audience set on the Claims
func (c Claims) GetAudience() ([]string, error) {
if !c.Has(Audience) {
return []string{}, ErrNotFound
}
switch val := c[Audience].(type) {
case []string:
return val, nil
}
return []string{}, ErrClaimValueInvalid
}
// SetSubject will set a subject value
func (c Claims) SetSubject(subject string) { c[Subject] = subject }
// DeleteSubject deletes token id
func (c Claims) DeleteSubject() { delete(c, Subject) }
// GetSubject will get the subject set on the Claims
func (c Claims) GetSubject() (string, error) {
if !c.Has(Subject) {
return "", ErrNotFound
}
switch val := c[Subject].(type) {
case string:
return val, nil
}
return "", ErrClaimValueInvalid
}
// SetIssuedAt will set an issued at timestamp in nanoseconds
func (c Claims) SetIssuedAt(issuedAt time.Time) { c[IssuedAt] = issuedAt.Unix() }
// DeleteIssuedAt deletes issued at
func (c Claims) DeleteIssuedAt() { delete(c, IssuedAt) }
// GetIssuedAt will get the issued at timestamp set on the Claims
func (c Claims) GetIssuedAt() (int64, error) {
if !c.Has(IssuedAt) {
return 0, ErrNotFound
}
issuedAt, err := c.GetInt(IssuedAt)
if err != nil {
return 0, ErrClaimValueInvalid
}
return int64(issuedAt), nil
}
// SetExpiresAt will set an expires at timestamp in nanoseconds
func (c Claims) SetExpiresAt(expiresAt time.Time) { c[ExpiresAt] = expiresAt.Unix() }
// DeleteExpiresAt deletes expires at
func (c Claims) DeleteExpiresAt() { delete(c, ExpiresAt) }
// GetExpiresAt will get the expires at timestamp set on the Claims
func (c Claims) GetExpiresAt() (int64, error) {
if !c.Has(ExpiresAt) {
return 0, ErrNotFound
}
expiresAt, err := c.GetInt(ExpiresAt)
if err != nil {
return 0, ErrClaimValueInvalid
}
return int64(expiresAt), nil
}
// SetNotBeforeAt will set an not before at timestamp in nanoseconds
func (c Claims) SetNotBeforeAt(notbeforeAt time.Time) { c[NotBeforeAt] = notbeforeAt.Unix() }
// DeleteNotBeforeAt deletes not before at
func (c Claims) DeleteNotBeforeAt() { delete(c, NotBeforeAt) }
// GetNotBeforeAt will get the not before at timestamp set on the Claims
func (c Claims) GetNotBeforeAt() (int64, error) {
if !c.Has(NotBeforeAt) {
return 0, ErrNotFound
}
notBeforeAt, err := c.GetInt(NotBeforeAt)
if err != nil {
return 0, ErrClaimValueInvalid
}
return int64(notBeforeAt), nil
}