72
72
Policies for the default sector/no sector (if no sector is set) can be defined
73
73
using the special ":default:" sector name.
74
74
Policies that should apply to all clients even if they don't have any
75
- credentials (default policies) can be set using the special "* " credential
75
+ credentials (default policies) can be set using the special ":default: " credential
76
76
reference/claim.
77
77
78
78
If a client has multiple claims/multiple policies apply (e.g. the default
94
94
}
95
95
}
96
96
},
97
- "policies ": {
97
+ "default_policies ": {
98
98
":key_2": {
99
99
"write": true,
100
100
"create": true
101
101
},
102
- "* ": {
102
+ ":default: ": {
103
103
"read": true,
104
104
"write": false,
105
105
"create": false
161
161
162
162
IMMUTABLE_PARAMETERS = ('ep' , 'd' , 'proxy' )
163
163
164
+ # Sector key in the security policy configuration whose value should be used
165
+ # for the "default" sector (i.e. if no sector is set).
166
+ # Might need to be changed if there is a situation where ":default:" is used
167
+ # as an actual sector name.
168
+ DEFAULT_SECTOR_KEY = ':default:'
169
+
170
+ # Claim/credential reference key in the policy map whose value should be
171
+ # treated as the "default" permissions for users that don't have any claims.
172
+ # Might need to be changed if there is a situation where ":default:" is used
173
+ # as an actual claim name (even though this can also be fixed by just using
174
+ # a different name, since the claim names are only used internally and
175
+ # arbritrarily set in the credentials file).
176
+ DEFAULT_CLAIM_KEY = ':default:'
177
+
164
178
def query_split (msg ):
165
179
"""Split a message's query up into (key, [*value]) pairs from a
166
180
?key=value&key2=value2 style Uri-Query options.
@@ -263,16 +277,16 @@ def _read_policy_entries(policy_data):
263
277
sectors = dict ()
264
278
for sector_name , sector_data in data ['sectors' ].items ():
265
279
endpoints = dict ()
266
- for endpoint_name , endpoint_data in sector_data [ 'endpoints' ] .items ():
267
- ep_policies = {cred_ref if cred_ref != "*" else None : _read_policy_entries (policy_data ) for cred_ref , policy_data in endpoint_data ['policies' ].items ()}
280
+ for endpoint_name , endpoint_data in sector_data . get ( 'endpoints' , dict ()) .items ():
281
+ ep_policies = {cred_ref if cred_ref != DEFAULT_CLAIM_KEY else None : _read_policy_entries (policy_data ) for cred_ref , policy_data in endpoint_data ['policies' ].items ()}
268
282
endpoints [endpoint_name ] = cls .EndpointPolicy (ep_policies )
269
283
270
- sec_policies = {cred_ref if cred_ref != "*" else None : _read_policy_entries (policy_data ) for cred_ref , policy_data in sector_data ['policies ' ].items ()}
271
- if sector_name == ":default:" :
284
+ sec_policies = {cred_ref if cred_ref != DEFAULT_CLAIM_KEY else None : _read_policy_entries (policy_data ) for cred_ref , policy_data in sector_data ['default_policies ' ].items ()}
285
+ if sector_name == DEFAULT_SECTOR_KEY :
272
286
sector_name = None
273
287
sectors [sector_name ] = cls .SectorPolicy (sec_policies , endpoints )
274
288
275
- return cls (True , sectors , True if 'registrar_has_permissions' not in data else data [ 'registrar_has_permissions' ] )
289
+ return cls (True , sectors , data . get ( 'registrar_has_permissions' , True ) )
276
290
277
291
def __init__ (self , enable , sectors = None , registrar_full_permissions = True ):
278
292
self .enable = enable
@@ -687,18 +701,19 @@ async def render_post(self, request):
687
701
688
702
return aiocoap .Message (code = aiocoap .CREATED , location_path = regresource .path )
689
703
690
- class RegistrationResource (Resource ):
704
+ class RegistrationResource (ThingWithCommonRD , Resource ):
691
705
"""The resource object wrapping a registration is just a very thin and
692
706
ephemeral object; all those methods could just as well be added to
693
707
Registration with `s/self.reg/self/g`, making RegistrationResource(reg) =
694
708
reg (or handleded in a single RegistrationDispatchSite), but this is kept
695
709
here for better separation of model and interface."""
696
710
697
- def __init__ (self , registration ):
711
+ def __init__ (self , common_rd , registration ):
712
+ super ().__init__ (common_rd )
698
713
self .reg = registration
699
714
700
715
def _get_permissions (self , request ):
701
- is_registrar = request .remote == self .reg .registrar
716
+ is_registrar = type ( request . remote ) is type ( self . reg . registrar ) and request .remote == self .reg .registrar
702
717
return self .common_rd .policy .get_permissions (request .remote .authenticated_claims ,
703
718
sector_name = self .reg .d ,
704
719
endpoint_name = self .reg .ep ,
@@ -709,7 +724,7 @@ async def render_get(self, request):
709
724
if not self ._get_permissions (request ) & CommonRD .SecurityPolicy .Permissions .READ :
710
725
raise error .Unauthorized ("Operation not allowed due to security policy" )
711
726
712
- return link_format_from_message (request , self .reg .links )
727
+ return link_format_to_message (request , self .reg .links )
713
728
714
729
def _update_params (self , msg ):
715
730
query = query_split (msg )
@@ -754,7 +769,7 @@ async def render(self, request):
754
769
except KeyError :
755
770
raise error .NotFound
756
771
757
- entity = RegistrationResource (entity )
772
+ entity = RegistrationResource (self . common_rd , entity )
758
773
759
774
return await entity .render (request .copy (uri_path = ()))
760
775
0 commit comments