-
Notifications
You must be signed in to change notification settings - Fork 3
/
user.go
261 lines (224 loc) · 5.12 KB
/
user.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package authb
import (
"fmt"
"time"
"github.com/nats-io/jwt/v2"
)
func (u *UserData) Subject() string {
return u.Claim.Subject
}
func (u *UserData) Name() string {
return u.Claim.Name
}
func (u *UserData) IsScoped() bool {
_, ok := u.AccountData.Claim.SigningKeys.GetScope(u.Claim.Issuer)
return ok
}
func (u *UserData) issue(key *Key) error {
token, err := u.AccountData.Operator.SigningService.Sign(u.Claim, key)
if err != nil {
return err
}
uc, err := jwt.DecodeUserClaims(token)
if err != nil {
return err
}
u.Claim = uc
u.Token = token
return nil
}
func (u *UserData) update() error {
if u.BaseData.readOnly {
return fmt.Errorf("account is read-only")
}
issuer := u.Claim.Issuer
k, _, err := u.AccountData.getKey(issuer)
if err != nil {
return err
}
token, err := u.AccountData.Operator.SigningService.Sign(u.Claim, k)
if err != nil {
return err
}
claim, err := jwt.DecodeUserClaims(token)
if err != nil {
return err
}
u.Claim = claim
u.Token = token
u.Loaded = claim.IssuedAt
u.Modified = true
return nil
}
func (u *UserData) MaxSubscriptions() int64 {
return u.Claim.Limits.Subs
}
func (u *UserData) SetMaxSubscriptions(max int64) error {
if u.RejectEdits {
return ErrUserIsScoped
}
u.Claim.Limits.Subs = max
return u.update()
}
func (u *UserData) MaxPayload() int64 {
return u.Claim.Limits.Payload
}
func (u *UserData) SetMaxPayload(max int64) error {
if u.RejectEdits {
return ErrUserIsScoped
}
u.Claim.Limits.Payload = max
return u.update()
}
func (u *UserData) MaxData() int64 {
return u.Claim.Limits.Data
}
func (u *UserData) SetMaxData(max int64) error {
if u.RejectEdits {
return ErrUserIsScoped
}
u.Claim.Limits.Data = max
return u.update()
}
func (u *UserData) SetBearerToken(tf bool) error {
if u.RejectEdits {
return ErrUserIsScoped
}
u.Claim.BearerToken = tf
return u.update()
}
func (u *UserData) BearerToken() bool {
return u.Claim.BearerToken
}
func (u *UserData) ConnectionTypes() ConnectionTypes {
v := &ConnectionTypesImpl{}
v.rejectEdits = u.RejectEdits
v.limits = &u.Claim.UserPermissionLimits
v.accountData = u.AccountData
v.userData = u
return v
}
func (u *UserData) PubPermissions() Permissions {
v := &PermissionsImpl{}
v.rejectEdits = u.RejectEdits
v.pub = true
v.limits = &u.Claim.UserPermissionLimits
v.accountData = u.AccountData
v.userData = u
return v
}
func (u *UserData) SubPermissions() Permissions {
v := &PermissionsImpl{}
v.rejectEdits = u.RejectEdits
v.limits = &u.Claim.UserPermissionLimits
v.accountData = u.AccountData
v.userData = u
return v
}
func (u *UserData) ResponsePermissions() ResponsePermissions {
v := &ResponsePermissionsImpl{}
v.rejectEdits = u.RejectEdits
v.limits = &u.Claim.UserPermissionLimits
v.accountData = u.AccountData
v.userData = u
return v
}
func (u *UserData) ConnectionSources() ConnectionSources {
v := &ConnectionSourcesImpl{}
v.rejectEdits = u.RejectEdits
v.limits = &u.Claim.UserPermissionLimits
v.accountData = u.AccountData
v.userData = u
return v
}
func (u *UserData) ConnectionTimes() ConnectionTimes {
v := &ConnectionTimesImpl{}
v.rejectEdits = u.RejectEdits
v.accountData = u.AccountData
v.userData = u
v.limits = &u.Claim.UserPermissionLimits
return v
}
func (u *UserData) Locale() string {
return u.Claim.Limits.Locale
}
func (u *UserData) SetLocale(locale string) error {
if u.RejectEdits {
return ErrUserIsScoped
}
u.Claim.Limits.Locale = locale
return u.update()
}
func (u *UserData) SetUserPermissionLimits(limits jwt.UserPermissionLimits) error {
if u.RejectEdits {
return ErrUserIsScoped
}
u.Claim.User.UserPermissionLimits = limits
return u.update()
}
func (u *UserData) UserPermissionLimits() jwt.UserPermissionLimits {
return u.Claim.User.UserPermissionLimits
}
func (u *UserData) Creds(expiry time.Duration) ([]byte, error) {
// remember the current configuration
token := u.Token
if expiry > 0 {
defer func() {
// restore the old values
u.Token = token
u.Claim, _ = jwt.DecodeUserClaims(token)
}()
// if we have an expires, set it
u.Claim.Expires = time.Now().Add(expiry).Unix()
if err := u.update(); err != nil {
return nil, err
}
}
return jwt.FormatUserConfig(u.Token, u.Key.Seed)
}
func (u *UserData) Issuer() string {
return u.Claim.Issuer
}
func (u *UserData) IssuerAccount() string {
if u.Claim.IssuerAccount != "" {
return u.Claim.IssuerAccount
}
return u.Claim.Issuer
}
func (u *UserData) Tags() Tags {
return &UserTags{
u: u,
}
}
type UserTags struct {
u *UserData
}
func (ut *UserTags) Add(tag ...string) error {
if err := NotEmpty(tag...); err != nil {
return err
}
ut.u.Claim.Tags.Add(tag...)
return ut.u.update()
}
func (ut *UserTags) Remove(tag string) (bool, error) {
ok := ut.u.Claim.Tags.Contains(tag)
if ok {
ut.u.Claim.Tags.Remove(tag)
err := ut.u.update()
return ok, err
}
return false, nil
}
func (ut *UserTags) Contains(tag string) bool {
return ut.u.Claim.Tags.Contains(tag)
}
func (ut *UserTags) Set(tag ...string) error {
if err := NotEmpty(tag...); err != nil {
return err
}
ut.u.Claim.Tags = tag
return ut.u.update()
}
func (ut *UserTags) All() ([]string, error) {
return ut.u.Claim.Tags, nil
}