8000 Adding parent error types and top-level codes · SHavanonda/firebase-admin-python@6b5b485 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6b5b485

Browse files
committed
Adding parent error types and top-level codes
1 parent 18c2395 commit 6b5b485

File tree

3 files changed

+72
-14
lines changed

3 files changed

+72
-14
lines changed

firebase_admin/_auth_utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import six
2121
from six.moves import urllib
2222

23+
from firebase_admin import exceptions
24+
2325

2426
MAX_CLAIMS_PAYLOAD_SIZE = 1000
2527
RESERVED_CLAIMS = set([
@@ -188,3 +190,15 @@ def validate_action_type(action_type):
188190
raise ValueError('Invalid action type provided action_type: {0}. \
189191
Valid values are {1}'.format(action_type, ', '.join(VALID_EMAIL_ACTION_TYPES)))
190192
return action_type
193+
194+
195+
class FirebaseAuthError(exceptions.FirebaseError):
196+
"""Represents an Exception encountered while invoking the Firebase auth API."""
197+
198+
def __init__(self, code, message, cause=None, http_response=None, auth_error_code=None):
199+
exceptions.FirebaseError.__init__(self, code, message, cause, http_response)
200+
self._auth_error_code = auth_error_code
201+
202+
@property
203+
def auth_error_code(self):
204+
return self._auth_error_code

firebase_admin/auth.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import time
2323

2424
import firebase_admin
25+
from firebase_admin import exceptions
26+
from firebase_admin import _auth_utils
2527
from firebase_admin import _http_client
2628
from firebase_admin import _token_gen
2729
from firebase_admin import _user_import
@@ -30,8 +32,8 @@
3032

3133

3234
_AUTH_ATTRIBUTE = '_auth'
33-
_ID_TOKEN_REVOKED = 'ID_TOKEN_REVOKED'
34-
_SESSION_COOKIE_REVOKED = 'SESSION_COOKIE_REVOKED'
35+
ID_TOKEN_REVOKED = 'ID_TOKEN_REVOKED'
36+
SESSION_COOKIE_REVOKED = 'SESSION_COOKIE_REVOKED'
3537

3638

3739
__all__ = [
@@ -70,6 +72,7 @@
7072
ActionCodeSettings = _user_mgt.ActionCodeSettings
7173
ErrorInfo = _user_import.ErrorInfo
7274
ExportedUserRecord = _user_mgt.ExportedUserRecord
75+
FirebaseAuthError = _auth_utils.FirebaseAuthError
7376
ListUsersPage = _user_mgt.ListUsersPage
7477
UserImportHash = _user_import.UserImportHash
7578
ImportUserRecord = _user_import.ImportUserRecord
@@ -147,7 +150,7 @@ def verify_id_token(id_token, app=None, check_revoked=False):
147150
token_verifier = _get_auth_service(app).token_verifier
148151
verified_claims = token_verifier.verify_id_token(id_token)
149152
if check_revoked:
150-
_check_jwt_revoked(verified_claims, _ID_TOKEN_REVOKED, 'ID token', app)
153+
_check_jwt_revoked(verified_claims, ID_TOKEN_REVOKED, 'ID token', app)
151154
return verified_claims
152155

153156
def create_session_cookie(id_token, expires_in, app=None):
@@ -196,7 +199,7 @@ def verify_session_cookie(session_cookie, check_revoked=False, app=None):
196199
token_verifier = _get_auth_service(app).token_verifier
197200
verified_claims = token_verifier.verify_session_cookie(session_cookie)
198201
if check_revoked:
199-
_check_jwt_revoked(verified_claims, _SESSION_COOKIE_REVOKED, 'session cookie', app)
202+
_check_jwt_revoked(verified_claims, SESSION_COOKIE_REVOKED, 'session cookie', app)
200203
return verified_claims
201204

202205
def revoke_refresh_tokens(uid, app=None):
@@ -528,16 +531,10 @@ def generate_sign_in_with_email_link(email, action_code_settings, app=None):
528531
def _check_jwt_revoked(verified_claims, error_code, label, app):
529532
user = get_user(verified_claims.get('uid'), app=app)
530533
if verified_claims.get('iat') * 1000 < user.tokens_valid_after_timestamp:
531-
raise AuthError(error_code, 'The Firebase {0} has been revoked.'.format(label))
532-
533-
534-
class AuthError(Exception):
535-
"""Represents an Exception encountered while invoking the Firebase auth API."""
536-
537-
def __init__(self, code, message, error=None):
538-
Exception.__init__(self, message)
539-
self.code = code
540-
self.detail = error
534+
raise FirebaseAuthError(
535+
exceptions.INVALID_ARGUMENT,
536+
'The Firebase {0} has been revoked.'.format(label),
537+
auth_error_code=error_code)
541538

542539

543540
class _AuthService(object):

firebase_admin/exceptions.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Copyright 2019 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
ALREADY_EXISTS = 'ALREADY_EXISTS'
17+
INVALID_ARGUMENT = 'INVALID_ARGUMENT'
18+
FAILED_PRECONDITION = 'FAILED_PRECONDITION'
19+
UNAUTHENTICATED = 'UNAUTHENTICATED'
20+
PERMISSION_DENIED = 'PERMISSION_DENIED'
21+
NOT_FOUND = 'NOT_FOUND'
22+
UNKNOWN = 'UNKNOWN'
23+
INTERNAL = 'INTERNAL'
24+
UNAVAILABLE = 'UNAVAILABLE'
25+
DEADLINE_EXCEEDED = 'DEADLINE_EXCEEDED'
26+
27+
28+
class FirebaseError(Exception):
29+
30+
def __init__(self, code, message, cause=None, http_response=None):
31+
Exception.__init__(self, message)
32+
self._code = code
33+
self._cause = cause
34+
self._http_response = http_response
35+
print('CONST', self._http_response)
36+
37+
@property
38+
def code(self):
39+
return self._code
40+
41+
@property
42+
def cause(self):
43+
return self._cause
44+
45+
@property
46+
def http_response(self):
47+
return self._http_response

0 commit comments

Comments
 (0)
0