Skip to content

Commit 60a3b29

Browse files
committed
Add bypass for token validation
1 parent c3ee137 commit 60a3b29

2 files changed

Lines changed: 12 additions & 0 deletions

File tree

manage_breast_screening/dicom/tests/test_token_validator.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,10 @@ def test_with_valid_token(
119119
assert validator(Mock(headers={"Authorization": "Bearer abc123"})) == {
120120
"sub": "1234567890"
121121
}
122+
123+
def test_authentication_bypass_enabled(self, mock_logger):
124+
with patch.dict("os.environ", {"BYPASS_API_TOKEN_AUTH": "true"}):
125+
validator = TokenValidator()
126+
assert validator(Mock(headers={"Authorization": "Bearer anytoken"})) == {
127+
"sub": "bypass_user"
128+
}

manage_breast_screening/dicom/token_validator.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
class TokenValidator(HttpBearer):
1515
def __init__(self):
16+
self.bypass_auth = os.getenv("BYPASS_API_TOKEN_AUTH", "false").lower() == "true"
1617
self.api_audience = os.getenv("API_AUDIENCE", "")
1718
self.tenant_id = os.getenv("TENANT_ID", "")
1819
self.discovery_keys_url = (
@@ -23,6 +24,10 @@ def __init__(self):
2324
self.issuer_url = "https://sts.windows.net/" + self.tenant_id + "/"
2425

2526
def authenticate(self, request, token) -> dict | None:
27+
if self.bypass_auth:
28+
logger.warning("Authentication bypass is enabled.")
29+
return {"sub": "bypass_user"}
30+
2631
rsa_key = self._rsa_key(token)
2732
if rsa_key:
2833
return self._decode(token, rsa_key)

0 commit comments

Comments
 (0)