mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 10:01:00 +01:00
The OAuth2 password flow in the OpenAPI schema declared its tokenUrl as /token, but the token endpoint is served at /api/token; a POST to /token is rejected with a 403 before any token code runs. Clients that read tokenUrl from the schema, such as Postman's OpenAPI import, request tokens from the wrong place. The test compares the schema's tokenUrl with the token route's own path, so a future prefix change can't drift them apart again. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
205 lines
5.7 KiB
Python
205 lines
5.7 KiB
Python
import asyncio
|
|
|
|
import pytest
|
|
from fastapi import status
|
|
from fastapi.exceptions import HTTPException
|
|
|
|
from config import OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS
|
|
from handler.auth.base_handler import auth_handler
|
|
from handler.auth.constants import EDIT_SCOPES
|
|
|
|
|
|
def test_refreshing_oauth_token_basic(client, refresh_token):
|
|
response = client.post(
|
|
"/api/token",
|
|
data={
|
|
"grant_type": "refresh_token",
|
|
"refresh_token": refresh_token,
|
|
},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["access_token"]
|
|
assert body["token_type"] == "bearer"
|
|
assert body["expires"] == OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS
|
|
|
|
|
|
def test_the_schema_token_url_is_the_token_route(client):
|
|
"""Clients such as Swagger UI's Authorize button request tokens from this URL."""
|
|
schemes = client.app.openapi()["components"]["securitySchemes"]
|
|
password_flow = schemes["OAuth2PasswordBearer"]["flows"]["password"]
|
|
|
|
assert password_flow["tokenUrl"] == client.app.url_path_for("token")
|
|
|
|
|
|
def test_refreshing_oauth_token_without_refresh_token(client):
|
|
try:
|
|
client.post(
|
|
"/api/token",
|
|
data={
|
|
"grant_type": "refresh_token",
|
|
},
|
|
)
|
|
except HTTPException as e:
|
|
assert e.status_code == status.HTTP_400_BAD_REQUEST
|
|
assert e.detail == "Missing refresh token"
|
|
|
|
|
|
def test_refreshing_oauth_token_with_invalid_refresh_token(client):
|
|
try:
|
|
client.post(
|
|
"/api/token",
|
|
data={
|
|
"grant_type": "refresh_token",
|
|
"refresh_token": "invalid_token",
|
|
},
|
|
)
|
|
except HTTPException as e:
|
|
assert e.status_code == status.HTTP_400_BAD_REQUEST
|
|
assert e.detail == "Invalid refresh token"
|
|
|
|
|
|
def test_refresh_token_rotation_invalidates_old_token(client, refresh_token):
|
|
first_response = client.post(
|
|
"/api/token",
|
|
data={
|
|
"grant_type": "refresh_token",
|
|
"refresh_token": refresh_token,
|
|
},
|
|
)
|
|
|
|
assert first_response.status_code == status.HTTP_200_OK
|
|
first_body = first_response.json()
|
|
|
|
assert first_body["access_token"]
|
|
assert first_body["refresh_token"]
|
|
assert first_body["refresh_token"] != refresh_token
|
|
|
|
new_refresh_token = first_body["refresh_token"]
|
|
|
|
second_response = client.post(
|
|
"/api/token",
|
|
data={
|
|
"grant_type": "refresh_token",
|
|
"refresh_token": refresh_token,
|
|
},
|
|
)
|
|
|
|
assert second_response.status_code == status.HTTP_401_UNAUTHORIZED
|
|
|
|
third_response = client.post(
|
|
"/api/token",
|
|
data={
|
|
"grant_type": "refresh_token",
|
|
"refresh_token": new_refresh_token,
|
|
},
|
|
)
|
|
|
|
assert third_response.status_code == status.HTTP_200_OK
|
|
|
|
|
|
def test_auth_via_upass(client, admin_user):
|
|
response = client.post(
|
|
"/api/token",
|
|
data={
|
|
"grant_type": "password",
|
|
"username": "test_admin",
|
|
"password": "test_admin_password",
|
|
},
|
|
)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
body = response.json()
|
|
assert body["access_token"]
|
|
assert body["refresh_token"]
|
|
assert body["token_type"] == "bearer"
|
|
assert body["expires"] == OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS
|
|
|
|
|
|
def test_auth_via_upass_checks_password_off_event_loop(
|
|
client, admin_user, monkeypatch: pytest.MonkeyPatch
|
|
):
|
|
on_loop: list[bool] = []
|
|
verify_password = auth_handler.verify_password
|
|
|
|
def recording_verify_password(plain: str, hashed: str | None) -> bool:
|
|
try:
|
|
asyncio.get_running_loop()
|
|
on_loop.append(True)
|
|
except RuntimeError:
|
|
on_loop.append(False)
|
|
return verify_password(plain, hashed)
|
|
|
|
monkeypatch.setattr(auth_handler, "verify_password", recording_verify_password)
|
|
|
|
response = client.post(
|
|
"/api/token",
|
|
data={
|
|
"grant_type": "password",
|
|
"username": "test_admin",
|
|
"password": "test_admin_password",
|
|
},
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_200_OK
|
|
assert on_loop == [False]
|
|
|
|
|
|
def test_auth_via_upass_with_invalid_credentials(client, admin_user):
|
|
try:
|
|
client.post(
|
|
"/api/token",
|
|
data={
|
|
"grant_type": "password",
|
|
"username": "test_admin",
|
|
"password": "a_bad_password",
|
|
},
|
|
)
|
|
except HTTPException as e:
|
|
assert e.status_code == status.HTTP_401_UNAUTHORIZED
|
|
assert e.detail == "Invalid username or password"
|
|
|
|
|
|
def test_auth_via_upass_with_excess_scopes(client, viewer_user):
|
|
try:
|
|
client.post(
|
|
"/api/token",
|
|
data={
|
|
"grant_type": "password",
|
|
"username": "test_viewer",
|
|
"password": "test_viewer_password",
|
|
"scopes": EDIT_SCOPES,
|
|
},
|
|
)
|
|
except HTTPException as e:
|
|
assert e.status_code == status.HTTP_403_FORBIDDEN
|
|
assert e.detail == "Insufficient scope"
|
|
|
|
|
|
def test_auth_with_invalid_grant_type(client):
|
|
try:
|
|
client.post(
|
|
"/api/token",
|
|
data={
|
|
"grant_type": "invalid_type",
|
|
},
|
|
)
|
|
except HTTPException as e:
|
|
assert e.status_code == status.HTTP_400_BAD_REQUEST
|
|
assert e.detail == "Invalid or unsupported grant type"
|
|
|
|
|
|
def test_refreshing_oauth_token_expired_refresh_token(
|
|
client, admin_user, expired_refresh_token
|
|
):
|
|
response = client.post(
|
|
"/api/token",
|
|
data={
|
|
"grant_type": "refresh_token",
|
|
"refresh_token": expired_refresh_token,
|
|
},
|
|
)
|
|
|
|
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|