| 1 |
from datetime import timedelta |
| 2 |
from http import HTTPStatus |
| 3 |
from unittest.mock import ANY |
| 4 |
|
| 5 |
from django.core.cache import cache |
| 6 |
from django.urls import reverse |
| 7 |
from django.utils import timezone |
| 8 |
from django.utils.http import urlencode |
| 9 |
|
| 10 |
import pytest |
| 11 |
from pytest_django.asserts import assertTemplateUsed |
| 12 |
|
| 13 |
from allauth.idp.oidc import app_settings |
| 14 |
from allauth.idp.oidc.internal.oauthlib import device_codes |
| 15 |
from allauth.idp.oidc.models import Client, Token |
| 16 |
|
| 17 |
|
| 18 |
@pytest.fixture |
| 19 |
def poll_sleep(): |
| 20 |
def f(device_code, seconds): |
| 21 |
data = cache.get(device_codes.cache_device_code_key(device_code)) |
| 22 |
if data: |
| 23 |
data["last_poll_at"] -= seconds |
| 24 |
device_codes.update_device_state(device_code, data) |
| 25 |
|
| 26 |
return f |
| 27 |
|
| 28 |
|
| 29 |
def test_device_flow_invalid_client( |
| 30 |
db, |
| 31 |
client, |
| 32 |
): |
| 33 |
payload = { |
| 34 |
"client_id": "unknown", |
| 35 |
} |
| 36 |
resp = client.post( |
| 37 |
reverse("idp:oidc:device_code"), |
| 38 |
data=urlencode(payload), |
| 39 |
content_type="application/x-www-form-urlencoded", |
| 40 |
) |
| 41 |
assert resp.status_code == HTTPStatus.BAD_REQUEST |
| 42 |
assert resp.json() == { |
| 43 |
"error": "invalid_request", |
| 44 |
"error_description": "Invalid client_id parameter value.", |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
def test_device_flow_invalid_scope(db, client, device_client): |
| 49 |
payload = { |
| 50 |
"client_id": device_client.id, |
| 51 |
"scope": "openid wrong", |
| 52 |
} |
| 53 |
resp = client.post( |
| 54 |
reverse("idp:oidc:device_code"), |
| 55 |
data=urlencode(payload), |
| 56 |
content_type="application/x-www-form-urlencoded", |
| 57 |
) |
| 58 |
assert resp.status_code == HTTPStatus.BAD_REQUEST |
| 59 |
assert resp.json() == {"error": "invalid_scope"} |
| 60 |
|
| 61 |
|
| 62 |
@pytest.mark.parametrize("action", ["deny", "confirm"]) |
| 63 |
@pytest.mark.parametrize("with_scope", [False, True]) |
| 64 |
@pytest.mark.parametrize( |
| 65 |
"resources", |
| 66 |
[ |
| 67 |
[], |
| 68 |
["https://api.example.com/a"], |
| 69 |
["https://api.example.com/a", "https://api.example.com/b"], |
| 70 |
], |
| 71 |
) |
| 72 |
def test_device_flow( |
| 73 |
client, |
| 74 |
auth_client, |
| 75 |
device_client, |
| 76 |
enable_cache, |
| 77 |
action, |
| 78 |
user, |
| 79 |
with_scope, |
| 80 |
poll_sleep, |
| 81 |
resources, |
| 82 |
): |
| 83 |
device_client.set_default_scopes(["profile"]) |
| 84 |
device_client.save() |
| 85 |
payload = { |
| 86 |
"client_id": device_client.id, |
| 87 |
} |
| 88 |
if with_scope: |
| 89 |
payload["scope"] = "openid email" |
| 90 |
if resources: |
| 91 |
payload["resource"] = resources |
| 92 |
resp = client.post( |
| 93 |
reverse("idp:oidc:device_code"), |
| 94 |
data=urlencode(payload, doseq=True), |
| 95 |
content_type="application/x-www-form-urlencoded", |
| 96 |
) |
| 97 |
assert resp.status_code == HTTPStatus.OK |
| 98 |
data = resp.json() |
| 99 |
assert data == { |
| 100 |
"verification_uri": "http://testserver/identity/o/device", |
| 101 |
"verification_uri_complete": ANY, |
| 102 |
"expires_in": app_settings.DEVICE_CODE_EXPIRES_IN, |
| 103 |
"user_code": ANY, |
| 104 |
"device_code": ANY, |
| 105 |
"interval": 5, |
| 106 |
} |
| 107 |
assert ( |
| 108 |
data["verification_uri_complete"] |
| 109 |
== f"{data['verification_uri']}?code={data['user_code']}" |
| 110 |
) |
| 111 |
|
| 112 |
resp = client.post( |
| 113 |
reverse("idp:oidc:token"), |
| 114 |
{ |
| 115 |
"device_code": data["device_code"], |
| 116 |
"grant_type": Client.GrantType.DEVICE_CODE, |
| 117 |
"client_id": device_client.id, |
| 118 |
}, |
| 119 |
) |
| 120 |
assert resp.status_code == HTTPStatus.BAD_REQUEST |
| 121 |
assert resp.json() == { |
| 122 |
"error": "authorization_pending", |
| 123 |
} |
| 124 |
|
| 125 |
resp = auth_client.get(data["verification_uri"]) |
| 126 |
assert resp.status_code == HTTPStatus.OK |
| 127 |
assertTemplateUsed(resp, "idp/oidc/device_authorization_code_form.html") |
| 128 |
assert resp.context["form"].errors == {} |
| 129 |
|
| 130 |
resp = auth_client.get(f"{data['verification_uri']}?code=wrong") |
| 131 |
assert resp.status_code == HTTPStatus.OK |
| 132 |
assertTemplateUsed(resp, "idp/oidc/device_authorization_code_form.html") |
| 133 |
assert resp.context["form"].errors == {"code": ["Incorrect code."]} |
| 134 |
|
| 135 |
resp = auth_client.get(data["verification_uri_complete"]) |
| 136 |
assert resp.status_code == HTTPStatus.OK |
| 137 |
assertTemplateUsed(resp, "idp/oidc/device_authorization_confirm_form.html") |
| 138 |
|
| 139 |
confirmed = action == "confirm" |
| 140 |
resp = auth_client.post(data["verification_uri_complete"], {"action": action}) |
| 141 |
assert resp.status_code == HTTPStatus.OK |
| 142 |
assertTemplateUsed( |
| 143 |
resp, |
| 144 |
( |
| 145 |
"idp/oidc/device_authorization_confirmed.html" |
| 146 |
if confirmed |
| 147 |
else "idp/oidc/device_authorization_denied.html" |
| 148 |
), |
| 149 |
) |
| 150 |
|
| 151 |
poll_sleep(data["device_code"], 10) |
| 152 |
resp = client.post( |
| 153 |
reverse("idp:oidc:token"), |
| 154 |
{ |
| 155 |
"device_code": data["device_code"], |
| 156 |
"grant_type": Client.GrantType.DEVICE_CODE, |
| 157 |
"client_id": device_client.id, |
| 158 |
}, |
| 159 |
) |
| 160 |
if confirmed: |
| 161 |
assert resp.status_code == HTTPStatus.OK |
| 162 |
assert resp.json() == { |
| 163 |
"access_token": ANY, |
| 164 |
"expires_in": 3600, |
| 165 |
"refresh_token": ANY, |
| 166 |
"scope": "openid email" if with_scope else "profile", |
| 167 |
"token_type": "Bearer", |
| 168 |
} |
| 169 |
token = Token.objects.lookup( |
| 170 |
Token.Type.ACCESS_TOKEN, resp.json()["access_token"] |
| 171 |
) |
| 172 |
assert set(token.get_resources()) == set(resources) |
| 173 |
assert token.user_id == user.pk |
| 174 |
# Single-use device codes. |
| 175 |
poll_sleep(data["device_code"], 10) |
| 176 |
resp = client.post( |
| 177 |
reverse("idp:oidc:token"), |
| 178 |
{ |
| 179 |
"device_code": data["device_code"], |
| 180 |
"grant_type": Client.GrantType.DEVICE_CODE, |
| 181 |
"client_id": device_client.id, |
| 182 |
}, |
| 183 |
) |
| 184 |
assert resp.status_code == HTTPStatus.BAD_REQUEST |
| 185 |
assert resp.json() == { |
| 186 |
"error": "invalid_grant", |
| 187 |
} |
| 188 |
|
| 189 |
else: |
| 190 |
assert resp.status_code == HTTPStatus.BAD_REQUEST |
| 191 |
assert resp.json() == { |
| 192 |
"error": "access_denied", |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
def test_device_flow_refresh_token_expiry( |
| 197 |
client, |
| 198 |
auth_client, |
| 199 |
device_client, |
| 200 |
enable_cache, |
| 201 |
user, |
| 202 |
poll_sleep, |
| 203 |
settings, |
| 204 |
): |
| 205 |
# The refresh token issued by the device grant carries the configured |
| 206 |
# expiry, exposed in the token response as ``refresh_expires_in``. |
| 207 |
settings.IDP_OIDC_REFRESH_TOKEN_EXPIRES_IN = 3600 |
| 208 |
resp = client.post( |
| 209 |
reverse("idp:oidc:device_code"), |
| 210 |
data=urlencode({"client_id": device_client.id, "scope": "openid"}), |
| 211 |
content_type="application/x-www-form-urlencoded", |
| 212 |
) |
| 213 |
assert resp.status_code == HTTPStatus.OK |
| 214 |
data = resp.json() |
| 215 |
resp = auth_client.get(data["verification_uri_complete"]) |
| 216 |
assert resp.status_code == HTTPStatus.OK |
| 217 |
resp = auth_client.post(data["verification_uri_complete"], {"action": "confirm"}) |
| 218 |
assert resp.status_code == HTTPStatus.OK |
| 219 |
poll_sleep(data["device_code"], 10) |
| 220 |
resp = client.post( |
| 221 |
reverse("idp:oidc:token"), |
| 222 |
{ |
| 223 |
"device_code": data["device_code"], |
| 224 |
"grant_type": Client.GrantType.DEVICE_CODE, |
| 225 |
"client_id": device_client.id, |
| 226 |
}, |
| 227 |
) |
| 228 |
assert resp.status_code == HTTPStatus.OK |
| 229 |
data = resp.json() |
| 230 |
assert abs(data["refresh_expires_in"] - 3600) < 30 |
| 231 |
refresh_token = Token.objects.lookup( |
| 232 |
Token.Type.REFRESH_TOKEN, data["refresh_token"] |
| 233 |
) |
| 234 |
expected = timezone.now() + timedelta(seconds=3600) |
| 235 |
assert abs((refresh_token.expires_at - expected).total_seconds()) < 30 |
| 236 |
|
| 237 |
|
| 238 |
def test_slow_down_flow(client, device_client, enable_cache, poll_sleep): |
| 239 |
payload = { |
| 240 |
"client_id": device_client.id, |
| 241 |
} |
| 242 |
resp = client.post( |
| 243 |
reverse("idp:oidc:device_code"), |
| 244 |
data=urlencode(payload), |
| 245 |
content_type="application/x-www-form-urlencoded", |
| 246 |
) |
| 247 |
assert resp.status_code == HTTPStatus.OK |
| 248 |
data = resp.json() |
| 249 |
|
| 250 |
for sleep, error in [ |
| 251 |
(0, "authorization_pending"), |
| 252 |
(3, "slow_down"), |
| 253 |
(3, "authorization_pending"), |
| 254 |
]: |
| 255 |
if sleep: |
| 256 |
poll_sleep(data["device_code"], sleep) |
| 257 |
resp = client.post( |
| 258 |
reverse("idp:oidc:token"), |
| 259 |
{ |
| 260 |
"device_code": data["device_code"], |
| 261 |
"grant_type": Client.GrantType.DEVICE_CODE, |
| 262 |
"client_id": device_client.id, |
| 263 |
}, |
| 264 |
) |
| 265 |
assert resp.status_code == HTTPStatus.BAD_REQUEST |
| 266 |
assert resp.json() == {"error": error} |
| 267 |
|
| 268 |
|
| 269 |
@pytest.mark.parametrize( |
| 270 |
"granted_resources,requested_resources,success", |
| 271 |
[ |
| 272 |
([], ["https://api.example.com/a"], True), |
| 273 |
(["https://api.example.com"], ["https://api.example.com/a"], True), |
| 274 |
(["https://api.example.org"], ["https://api.example.com/a"], False), |
| 275 |
( |
| 276 |
["https://api.example.org", "https://api.example.com"], |
| 277 |
["https://api.example.com/a"], |
| 278 |
True, |
| 279 |
), |
| 280 |
(["https://api.example.com/a"], ["https://api.example.com/b"], False), |
| 281 |
], |
| 282 |
) |
| 283 |
def test_device_flow_requested_vs_granted_resources( |
| 284 |
client, |
| 285 |
auth_client, |
| 286 |
device_client, |
| 287 |
enable_cache, |
| 288 |
user, |
| 289 |
poll_sleep, |
| 290 |
granted_resources, |
| 291 |
requested_resources, |
| 292 |
success, |
| 293 |
): |
| 294 |
device_client.set_default_scopes(["profile"]) |
| 295 |
device_client.save() |
| 296 |
payload = { |
| 297 |
"client_id": device_client.id, |
| 298 |
"scope": "openid email", |
| 299 |
} |
| 300 |
if granted_resources: |
| 301 |
payload["resource"] = granted_resources |
| 302 |
resp = client.post( |
| 303 |
reverse("idp:oidc:device_code"), |
| 304 |
data=urlencode(payload, doseq=True), |
| 305 |
content_type="application/x-www-form-urlencoded", |
| 306 |
) |
| 307 |
assert resp.status_code == HTTPStatus.OK |
| 308 |
data = resp.json() |
| 309 |
|
| 310 |
resp = auth_client.get(data["verification_uri"]) |
| 311 |
assert resp.status_code == HTTPStatus.OK |
| 312 |
assertTemplateUsed(resp, "idp/oidc/device_authorization_code_form.html") |
| 313 |
assert resp.context["form"].errors == {} |
| 314 |
|
| 315 |
resp = auth_client.get(data["verification_uri_complete"]) |
| 316 |
assert resp.status_code == HTTPStatus.OK |
| 317 |
assertTemplateUsed(resp, "idp/oidc/device_authorization_confirm_form.html") |
| 318 |
|
| 319 |
resp = auth_client.post(data["verification_uri_complete"], {"action": "confirm"}) |
| 320 |
assert resp.status_code == HTTPStatus.OK |
| 321 |
poll_sleep(data["device_code"], 10) |
| 322 |
data = { |
| 323 |
"device_code": data["device_code"], |
| 324 |
"grant_type": Client.GrantType.DEVICE_CODE, |
| 325 |
"client_id": device_client.id, |
| 326 |
} |
| 327 |
if requested_resources: |
| 328 |
data["resource"] = requested_resources |
| 329 |
resp = client.post( |
| 330 |
reverse("idp:oidc:token"), |
| 331 |
data, |
| 332 |
) |
| 333 |
if success: |
| 334 |
assert resp.status_code == HTTPStatus.OK |
| 335 |
assert resp.json() == { |
| 336 |
"access_token": ANY, |
| 337 |
"expires_in": 3600, |
| 338 |
"refresh_token": ANY, |
| 339 |
"scope": "openid email", |
| 340 |
"token_type": "Bearer", |
| 341 |
} |
| 342 |
token = Token.objects.lookup( |
| 343 |
Token.Type.ACCESS_TOKEN, resp.json()["access_token"] |
| 344 |
) |
| 345 |
assert set(token.get_resources()) == set(requested_resources) |
| 346 |
else: |
| 347 |
assert resp.status_code == HTTPStatus.BAD_REQUEST |
| 348 |
data = resp.json() |
| 349 |
assert data == { |
| 350 |
"error": "invalid_target", |
| 351 |
"error_description": "The requested resource is invalid, missing, unknown, or malformed.", |
| 352 |
} |