File size: 5,506 Bytes
7475f0d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | """
Device Code Flow implementation for OAuth 2.0 device authorization grant (RFC 8628).
"""
import json
import time
from typing import Dict
from typing import Optional
import requests
from pydantic import BaseModel
from anaconda_auth.client import BaseClient
from anaconda_auth.config import AnacondaAuthSite
from anaconda_auth.exceptions import DeviceFlowDenied
from anaconda_auth.exceptions import DeviceFlowError
from anaconda_auth.exceptions import DeviceFlowTimeout
class DeviceAuthorizationResponse(BaseModel):
device_code: str
user_code: str
verification_uri: str
verification_uri_complete: str
expires_in: int = 60
interval: int = 5
class DeviceCodeFlow:
"""
OAuth 2.0 Device Code Flow implementation.
This implements RFC 8628 for devices that are either browserless
or have limited input capabilities.
"""
config: AnacondaAuthSite
authorize_response: Optional[DeviceAuthorizationResponse]
def __init__(self, config: AnacondaAuthSite):
"""
Initialize device code flow.
Args:
config: Configuration for the client
"""
self.config = config
self.client = BaseClient(site=config)
# Device authorization response data
self.authorize_response = None
def initiate_device_authorization(self) -> DeviceAuthorizationResponse:
"""
Initiate device authorization request.
Returns:
Tuple of (user_code, verification_uri) to display to user
"""
data = {"client_id": self.config.client_id, "scope": "openid"}
if self.config.oidc.device_authorization_endpoint is None:
raise DeviceFlowError("Server does not support device authorization")
try:
response = self.client.post(
self.config.oidc.device_authorization_endpoint,
data=data,
verify=self.config.ssl_verify,
headers={"Content-Type": "application/x-www-form-urlencoded"},
)
response.raise_for_status()
auth_response = response.json()
self.authorize_response = DeviceAuthorizationResponse(**auth_response)
return self.authorize_response
except requests.RequestException as e:
raise DeviceFlowError(f"Device authorization request failed: {e}")
except KeyError as e:
raise DeviceFlowError(f"Missing required field in response: {e}")
def poll_for_token(self) -> Dict[str, str]:
"""
Poll the token endpoint until authorization is complete.
Returns:
Token response containing access_token, etc.
"""
if not self.authorize_response:
raise DeviceFlowError("Must call initiate_device_authorization first")
start_time = time.time()
expires_in = self.authorize_response.expires_in
interval = self.authorize_response.interval
while time.time() - start_time < expires_in:
try:
token_response = self._request_token()
return token_response
except DeviceFlowTimeout:
raise
except DeviceFlowDenied:
raise
except DeviceFlowError as e:
# Check for authorization_pending
if "authorization_pending" in str(e).lower():
time.sleep(interval)
continue
elif "slow_down" in str(e).lower():
# Server asked us to slow down
interval = min(interval + 5, 30)
time.sleep(interval)
continue
else:
raise
raise DeviceFlowTimeout("Device authorization timed out")
def _request_token(self) -> Dict[str, str]:
"""Make a single token request."""
if not self.authorize_response:
raise DeviceFlowError("Must call initiate_device_authorization first")
data = {
"grant_type": "urn:ietf:params:oauth:grant-type:device_code",
"device_code": self.authorize_response.device_code,
"client_id": self.config.client_id,
}
response = self.client.post(
self.config.oidc.token_endpoint,
data=data,
verify=self.config.ssl_verify,
headers={"Content-Type": "application/x-www-form-urlencoded"},
)
if response.status_code == 200:
return response.json()
# Handle error responses
try:
error_data = response.json()
error_code = error_data.get("error", "unknown_error")
error_description = error_data.get("error_description", "")
if error_code == "authorization_pending":
raise DeviceFlowError("authorization_pending")
elif error_code == "slow_down":
raise DeviceFlowError("slow_down")
elif error_code == "expired_token":
raise DeviceFlowTimeout("Device code expired")
elif error_code == "access_denied":
raise DeviceFlowDenied("User denied authorization")
else:
raise DeviceFlowError(
f"Token request failed: {error_code} - {error_description}"
)
except json.JSONDecodeError:
response.raise_for_status()
raise DeviceFlowError("Token request failed")
|