@@ -99,8 +99,7 @@ def __init__(
9999 key ,
100100 capabilities ,
101101 expiration_timestamp_or_none ,
102- bucket_ids_or_none ,
103- bucket_names_or_none ,
102+ buckets_or_none ,
104103 name_prefix_or_none ,
105104 ):
106105 self .name = name
@@ -109,15 +108,19 @@ def __init__(
109108 self .key = key
110109 self .capabilities = capabilities
111110 self .expiration_timestamp_or_none = expiration_timestamp_or_none
112- self .bucket_ids_or_none = bucket_ids_or_none
113- self .bucket_names_or_none = bucket_names_or_none
111+ self .buckets_or_none = buckets_or_none
114112 self .name_prefix_or_none = name_prefix_or_none
115113
114+ def _get_bucket_ids (self ):
115+ if self .buckets_or_none is None :
116+ return None
117+
118+ return [item ['id' ] for item in self .buckets_or_none ]
119+
116120 def as_key (self ):
117121 return dict (
118122 accountId = self .account_id ,
119- bucketIds = self .bucket_ids_or_none ,
120- bucketNames = self .bucket_names_or_none ,
123+ bucketIds = self ._get_bucket_ids (),
121124 applicationKeyId = self .application_key_id ,
122125 capabilities = self .capabilities ,
123126 expirationTimestamp = self .expiration_timestamp_or_none
@@ -134,15 +137,15 @@ def as_created_key(self):
134137 """
135138 result = self .as_key ()
136139 result ['applicationKey' ] = self .key
140+
137141 return result
138142
139143 def get_allowed (self ):
140144 """
141145 Return the 'allowed' structure to include in the response from b2_authorize_account.
142146 """
143147 return dict (
144- bucketIds = self .bucket_ids_or_none ,
145- bucketName = self .bucket_names_or_none ,
148+ buckets = self .buckets_or_none ,
146149 capabilities = self .capabilities ,
147150 namePrefix = self .name_prefix_or_none ,
148151 )
@@ -1372,8 +1375,7 @@ def create_account(self):
13721375 key = master_key ,
13731376 capabilities = ALL_CAPABILITIES ,
13741377 expiration_timestamp_or_none = None ,
1375- bucket_ids_or_none = None ,
1376- bucket_names_or_none = None ,
1378+ buckets_or_none = None ,
13771379 name_prefix_or_none = None ,
13781380 )
13791381
@@ -1401,17 +1403,6 @@ def authorize_account(self, realm_url, application_key_id, application_key):
14011403 self .auth_token_to_key [auth_token ] = key_sim
14021404
14031405 allowed = key_sim .get_allowed ()
1404- bucketIds = allowed .get ('bucketIds' )
1405-
1406- if bucketIds is not None :
1407- allowed ['bucketNames' ] = []
1408- for _id in bucketIds :
1409- if _id in self .bucket_id_to_bucket :
1410- allowed ['bucketNames' ].append (self .bucket_id_to_bucket [_id ].bucket_name )
1411- else :
1412- allowed ['bucketNames' ].append (None )
1413- else :
1414- allowed ['bucketNames' ] = None
14151406
14161407 return dict (
14171408 accountId = key_sim .account_id ,
@@ -1425,8 +1416,6 @@ def authorize_account(self, realm_url, application_key_id, application_key):
14251416 absoluteMinimumPartSize = self .MIN_PART_SIZE ,
14261417 allowed = allowed ,
14271418 s3ApiUrl = self .S3_API_URL ,
1428- bucketIds = allowed ['bucketIds' ],
1429- bucketNames = allowed ['bucketNames' ],
14301419 capabilities = allowed ['capabilities' ],
14311420 namePrefix = allowed ['namePrefix' ],
14321421 ),
@@ -1506,16 +1495,18 @@ def create_key(
15061495 self .app_key_counter += 1
15071496 application_key_id = 'appKeyId%d' % (index ,)
15081497 app_key = 'appKey%d' % (index ,)
1509- bucket_names_or_none = None
1498+
1499+ buckets = None
1500+
15101501 if bucket_ids is not None :
15111502 # It is possible for bucketId to be filled and bucketName to be empty.
15121503 # It can happen when the bucket was deleted.
1513- bucket_names_or_none = []
1504+ buckets = []
15141505 for _id in bucket_ids :
15151506 try :
1516- bucket_names_or_none .append (self ._get_bucket_by_id (_id ).bucket_name )
1507+ buckets .append ({ 'id' : _id , 'name' : self ._get_bucket_by_id (_id ).bucket_name } )
15171508 except NonExistentBucket :
1518- bucket_names_or_none .append (None )
1509+ buckets .append ({ 'id' : _id , 'name' : None } )
15191510
15201511 key_sim = KeySimulator (
15211512 account_id = account_id ,
@@ -1524,8 +1515,7 @@ def create_key(
15241515 key = app_key ,
15251516 capabilities = capabilities ,
15261517 expiration_timestamp_or_none = expiration_timestamp_or_none ,
1527- bucket_ids_or_none = bucket_ids ,
1528- bucket_names_or_none = bucket_names_or_none ,
1518+ buckets_or_none = buckets ,
15291519 name_prefix_or_none = name_prefix ,
15301520 )
15311521 self .key_id_to_key [application_key_id ] = key_sim
@@ -2113,8 +2103,17 @@ def _assert_account_auth(
21132103 raise InvalidAuthToken ('auth token expired' , 'auth_token_expired' )
21142104 if capability not in key_sim .capabilities :
21152105 raise Unauthorized ('' , 'unauthorized' )
2116- if key_sim .bucket_ids_or_none and bucket_id not in key_sim .bucket_ids_or_none :
2117- raise Unauthorized ('' , 'unauthorized' )
2106+
2107+ if key_sim .buckets_or_none :
2108+ found = False
2109+ for item in key_sim .buckets_or_none :
2110+ if item ['id' ] == bucket_id :
2111+ found = True
2112+ break
2113+
2114+ if not found :
2115+ raise Unauthorized ('' , 'unauthorized' )
2116+
21182117 if key_sim .name_prefix_or_none is not None :
21192118 if file_name is not None and not file_name .startswith (key_sim .name_prefix_or_none ):
21202119 raise Unauthorized ('' , 'unauthorized' )
0 commit comments