forked from pubnub/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grant_common.go
377 lines (325 loc) · 9.81 KB
/
grant_common.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
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
package pubnub
import (
"bytes"
"encoding/base64"
"strings"
cbor "github.com/brianolson/cbor_go"
)
// PNGrantBitMask is the type for perms BitMask
type PNGrantBitMask int64
const (
// PNRead Read Perms
PNRead PNGrantBitMask = 1
// PNWrite Write Perms
PNWrite = 2
// PNManage Manage Perms
PNManage = 4
// PNDelete Delete Perms
PNDelete = 8
// PNCreate Create Perms
PNCreate = 16
// PNGet Get Perms
PNGet = 32
// PNUpdate Update Perms
PNUpdate = 64
// PNJoin Join Perms
PNJoin = 128
)
// PNGrantType grant types
type PNGrantType int
const (
// PNReadEnabled Read Enabled. Applies to Subscribe, History, Presence, Objects
PNReadEnabled PNGrantType = 1 + iota
// PNWriteEnabled Write Enabled. Applies to Publish, Objects
PNWriteEnabled
// PNManageEnabled Manage Enabled. Applies to Channel-Groups, Objects
PNManageEnabled
// PNDeleteEnabled Delete Enabled. Applies to History, Objects
PNDeleteEnabled
// PNCreateEnabled Create Enabled. Applies to Objects
PNCreateEnabled
// PNGetEnabled Get Enabled. Applies to Objects
PNGetEnabled
// PNUpdateEnabled Update Enabled. Applies to Objects
PNUpdateEnabled
// PNJoinEnabled Join Enabled. Applies to Objects
PNJoinEnabled
)
// PNResourceType grant types
type PNResourceType int
const (
// PNChannels for channels
PNChannels PNResourceType = 1 + iota
// PNGroups for groups
PNGroups
// PNUsers for users
PNUUIDs
)
// ChannelPermissions contains all the acceptable perms for channels
type ChannelPermissions struct {
Read bool
Write bool
Delete bool
Get bool
Manage bool
Update bool
Join bool
}
type SpacePermissions ChannelPermissions
func toChannelsPermissionsMap(spacesPermissions map[SpaceId]SpacePermissions) map[string]ChannelPermissions {
var channelsPermissions = make(map[string]ChannelPermissions)
for name, p := range spacesPermissions {
channelsPermissions[string(name)] = p.toChannelPermissions()
}
return channelsPermissions
}
func toChannelPatternsPermissionsMap(spacesPermissions map[string]SpacePermissions) map[string]ChannelPermissions {
var channelsPermissions = make(map[string]ChannelPermissions)
for name, p := range spacesPermissions {
channelsPermissions[name] = p.toChannelPermissions()
}
return channelsPermissions
}
func (p SpacePermissions) toChannelPermissions() ChannelPermissions {
return ChannelPermissions{
Read: p.Read,
Write: p.Write,
Delete: p.Delete,
Get: p.Get,
Manage: p.Manage,
Update: p.Update,
Join: p.Join,
}
}
// GroupPermissions contains all the acceptable perms for groups
type GroupPermissions struct {
Read bool
Manage bool
}
type UUIDPermissions struct {
Get bool
Update bool
Delete bool
}
type UserPermissions UUIDPermissions
func toUUIDsPermissionsMap(usersPermissions map[UserId]UserPermissions) map[string]UUIDPermissions {
var channelsPermissions = make(map[string]UUIDPermissions)
for name, p := range usersPermissions {
channelsPermissions[string(name)] = p.toUUIDPermissions()
}
return channelsPermissions
}
func toUUIDPatternsPermissionsMap(usersPermissions map[string]UserPermissions) map[string]UUIDPermissions {
var channelsPermissions = make(map[string]UUIDPermissions)
for name, p := range usersPermissions {
channelsPermissions[name] = p.toUUIDPermissions()
}
return channelsPermissions
}
func (p UserPermissions) toUUIDPermissions() UUIDPermissions {
return UUIDPermissions{
Delete: p.Delete,
Get: p.Get,
Update: p.Update,
}
}
// PNPAMEntityData is the struct containing the access details of the channels.
type PNPAMEntityData struct {
Name string
AuthKeys map[string]*PNAccessManagerKeyData
ReadEnabled bool
WriteEnabled bool
ManageEnabled bool
DeleteEnabled bool
GetEnabled bool
UpdateEnabled bool
JoinEnabled bool
TTL int
}
// PNAccessManagerKeyData is the struct containing the access details of the channel groups.
type PNAccessManagerKeyData struct {
ReadEnabled bool
WriteEnabled bool
ManageEnabled bool
DeleteEnabled bool
GetEnabled bool
UpdateEnabled bool
JoinEnabled bool
TTL int
}
// GetPermissions decodes the CBORToken
func GetPermissions(token string) (PNGrantTokenDecoded, error) {
token = strings.Replace(token, "-", "+", -1)
token = strings.Replace(token, "_", "/", -1)
if i := len(token) % 4; i != 0 {
token += strings.Repeat("=", 4-i)
}
var cborObject PNGrantTokenDecoded
value, decodeErr := base64.StdEncoding.DecodeString(token)
if decodeErr != nil {
return cborObject, decodeErr
}
c := cbor.NewDecoder(bytes.NewReader(value))
err1 := c.Decode(&cborObject)
if err1 != nil {
return cborObject, err1
}
return cborObject, nil
}
type PNToken struct {
Version int
Timestamp int64
TTL int
AuthorizedUUID string
Resources PNTokenResources
Patterns PNTokenResources
Meta map[string]interface{}
}
type PNTokenResources struct {
Channels map[string]ChannelPermissions
ChannelGroups map[string]GroupPermissions
UUIDs map[string]UUIDPermissions
}
func ParseToken(token string) (*PNToken, error) {
permissions, err := GetPermissions(token)
if err != nil {
return nil, err
}
resources := grantResourcesToPNTokenResources(permissions.Resources)
patterns := grantResourcesToPNTokenResources(permissions.Patterns)
return &PNToken{
Version: permissions.Version,
Meta: permissions.Meta,
TTL: permissions.TTL,
Timestamp: permissions.Timestamp,
AuthorizedUUID: permissions.AuthorizedUUID,
Resources: resources,
Patterns: patterns,
}, nil
}
func grantResourcesToPNTokenResources(grantResources GrantResources) PNTokenResources {
tokenResources := PNTokenResources{
Channels: make(map[string]ChannelPermissions),
ChannelGroups: make(map[string]GroupPermissions),
UUIDs: make(map[string]UUIDPermissions),
}
for k, v := range grantResources.Channels {
tokenResources.Channels[k] = parseGrantPerms(v, PNChannels).(ChannelPermissions)
}
for k, v := range grantResources.Groups {
tokenResources.ChannelGroups[k] = parseGrantPerms(v, PNGroups).(GroupPermissions)
}
for k, v := range grantResources.UUIDs {
tokenResources.UUIDs[k] = parseGrantPerms(v, PNUUIDs).(UUIDPermissions)
}
return tokenResources
}
// ParseGrantResources parses the token for permissions and adds them along the other values to the GrantResourcesWithPermissions struct
func ParseGrantResources(res GrantResources, token string, timetoken int64, ttl int) *GrantResourcesWithPermissions {
channels := make(map[string]ChannelPermissionsWithToken, len(res.Channels))
for k, v := range res.Channels {
channels[k] = ChannelPermissionsWithToken{
Permissions: parseGrantPerms(v, PNChannels).(ChannelPermissions),
BitMaskPerms: v,
Token: token,
Timestamp: timetoken,
TTL: ttl,
}
}
groups := make(map[string]GroupPermissionsWithToken, len(res.Groups))
for k, v := range res.Groups {
groups[k] = GroupPermissionsWithToken{
Permissions: parseGrantPerms(v, PNGroups).(GroupPermissions),
BitMaskPerms: v,
Token: token,
Timestamp: timetoken,
TTL: ttl,
}
}
g := GrantResourcesWithPermissions{
Channels: channels,
Groups: groups,
}
return &g
}
func parseGrantPerms(i int64, resourceType PNResourceType) interface{} {
read := i&int64(PNRead) != 0
write := i&int64(PNWrite) != 0
manage := i&int64(PNManage) != 0
delete := i&int64(PNDelete) != 0
get := i&int64(PNGet) != 0
update := i&int64(PNUpdate) != 0
join := i&int64(PNJoin) != 0
switch resourceType {
case PNChannels:
return ChannelPermissions{
Read: read,
Write: write,
Delete: delete,
Update: update,
Get: get,
Join: join,
Manage: manage,
}
case PNGroups:
return GroupPermissions{
Read: read,
Manage: manage,
}
default:
return UUIDPermissions{
Get: get,
Update: update,
Delete: delete,
}
}
}
// ChannelPermissionsWithToken is used for channels resource type permissions
type ChannelPermissionsWithToken struct {
Permissions ChannelPermissions
BitMaskPerms int64
Token string
Timestamp int64
TTL int
}
// GroupPermissionsWithToken is used for groups resource type permissions
type GroupPermissionsWithToken struct {
Permissions GroupPermissions
BitMaskPerms int64
Token string
Timestamp int64
TTL int
}
// GrantResourcesWithPermissions is used as a common struct to store all resource type permissions
type GrantResourcesWithPermissions struct {
Channels map[string]ChannelPermissionsWithToken
Groups map[string]GroupPermissionsWithToken
ChannelsPattern map[string]ChannelPermissionsWithToken
GroupsPattern map[string]GroupPermissionsWithToken
}
// PermissionsBody is the struct used to decode the server response
type PermissionsBody struct {
Resources GrantResources `json:"resources"`
Patterns GrantResources `json:"patterns"`
Meta map[string]interface{} `json:"meta"`
AuthorizedUUID string `json:"uuid,omitempty"`
}
// GrantResources is the struct used to decode the server response
type GrantResources struct {
Channels map[string]int64 `json:"channels" cbor:"chan"`
Groups map[string]int64 `json:"groups" cbor:"grp"`
UUIDs map[string]int64 `json:"uuids" cbor:"uuid"`
Users map[string]int64 `json:"users" cbor:"usr"`
Spaces map[string]int64 `json:"spaces" cbor:"spc"`
}
// PNGrantTokenDecoded is the struct used to decode the server response
type PNGrantTokenDecoded struct {
Resources GrantResources `cbor:"res"`
Patterns GrantResources `cbor:"pat"`
Meta map[string]interface{} `cbor:"meta"`
Signature []byte `cbor:"sig"`
Version int `cbor:"v"`
Timestamp int64 `cbor:"t"`
TTL int `cbor:"ttl"`
AuthorizedUUID string `cbor:"uuid"`
}