-
Notifications
You must be signed in to change notification settings - Fork 7.9k
feat: Config-driven opt-in authentication registry with multi-platform support #2393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Copilot
wants to merge
9
commits into
main
Choose a base branch
from
copilot/add-authentication-provider-registry
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fa7479a
Initial plan
Copilot fbf19dd
feat: add authentication provider registry (GitHub + Azure DevOps)
Copilot 9c292d9
feat: add try-each-provider HTTP helper and wire all catalog fetches …
mnriem eee6119
fix: address review — fix stale docstrings, restore Accept header, ad…
mnriem 44f3ec4
feat: config-driven opt-in auth via ~/.specify/auth.json
mnriem 543ed48
fix: address review — unused imports, host normalization, provider+sc…
mnriem 36919ec
fix: reject unknown providers, validate azure-ad fields, strip Author…
mnriem 3cd5541
fix: extract shared auth test helpers, global config isolation, align…
mnriem 105f03b
fix: preserve auth header across multi-hop redirect chains
mnriem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| # Authentication | ||
|
|
||
| Specify CLI uses **opt-in authentication** for HTTP requests to catalog | ||
| sources, extension downloads, and release checks. No credentials are | ||
| sent unless you explicitly configure them. | ||
|
|
||
| ## Configuration | ||
|
|
||
| Create `~/.specify/auth.json` to enable authentication: | ||
|
|
||
| ```json | ||
| { | ||
| "providers": [ | ||
| { | ||
| "hosts": ["github.com", "api.github.com", "raw.githubusercontent.com", "codeload.github.com"], | ||
| "provider": "github", | ||
| "auth": "bearer", | ||
| "token_env": "GH_TOKEN" | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| > **Security:** Restrict the file to owner-only access: | ||
| > ``` | ||
| > chmod 600 ~/.specify/auth.json | ||
| > ``` | ||
|
|
||
| Without this file, all HTTP requests are unauthenticated. | ||
|
|
||
| ## Fields | ||
|
|
||
| Each entry in the `providers` array has the following fields: | ||
|
|
||
| | Field | Required | Description | | ||
| |---|---|---| | ||
| | `hosts` | Yes | Array of hostnames this entry applies to. Supports wildcards (e.g. `*.visualstudio.com`). | | ||
| | `provider` | Yes | Built-in provider key: `github` or `azure-devops`. | | ||
| | `auth` | Yes | Auth scheme (see below). | | ||
| | `token` | No | Token value (inline). Use `token_env` instead when possible. | | ||
| | `token_env` | No | Environment variable name to read the token from. | | ||
|
|
||
| For `azure-ad` auth, additional fields are required: | ||
|
|
||
| | Field | Required | Description | | ||
| |---|---|---| | ||
| | `tenant_id` | Yes | Azure AD tenant ID. | | ||
| | `client_id` | Yes | Service principal client ID. | | ||
| | `client_secret_env` | Yes | Environment variable containing the client secret. | | ||
|
|
||
| Either `token` or `token_env` must be set for `bearer` and `basic-pat` schemes. | ||
|
|
||
| ## Providers and auth schemes | ||
|
|
||
| ### GitHub (`github`) | ||
|
|
||
| | Scheme | Header | Use for | | ||
| |---|---|---| | ||
| | `bearer` | `Authorization: Bearer <token>` | PATs, fine-grained PATs, OAuth tokens, GitHub App tokens | | ||
|
|
||
| **Example — PAT via environment variable:** | ||
|
|
||
| ```json | ||
| { | ||
| "hosts": ["github.com", "api.github.com", "raw.githubusercontent.com", "codeload.github.com"], | ||
| "provider": "github", | ||
| "auth": "bearer", | ||
| "token_env": "GH_TOKEN" | ||
| } | ||
| ``` | ||
|
|
||
| ### Azure DevOps (`azure-devops`) | ||
|
|
||
| | Scheme | Header | Use for | | ||
| |---|---|---| | ||
| | `basic-pat` | `Authorization: Basic base64(:<PAT>)` | Personal Access Tokens | | ||
| | `bearer` | `Authorization: Bearer <token>` | Pre-acquired OAuth / Azure AD tokens | | ||
| | `azure-cli` | `Authorization: Bearer <token>` | Token acquired via `az account get-access-token` | | ||
| | `azure-ad` | `Authorization: Bearer <token>` | Token acquired via OAuth2 client credentials flow | | ||
|
|
||
| **Example — PAT via environment variable:** | ||
|
|
||
| ```json | ||
| { | ||
| "hosts": ["dev.azure.com"], | ||
| "provider": "azure-devops", | ||
| "auth": "basic-pat", | ||
| "token_env": "AZURE_DEVOPS_PAT" | ||
| } | ||
| ``` | ||
|
|
||
| **Example — Azure CLI (interactive login):** | ||
|
|
||
| ```json | ||
| { | ||
| "hosts": ["dev.azure.com"], | ||
| "provider": "azure-devops", | ||
| "auth": "azure-cli" | ||
| } | ||
| ``` | ||
|
|
||
| Requires `az login` to have been run beforehand. | ||
|
|
||
| **Example — Azure AD service principal (CI/automation):** | ||
|
|
||
| ```json | ||
| { | ||
| "hosts": ["dev.azure.com"], | ||
| "provider": "azure-devops", | ||
| "auth": "azure-ad", | ||
| "tenant_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", | ||
| "client_id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", | ||
| "client_secret_env": "AZURE_CLIENT_SECRET" | ||
| } | ||
| ``` | ||
|
|
||
| ## Multiple entries | ||
|
|
||
| You can configure multiple entries for different hosts or organizations: | ||
|
|
||
| ```json | ||
| { | ||
| "providers": [ | ||
| { | ||
| "hosts": ["github.com", "api.github.com", "raw.githubusercontent.com", "codeload.github.com"], | ||
| "provider": "github", | ||
| "auth": "bearer", | ||
| "token_env": "GH_TOKEN" | ||
| }, | ||
| { | ||
| "hosts": ["dev.azure.com"], | ||
| "provider": "azure-devops", | ||
| "auth": "basic-pat", | ||
| "token_env": "AZURE_DEVOPS_PAT" | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| ## How it works | ||
|
|
||
| 1. For each outbound HTTP request, the URL hostname is matched against | ||
| the `hosts` patterns in `auth.json`. | ||
| 2. If a match is found, the corresponding provider resolves the token | ||
| and attaches the appropriate `Authorization` header. | ||
| 3. If the request receives a 401 or 403, the next matching entry is tried. | ||
| 4. After all matching entries are exhausted, an unauthenticated request | ||
| is attempted as a final fallback. | ||
| 5. On redirects, the `Authorization` header is stripped if the redirect | ||
| target leaves the entry's declared hosts — preventing credential | ||
| leakage to CDNs or third-party services. | ||
|
|
||
| ## Template | ||
|
|
||
| A reference `auth.json` with GitHub pre-configured: | ||
|
|
||
| ```json | ||
| { | ||
| "providers": [ | ||
| { | ||
| "hosts": [ | ||
| "github.com", | ||
| "api.github.com", | ||
| "raw.githubusercontent.com", | ||
| "codeload.github.com" | ||
| ], | ||
| "provider": "github", | ||
| "auth": "bearer", | ||
| "token_env": "GH_TOKEN" | ||
| } | ||
| ] | ||
| } | ||
| ``` | ||
|
|
||
| To use it: | ||
|
|
||
| ```bash | ||
| mkdir -p ~/.specify | ||
| # Copy the JSON above into ~/.specify/auth.json | ||
| chmod 600 ~/.specify/auth.json | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| """Authentication provider registry for multi-platform support. | ||
|
|
||
| Credentials are **opt-in only**. No authentication headers are sent unless | ||
| the user creates ``~/.specify/auth.json`` mapping hosts to providers. | ||
| Provider classes define *how* to authenticate (Bearer, Basic-PAT, etc.) | ||
| while the config file defines *where* and *with what credentials*. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING | ||
|
|
||
| if TYPE_CHECKING: | ||
| from .base import AuthProvider | ||
|
|
||
| # Maps provider key → AuthProvider class instance. | ||
| AUTH_REGISTRY: dict[str, AuthProvider] = {} | ||
|
|
||
|
|
||
| def _register(provider: AuthProvider) -> None: | ||
| """Register a provider instance in the global registry. | ||
|
|
||
| Raises ``ValueError`` for falsy keys and ``KeyError`` for duplicates. | ||
| """ | ||
| key = provider.key | ||
| if not key: | ||
| raise ValueError("Cannot register provider with an empty key.") | ||
| if key in AUTH_REGISTRY: | ||
| raise KeyError(f"Provider with key {key!r} is already registered.") | ||
| AUTH_REGISTRY[key] = provider | ||
|
|
||
|
|
||
| def get_provider(key: str) -> AuthProvider | None: | ||
| """Return the provider for *key*, or ``None`` if not registered.""" | ||
| return AUTH_REGISTRY.get(key) | ||
|
|
||
|
|
||
| # -- Register built-in providers ----------------------------------------- | ||
|
|
||
|
|
||
| def _register_builtins() -> None: | ||
| """Register all built-in authentication providers (alphabetical).""" | ||
| from .azure_devops import AzureDevOpsAuth | ||
| from .github import GitHubAuth | ||
|
|
||
| _register(AzureDevOpsAuth()) | ||
| _register(GitHubAuth()) | ||
|
|
||
|
|
||
| _register_builtins() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tables use leading double pipes (
||) which creates an empty first column and is likely unintended / may break markdown rendering and linting. Consider converting these to standard markdown table syntax with single leading/trailing|.