Skip to content

Commit f943c47

Browse files
committed
Remove CAS-specific logic
1 parent c5b9025 commit f943c47

File tree

3 files changed

+42
-44
lines changed

3 files changed

+42
-44
lines changed

src/scaup/auth/micro.py

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
from typing import TypeVar
1+
from typing import List, TypeVar
22

33
import requests
44
from fastapi import Depends, HTTPException, Request, status
55
from fastapi.security import HTTPAuthorizationCredentials
6+
from jwt import InvalidAlgorithmError, InvalidAudienceError
67
from lims_utils.auth import GenericUser
78
from lims_utils.logging import app_logger
89
from lims_utils.models import parse_proposal
@@ -23,21 +24,8 @@
2324
T = TypeVar("T")
2425

2526

26-
def _is_cas_token(token: str):
27-
return len(token) > 2 and token[:2] == "AT"
28-
29-
3027
def _get_user(token: str):
31-
if _is_cas_token(token):
32-
response = requests.get(
33-
Config.auth.endpoint + "/user",
34-
headers={"Authorization": f"Bearer {token}"},
35-
)
36-
37-
if response.status_code != 200:
38-
raise HTTPException(status_code=response.status_code, detail=response.json().get("detail"))
39-
return response.json()
40-
else:
28+
try:
4129
# TODO: replace this once something more permanent becomes available
4230
user = decode_jwt(token, "scaup_general")
4331
app_id = user.get("sub")
@@ -51,6 +39,15 @@ def _get_user(token: str):
5139
"permissions": user.get("permissions"),
5240
"email": "",
5341
}
42+
except (InvalidAudienceError, InvalidAlgorithmError):
43+
response = requests.get(
44+
Config.auth.endpoint + "/user",
45+
headers={"Authorization": f"Bearer {token}"},
46+
)
47+
48+
if response.status_code != 200:
49+
raise HTTPException(status_code=response.status_code, detail=response.json().get("detail"))
50+
return response.json()
5451

5552

5653
class User(GenericUser):
@@ -67,33 +64,33 @@ def __init__(
6764

6865

6966
def _check_perms(data_id: T, endpoint: str, token: str) -> T:
70-
if not _is_cas_token(token):
71-
user = _get_user(token)
72-
if is_admin(user["permissions"]):
67+
try:
68+
permissions: List[str] = decode_jwt(token, "scaup_general").get("permissions", [])
69+
if is_admin(permissions):
7370
return data_id
7471

7572
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Provided JWT lacks permissions")
73+
except (InvalidAudienceError, InvalidAlgorithmError):
74+
response = requests.get(
75+
"".join(
76+
[
77+
Config.auth.endpoint,
78+
"/permission/",
79+
endpoint,
80+
"/",
81+
str(data_id) if endpoint != "proposal" else str(data_id) + "/inSessions",
82+
]
83+
),
84+
headers={"Authorization": f"Bearer {token}"},
85+
)
86+
87+
if response.status_code != 200:
88+
detail = response.json().get("detail")
89+
app_logger.error(f"Microauth returned {response.status_code}: {detail}")
90+
91+
raise HTTPException(status_code=response.status_code, detail=detail)
7692

77-
response = requests.get(
78-
"".join(
79-
[
80-
Config.auth.endpoint,
81-
"/permission/",
82-
endpoint,
83-
"/",
84-
str(data_id) if endpoint != "proposal" else str(data_id) + "/inSessions",
85-
]
86-
),
87-
headers={"Authorization": f"Bearer {token}"},
88-
)
89-
90-
if response.status_code != 200:
91-
detail = response.json().get("detail")
92-
app_logger.error(f"Microauth returned {response.status_code}: {detail}")
93-
94-
raise HTTPException(status_code=response.status_code, detail=detail)
95-
96-
return data_id
93+
return data_id
9794

9895

9996
def _generic_table_check(

src/scaup/utils/auth.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
from typing import Any
22

33
from fastapi import HTTPException, status
4-
from jwt import DecodeError, ExpiredSignatureError, InvalidAudienceError, decode
4+
from jwt import DecodeError, decode
55
from lims_utils.auth import GenericUser
66
from lims_utils.logging import app_logger
77

88
from .config import Config
99

1010

11-
def is_admin(perms: list[int]):
11+
def is_admin(perms: list[str]):
1212
return bool(set(Config.auth.read_all_perms) & set(perms))
1313

1414

@@ -30,8 +30,8 @@ def decode_jwt(token: str, aud: str = Config.shipping_service.callback_url) -> d
3030
)
3131

3232
return decoded_body
33-
except (DecodeError, ExpiredSignatureError, InvalidAudienceError) as e:
34-
app_logger.warning(f"Error while parsing token {token}: {e}")
33+
except DecodeError as e:
34+
app_logger.warning(f"Error while parsing token: {e}")
3535
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid token provided")
3636

3737

tests/utils/test_auth.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import jwt
22
import pytest
33
from fastapi import HTTPException
4+
from jwt.exceptions import ExpiredSignatureError, InvalidAudienceError
45

56
from scaup.utils.auth import check_em_staff, check_jwt
67
from scaup.utils.config import Config
@@ -33,7 +34,7 @@ def test_jwt_invalid_aud():
3334
"""Should not allow unmatched audiences"""
3435
token = jwt.encode({"id": 1, "exp": 9e9, "aud": "invalid-aud"}, Config.auth.jwt_private, algorithm="ES256")
3536

36-
with pytest.raises(HTTPException, match="401: Invalid token provided"):
37+
with pytest.raises(InvalidAudienceError):
3738
check_jwt(token, 1)
3839

3940

@@ -43,7 +44,7 @@ def test_jwt_invalid_exp():
4344
{"id": 1, "exp": 0, "aud": Config.shipping_service.callback_url}, Config.auth.jwt_private, algorithm="ES256"
4445
)
4546

46-
with pytest.raises(HTTPException, match="401: Invalid token provided"):
47+
with pytest.raises(ExpiredSignatureError):
4748
check_jwt(token, 1)
4849

4950

0 commit comments

Comments
 (0)