@@ -60,6 +60,7 @@ def generate_arena_token(
60
60
Returns:
61
61
str: JWT or None
62
62
"""
63
+ # TODO: realm cannot contain any /
63
64
config = settings .PUBSUB
64
65
if not realm :
65
66
realm = config ["mqtt_realm" ]
@@ -99,15 +100,24 @@ def generate_arena_token(
99
100
roomname = re .sub (r"[!#$&'()*+,\/:;=?@[\]]" , '_' , ns_scene .lower ())
100
101
payload ["room" ] = roomname
101
102
102
- pubs , subs = get_pubsub_topics_api_v1 (
103
- user ,
104
- username ,
105
- realm ,
106
- ns_scene ,
107
- ns_device ,
108
- ids ,
109
- perm ,
110
- )
103
+ # ns_scene, ns_device can/must contain only one '/'
104
+ namespace = scene = device = None
105
+ if ns_scene :
106
+ parts = ns_scene .split ("/" )
107
+ if len (parts ) != 2 :
108
+ return None
109
+ namespace = parts [0 ]
110
+ scene = parts [1 ]
111
+ elif ns_device :
112
+ parts = ns_device .split ("/" )
113
+ if len (parts ) != 2 :
114
+ return None
115
+ namespace = parts [0 ]
116
+ device = parts [1 ]
117
+
118
+ pubs , subs = pubsub_api_v1 (
119
+ user , username , realm , namespace , scene , device , ids , perm )
120
+
111
121
if len (subs ) > 0 :
112
122
payload ["subs" ] = clean_topics (subs )
113
123
if len (pubs ) > 0 :
@@ -116,12 +126,13 @@ def generate_arena_token(
116
126
return jwt .encode (payload , private_key , algorithm = "RS256" , headers = headers )
117
127
118
128
119
- def get_pubsub_topics_api_v1 (
129
+ def pubsub_api_v1 (
120
130
user ,
121
131
username ,
122
132
realm ,
123
- ns_scene ,
124
- ns_device ,
133
+ namespace ,
134
+ scene ,
135
+ device ,
125
136
ids ,
126
137
perm ,
127
138
):
@@ -133,16 +144,16 @@ def get_pubsub_topics_api_v1(
133
144
pubs = []
134
145
subs = []
135
146
# everyone should be able to read all public scenes
136
- if not ns_device : # scene token scenario
147
+ if not device : # scene token scenario
137
148
subs .append (f"{ realm } /s/{ PUBLIC_NAMESPACE } /#" )
138
149
# And transmit env data
139
150
pubs .append (f"{ realm } /env/{ PUBLIC_NAMESPACE } /#" )
140
151
# user presence objects
141
152
if user .is_authenticated :
142
- if ns_device : # device token scenario
153
+ if device : # device token scenario
143
154
# device owners have rights to their device objects only
144
- subs .append (f"{ realm } /d/{ ns_device } /#" )
145
- pubs .append (f"{ realm } /d/{ ns_device } /#" )
155
+ subs .append (f"{ realm } /d/{ namespace } / { device } /#" )
156
+ pubs .append (f"{ realm } /d/{ namespace } / { device } /#" )
146
157
else : # scene token scenario
147
158
# scene rights default by namespace
148
159
if user .is_staff :
@@ -153,8 +164,8 @@ def get_pubsub_topics_api_v1(
153
164
subs .append (f"{ realm } /env/#" )
154
165
pubs .append (f"{ realm } /env/#" )
155
166
# vio experiments, staff only
156
- if ns_scene :
157
- pubs .append (f"{ realm } /vio/{ ns_scene } /#" )
167
+ if scene :
168
+ pubs .append (f"{ realm } /vio/{ namespace } / { scene } /#" )
158
169
else :
159
170
# scene owners have rights to their scene objects only
160
171
subs .append (f"{ realm } /s/{ username } /#" )
@@ -165,7 +176,7 @@ def get_pubsub_topics_api_v1(
165
176
# add scenes that have been granted by other owners
166
177
u_scenes = Scene .objects .filter (editors = user )
167
178
for u_scene in u_scenes :
168
- if not ns_scene or (ns_scene and u_scene .name == ns_scene ):
179
+ if not scene or (scene and u_scene .name == f" { namespace } / { scene } " ):
169
180
subs .append (f"{ realm } /s/{ u_scene .name } /#" )
170
181
pubs .append (f"{ realm } /s/{ u_scene .name } /#" )
171
182
subs .append (f"{ realm } /env/{ u_scene .name } /#" )
@@ -180,25 +191,24 @@ def get_pubsub_topics_api_v1(
180
191
subs .append (f"{ realm } /d/{ username } /#" )
181
192
pubs .append (f"{ realm } /d/{ username } /#" )
182
193
# anon/non-owners have rights to view scene objects only
183
- if ns_scene and not user .is_staff :
194
+ if scene and not user .is_staff :
184
195
# did the user set specific public read or public write?
185
196
if not user .is_authenticated and not perm ["anonymous_users" ]:
186
197
return None # anonymous not permitted
187
198
if perm ["public_read" ]:
188
- subs .append (f"{ realm } /s/{ ns_scene } /#" )
199
+ subs .append (f"{ realm } /s/{ namespace } / { scene } /#" )
189
200
# Interactivity to extent of viewing objects is similar to publishing env
190
- pubs .append (f"{ realm } /env/{ ns_scene } /#" )
201
+ pubs .append (f"{ realm } /env/{ namespace } / { scene } /#" )
191
202
if perm ["public_write" ]:
192
- pubs .append (f"{ realm } /s/{ ns_scene } /#" )
203
+ pubs .append (f"{ realm } /s/{ namespace } / { scene } /#" )
193
204
# user presence objects
194
205
if ids and perm ["users" ]: # probable web browser write
195
- pubs .append (f"{ realm } /s/{ ns_scene } /{ ids ['camid' ]} " )
196
- pubs .append (f"{ realm } /s/{ ns_scene } /{ ids ['camid' ]} /#" )
197
- pubs .append (f"{ realm } /s/{ ns_scene } /{ ids ['handleftid' ]} " )
198
- pubs .append (f"{ realm } /s/{ ns_scene } /{ ids ['handrightid' ]} " )
206
+ pubs .append (f"{ realm } /s/{ namespace } / { scene } /{ ids ['camid' ]} " )
207
+ pubs .append (f"{ realm } /s/{ namespace } / { scene } /{ ids ['camid' ]} /#" )
208
+ pubs .append (f"{ realm } /s/{ namespace } / { scene } /{ ids ['handleftid' ]} " )
209
+ pubs .append (f"{ realm } /s/{ namespace } / { scene } /{ ids ['handrightid' ]} " )
199
210
# chat messages
200
- if ns_scene and ids and perm ["users" ]:
201
- namespace = ns_scene .split ("/" )[0 ]
211
+ if scene and ids and perm ["users" ]:
202
212
userhandle = ids ["userid" ] + \
203
213
base64 .b64encode (ids ["userid" ].encode ()).decode ()
204
214
# receive private messages: Read
@@ -210,7 +220,7 @@ def get_pubsub_topics_api_v1(
210
220
# private messages to user: Write
211
221
pubs .append (f"{ realm } /c/{ namespace } /p/+/{ userhandle } " )
212
222
# apriltags
213
- if ns_scene :
223
+ if scene :
214
224
subs .append (f"{ realm } /g/a/#" )
215
225
pubs .append (f"{ realm } /g/a/#" )
216
226
# arts runtime-mngr
0 commit comments