Skip to content

Commit 69264dc

Browse files
authored
feat(auth): Added InsufficientPermissionError type (#354)
* Added InsufficientPermissionError type * Fixing lint error
1 parent 3c504e6 commit 69264dc

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

firebase_admin/_auth_utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,18 @@ def __init__(self, message, cause, http_response):
211211
exceptions.AlreadyExistsError.__init__(self, message, cause, http_response)
212212

213213

214+
class InsufficientPermissionError(exceptions.PermissionDeniedError):
215+
"""The credential used to initialize the SDK lacks required permissions."""
216+
217+
default_message = ('The credential used to initialize the SDK has insufficient '
218+
'permissions to perform the requested operation. See '
219+
'https://firebase.google.com/docs/admin/setup for details '
220+
'on how to initialize the Admin SDK with appropriate permissions')
221+
222+
def __init__(self, message, cause, http_response):
223+
exceptions.PermissionDeniedError.__init__(self, message, cause, http_response)
224+
225+
214226
class InvalidDynamicLinkDomainError(exceptions.InvalidArgumentError):
215227
"""Dynamic link domain in ActionCodeSettings is not authorized."""
216228

@@ -258,6 +270,7 @@ def __init__(self, message, cause=None, http_response=None):
258270
'DUPLICATE_EMAIL': EmailAlreadyExistsError,
259271
'DUPLICATE_LOCAL_ID': UidAlreadyExistsError,
260272
'EMAIL_EXISTS': EmailAlreadyExistsError,
273+
'INSUFFICIENT_PERMISSION': InsufficientPermissionError,
261274
'INVALID_DYNAMIC_LINK_DOMAIN': InvalidDynamicLinkDomainError,
262275
'INVALID_ID_TOKEN': InvalidIdTokenError,
263276
'PHONE_NUMBER_EXISTS': PhoneNumberAlreadyExistsError,

firebase_admin/auth.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
'ExpiredSessionCookieError',
4444
'ExportedUserRecord',
4545
'ImportUserRecord',
46+
'InsufficientPermissionError',
4647
'InvalidDynamicLinkDomainError',
4748
'InvalidIdTokenError',
4849
'InvalidSessionCookieError',
@@ -89,6 +90,7 @@
8990
ExpiredSessionCookieError = _token_gen.ExpiredSessionCookieError
9091
ExportedUserRecord = _user_mgt.ExportedUserRecord
9192
ImportUserRecord = _user_import.ImportUserRecord
93+
InsufficientPermissionError = _auth_utils.InsufficientPermissionError
9294
InvalidDynamicLinkDomainError = _auth_utils.InvalidDynamicLinkDomainError
9395
InvalidIdTokenError = _auth_utils.InvalidIdTokenError
9496
InvalidSessionCookieError = _token_gen.InvalidSessionCookieError

tests/test_user_mgt.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,21 @@ def test_list_users_error(self, user_mgt_app):
743743
auth.list_users(app=user_mgt_app)
744744
assert str(excinfo.value) == 'Unexpected error response: {"error":"test"}'
745745

746+
def test_permission_error(self, user_mgt_app):
747+
_instrument_user_manager(
748+
user_mgt_app, 400, '{"error": {"message": "INSUFFICIENT_PERMISSION"}}')
749+
with pytest.raises(auth.InsufficientPermissionError) as excinfo:
750+
auth.list_users(app=user_mgt_app)
751+
assert isinstance(excinfo.value, exceptions.PermissionDeniedError)
752+
msg = ('The credential used to initialize the SDK has insufficient '
753+
'permissions to perform the requested operation. See '
754+
'https://firebase.google.com/docs/admin/setup for details '
755+
'on how to initialize the Admin SDK with appropriate permissions '
756+
'(INSUFFICIENT_PERMISSION).')
757+
assert str(excinfo.value) == msg
758+
assert excinfo.value.http_response is not None
759+
assert excinfo.value.cause is not None
760+
746761
def _check_page(self, page):
747762
assert isinstance(page, auth.ListUsersPage)
748763
index = 0

0 commit comments

Comments
 (0)