Skip to content

Commit fc9dee4

Browse files
committed
Merge branch 'admin-auth-fix'
2 parents c0d25bb + db7900e commit fc9dee4

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 0.3.2
2+
- Improvements to the admin role checks
3+
- Fix an issue where the token for the auth0management endpoint for checking user roles would expire and cause requests to return less data than they should
14

25
## 0.3.1
36
- Improvements to logging

common/helpers.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -269,14 +269,17 @@ def check_permissions(user, permissions_to_check):
269269
raise AuthError(
270270
"You have not been granted the necessary permissions to access to this resource. You are missing the following permissions: " + perms_needed, 403)
271271

272-
def check_for_role(role:str):
273-
"""Performs a simple, stupid, name-based check against the roles that a user has.
272+
def check_for_roles(roles:list, accept_any=True):
273+
"""Performs a simple, stupid, name-based check against the roles that a user has.
274+
275+
This must be used after the @requires_auth decorator is applied
274276
275277
Args:
276-
role (str): the name of the role to check if the user has
278+
roles (list): a list of names of roles to check
279+
accept_any (boolean): True to accept ANY of the provided roles. False to accept only ALL provided roles. Defaults to True.
277280
278281
Returns:
279-
bool: true if the user has the role, false if they dont, and None if there is no currently authenticated user (this must be used after the @requires_auth decorator is applied)
282+
bool: true if the user has any of the roles provided, false if the user has none of them, and None if there is no currently authenticated user
280283
"""
281284
user_id = get_api_user_id()
282285
#TODO: make management API optional and check if it is present
@@ -288,15 +291,20 @@ def check_for_role(role:str):
288291

289292
if user_id != "":
290293
roles_json = management_API.get_roles_for_user(user_id)
291-
role_names = [r["name"].lower() for r in roles_json]
292-
293-
return role.lower() in role_names
294+
# current_app.logger.info(roles_json)
295+
user_role_names = [r["name"].lower() for r in roles_json]
296+
user_role_names = set(user_role_names)
297+
requested_roles = set([r.lower() for r in roles])
298+
299+
required_threshold = 1 if accept_any else len(requested_roles)
300+
301+
return len(requested_roles & user_role_names) >= required_threshold
294302
else:
295303
return None
296304

297305
def check_ownership(school):
298306
if get_api_user_id() not in school.owner_id:
299-
raise Oops("Authorizing user is not the owner of this school", 401)
307+
raise Oops("Authorizing user does not have permission to access the requested school", 401)
300308

301309

302310
def list_owned_school_ids(cursor, school_id):
@@ -392,7 +400,7 @@ def requires_admin(f):
392400
@wraps(f)
393401
def decorated(*args, **kwargs):
394402

395-
is_admin = check_for_role("admin")
403+
is_admin = check_for_roles(["admin", "school admin"])
396404
if is_admin is None:
397405
raise Oops("There must be a user signed in to perform this action",
398406
400, title="No User Authorization")

common/services/auth0management.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ def get_roles_for_user(self, user_id):
3030
{"Authorization": "Bearer " + self.access_token}}
3131
resp = requests.get(url, headers=heads)
3232
data = resp.json()
33-
return data
33+
if isinstance(data, list):
34+
return data
35+
elif (data.get("statusCode") == 401 and "xpired token" in data.get("message")):
36+
self.access_token = self.get_token()
37+
return self.get_roles_for_user(user_id)
38+
else:
39+
logging.error("encountered unexpected auth0 management API response")
40+
logging.error(data)
41+
return []
3442

3543

3644
def get_token(self):

docs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def create_docs(app):
1616
# info: {
1717
spec = APISpec(
1818
title="ClassClock API",
19-
version="0.3.1",
19+
version="0.3.2",
2020
openapi_version='2.0',
2121

2222
plugins=[

0 commit comments

Comments
 (0)