auth

package
v1.0.21 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 23 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package auth manages credential storage for Aviary.

Index

Constants

View Source
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).

View Source
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.

View Source
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.

View Source
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).

View Source
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.

View Source
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

func CopilotTokenExchange(ctx context.Context, ghToken string) (string, time.Time, error)

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 IsNotFound

func IsNotFound(err error) bool

IsNotFound reports whether err is an ErrNotFound.

func OpenBrowser

func OpenBrowser(rawURL string) error

OpenBrowser opens rawURL in the system default browser.

func ParseRef

func ParseRef(ref string) (provider, name string, ok bool)

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

func Resolve(store Store, ref string) (string, error)

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.

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.

func NewFileStore

func NewFileStore(path string) (*FileStore, error)

NewFileStore creates a FileStore backed by path. The file is created if it does not exist.

func (*FileStore) Delete

func (s *FileStore) Delete(key string) error

Delete removes a credential.

func (*FileStore) Get

func (s *FileStore) Get(key string) (string, error)

Get retrieves a credential.

func (*FileStore) List

func (s *FileStore) List() ([]string, error)

List returns all stored keys sorted.

func (*FileStore) Set

func (s *FileStore) Set(key, value string) error

Set stores a credential.

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

type PKCEParams struct {
	Verifier  string
	Challenge string
}

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL