Skip to content
Snippets Groups Projects
Unverified Commit 9aeb443b authored by Duncan Macleod's avatar Duncan Macleod
Browse files

application: improve auth handling

if all auth methods rejected, just return 'unauthorised', dont tell the user why (we don't want to leak the auth information to a bad actor)
parent 397a9727
Branches main
No related tags found
No related merge requests found
Pipeline #504241 failed
...@@ -27,14 +27,17 @@ import logging ...@@ -27,14 +27,17 @@ import logging
import time import time
def _authorize(environ, *auth_funcs):
def _authorize(admin, environ, *auth_funcs):
"""Authorize a response. """Authorize a response.
This function loops through the ``auth_funcs``, and returns as soon This function loops through the ``auth_funcs``, and returns as soon
as an authorised response comes back. as an authorised response comes back.
""" """
method = environ["REQUEST_METHOD"]
uri = environ["REQUEST_URI"]
# if request is not a GET, it's a write operation # if request is not a GET, it's a write operation
iswrite = environ["REQUEST_METHOD"].upper() != "GET" iswrite = method.upper() != "GET"
res = None res = None
for auth_func in auth_funcs: for auth_func in auth_funcs:
...@@ -42,8 +45,8 @@ def _authorize(environ, *auth_funcs): ...@@ -42,8 +45,8 @@ def _authorize(environ, *auth_funcs):
try: try:
res = auth_func( res = auth_func(
environ, environ,
environ['REQUEST_METHOD'], method,
environ['REQUEST_URI'], uri,
iswrite, iswrite,
) )
except: # something went wrong, try the next method except: # something went wrong, try the next method
...@@ -52,12 +55,12 @@ def _authorize(environ, *auth_funcs): ...@@ -52,12 +55,12 @@ def _authorize(environ, *auth_funcs):
if res[0] == 200: if res[0] == 200:
return res return res
# all of the authorisation functions _failed_!
if res is None: if res is None:
# all of the authorisation functions failed!
raise RuntimeError("no authorisation available") raise RuntimeError("no authorisation available")
# return unauthorised response (likely 401) # all of the authorisation functions rejected the request
return res return admin.log_and_set_http_code(401, 36, method, "Unauthorised", uri)
def application(environ, start_response): def application(environ, start_response):
...@@ -79,6 +82,7 @@ def application(environ, start_response): ...@@ -79,6 +82,7 @@ def application(environ, start_response):
# Attempt authorisation # Attempt authorisation
try: try:
res = _authorize( res = _authorize(
admin,
environ, environ,
ldbdsauth.check_authorization_scitoken, ldbdsauth.check_authorization_scitoken,
ldbdwauth.check_authorization_gridmap, ldbdwauth.check_authorization_gridmap,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment