| 1 |
import json |
| 2 |
from http import HTTPStatus |
| 3 |
from typing import Any |
| 4 |
from unittest.mock import ANY |
| 5 |
|
| 6 |
from django.core.exceptions import ValidationError |
| 7 |
from django.http import JsonResponse |
| 8 |
from django.urls import NoReverseMatch, reverse |
| 9 |
|
| 10 |
import pytest |
| 11 |
|
| 12 |
from allauth.core.exceptions import ImmediateHttpResponse |
| 13 |
from allauth.idp.oidc.adapter import DefaultOIDCAdapter |
| 14 |
from allauth.idp.oidc.models import Client, Token |
| 15 |
|
| 16 |
|
| 17 |
class DCRTestAdapter(DefaultOIDCAdapter): |
| 18 |
def validate_client_registration(self, *, client_metadata, **kwargs): |
| 19 |
name = client_metadata.get("client_name") |
| 20 |
if name == "Validation Error": |
| 21 |
raise ValidationError("Custom validation error.") |
| 22 |
elif name == "Immediate Response": |
| 23 |
raise ImmediateHttpResponse( |
| 24 |
JsonResponse( |
| 25 |
status=HTTPStatus.GONE, |
| 26 |
data={"error": "custom", "error_description": "Custom."}, |
| 27 |
) |
| 28 |
) |
| 29 |
|
| 30 |
|
| 31 |
@pytest.fixture |
| 32 |
def initial_access_token_required(): |
| 33 |
return False |
| 34 |
|
| 35 |
|
| 36 |
@pytest.fixture |
| 37 |
def dcr_enabled(settings_impacting_urls, initial_access_token_required): |
| 38 |
with settings_impacting_urls( |
| 39 |
IDP_OIDC_DCR_ENABLED=True, |
| 40 |
IDP_OIDC_DCR_REQUIRES_INITIAL_ACCESS_TOKEN=initial_access_token_required, |
| 41 |
IDP_OIDC_ADAPTER=("tests.apps.idp.oidc.test_dcr.DCRTestAdapter"), |
| 42 |
): |
| 43 |
yield |
| 44 |
|
| 45 |
|
| 46 |
@pytest.fixture |
| 47 |
def register_client(client, dcr_enabled, db): |
| 48 |
def register(data: Any, authorization: str | None = None): |
| 49 |
kwargs = {} |
| 50 |
if authorization is not None: |
| 51 |
kwargs["HTTP_AUTHORIZATION"] = authorization |
| 52 |
return client.post( |
| 53 |
reverse("idp:oidc:client_registration"), |
| 54 |
data=json.dumps(data), |
| 55 |
content_type="application/json", |
| 56 |
**kwargs, |
| 57 |
) |
| 58 |
|
| 59 |
return register |
| 60 |
|
| 61 |
|
| 62 |
def test_public_client(register_client): |
| 63 |
client_metadata = { |
| 64 |
"client_name": "Dynamic Client", |
| 65 |
"redirect_uris": ["https://dynaclient.org/callback"], |
| 66 |
"grant_types": ["authorization_code"], |
| 67 |
"response_types": ["code"], |
| 68 |
"token_endpoint_auth_method": "none", |
| 69 |
"logo_uri": "https://client.example.org/logo.png", |
| 70 |
"scope": "openid profile", |
| 71 |
} |
| 72 |
resp = register_client(client_metadata) |
| 73 |
assert resp.status_code == HTTPStatus.CREATED |
| 74 |
data = resp.json() |
| 75 |
assert data == { |
| 76 |
"client_id": ANY, |
| 77 |
"client_id_issued_at": ANY, |
| 78 |
"client_name": "Dynamic Client", |
| 79 |
"redirect_uris": ["https://dynaclient.org/callback"], |
| 80 |
"grant_types": ["authorization_code"], |
| 81 |
"response_types": ["code"], |
| 82 |
"token_endpoint_auth_method": "none", |
| 83 |
"scope": "openid profile", |
| 84 |
} |
| 85 |
client = Client.objects.get(id=data["client_id"]) |
| 86 |
assert client.type == Client.Type.PUBLIC |
| 87 |
assert client.get_scopes() == ["openid", "profile"] |
| 88 |
assert client.get_redirect_uris() == ["https://dynaclient.org/callback"] |
| 89 |
assert client.data["dcr"] |
| 90 |
assert client.data["client_metadata"] == client_metadata |
| 91 |
|
| 92 |
|
| 93 |
def test_confidential_client(register_client): |
| 94 |
resp = register_client( |
| 95 |
{ |
| 96 |
"client_name": "My Server", |
| 97 |
"redirect_uris": ["https://example.com/callback"], |
| 98 |
"post_logout_redirect_uris": ["https://example.com/logout"], |
| 99 |
"token_endpoint_auth_method": "client_secret_basic", |
| 100 |
}, |
| 101 |
) |
| 102 |
assert resp.status_code == HTTPStatus.CREATED |
| 103 |
data = resp.json() |
| 104 |
assert "client_secret" in data |
| 105 |
assert data["client_secret_expires_at"] == 0 |
| 106 |
|
| 107 |
client = Client.objects.get(id=data["client_id"]) |
| 108 |
assert client.type == Client.Type.CONFIDENTIAL |
| 109 |
assert client.check_secret(data["client_secret"]) |
| 110 |
assert client.get_post_logout_redirect_uris() == ["https://example.com/logout"] |
| 111 |
|
| 112 |
|
| 113 |
def test_missing_redirect_uris(register_client): |
| 114 |
resp = register_client({"client_name": "Client without redirect URIs"}) |
| 115 |
assert resp.status_code == HTTPStatus.BAD_REQUEST |
| 116 |
assert resp.json() == { |
| 117 |
"error": "invalid_redirect_uri", |
| 118 |
"error_description": "'redirect_uris': This field is required.", |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
def test_invalid_post_logout_redirect_uris(register_client): |
| 123 |
resp = register_client( |
| 124 |
{ |
| 125 |
"redirect_uris": ["https://example.com/callback"], |
| 126 |
"post_logout_redirect_uris": ["https://example.com/logout", 123], |
| 127 |
} |
| 128 |
) |
| 129 |
assert resp.status_code == HTTPStatus.BAD_REQUEST |
| 130 |
assert resp.json() == { |
| 131 |
"error": "invalid_client_metadata", |
| 132 |
"error_description": ( |
| 133 |
"'post_logout_redirect_uris': post_logout_redirect_uris must be an" |
| 134 |
" array of strings." |
| 135 |
), |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
def test_invalid_json_value(register_client): |
| 140 |
resp = register_client(666) |
| 141 |
assert resp.status_code == HTTPStatus.BAD_REQUEST |
| 142 |
assert resp.json() == { |
| 143 |
"error": "invalid_client_metadata", |
| 144 |
"error_description": "Invalid JSON data.", |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
def test_invalid_content_type(client, dcr_enabled, db): |
| 149 |
resp = client.post( |
| 150 |
reverse("idp:oidc:client_registration"), |
| 151 |
data="not JSON", |
| 152 |
content_type="text/plain", |
| 153 |
) |
| 154 |
assert resp.status_code == HTTPStatus.BAD_REQUEST |
| 155 |
assert resp.json() == { |
| 156 |
"error": "invalid_client_metadata", |
| 157 |
"error_description": "Invalid JSON data.", |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
def test_default_scopes(register_client): |
| 162 |
resp = register_client( |
| 163 |
{ |
| 164 |
"redirect_uris": ["https://example.com/callback"], |
| 165 |
}, |
| 166 |
) |
| 167 |
assert resp.status_code == HTTPStatus.CREATED |
| 168 |
assert resp.json()["scope"] == "openid" |
| 169 |
|
| 170 |
|
| 171 |
def test_default_auth_method(register_client): |
| 172 |
resp = register_client( |
| 173 |
{"redirect_uris": ["https://example.com/callback"]}, |
| 174 |
) |
| 175 |
assert resp.status_code == HTTPStatus.CREATED |
| 176 |
data = resp.json() |
| 177 |
assert data["token_endpoint_auth_method"] == "client_secret_basic" |
| 178 |
assert "client_secret" in data |
| 179 |
|
| 180 |
|
| 181 |
def test_dcr_disabled_by_default(): |
| 182 |
with pytest.raises(NoReverseMatch): |
| 183 |
reverse("idp:oidc:client_registration") |
| 184 |
|
| 185 |
|
| 186 |
def test_configuration_includes_registration_endpoint(client, dcr_enabled, db): |
| 187 |
resp = client.get(reverse("idp:oidc:configuration")) |
| 188 |
data = resp.json() |
| 189 |
assert "registration_endpoint" in data |
| 190 |
assert data["registration_endpoint"].endswith( |
| 191 |
reverse("idp:oidc:client_registration") |
| 192 |
) |
| 193 |
|
| 194 |
|
| 195 |
def test_configuration_excludes_registration_when_disabled(client, db): |
| 196 |
resp = client.get(reverse("idp:oidc:configuration")) |
| 197 |
data = resp.json() |
| 198 |
assert "registration_endpoint" not in data |
| 199 |
|
| 200 |
|
| 201 |
def test_rate_limits(register_client, enable_cache, settings): |
| 202 |
settings.IDP_OIDC_RATE_LIMITS = {"client_registration": "2/m/ip"} |
| 203 |
for attempt in range(3): |
| 204 |
resp = register_client( |
| 205 |
{ |
| 206 |
"redirect_uris": ["https://example.com/callback"], |
| 207 |
} |
| 208 |
) |
| 209 |
assert resp.status_code == ( |
| 210 |
HTTPStatus.CREATED if attempt <= 1 else HTTPStatus.TOO_MANY_REQUESTS |
| 211 |
) |
| 212 |
|
| 213 |
|
| 214 |
@pytest.mark.parametrize( |
| 215 |
"client_name,status,result", |
| 216 |
[ |
| 217 |
( |
| 218 |
"Validation Error", |
| 219 |
HTTPStatus.BAD_REQUEST, |
| 220 |
{ |
| 221 |
"error": "invalid_client_metadata", |
| 222 |
"error_description": "Custom validation error.", |
| 223 |
}, |
| 224 |
), |
| 225 |
( |
| 226 |
"Immediate Response", |
| 227 |
HTTPStatus.GONE, |
| 228 |
{ |
| 229 |
"error": "custom", |
| 230 |
"error_description": "Custom.", |
| 231 |
}, |
| 232 |
), |
| 233 |
( |
| 234 |
"Too long" * 100, |
| 235 |
HTTPStatus.BAD_REQUEST, |
| 236 |
{ |
| 237 |
"error": "invalid_client_metadata", |
| 238 |
"error_description": "'client_name': Ensure this value has at most 100 characters (it has 800).", |
| 239 |
}, |
| 240 |
), |
| 241 |
( |
| 242 |
"<script>alert(1)</script>", |
| 243 |
HTTPStatus.BAD_REQUEST, |
| 244 |
{ |
| 245 |
"error": "invalid_client_metadata", |
| 246 |
"error_description": "'client_name': Client name must not contain '<' or '>'.", |
| 247 |
}, |
| 248 |
), |
| 249 |
], |
| 250 |
) |
| 251 |
def test_validation_errors(register_client, client_name, result, status): |
| 252 |
resp = register_client( |
| 253 |
{ |
| 254 |
"client_name": client_name, |
| 255 |
"redirect_uris": ["https://example.com/callback"], |
| 256 |
}, |
| 257 |
) |
| 258 |
assert resp.status_code == status |
| 259 |
assert resp.json() == result |
| 260 |
|
| 261 |
|
| 262 |
@pytest.mark.parametrize("initial_access_token_required", (True,)) |
| 263 |
@pytest.mark.parametrize( |
| 264 |
"authorization_method,authorization_token,token_type,status", |
| 265 |
[ |
| 266 |
(None, None, None, HTTPStatus.UNAUTHORIZED), |
| 267 |
("Bearer", "iat", Token.Type.INITIAL_ACCESS_TOKEN, HTTPStatus.CREATED), |
| 268 |
("Bearer", "at", Token.Type.ACCESS_TOKEN, HTTPStatus.UNAUTHORIZED), |
| 269 |
("BeArEr", "iat", Token.Type.INITIAL_ACCESS_TOKEN, HTTPStatus.CREATED), |
| 270 |
("Token", "iat", Token.Type.INITIAL_ACCESS_TOKEN, HTTPStatus.UNAUTHORIZED), |
| 271 |
], |
| 272 |
) |
| 273 |
def test_authorization( |
| 274 |
initial_access_token_required, |
| 275 |
register_client, |
| 276 |
authorization_method, |
| 277 |
authorization_token, |
| 278 |
token_type, |
| 279 |
status, |
| 280 |
): |
| 281 |
authorization = None |
| 282 |
if authorization_method: |
| 283 |
authorization = f"{authorization_method} {authorization_token}" |
| 284 |
if token_type: |
| 285 |
token = Token(type=token_type) |
| 286 |
token.set_value(authorization_token) |
| 287 |
token.save() |
| 288 |
resp = register_client( |
| 289 |
{ |
| 290 |
"redirect_uris": ["https://example.com/callback"], |
| 291 |
}, |
| 292 |
authorization=authorization, |
| 293 |
) |
| 294 |
assert resp.status_code == status |