Skip to content

Commit b27216b

Browse files
authored
Introducing TokenSignError to represent custom token creation errors (#302)
* Migrated FCM send APIs to the new error handling regime * Moved error parsing logic to _utils * Refactored OP error handling code * Fixing a broken test * Added utils for handling googleapiclient errors * Added tests for new error handling logic * Updated public API docs * Fixing test for python3 * Cleaning up the error code lookup code * Cleaning up the error parsing APIs * Cleaned up error parsing logic; Updated docs * Migrated the FCM IID APIs to the new error types * Migrated custom token API to new error types
1 parent fa843f3 commit b27216b

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

firebase_admin/_token_gen.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@
2121
import requests
2222
import six
2323
from google.auth import credentials
24-
from google.auth import exceptions
2524
from google.auth import iam
2625
from google.auth import jwt
2726
from google.auth import transport
27+
import google.auth.exceptions
2828
import google.oauth2.id_token
2929
import google.oauth2.service_account
3030

31+
from firebase_admin import exceptions
32+
3133

3234
# ID token constants
3335
ID_TOKEN_ISSUER_PREFIX = 'https://securetoken.google.com/'
@@ -53,7 +55,6 @@
5355

5456
# Error codes
5557
COOKIE_CREATE_ERROR = 'COOKIE_CREATE_ERROR'
56-
TOKEN_SIGN_ERROR = 'TOKEN_SIGN_ERROR'
5758

5859

5960
class ApiCallError(Exception):
@@ -177,9 +178,9 @@ def create_custom_token(self, uid, developer_claims=None):
177178
payload['claims'] = developer_claims
178179
try:
179180
return jwt.encode(signing_provider.signer, payload)
180-
except exceptions.TransportError as error:
181+
except google.auth.exceptions.TransportError as error:
181182
msg = 'Failed to sign custom token. {0}'.format(error)
182-
raise ApiCallError(TOKEN_SIGN_ERROR, msg, error)
183+
raise TokenSignError(msg, error)
183184

184185

185186
def create_session_cookie(self, id_token, expires_in):
@@ -339,3 +340,10 @@ def verify(self, token, request):
339340
certs_url=self.cert_url)
340341
verified_claims['uid'] = verified_claims['sub']
341342
return verified_claims
343+
344+
345+
class TokenSignError(exceptions.UnknownError):
346+
"""Unexpected error while signing a Firebase custom token."""
347+
348+
def __init__(self, message, cause):
349+
exceptions.UnknownError.__init__(self, message, cause)

firebase_admin/auth.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
'ExportedUserRecord',
4343
'ImportUserRecord',
4444
'ListUsersPage',
45+
'TokenSignError',
4546
'UserImportHash',
4647
'UserImportResult',
4748
'UserInfo',
@@ -75,6 +76,7 @@
7576
ListUsersPage = _user_mgt.ListUsersPage
7677
UserImportHash = _user_import.UserImportHash
7778
ImportUserRecord = _user_import.ImportUserRecord
79+
TokenSignError = _token_gen.TokenSignError
7880
UserImportResult = _user_import.UserImportResult
7981
UserInfo = _user_mgt.UserInfo
8082
UserMetadata = _user_mgt.UserMetadata
@@ -115,13 +117,10 @@ def create_custom_token(uid, developer_claims=None, app=None):
115117
116118
Raises:
117119
ValueError: If input parameters are invalid.
118-
AuthError: If an error occurs while creating the token using the remote IAM service.
120+
TokenSignError: If an error occurs while signing the token using the remote IAM service.
119121
"""
120122
token_generator = _get_auth_service(app).token_generator
121-
try:
122-
return token_generator.create_custom_token(uid, developer_claims)
123-
except _token_gen.ApiCallError as error:
124-
raise AuthError(error.code, str(error), error.detail)
123+
return token_generator.create_custom_token(uid, developer_claims)
125124

126125

127126
def verify_id_token(id_token, app=None, check_revoked=False):

tests/test_token_gen.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import time
2222

2323
from google.auth import crypt
24-
from google.auth import exceptions
2524
from google.auth import jwt
25+
import google.auth.exceptions
2626
import google.oauth2.id_token
2727
import pytest
2828
from pytest_localserver import plugin
@@ -31,6 +31,7 @@
3131
import firebase_admin
3232
from firebase_admin import auth
3333
from firebase_admin import credentials
34+
from firebase_admin import exceptions
3435
from firebase_admin import _token_gen
3536
from tests import testutils
3637

@@ -219,10 +220,12 @@ def test_sign_with_iam_error(self):
219220
try:
220221
iam_resp = '{"error": {"code": 403, "message": "test error"}}'
221222
_overwrite_iam_request(app, testutils.MockRequest(403, iam_resp))
222-
with pytest.raises(auth.AuthError) as excinfo:
223+
with pytest.raises(auth.TokenSignError) as excinfo:
223224
auth.create_custom_token(MOCK_UID, app=app)
224-
assert excinfo.value.code == _token_gen.TOKEN_SIGN_ERROR
225-
assert iam_resp in str(excinfo.value)
225+
error = excinfo.value
226+
assert error.code == exceptions.UNKNOWN
227+
assert iam_resp in str(error)
228+
assert isinstance(error.cause, google.auth.exceptions.TransportError)
226229
finally:
227230
firebase_admin.delete_app(app)
228231

@@ -421,7 +424,7 @@ def test_custom_token(self, auth_app):
421424

422425
def test_certificate_request_failure(self, user_mgt_app):
423426
_overwrite_cert_request(user_mgt_app, testutils.MockRequest(404, 'not found'))
424-
with pytest.raises(exceptions.TransportError):
427+
with pytest.raises(google.auth.exceptions.TransportError):
425428
auth.verify_id_token(TEST_ID_TOKEN, app=user_mgt_app)
426429

427430

@@ -521,7 +524,7 @@ def test_custom_token(self, auth_app):
521524

522525
def test_certificate_request_failure(self, user_mgt_app):
523526
_overwrite_cert_request(user_mgt_app, testutils.MockRequest(404, 'not found'))
524-
with pytest.raises(exceptions.TransportError):
527+
with pytest.raises(google.auth.exceptions.TransportError):
525528
auth.verify_session_cookie(TEST_SESSION_COOKIE, app=user_mgt_app)
526529

527530

0 commit comments

Comments
 (0)