|
| 1 | +from dataclasses import dataclass |
| 2 | +from typing import Any |
| 3 | +from urllib.parse import urlencode |
| 4 | + |
| 5 | +from starlette.requests import Request |
| 6 | +from starlette.responses import HTMLResponse, RedirectResponse, Response |
| 7 | + |
| 8 | +from mcp.server.auth.handlers.authorize import AuthorizationHandler |
| 9 | +from mcp.server.auth.provider import OAuthAuthorizationServerProvider |
| 10 | + |
| 11 | + |
| 12 | +@dataclass |
| 13 | +class ConsentHandler: |
| 14 | + provider: OAuthAuthorizationServerProvider[Any, Any, Any] |
| 15 | + |
| 16 | + async def handle(self, request: Request) -> Response: |
| 17 | + # This handles both showing the consent form (GET) and processing consent (POST) |
| 18 | + |
| 19 | + if request.method == "GET": |
| 20 | + # Show consent form |
| 21 | + return await self._show_consent_form(request) |
| 22 | + elif request.method == "POST": |
| 23 | + # Process consent |
| 24 | + return await self._process_consent(request) |
| 25 | + else: |
| 26 | + return HTMLResponse(status_code=405, content="Method not allowed") |
| 27 | + |
| 28 | + async def _show_consent_form(self, request: Request) -> HTMLResponse: |
| 29 | + client_id = request.query_params.get("client_id", "") |
| 30 | + redirect_uri = request.query_params.get("redirect_uri", "") |
| 31 | + state = request.query_params.get("state", "") |
| 32 | + scopes = request.query_params.get("scopes", "") |
| 33 | + code_challenge = request.query_params.get("code_challenge", "") |
| 34 | + response_type = request.query_params.get("response_type", "") |
| 35 | + |
| 36 | + # TODO: get this passed in |
| 37 | + target_url = "/consent" |
| 38 | + |
| 39 | + # Create a simple consent form |
| 40 | + |
| 41 | + html_content = f""" |
| 42 | +<!DOCTYPE html> |
| 43 | +<html> |
| 44 | +<head> |
| 45 | + <title>Authorization Required</title> |
| 46 | + <meta charset="utf-8"> |
| 47 | + <meta name="viewport" content="width=device-width, initial-scale=1"> |
| 48 | + <style> |
| 49 | + body {{ |
| 50 | + font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, sans-serif; |
| 51 | + display: flex; |
| 52 | + justify-content: center; |
| 53 | + align-items: center; |
| 54 | + min-height: 100vh; |
| 55 | + margin: 0; |
| 56 | + padding: 20px; |
| 57 | + background-color: #f5f5f5; |
| 58 | + }} |
| 59 | + .consent-form {{ |
| 60 | + background: white; |
| 61 | + padding: 40px; |
| 62 | + border-radius: 8px; |
| 63 | + box-shadow: 0 2px 4px rgba(0,0,0,0.1); |
| 64 | + width: 100%; |
| 65 | + max-width: 400px; |
| 66 | + }} |
| 67 | + h1 {{ |
| 68 | + margin: 0 0 20px 0; |
| 69 | + font-size: 24px; |
| 70 | + font-weight: 600; |
| 71 | + }} |
| 72 | + p {{ |
| 73 | + margin-bottom: 20px; |
| 74 | + color: #666; |
| 75 | + }} |
| 76 | + .client-info {{ |
| 77 | + background: #f8f8f8; |
| 78 | + padding: 15px; |
| 79 | + border-radius: 4px; |
| 80 | + margin-bottom: 20px; |
| 81 | + }} |
| 82 | + .scopes {{ |
| 83 | + margin-bottom: 20px; |
| 84 | + }} |
| 85 | + .scope-item {{ |
| 86 | + padding: 8px 0; |
| 87 | + border-bottom: 1px solid #eee; |
| 88 | + }} |
| 89 | + .scope-item:last-child {{ |
| 90 | + border-bottom: none; |
| 91 | + }} |
| 92 | + .button-group {{ |
| 93 | + display: flex; |
| 94 | + gap: 10px; |
| 95 | + }} |
| 96 | + button {{ |
| 97 | + flex: 1; |
| 98 | + padding: 10px; |
| 99 | + border: none; |
| 100 | + border-radius: 4px; |
| 101 | + cursor: pointer; |
| 102 | + font-size: 16px; |
| 103 | + }} |
| 104 | + .approve {{ |
| 105 | + background: #0366d6; |
| 106 | + color: white; |
| 107 | + }} |
| 108 | + .deny {{ |
| 109 | + background: #f6f8fa; |
| 110 | + color: #24292e; |
| 111 | + border: 1px solid #d1d5da; |
| 112 | + }} |
| 113 | + button:hover {{ |
| 114 | + opacity: 0.9; |
| 115 | + }} |
| 116 | + </style> |
| 117 | +</head> |
| 118 | +<body> |
| 119 | + <div class="consent-form"> |
| 120 | + <h1>Authorization Request</h1> |
| 121 | + <p>The application <strong>{client_id}</strong> is requesting access to your resources.</p> |
| 122 | +
|
| 123 | + <div class="client-info"> |
| 124 | + <strong>Client ID:</strong> {client_id}<br> |
| 125 | + <strong>Redirect URI:</strong> {redirect_uri} |
| 126 | + </div> |
| 127 | +
|
| 128 | + <div class="scopes"> |
| 129 | + <strong>Requested Permissions:</strong> |
| 130 | + {self._format_scopes(scopes)} |
| 131 | + </div> |
| 132 | +
|
| 133 | + <form method="POST" action="{target_url}"> |
| 134 | + <input type="hidden" name="client_id" value="{client_id}"> |
| 135 | + <input type="hidden" name="redirect_uri" value="{redirect_uri}"> |
| 136 | + <input type="hidden" name="state" value="{state}"> |
| 137 | + <input type="hidden" name="scopes" value="{scopes}"> |
| 138 | + <input type="hidden" name="code_challenge" value="{code_challenge}"> |
| 139 | + <input type="hidden" name="response_type" value="{response_type}"> |
| 140 | +
|
| 141 | + <div class="button-group"> |
| 142 | + <button type="submit" name="action" value="approve" class="approve">Approve</button> |
| 143 | + <button type="submit" name="action" value="deny" class="deny">Deny</button> |
| 144 | + </div> |
| 145 | + </form> |
| 146 | + </div> |
| 147 | +</body> |
| 148 | +</html> |
| 149 | +""" |
| 150 | + return HTMLResponse(content=html_content) |
| 151 | + |
| 152 | + async def _process_consent(self, request: Request) -> RedirectResponse: |
| 153 | + form_data = await request.form() |
| 154 | + action = form_data.get("action") |
| 155 | + |
| 156 | + if action == "approve": |
| 157 | + # Grant consent and continue with authorization |
| 158 | + client_id = form_data.get("client_id") |
| 159 | + if client_id: |
| 160 | + client = await self.provider.get_client(client_id) |
| 161 | + if client: |
| 162 | + await self.provider.grant_client_consent(client) |
| 163 | + |
| 164 | + # Redirect back to authorization endpoint with original parameters |
| 165 | + auth_params = urlencode({ |
| 166 | + k: v for k, v in form_data.items() |
| 167 | + if k in ["client_id", "redirect_uri", "state", "scopes", "code_challenge", "response_type"] |
| 168 | + }) |
| 169 | + |
| 170 | + return RedirectResponse( |
| 171 | + # TODO: get this passed in |
| 172 | + url=f"/authorize?{auth_params}", |
| 173 | + status_code=302, |
| 174 | + headers={"Cache-Control": "no-store"}, |
| 175 | + ) |
| 176 | + else: |
| 177 | + # User denied consent |
| 178 | + redirect_uri = form_data.get("redirect_uri") |
| 179 | + state = form_data.get("state") |
| 180 | + |
| 181 | + error_params = { |
| 182 | + "error": "access_denied", |
| 183 | + "error_description": "User denied the authorization request" |
| 184 | + } |
| 185 | + if state: |
| 186 | + error_params["state"] = state |
| 187 | + |
| 188 | + if redirect_uri: |
| 189 | + return RedirectResponse( |
| 190 | + url=f"{redirect_uri}?{urlencode(error_params)}", |
| 191 | + status_code=302, |
| 192 | + headers={"Cache-Control": "no-store"}, |
| 193 | + ) |
| 194 | + else: |
| 195 | + return HTMLResponse( |
| 196 | + status_code=400, |
| 197 | + content=f"Access denied: {error_params['error_description']}" |
| 198 | + ) |
| 199 | + |
| 200 | + def _format_scopes(self, scopes: str) -> str: |
| 201 | + if not scopes: |
| 202 | + return "<p>No specific permissions requested</p>" |
| 203 | + |
| 204 | + scope_list = scopes.split() |
| 205 | + if not scope_list: |
| 206 | + return "<p>No specific permissions requested</p>" |
| 207 | + |
| 208 | + scope_html = "" |
| 209 | + for scope in scope_list: |
| 210 | + scope_html += f'<div class="scope-item">{scope}</div>' |
| 211 | + |
| 212 | + return scope_html |
0 commit comments