| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import time |
| 4 |
from typing import Any |
| 5 |
|
| 6 |
from django.contrib.auth import get_user_model |
| 7 |
from django.contrib.auth.models import AbstractBaseUser |
| 8 |
from django.core.cache import cache |
| 9 |
from django.http import HttpRequest |
| 10 |
|
| 11 |
from oauthlib.oauth2.rfc6749.errors import ( |
| 12 |
InvalidClientError, |
| 13 |
InvalidGrantError, |
| 14 |
InvalidRequestError, |
| 15 |
UnsupportedGrantTypeError, |
| 16 |
) |
| 17 |
from oauthlib.oauth2.rfc8628.errors import ( |
| 18 |
AccessDenied, |
| 19 |
AuthorizationPendingError, |
| 20 |
SlowDownError, |
| 21 |
) |
| 22 |
|
| 23 |
from allauth.account.adapter import get_adapter as get_account_adapter |
| 24 |
from allauth.account.internal.userkit import str_to_user_id, user_id_to_str |
| 25 |
from allauth.core.internal.cryptokit import compare_user_code |
| 26 |
from allauth.idp.oidc.internal.clientkit import lookup_public_client |
| 27 |
from allauth.idp.oidc.internal.oauthlib.utils import get_validator_context |
| 28 |
from allauth.idp.oidc.models import Client |
| 29 |
|
| 30 |
|
| 31 |
def cache_user_code_key(user_code: str) -> str: |
| 32 |
return f"allauth.idp.oidc.user_code[{user_code.lower()}]" |
| 33 |
|
| 34 |
|
| 35 |
def cache_device_code_key(device_code: str) -> str: |
| 36 |
return f"allauth.idp.oidc.device_code[{device_code}]" |
| 37 |
|
| 38 |
|
| 39 |
def create(client_id: str, scope: list[str] | None, data: dict[str, Any]) -> None: |
| 40 |
ctx = get_validator_context() |
| 41 |
cache.set( |
| 42 |
cache_user_code_key(data["user_code"]), |
| 43 |
data["device_code"], |
| 44 |
timeout=data["expires_in"], |
| 45 |
) |
| 46 |
cache.set( |
| 47 |
cache_device_code_key(data["device_code"]), |
| 48 |
{ |
| 49 |
"expires_at": time.time() + data["expires_in"], |
| 50 |
"granted": None, |
| 51 |
"last_poll_at": 0, |
| 52 |
"client_id": client_id, |
| 53 |
"scope": scope, |
| 54 |
"device": data, |
| 55 |
"resources": ctx.requested_resources, |
| 56 |
}, |
| 57 |
timeout=data["expires_in"], |
| 58 |
) |
| 59 |
|
| 60 |
|
| 61 |
def validate_user_code(code: str) -> tuple[str, Client]: |
| 62 |
data: dict[str, Any] | None = None |
| 63 |
device_code = cache.get(cache_user_code_key(code)) |
| 64 |
if device_code: |
| 65 |
data = cache.get(cache_device_code_key(device_code)) |
| 66 |
if ( |
| 67 |
not data |
| 68 |
or data["granted"] is not None |
| 69 |
or not compare_user_code(actual=code, expected=data["device"]["user_code"]) |
| 70 |
): |
| 71 |
raise get_account_adapter().validation_error("incorrect_code") |
| 72 |
client = lookup_public_client(data["client_id"]) |
| 73 |
if not client: |
| 74 |
raise get_account_adapter().validation_error("incorrect_code") |
| 75 |
return device_code, client |
| 76 |
|
| 77 |
|
| 78 |
def confirm_or_deny_device_code( |
| 79 |
user: AbstractBaseUser, device_code: str, confirm: bool |
| 80 |
) -> bool: |
| 81 |
data = cache.get(cache_device_code_key(device_code)) |
| 82 |
if data is None or data["granted"] is not None: |
| 83 |
return False |
| 84 |
data["granted"] = confirm |
| 85 |
data["user"] = user_id_to_str(user) |
| 86 |
return update_device_state(device_code, data) |
| 87 |
|
| 88 |
|
| 89 |
def update_device_state(device_code: str, data: dict[str, Any]) -> bool: |
| 90 |
timeout = int(data["expires_at"] - time.time()) |
| 91 |
if timeout < 0: |
| 92 |
return False |
| 93 |
cache.set(cache_device_code_key(device_code), data, timeout=timeout) |
| 94 |
return True |
| 95 |
|
| 96 |
|
| 97 |
def poll_device_code( |
| 98 |
request: HttpRequest, |
| 99 |
) -> tuple[AbstractBaseUser, dict[str, Any]]: |
| 100 |
client_id = request.POST.get("client_id") |
| 101 |
device_code = request.POST.get("device_code") |
| 102 |
if not client_id or not device_code: |
| 103 |
raise InvalidRequestError |
| 104 |
client = lookup_public_client(client_id) |
| 105 |
if not client: |
| 106 |
raise InvalidClientError |
| 107 |
if client.GrantType.DEVICE_CODE not in client.get_grant_types(): |
| 108 |
raise UnsupportedGrantTypeError |
| 109 |
cache_key = cache_device_code_key(device_code) |
| 110 |
data = cache.get(cache_key) |
| 111 |
if data is None or data["client_id"] != client_id: |
| 112 |
raise InvalidGrantError |
| 113 |
now = time.time() |
| 114 |
if data["last_poll_at"] + data["device"]["interval"] > now: |
| 115 |
raise SlowDownError |
| 116 |
data["last_poll_at"] = now |
| 117 |
update_device_state(device_code, data) |
| 118 |
granted = data.get("granted") |
| 119 |
if granted is None: |
| 120 |
raise AuthorizationPendingError |
| 121 |
elif granted is False: |
| 122 |
raise AccessDenied |
| 123 |
assert granted is True # nosec |
| 124 |
cache.delete(cache_key) |
| 125 |
|
| 126 |
user = get_user_model().objects.filter(pk=str_to_user_id(data["user"])).first() |
| 127 |
if user is None or not user.is_active: |
| 128 |
raise AccessDenied |
| 129 |
ctx = get_validator_context() |
| 130 |
ctx.granted_resources = data["resources"] |
| 131 |
return user, data |