Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
better test for token
Browse files Browse the repository at this point in the history
  • Loading branch information
blankdots committed Mar 1, 2019
1 parent 095700e commit 5fb77aa
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions beacon_api/utils/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,17 @@ async def get_key():
raise BeaconServerError("Could not retrieve OAuth2 public key.")


def token_scheme_check(token, scheme, obj, host):
"""Check if token has proper scheme and was provided."""
if not re.match('Bearer', scheme):
raise BeaconUnauthorised(obj, host, "invalid_token", 'Invalid token scheme, Bearer required.')

if token is None:
BeaconUnauthorised(obj, host, "invalid_token", f'Token cannot be empty.')


def token_auth():
"""Check if token if valid and authenticate.
"""Check if token is valid and authenticate.
Decided against: https://github.com/hzlmn/aiohttp-jwt, as we need to verify
token issuer and bona_fide_status.
Expand All @@ -140,10 +149,8 @@ async def token_middleware(request, handler):
except Exception as e:
raise BeaconUnauthorised(obj, request.host, "invalid_token", str(e))

if not re.match('Bearer', scheme):
raise BeaconUnauthorised(obj, request.host, "invalid_token", 'Invalid token scheme, Bearer required.')
token_scheme_check(token, scheme, obj, request.host)

assert token is not None, BeaconUnauthorised(obj, request.host, "invalid_token", f'Token cannot be empty.')
key = await get_key()
issuers = OAUTH2_CONFIG.issuers.split(',')
try:
Expand All @@ -159,9 +166,9 @@ async def token_middleware(request, handler):
# by updating the set, replicating the line below with the permissions function and its associated claim
controlled_datasets.update(get_rems_controlled(decodedData["permissions_rems"]) if "permissions_rems" in decodedData else {})
all_controlled = list(controlled_datasets) if bool(controlled_datasets) else None
request["token"] = {"bona_fide_status": True if await check_bona_fide_status(token, obj, request.host) else False,
request["token"] = {"bona_fide_status": True if await check_bona_fide_status(token, obj, request.host) is not None else False,
"permissions": all_controlled,
# additional checks can be performed against this authenticated
# additional checks can be performed against this authenticated key
# currently if a token is valid that means request is authenticated
"authenticated": True}
return await handler(request)
Expand Down

0 comments on commit 5fb77aa

Please sign in to comment.