|
8 | 8 | from fastapi import Request |
9 | 9 |
|
10 | 10 | from dara.core.auth.basic import BasicAuthConfig, MultiBasicAuthConfig |
11 | | -from dara.core.auth.definitions import ID_TOKEN, JWT_ALGO, SESSION_ID, SESSION_TOKEN_COOKIE_NAME, TokenData |
| 11 | +from dara.core.auth.definitions import ( |
| 12 | + EXPIRED_TOKEN_ERROR, |
| 13 | + ID_TOKEN, |
| 14 | + JWT_ALGO, |
| 15 | + SESSION_ID, |
| 16 | + SESSION_TOKEN_COOKIE_NAME, |
| 17 | + TokenData, |
| 18 | +) |
12 | 19 | from dara.core.auth.utils import token_refresh_cache |
13 | 20 | from dara.core.configuration import ConfigurationBuilder |
14 | 21 | from dara.core.http import get |
@@ -87,7 +94,8 @@ def handle(): |
87 | 94 |
|
88 | 95 | # Test no auth |
89 | 96 | response = await client.get('/api/test-ext/test') |
90 | | - assert response.status_code == 400 |
| 97 | + assert response.status_code == 401 |
| 98 | + assert response.json()['detail'] == EXPIRED_TOKEN_ERROR |
91 | 99 |
|
92 | 100 | # Test wrong scheme |
93 | 101 | response = await client.get('/api/test-ext/test', headers={'Authorization': 'Basic user:pw'}) |
@@ -150,6 +158,21 @@ async def test_verify_session_invalid_cookie_clears_auth_cookies_without_refresh |
150 | 158 | assert any(cookie.startswith(f'{SESSION_TOKEN_COOKIE_NAME}="";') for cookie in cleared_cookies) |
151 | 159 |
|
152 | 160 |
|
| 161 | +async def test_verify_session_missing_auth_returns_unauthorized(): |
| 162 | + """Check that missing auth credentials are treated as an auth failure, not a bad request.""" |
| 163 | + |
| 164 | + config = ConfigurationBuilder() |
| 165 | + config.add_auth(BasicAuthConfig('test', 'test')) |
| 166 | + |
| 167 | + app = _start_application(config._to_configuration()) |
| 168 | + |
| 169 | + async with AsyncClient(app) as client: |
| 170 | + response = await client.post('/api/auth/verify-session') |
| 171 | + |
| 172 | + assert response.status_code == 401 |
| 173 | + assert response.json()['detail'] == EXPIRED_TOKEN_ERROR |
| 174 | + |
| 175 | + |
153 | 176 | async def test_authorization_header_injected_from_session_cookie(): |
154 | 177 | """Check that a missing Authorization header is synthesized from the session cookie""" |
155 | 178 |
|
@@ -355,8 +378,8 @@ async def test_stale_session_cookie_recovers_after_refresh_attempt(): |
355 | 378 |
|
356 | 379 | # Cookie jar should no longer contain stale auth cookies after refresh response. |
357 | 380 | followup_verify_response = await client.post('/api/auth/verify-session') |
358 | | - assert followup_verify_response.status_code == 400 |
359 | | - assert followup_verify_response.json()['detail']['message'] == 'No auth credentials passed' |
| 381 | + assert followup_verify_response.status_code == 401 |
| 382 | + assert followup_verify_response.json()['detail'] == EXPIRED_TOKEN_ERROR |
360 | 383 |
|
361 | 384 |
|
362 | 385 | async def test_refresh_token_unsupported(): |
|
0 commit comments