Skip to content

Commit

Permalink
rename Auth0...Error to Auth0...Exception
Browse files Browse the repository at this point in the history
  • Loading branch information
dorinclisu committed Apr 18, 2021
1 parent c13bd39 commit fe2b581
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

setuptools.setup(
name='fastapi-auth0',
version='0.1.7',
version='0.2.0',
description='Easy auth0.com integration for FastAPI',
long_description=readme,
long_description_content_type='text/markdown',
Expand Down
2 changes: 1 addition & 1 deletion src/fastapi_auth0/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from .auth import Auth0, Auth0User, Auth0UnauthenticatedError, Auth0UnauthorizedError
from .auth import Auth0, Auth0User, Auth0UnauthenticatedException, Auth0UnauthorizedException
from .auth import security_responses, auth0_rule_namespace
20 changes: 10 additions & 10 deletions src/fastapi_auth0/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
auth0_rule_namespace: str = os.getenv('AUTH0_RULE_NAMESPACE', 'https://github.com/dorinclisu/fastapi-auth0')


class Auth0UnauthenticatedError(HTTPException):
class Auth0UnauthenticatedException(HTTPException):
def __init__(self, **kwargs):
super().__init__(401, **kwargs)

class Auth0UnauthorizedError(HTTPException):
class Auth0UnauthorizedException(HTTPException):
def __init__(self, **kwargs):
super().__init__(403, **kwargs)

Expand Down Expand Up @@ -132,26 +132,26 @@ async def get_user(self,

except jwt.ExpiredSignatureError:
if self.auto_error:
raise Auth0UnauthenticatedError(detail='Expired token')
raise Auth0UnauthenticatedException(detail='Expired token')
else:
return None

except jwt.JWTClaimsError:
if self.auto_error:
raise Auth0UnauthenticatedError(detail='Invalid token claims (please check issuer and audience)')
raise Auth0UnauthenticatedException(detail='Invalid token claims (please check issuer and audience)')
else:
return None

except jwt.JWTError:
if self.auto_error:
raise Auth0UnauthenticatedError(detail='Malformed token')
raise Auth0UnauthenticatedException(detail='Malformed token')
else:
return None

except Exception as e:
logging.error(f'Handled exception decoding token: "{e}"')
if self.auto_error:
raise Auth0UnauthenticatedError(detail='Error decoding token')
raise Auth0UnauthenticatedException(detail='Error decoding token')
else:
return None

Expand All @@ -163,24 +163,24 @@ async def get_user(self,

for scope in security_scopes.scopes:
if scope not in token_scopes:
raise Auth0UnauthorizedError(detail=f'Missing "{scope}" scope',
raise Auth0UnauthorizedException(detail=f'Missing "{scope}" scope',
headers={'WWW-Authenticate': f'Bearer scope="{security_scopes.scope_str}"'})
else:
# This is an unlikely case but handle it just to be safe (perhaps auth0 will change the scope format)
raise Auth0UnauthorizedError(detail='Token "scope" field must be a string')
raise Auth0UnauthorizedException(detail='Token "scope" field must be a string')

try:
user = self.auth0_user_model(**payload)

if self.email_auto_error and not user.email:
raise Auth0UnauthorizedError(detail=f'Missing email claim (check auth0 rule "Add email to access token")')
raise Auth0UnauthorizedException(detail=f'Missing email claim (check auth0 rule "Add email to access token")')

return user

except ValidationError as e:
logging.error(f'Handled exception parsing Auth0User: "{e}"')
if self.auto_error:
raise Auth0UnauthorizedError(detail='Error parsing Auth0User')
raise Auth0UnauthorizedException(detail='Error parsing Auth0User')
else:
return None

Expand Down

0 comments on commit fe2b581

Please sign in to comment.