-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathgrant_request.go
More file actions
761 lines (637 loc) · 20.2 KB
/
Copy pathgrant_request.go
File metadata and controls
761 lines (637 loc) · 20.2 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
package pubnub
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/url"
"strconv"
"strings"
"time"
"github.com/pubnub/go/v9/pnerr"
)
const grantPath = "/v2/auth/grant/sub-key/%s"
var emptyGrantResponse *GrantResponse
type grantBuilder struct {
opts *grantOpts
}
func newGrantBuilder(pubnub *PubNub) *grantBuilder {
return newGrantBuilderWithContext(pubnub, pubnub.ctx)
}
func newGrantBuilderWithContext(pubnub *PubNub, context Context) *grantBuilder {
builder := grantBuilder{
opts: newGrantOpts(
pubnub,
context,
),
}
return &builder
}
func (b *grantBuilder) Read(read bool) *grantBuilder {
b.opts.Read = read
return b
}
func (b *grantBuilder) Write(write bool) *grantBuilder {
b.opts.Write = write
return b
}
func (b *grantBuilder) Manage(manage bool) *grantBuilder {
b.opts.Manage = manage
return b
}
func (b *grantBuilder) Delete(del bool) *grantBuilder {
b.opts.Delete = del
return b
}
func (b *grantBuilder) Get(get bool) *grantBuilder {
b.opts.Get = get
b.opts.isGetSet = true
return b
}
func (b *grantBuilder) Update(update bool) *grantBuilder {
b.opts.Update = update
b.opts.isUpdateSet = true
return b
}
func (b *grantBuilder) Join(join bool) *grantBuilder {
b.opts.Join = join
b.opts.isJoinSet = true
return b
}
// TTL in minutes for which granted permissions are valid.
//
// Min: 1
// Max: 525600
// Default: 1440
//
// Setting value to 0 will apply the grant indefinitely (forever grant).
func (b *grantBuilder) TTL(ttl int) *grantBuilder {
b.opts.TTL = ttl
b.opts.setTTL = true
return b
}
// AuthKeys sets the AuthKeys for the Grant request.
func (b *grantBuilder) AuthKeys(authKeys []string) *grantBuilder {
b.opts.AuthKeys = authKeys
return b
}
// Channels sets the Channels for the Grant request.
func (b *grantBuilder) Channels(channels []string) *grantBuilder {
b.opts.Channels = channels
return b
}
// ChannelGroups sets the ChannelGroups for the Grant request.
func (b *grantBuilder) ChannelGroups(groups []string) *grantBuilder {
b.opts.ChannelGroups = groups
return b
}
// ChannelGroups sets the ChannelGroups for the Grant request.
func (b *grantBuilder) UUIDs(targetUUIDs []string) *grantBuilder {
b.opts.UUIDs = targetUUIDs
return b
}
// Meta sets the Meta for the Grant request.
func (b *grantBuilder) Meta(meta map[string]interface{}) *grantBuilder {
b.opts.Meta = meta
return b
}
// QueryParam accepts a map, the keys and values of the map are passed as the query string parameters of the URL called by the API.
func (b *grantBuilder) QueryParam(queryParam map[string]string) *grantBuilder {
b.opts.QueryParam = queryParam
return b
}
// GetLogParams returns the user-provided parameters for logging
func (o *grantOpts) GetLogParams() map[string]interface{} {
params := map[string]interface{}{
"AuthKeys": o.AuthKeys,
"Channels": o.Channels,
"ChannelGroups": o.ChannelGroups,
"UUIDs": o.UUIDs,
"Read": o.Read,
"Write": o.Write,
"Manage": o.Manage,
"Delete": o.Delete,
"Get": o.Get,
"Update": o.Update,
"Join": o.Join,
}
if o.setTTL {
params["TTL"] = o.TTL
}
if o.Meta != nil {
params["Meta"] = fmt.Sprintf("%v", o.Meta)
}
return params
}
// Execute runs the Grant request.
func (b *grantBuilder) Execute() (*GrantResponse, StatusResponse, error) {
b.opts.pubnub.loggerManager.LogUserInput(PNLogLevelDebug, PNAccessManagerGrant, b.opts.GetLogParams(), true)
rawJSON, status, err := executeRequest(b.opts)
if err != nil {
return emptyGrantResponse, status, err
}
return newGrantResponse(rawJSON, status)
}
func newGrantOpts(pubnub *PubNub, ctx Context) *grantOpts {
return &grantOpts{
endpointOpts: endpointOpts{
pubnub: pubnub,
ctx: ctx,
},
}
}
type grantOpts struct {
endpointOpts
AuthKeys []string
Channels []string
ChannelGroups []string
UUIDs []string
QueryParam map[string]string
Meta map[string]interface{}
// Stringified permissions
// Setting 'true' or 'false' will apply permissions to level
Read bool
Write bool
Manage bool
Delete bool
Get bool
Update bool
Join bool
// Max: 525600
// Min: 1
// Default: 1440
// Setting 0 will apply the grant indefinitely
TTL int
// nil hacks
setTTL bool
isGetSet bool
isUpdateSet bool
isJoinSet bool
}
func (o *grantOpts) validate() error {
if o.config().PublishKey == "" {
return newValidationError(o, StrMissingPubKey)
}
if o.config().SubscribeKey == "" {
return newValidationError(o, StrMissingSubKey)
}
if o.config().SecretKey == "" {
return newValidationError(o, StrMissingSecretKey)
}
return nil
}
func (o *grantOpts) buildPath() (string, error) {
return fmt.Sprintf(grantPath, o.pubnub.Config.SubscribeKey), nil
}
func (o *grantOpts) buildQuery() (*url.Values, error) {
q := defaultQuery(o.pubnub.Config.UUID, o.pubnub.telemetryManager)
if o.Read {
q.Set("r", "1")
} else {
q.Set("r", "0")
}
if o.Write {
q.Set("w", "1")
} else {
q.Set("w", "0")
}
if o.Manage {
q.Set("m", "1")
} else {
q.Set("m", "0")
}
if o.Delete {
q.Set("d", "1")
} else {
q.Set("d", "0")
}
if o.isGetSet {
if o.Get {
q.Set("g", "1")
} else {
q.Set("g", "0")
}
}
if o.isUpdateSet {
if o.Update {
q.Set("u", "1")
} else {
q.Set("u", "0")
}
}
if o.isJoinSet {
if o.Join {
q.Set("j", "1")
} else {
q.Set("j", "0")
}
}
if len(o.AuthKeys) > 0 {
q.Set("auth", strings.Join(o.AuthKeys, ","))
}
if len(o.Channels) > 0 {
q.Set("channel", strings.Join(o.Channels, ","))
}
if len(o.ChannelGroups) > 0 {
q.Set("channel-group", strings.Join(o.ChannelGroups, ","))
}
if len(o.UUIDs) > 0 {
q.Set("target-uuid", strings.Join(o.UUIDs, ","))
}
if o.setTTL {
if o.TTL >= -1 {
q.Set("ttl", fmt.Sprintf("%d", o.TTL))
}
}
timestamp := time.Now().Unix()
q.Set("timestamp", strconv.Itoa(int(timestamp)))
SetQueryParam(q, o.QueryParam)
return q, nil
}
func (o *grantOpts) operationType() OperationType {
return PNAccessManagerGrant
}
// GrantResponse is the struct returned when the Execute function of Grant is called.
type GrantResponse struct {
Level string
SubscribeKey string
TTL int
Channels map[string]*PNPAMEntityData
ChannelGroups map[string]*PNPAMEntityData
UUIDs map[string]*PNPAMEntityData
ReadEnabled bool
WriteEnabled bool
ManageEnabled bool
DeleteEnabled bool
GetEnabled bool
UpdateEnabled bool
JoinEnabled bool
}
func newGrantResponse(jsonBytes []byte, status StatusResponse) (
*GrantResponse, StatusResponse, error) {
resp := &GrantResponse{}
var value interface{}
err := json.Unmarshal(jsonBytes, &value)
if err != nil {
e := pnerr.NewResponseParsingError("Error unmarshalling response",
io.NopCloser(bytes.NewBufferString(string(jsonBytes))), err)
return emptyGrantResponse, status, e
}
constructedChannels := make(map[string]*PNPAMEntityData)
constructedGroups := make(map[string]*PNPAMEntityData)
constructedUUIDs := make(map[string]*PNPAMEntityData)
grantData, ok := value.(map[string]interface{})
if !ok {
e := newGrantResponseParsingError(jsonBytes, "invalid JSON structure",
fmt.Errorf("expected map[string]interface{}, got %T", value))
return emptyGrantResponse, status, e
}
payload, ok := grantData["payload"]
if !ok {
e := newGrantResponseParsingError(jsonBytes, "missing payload field",
fmt.Errorf("payload field not found in response"))
return emptyGrantResponse, status, e
}
if payload == nil {
e := newGrantResponseParsingError(jsonBytes, "null payload",
fmt.Errorf("payload field is null"))
return emptyGrantResponse, status, e
}
parsedPayload, ok := payload.(map[string]interface{})
if !ok {
e := newGrantResponseParsingError(jsonBytes, "invalid payload structure",
fmt.Errorf("expected map[string]interface{} for payload, got %T", payload))
return emptyGrantResponse, status, e
}
rootAuths, _, err := grantOptionalMap(parsedPayload, "auths")
if err != nil {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, "invalid auths field", err)
}
ttl, _, err := grantOptionalNumber(parsedPayload, "ttl")
if err != nil {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, "invalid ttl field", err)
}
if val, ok := parsedPayload["channel"]; ok {
channelName, ok := val.(string)
if !ok {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, "invalid channel field",
fmt.Errorf("expected string for channel, got %T", val))
}
constructedAuthKeys := make(map[string]*PNAccessManagerKeyData)
entityData := &PNPAMEntityData{
Name: channelName,
}
for key, value := range rootAuths {
keyData, err := createPNAccessManagerKeyData(value, entityData, false)
if err != nil {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, fmt.Sprintf("invalid auth key %q", key), err)
}
constructedAuthKeys[key] = keyData
}
entityData.AuthKeys = constructedAuthKeys
entityData.TTL = int(ttl)
constructedChannels[channelName] = entityData
}
if val, ok := parsedPayload["channel-groups"]; ok {
constructedAuthKey := make(map[string]*PNAccessManagerKeyData)
entityData := &PNPAMEntityData{
Name: "",
}
if groupName, ok := val.(string); ok {
entityData.Name = groupName
for authKeyName, value := range rootAuths {
keyData, err := createPNAccessManagerKeyData(value, entityData, false)
if err != nil {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, fmt.Sprintf("invalid auth key %q", authKeyName), err)
}
constructedAuthKey[authKeyName] = keyData
}
entityData.AuthKeys = constructedAuthKey
constructedGroups[groupName] = entityData
} else if groupMap, ok := val.(map[string]interface{}); ok {
for groupName, value := range groupMap {
valueMap, ok := value.(map[string]interface{})
if !ok {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, fmt.Sprintf("invalid channel group %q", groupName),
fmt.Errorf("expected map[string]interface{}, got %T", value))
}
if keys, ok := valueMap["auths"]; ok {
parsedKeys, ok := keys.(map[string]interface{})
if !ok {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, fmt.Sprintf("invalid auths for channel group %q", groupName),
fmt.Errorf("expected map[string]interface{}, got %T", keys))
}
for keyName, value := range parsedKeys {
keyData, err := createPNAccessManagerKeyData(value, entityData, false)
if err != nil {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, fmt.Sprintf("invalid auth key %q", keyName), err)
}
constructedAuthKey[keyName] = keyData
}
}
if _, err := createPNAccessManagerKeyData(valueMap, entityData, true); err != nil {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, fmt.Sprintf("invalid channel group %q permissions", groupName), err)
}
if val, ok := parsedPayload["ttl"]; ok {
parsedVal, ok := grantNumber(val)
if !ok {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, "invalid ttl field",
fmt.Errorf("expected number for ttl, got %T", val))
}
entityData.TTL = int(parsedVal)
}
entityData.AuthKeys = constructedAuthKey
constructedGroups[groupName] = entityData
}
} else {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, "invalid channel-groups field",
fmt.Errorf("expected string or map[string]interface{}, got %T", val))
}
}
if val, ok := parsedPayload["channels"]; ok {
channelMap, ok := val.(map[string]interface{})
if !ok {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, "invalid channels field",
fmt.Errorf("expected map[string]interface{}, got %T", val))
}
for channelName, value := range channelMap {
channelData, err := fetchChannel(channelName, value, parsedPayload)
if err != nil {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, fmt.Sprintf("invalid channel %q", channelName), err)
}
constructedChannels[channelName] = channelData
}
}
if val, ok := parsedPayload["uuids"]; ok {
uuids, ok := val.(map[string]interface{})
if !ok {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, "invalid uuids field",
fmt.Errorf("expected map[string]interface{}, got %T", val))
}
for uuid, value := range uuids {
uuidData, err := fetchChannel(uuid, value, parsedPayload)
if err != nil {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, fmt.Sprintf("invalid uuid %q", uuid), err)
}
constructedUUIDs[uuid] = uuidData
}
}
level, _ := parsedPayload["level"].(string)
subKey, _ := parsedPayload["subscribe_key"].(string)
resp.Level = level
resp.SubscribeKey = subKey
resp.Channels = constructedChannels
resp.ChannelGroups = constructedGroups
resp.UUIDs = constructedUUIDs
if resp.ReadEnabled, err = parsePerms(parsedPayload, "r"); err != nil {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, "invalid r permission", err)
}
if resp.WriteEnabled, err = parsePerms(parsedPayload, "w"); err != nil {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, "invalid w permission", err)
}
if resp.ManageEnabled, err = parsePerms(parsedPayload, "m"); err != nil {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, "invalid m permission", err)
}
if resp.DeleteEnabled, err = parsePerms(parsedPayload, "d"); err != nil {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, "invalid d permission", err)
}
if resp.GetEnabled, err = parsePerms(parsedPayload, "g"); err != nil {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, "invalid g permission", err)
}
if resp.UpdateEnabled, err = parsePerms(parsedPayload, "u"); err != nil {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, "invalid u permission", err)
}
if resp.JoinEnabled, err = parsePerms(parsedPayload, "j"); err != nil {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, "invalid j permission", err)
}
if r, ok := parsedPayload["ttl"]; ok {
parsedValue, ok := grantNumber(r)
if !ok {
return emptyGrantResponse, status, newGrantResponseParsingError(jsonBytes, "invalid ttl field",
fmt.Errorf("expected number for ttl, got %T", r))
}
resp.TTL = int(parsedValue)
}
return resp, status, nil
}
func newGrantResponseParsingError(jsonBytes []byte, message string, err error) error {
return pnerr.NewResponseParsingError("Error parsing response: "+message,
io.NopCloser(bytes.NewBufferString(string(jsonBytes))), err)
}
func grantNumber(value interface{}) (float64, bool) {
switch typedValue := value.(type) {
case float64:
return typedValue, true
case int:
return float64(typedValue), true
default:
return 0, false
}
}
func grantOptionalNumber(parsedPayload map[string]interface{}, name string) (float64, bool, error) {
value, ok := parsedPayload[name]
if !ok {
return 0, false, nil
}
parsedValue, ok := grantNumber(value)
if !ok {
return 0, true, fmt.Errorf("expected number for %s, got %T", name, value)
}
return parsedValue, true, nil
}
func grantOptionalMap(parsedPayload map[string]interface{}, name string) (map[string]interface{}, bool, error) {
value, ok := parsedPayload[name]
if !ok {
return nil, false, nil
}
parsedValue, ok := value.(map[string]interface{})
if !ok {
return nil, true, fmt.Errorf("expected map[string]interface{} for %s, got %T", name, value)
}
return parsedValue, true, nil
}
func parsePerms(parsedPayload map[string]interface{}, name string) (bool, error) {
if r, ok := parsedPayload[name]; ok {
parsedValue, ok := grantNumber(r)
if !ok {
return false, fmt.Errorf("expected number for %s, got %T", name, r)
}
if parsedValue == float64(1) {
return true, nil
} else {
return false, nil
}
}
return false, nil
}
func fetchChannel(channelName string, value interface{}, parsedPayload map[string]interface{}) (*PNPAMEntityData, error) {
auths := make(map[string]*PNAccessManagerKeyData)
entityData := &PNPAMEntityData{
Name: channelName,
}
valueMap, ok := value.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("expected map[string]interface{}, got %T", value)
}
if val, ok := valueMap["auths"]; ok {
parsedValue, ok := val.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("expected map[string]interface{} for auths, got %T", val)
}
for key, value := range parsedValue {
keyData, err := createPNAccessManagerKeyData(value, entityData, false)
if err != nil {
return nil, fmt.Errorf("invalid auth key %q: %w", key, err)
}
auths[key] = keyData
}
}
if _, err := createPNAccessManagerKeyData(value, entityData, true); err != nil {
return nil, err
}
if val, ok := parsedPayload["ttl"]; ok {
parsedVal, ok := grantNumber(val)
if !ok {
return nil, fmt.Errorf("expected number for ttl, got %T", val)
}
entityData.TTL = int(parsedVal)
}
entityData.AuthKeys = auths
return entityData, nil
}
func readKeyData(val interface{}, keyData *PNAccessManagerKeyData, entityData *PNPAMEntityData, writeToEntityData bool, grantType PNGrantType) error {
parsedValue, ok := grantNumber(val)
if !ok {
return fmt.Errorf("expected number for permission, got %T", val)
}
readValue := false
if parsedValue == float64(1) {
readValue = true
}
if writeToEntityData {
switch grantType {
case PNReadEnabled:
entityData.ReadEnabled = readValue
case PNWriteEnabled:
entityData.WriteEnabled = readValue
case PNManageEnabled:
entityData.ManageEnabled = readValue
case PNDeleteEnabled:
entityData.DeleteEnabled = readValue
case PNGetEnabled:
entityData.GetEnabled = readValue
case PNUpdateEnabled:
entityData.UpdateEnabled = readValue
case PNJoinEnabled:
entityData.JoinEnabled = readValue
}
} else {
switch grantType {
case PNReadEnabled:
keyData.ReadEnabled = readValue
case PNWriteEnabled:
keyData.WriteEnabled = readValue
case PNManageEnabled:
keyData.ManageEnabled = readValue
case PNDeleteEnabled:
keyData.DeleteEnabled = readValue
case PNGetEnabled:
keyData.GetEnabled = readValue
case PNUpdateEnabled:
keyData.UpdateEnabled = readValue
case PNJoinEnabled:
keyData.JoinEnabled = readValue
}
}
return nil
}
func createPNAccessManagerKeyData(value interface{}, entityData *PNPAMEntityData, writeToEntityData bool) (*PNAccessManagerKeyData, error) {
valueMap, ok := value.(map[string]interface{})
if !ok {
return nil, fmt.Errorf("expected map[string]interface{}, got %T", value)
}
keyData := &PNAccessManagerKeyData{}
if val, ok := valueMap["r"]; ok {
if err := readKeyData(val, keyData, entityData, writeToEntityData, PNReadEnabled); err != nil {
return nil, fmt.Errorf("invalid r permission: %w", err)
}
}
if val, ok := valueMap["w"]; ok {
if err := readKeyData(val, keyData, entityData, writeToEntityData, PNWriteEnabled); err != nil {
return nil, fmt.Errorf("invalid w permission: %w", err)
}
}
if val, ok := valueMap["m"]; ok {
if err := readKeyData(val, keyData, entityData, writeToEntityData, PNManageEnabled); err != nil {
return nil, fmt.Errorf("invalid m permission: %w", err)
}
}
if val, ok := valueMap["d"]; ok {
if err := readKeyData(val, keyData, entityData, writeToEntityData, PNDeleteEnabled); err != nil {
return nil, fmt.Errorf("invalid d permission: %w", err)
}
}
if val, ok := valueMap["g"]; ok {
if err := readKeyData(val, keyData, entityData, writeToEntityData, PNGetEnabled); err != nil {
return nil, fmt.Errorf("invalid g permission: %w", err)
}
}
if val, ok := valueMap["u"]; ok {
if err := readKeyData(val, keyData, entityData, writeToEntityData, PNUpdateEnabled); err != nil {
return nil, fmt.Errorf("invalid u permission: %w", err)
}
}
if val, ok := valueMap["j"]; ok {
if err := readKeyData(val, keyData, entityData, writeToEntityData, PNJoinEnabled); err != nil {
return nil, fmt.Errorf("invalid j permission: %w", err)
}
}
if val, ok := valueMap["ttl"]; ok {
parsedVal, ok := grantNumber(val)
if !ok {
return nil, fmt.Errorf("expected number for ttl, got %T", val)
}
entityData.TTL = int(parsedVal)
}
return keyData, nil
}