"""{{ display_name }} MCP server — generated by mcpify. Do not hand-edit."""

from __future__ import annotations

from starlette.datastructures import Headers

from ..auth.request_credentials import RequestCredentials


def extract_authorization_header(headers: Headers) -> str | None:
    return headers.get("authorization")


class ExtractError(Exception):
    """The header this auth method needs wasn't present (or was empty),
    or the auth method's declared location can't be relayed over HTTP
    transport at all — only `"header"` locations can (query/cookie
    schemes, and OAuth1's `"none"`, are rejected here).
    """


def extract_request_credentials(
    headers: Headers, header_location: str, header_name: str
) -> RequestCredentials:
    """Extracts the credential value for the active auth method's declared
    location (`auth.auth_manager.header_location_for`) off an incoming
    HTTP request. The extracted value is relayed verbatim to the facaded
    API — whatever the caller sent, prefixed or not.
    """
    if header_location != "header":
        raise ExtractError(f"unsupported credential location '{header_location}'")

    value = headers.get(header_name)
    if not value:
        raise ExtractError(f"missing or empty '{header_name}' header")

    return RequestCredentials(header_name=header_name, value=value)
