mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-20 07:21:13 -05:00
Register a before_request event for all blueprints, that defines the authentication requirement.
There are three authentication methods: - requires_no_auth - public endpoint that does not require an Authorisation header - requires_auth - public endpoints that need an API key in the Authorisation header - requires_admin_auth - private endpoint that requires an Authorisation header which contains the API key for the defined as the client admin user
This commit is contained in:
@@ -39,14 +39,13 @@ def get_auth_token(req):
|
||||
return auth_header[7:]
|
||||
|
||||
|
||||
def requires_no_auth():
|
||||
pass
|
||||
|
||||
|
||||
def requires_admin_auth():
|
||||
auth_token = get_auth_token(request)
|
||||
try:
|
||||
client = get_token_issuer(auth_token)
|
||||
except TokenDecodeError as e:
|
||||
raise AuthError(e.message, 403)
|
||||
except TokenIssuerError:
|
||||
raise AuthError("Invalid token: iss not provided", 403)
|
||||
client = __get_token_issuer(auth_token)
|
||||
|
||||
if client == current_app.config.get('ADMIN_CLIENT_USER_NAME'):
|
||||
g.service_id = current_app.config.get('ADMIN_CLIENT_USER_NAME')
|
||||
@@ -57,16 +56,7 @@ def requires_admin_auth():
|
||||
|
||||
def requires_auth():
|
||||
auth_token = get_auth_token(request)
|
||||
try:
|
||||
client = get_token_issuer(auth_token)
|
||||
except TokenDecodeError as e:
|
||||
raise AuthError(e.message, 403)
|
||||
except TokenIssuerError:
|
||||
raise AuthError("Invalid token: iss not provided", 403)
|
||||
|
||||
if client == current_app.config.get('ADMIN_CLIENT_USER_NAME'):
|
||||
g.service_id = current_app.config.get('ADMIN_CLIENT_USER_NAME')
|
||||
return handle_admin_key(auth_token, current_app.config.get('ADMIN_CLIENT_SECRET'))
|
||||
client = __get_token_issuer(auth_token)
|
||||
|
||||
try:
|
||||
service = dao_fetch_service_by_id(client)
|
||||
@@ -98,12 +88,22 @@ def requires_auth():
|
||||
raise AuthError("Invalid token: signature, api token is not valid", 403)
|
||||
|
||||
|
||||
def __get_token_issuer(auth_token):
|
||||
try:
|
||||
client = get_token_issuer(auth_token)
|
||||
except TokenIssuerError:
|
||||
raise AuthError("Invalid token: iss field not provided", 403)
|
||||
except TokenDecodeError as e:
|
||||
raise AuthError("Invalid token: signature, api token is not valid", 403)
|
||||
return client
|
||||
|
||||
|
||||
def handle_admin_key(auth_token, secret):
|
||||
try:
|
||||
get_decode_errors(auth_token, secret)
|
||||
return
|
||||
except TokenDecodeError as e:
|
||||
raise AuthError(e.message, 403)
|
||||
raise AuthError("Invalid token: signature, api token is not valid", 403)
|
||||
|
||||
|
||||
def get_decode_errors(auth_token, unsigned_secret):
|
||||
|
||||
Reference in New Issue
Block a user