Documentation
¶
Overview ¶
Package auth manages credential storage for Aviary.
Index ¶
- Constants
- func AnthropicBuildAuthorizeURL(pkce PKCEParams, mode string) string
- func CopilotTokenExchange(ctx context.Context, ghToken string) (string, time.Time, error)
- func IsNotFound(err error) bool
- func OpenBrowser(rawURL string) error
- func ParseRef(ref string) (provider, name string, ok bool)
- func Resolve(store Store, ref string) (string, error)
- func StoreCopilotDeviceState(s *CopilotDeviceState)
- func StorePendingPKCE(provider string, p PKCEParams)
- func ValidateCredential(name, value string) error
- type BrowserLoginStart
- type CopilotDeviceState
- type ErrNotFound
- type FileStore
- type KeychainStore
- type OAuthToken
- func AnthropicExchange(ctx context.Context, code, verifier string) (*OAuthToken, error)
- func AnthropicRefresh(ctx context.Context, refreshToken string) (*OAuthToken, error)
- func CompleteGeminiLogin(ctx context.Context) (*OAuthToken, error)
- func CompleteOpenAILogin(ctx context.Context) (*OAuthToken, error)
- func CopilotLogin(ctx context.Context) (*OAuthToken, error)
- func CopilotPollDevice(ctx context.Context, state *CopilotDeviceState) (*OAuthToken, error)
- func GeminiLogin(ctx context.Context) (*OAuthToken, error)
- func GeminiRefresh(ctx context.Context, refreshToken string) (*OAuthToken, error)
- func GitHubExchange(ctx context.Context, code, verifier, redirectURI, clientID string) (*OAuthToken, error)
- func GitHubLogin(ctx context.Context) (*OAuthToken, error)
- func OpenAILogin(ctx context.Context) (*OAuthToken, error)
- func OpenAIRefresh(ctx context.Context, refreshToken string) (*OAuthToken, error)
- type PKCEParams
- type Store
Constants ¶
const ( AnthropicClientID = "9d1c250a-e61b-44d9-88ed-5944d1962f5e" AnthropicAuthorizeURL = "https://claude.ai/oauth/authorize" AnthropicTokenURL = "https://platform.claude.com/v1/oauth/token" AnthropicRedirectURI = "https://platform.claude.com/oauth/code/callback" AnthropicScope = "user:profile user:inference" )
Anthropic OAuth constants for the claude.ai consumer OAuth flow (Pro/Max plans). Authorize goes to claude.ai; token exchange and redirect URI go to platform.claude.com. Client ID, URLs, and scope sourced from the official claude-code CLI (v2.1.63).
const ( OpenAIClientID = "app_EMoamEEZ73f0CkXaXp7hrann" OpenAIIssuer = "https://auth.openai.com" OpenAIAuthorizeURL = "https://auth.openai.com/oauth/authorize" OpenAITokenURL = "https://auth.openai.com/oauth/token" OpenAIScope = "openid profile email offline_access" OpenAICallbackPort = 1455 )
OpenAI / Codex OAuth constants. Sourced from the official openai/codex CLI (codex-rs/login/src/server.rs and codex-rs/core/src/auth.rs). CLIENT_ID and issuer are stable public values.
const ( GitHubAuthorizeURL = "https://github.com/login/oauth/authorize" GitHubTokenURL = "https://github.com/login/oauth/access_token" GitHubCallbackPort = 1466 )
GitHub OAuth endpoints used for PKCE login. The client ID must be supplied by setting the GITHUB_OAUTH_CLIENT_ID environment variable.
const ( GeminiClientID = "681255809395-oo8ft2oprdrnp9e3aqf6av3hmdib135j.apps.googleusercontent.com" GeminiClientSecret = "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" GeminiAuthorizeURL = "https://accounts.google.com/o/oauth2/v2/auth" GeminiTokenURL = "https://oauth2.googleapis.com/token" GeminiScope = "https://www.googleapis.com/auth/cloud-platform openid email profile" GeminiCallbackPort = 45289 )
Gemini OAuth constants for the gemini-cli OAuth flow. Client ID, secret, and redirect port sourced from the official google-gemini/gemini-cli (packages/core/src/code_assist/oauth2.ts).
const ( CopilotClientID = "Iv1.b507a08c87ecfe98" CopilotDeviceCodeURL = "https://github.com/login/device/code" CopilotDeviceTokenURL = "https://github.com/login/oauth/access_token" CopilotDeviceScope = "read:user" )
GitHub Copilot OAuth constants. The client ID is the public GitHub Copilot VS Code extension OAuth app, widely used by third-party Copilot integrations.
const BrowserLoginTimeout = 2 * time.Minute
BrowserLoginTimeout is how long browser-based OAuth callback listeners stay open before Aviary automatically tears them down.
Variables ¶
This section is empty.
Functions ¶
func AnthropicBuildAuthorizeURL ¶
func AnthropicBuildAuthorizeURL(pkce PKCEParams, mode string) string
AnthropicBuildAuthorizeURL builds the claude.ai OAuth authorization URL. The redirect_uri points to platform.claude.com/oauth/code/callback, which displays the authorization code on-screen so the user can copy-paste it back.
func CopilotTokenExchange ¶
CopilotTokenExchange exchanges a GitHub user token (PAT or OAuth) for a short-lived GitHub Copilot API token. The Copilot token is valid for ~30 min. Returns (copilotToken, expiresAt, error).
func OpenBrowser ¶
OpenBrowser opens rawURL in the system default browser.
func ParseRef ¶
ParseRef parses "auth:<key>" into provider and name when the key itself uses the conventional "<provider>:<name>" shape. Returns provider="", name="" if ref is not an auth reference.
func Resolve ¶
Resolve resolves an auth reference string of the form "auth:<key>" using the provided Store. If ref does not start with "auth:", it is returned as-is (treated as a literal value).
func StoreCopilotDeviceState ¶
func StoreCopilotDeviceState(s *CopilotDeviceState)
StoreCopilotDeviceState saves in-flight device state for a two-step flow.
func StorePendingPKCE ¶
func StorePendingPKCE(provider string, p PKCEParams)
StorePendingPKCE saves a PKCE state for a named login flow.
func ValidateCredential ¶ added in v1.0.36
ValidateCredential reports whether name and value can be stored as a credential. Both must contain non-whitespace characters; use Delete to clear a credential instead of storing an empty value.
Types ¶
type BrowserLoginStart ¶ added in v0.4.1
type BrowserLoginStart struct {
AuthorizeURL string
CallbackURL string
BrowserOpened bool
BrowserOpenErr string
ExpiresAt time.Time
}
BrowserLoginStart describes a browser-based OAuth flow that has been prepared and is waiting for the user to complete it in the browser.
func StartGeminiLogin ¶ added in v0.4.1
func StartGeminiLogin() (*BrowserLoginStart, error)
StartGeminiLogin starts a local HTTP callback server on port 45289, opens the browser to Google's OAuth consent screen, and returns the URLs needed to complete the flow. The token exchange is finished by CompleteGeminiLogin. Matches the google-gemini/gemini-cli authentication flow.
func StartOpenAILogin ¶ added in v0.4.1
func StartOpenAILogin() (*BrowserLoginStart, error)
StartOpenAILogin starts a local HTTP callback server on port 1455, opens the browser to the OpenAI/Codex OAuth consent screen, and returns the URLs needed to complete the flow. The token exchange is finished by CompleteOpenAILogin.
The flow matches the official openai/codex CLI (codex-rs/login/src/server.rs):
- redirect_uri uses "localhost" (not 127.0.0.1) and path "/auth/callback"
- authorize URL includes id_token_add_organizations, codex_cli_simplified_flow, originator
- state is a separate random value independent of the PKCE verifier
- after code exchange the id_token is swapped for a real OpenAI API key
type CopilotDeviceState ¶
type CopilotDeviceState struct {
DeviceCode string
UserCode string
VerificationURI string
Interval int
ExpiresIn int
}
CopilotDeviceState holds the in-flight state for a GitHub Copilot device flow.
func CopilotDeviceCode ¶
func CopilotDeviceCode(ctx context.Context) (*CopilotDeviceState, error)
CopilotDeviceCode requests a device code from GitHub and returns the state that should be displayed to the user (user_code + verification_uri).
func LoadCopilotDeviceState ¶
func LoadCopilotDeviceState() (*CopilotDeviceState, bool)
LoadCopilotDeviceState retrieves and clears the stored device state. Returns false if none is stored.
type ErrNotFound ¶
type ErrNotFound struct {
Key string
}
ErrNotFound is returned when a credential key is not found.
func (*ErrNotFound) Error ¶
func (e *ErrNotFound) Error() string
type FileStore ¶
type FileStore struct {
// contains filtered or unexported fields
}
FileStore is a JSON-file-backed auth store.
Several processes share one credentials file: a running server and the CLI (`aviary auth set`, `aviary auth login`, ...) each open their own FileStore on it. So the file, not the in-memory map, is the source of truth: every read re-reads it, and every write re-reads it, applies one change, and replaces it atomically while holding a lock file shared across processes. A write never drops keys another process added.
func NewFileStore ¶
NewFileStore creates a FileStore backed by path. The file is created on the first write if it does not exist.
type KeychainStore ¶
type KeychainStore struct {
// contains filtered or unexported fields
}
KeychainStore uses the system keychain (macOS/Windows/Linux) as a backend.
func NewKeychainStore ¶
func NewKeychainStore() *KeychainStore
NewKeychainStore returns a new KeychainStore.
func (*KeychainStore) Delete ¶
func (s *KeychainStore) Delete(key string) error
Delete removes a credential from the system keychain.
func (*KeychainStore) Get ¶
func (s *KeychainStore) Get(key string) (string, error)
Get retrieves a credential from the system keychain.
func (*KeychainStore) List ¶
func (s *KeychainStore) List() ([]string, error)
List returns all tracked credential keys.
func (*KeychainStore) Set ¶
func (s *KeychainStore) Set(key, value string) error
Set stores a credential in the system keychain.
type OAuthToken ¶
type OAuthToken struct {
AccessToken string `json:"access"`
RefreshToken string `json:"refresh"`
ExpiresAt int64 `json:"expires_at"` // Unix milliseconds
}
OAuthToken holds the result of a successful OAuth token exchange.
func AnthropicExchange ¶
func AnthropicExchange(ctx context.Context, code, verifier string) (*OAuthToken, error)
AnthropicExchange exchanges an authorization code for OAuth tokens. verifier must match the one used to build the authorize URL.
The code returned by Anthropic's callback page may be in "code#state" format; both parts are forwarded to the token endpoint as required.
func AnthropicRefresh ¶
func AnthropicRefresh(ctx context.Context, refreshToken string) (*OAuthToken, error)
AnthropicRefresh refreshes an expired Anthropic OAuth access token.
func CompleteGeminiLogin ¶ added in v0.4.1
func CompleteGeminiLogin(ctx context.Context) (*OAuthToken, error)
CompleteGeminiLogin waits for a previously started Gemini OAuth flow to finish and then exchanges the callback for tokens.
func CompleteOpenAILogin ¶ added in v0.4.1
func CompleteOpenAILogin(ctx context.Context) (*OAuthToken, error)
CompleteOpenAILogin waits for a previously started OpenAI OAuth flow to finish and then exchanges the callback for tokens.
func CopilotLogin ¶
func CopilotLogin(ctx context.Context) (*OAuthToken, error)
CopilotLogin is the CLI convenience wrapper: runs the full device flow in one call, printing the user code and verification URL to stdout.
func CopilotPollDevice ¶
func CopilotPollDevice(ctx context.Context, state *CopilotDeviceState) (*OAuthToken, error)
CopilotPollDevice polls GitHub until the user authorizes the device flow.
func GeminiLogin ¶
func GeminiLogin(ctx context.Context) (*OAuthToken, error)
GeminiLogin starts and completes the Gemini OAuth flow in one blocking call.
func GeminiRefresh ¶
func GeminiRefresh(ctx context.Context, refreshToken string) (*OAuthToken, error)
GeminiRefresh refreshes an expired Gemini OAuth access token.
func GitHubExchange ¶
func GitHubExchange(ctx context.Context, code, verifier, redirectURI, clientID string) (*OAuthToken, error)
GitHubExchange exchanges an authorization code for a GitHub OAuth token.
func GitHubLogin ¶
func GitHubLogin(ctx context.Context) (*OAuthToken, error)
GitHubLogin performs a GitHub OAuth PKCE login. Requires GITHUB_OAUTH_CLIENT_ID to be set; falls back to reading the gh CLI token from hosts.yml if not set.
func OpenAILogin ¶
func OpenAILogin(ctx context.Context) (*OAuthToken, error)
OpenAILogin starts and completes the OpenAI OAuth flow in one blocking call.
func OpenAIRefresh ¶
func OpenAIRefresh(ctx context.Context, refreshToken string) (*OAuthToken, error)
OpenAIRefresh refreshes an expired OpenAI/Codex OAuth access token.
func (*OAuthToken) IsExpired ¶
func (t *OAuthToken) IsExpired() bool
IsExpired reports whether the token has expired (within a 30-second buffer).
type PKCEParams ¶
PKCEParams holds the PKCE code verifier and its SHA-256 challenge.
func GeneratePKCE ¶
func GeneratePKCE() (PKCEParams, error)
GeneratePKCE creates new random PKCE parameters.
func LoadPendingPKCE ¶
func LoadPendingPKCE(provider string) (PKCEParams, bool)
LoadPendingPKCE retrieves and removes the stored PKCE state. Returns false if none is found.
type Store ¶
type Store interface {
// Set stores a credential under the given key.
Set(key, value string) error
// Get retrieves a credential by key.
Get(key string) (string, error)
// Delete removes a credential by key.
Delete(key string) error
// List returns all stored credential keys.
List() ([]string, error)
}
Store is the interface for credential storage backends.