Skip to content

Commit

Permalink
Raise error if authentication fails
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitijrajsharma committed Nov 17, 2023
1 parent c7247fc commit 113eb2e
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions API/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,32 @@ def get_user_from_db(osm_id: int):
return user


def login_required(access_token: str = Header(...)):
user = AuthUser(**osm_auth.deserialize_access_token(access_token))
def get_osm_auth_user(access_token):
try:
user = AuthUser(**osm_auth.deserialize_access_token(access_token))
except Exception as ex:
raise HTTPException(
status_code=403, detail=[{"msg": "OSM Authentication failed"}]
)
db_user = get_user_from_db(user.id)
user.role = db_user["role"]
return user


def login_required(access_token: str = Header(...)):
return get_osm_auth_user(access_token)


def get_optional_user(access_token: str = Header(default=None)) -> AuthUser:
if access_token:
user = AuthUser(**osm_auth.deserialize_access_token(access_token))
db_user = get_user_from_db(user.id)
user.role = db_user["role"]
return user
return get_osm_auth_user(access_token)
else:
# If no token provided, return a user with limited options or guest user
return AuthUser(id=0, username="guest", img_url=None)


def admin_required(user: AuthUser = Depends(login_required)):
db_user = get_user_from_db(user.id)
print(db_user)
if not db_user["role"] is UserRole.ADMIN.value:
raise HTTPException(status_code=403, detail="User is not an admin")
return user
Expand Down

0 comments on commit 113eb2e

Please sign in to comment.