forked from pubnub/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grant_common.go
302 lines (268 loc) · 7.55 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
package pubnub
import (
"bytes"
"encoding/base64"
"fmt"
"math/bits"
"strconv"
"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
)
// 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
)
// PNResourceType grant types
type PNResourceType int
const (
// PNChannels for channels
PNChannels PNResourceType = 1 + iota
// PNGroups for groups
PNGroups
// PNUsers for users
PNUsers
// PNSpaces for spaces
PNSpaces
)
// ChannelPermissions contains all the acceptable perms for channels
type ChannelPermissions struct {
Read bool
Write bool
Delete bool
}
// GroupPermissions contains all the acceptable perms for groups
type GroupPermissions struct {
Read bool
Manage bool
}
// UserSpacePermissions contains all the acceptable perms for Users and Spaces
type UserSpacePermissions struct {
Read bool
Write bool
Manage bool
Delete bool
Create bool
}
// ResourcePermissions contains all the applicable perms for bitmask translations.
type ResourcePermissions struct {
Read bool
Write bool
Manage bool
Delete bool
Create bool
}
// 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
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
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
}
func parseGrantPerms(i int64, resourceType PNResourceType) interface{} {
b := fmt.Sprintf("%08b\n", bits.Reverse8(uint8(i)))
r := ResourcePermissions{
Read: false,
Write: false,
Manage: false,
Delete: false,
Create: false,
}
for k, v := range b {
i, _ := strconv.Atoi(string(v))
switch k {
case 0:
r.Read = (i == 1)
case 1:
r.Write = (i == 1)
case 2:
r.Manage = (i == 1)
case 3:
r.Delete = (i == 1)
case 4:
r.Create = (i == 1)
}
}
switch resourceType {
case PNChannels:
return ChannelPermissions{
Read: r.Read,
Write: r.Write,
Delete: r.Delete,
}
case PNGroups:
return GroupPermissions{
Read: r.Read,
Manage: r.Manage,
}
default:
return UserSpacePermissions{
Read: r.Read,
Write: r.Write,
Delete: r.Delete,
Manage: r.Manage,
Create: r.Create,
}
}
}
// 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,
}
}
spaces := make(map[string]UserSpacePermissionsWithToken, len(res.Spaces))
for k, v := range res.Spaces {
spaces[k] = UserSpacePermissionsWithToken{
Permissions: parseGrantPerms(v, PNSpaces).(UserSpacePermissions),
BitMaskPerms: v,
Token: token,
Timestamp: timetoken,
TTL: ttl,
}
}
users := make(map[string]UserSpacePermissionsWithToken, len(res.Users))
for k, v := range res.Users {
users[k] = UserSpacePermissionsWithToken{
Permissions: parseGrantPerms(v, PNUsers).(UserSpacePermissions),
BitMaskPerms: v,
Token: token,
Timestamp: timetoken,
TTL: ttl,
}
}
g := GrantResourcesWithPermissions{
Channels: channels,
Users: users,
Groups: groups,
Spaces: spaces,
}
return &g
}
// 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
}
// UserSpacePermissionsWithToken is used for users/spaces resource type permissions
type UserSpacePermissionsWithToken struct {
Permissions UserSpacePermissions
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
Users map[string]UserSpacePermissionsWithToken
Spaces map[string]UserSpacePermissionsWithToken
ChannelsPattern map[string]ChannelPermissionsWithToken
GroupsPattern map[string]GroupPermissionsWithToken
UsersPattern map[string]UserSpacePermissionsWithToken
SpacesPattern map[string]UserSpacePermissionsWithToken
}
// 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"`
}
// 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"`
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"`
}