Documentation
¶
Overview ¶
Package auth implements login flows (loopback + PKCE, device code), multi-account token management and transparent refresh for Re:Earth's Auth0 tenant.
Index ¶
- Constants
- Variables
- func EnvNames(cfg *config.Config) []string
- func FetchUser(ctx context.Context, client *http.Client, env *Env, tok *oauth2.Token) (*config.User, error)
- func Login(ctx context.Context, o LoginOptions) (*oauth2.Token, error)
- func Revoke(ctx context.Context, client *http.Client, env *Env, refreshToken string) error
- func ValidateAccountName(name string) error
- type Env
- type Flow
- type FlowEnv
- type LoginOptions
- type Manager
- func (m *Manager) Resolve(flagAccount string, project *config.Project) (*Resolved, error)
- func (m *Manager) SaveLogin(name string, acc *config.Account, env *Env, tok *oauth2.Token) error
- func (m *Manager) SaveToken(name string, acc *config.Account, token string) error
- func (m *Manager) Store(acc *config.Account) credstore.Store
- func (m *Manager) TokenSource(r *Resolved) (*Source, error)
- type Resolved
- type Source
Constants ¶
const ( EnvProd = "prod" EnvStaging = "staging" )
const ( SourceFlag = "flag" SourceEnv = "env" SourceToken = "REEARTH_TOKEN" SourceProject = "project" SourceConfig = "config" )
Where the resolved account came from.
Variables ¶
var DefaultLoopbackPorts = []int{8085, 8086, 8087}
DefaultLoopbackPorts are tried in order for the OAuth redirect when no port is configured. Auth0 matches the port of a callback URL exactly, even for loopback addresses, so the redirect cannot use a random port: each of these must be registered as http://127.0.0.1:<port>/callback in the application.
var Scopes = []string{"openid", "profile", "email", "offline_access"}
Scopes requested at login. offline_access yields a refresh token.
Functions ¶
func FetchUser ¶
func FetchUser(ctx context.Context, client *http.Client, env *Env, tok *oauth2.Token) (*config.User, error)
FetchUser calls the OIDC userinfo endpoint.
func ValidateAccountName ¶
ValidateAccountName checks that an account name is safe to use as a keyring key and file name.
Types ¶
type Env ¶
type Env struct {
Name string
Domain string // e.g. https://auth.reearth.io
ClientID string
Audience string
// ProductURLs overrides product base URLs (from user-defined envs).
ProductURLs map[string]string
}
Env is a resolved authentication environment.
func ResolveEnv ¶
ResolveEnv returns the environment by name. User-defined envs in the config override built-ins. In development builds, REEARTH_AUTH_DOMAIN / REEARTH_AUTH_CLIENT_ID / REEARTH_AUTH_AUDIENCE override any env; release builds ignore them.
type Flow ¶
type Flow int
func DetectFlow ¶
DetectFlow picks the login flow. The loopback flow needs a browser on the same machine that can reach 127.0.0.1; otherwise the device flow is used.
type FlowEnv ¶
FlowEnv abstracts the platform for DetectFlow (for tests). Environment variables are read through envvar.
type LoginOptions ¶
type LoginOptions struct {
Env *Env
Flow Flow
IO *iostreams.IOStreams
OpenBrowser func(url string) error
// HTTPClient is used for token requests (optional).
HTTPClient *http.Client
// Timeout bounds the whole interactive login (default 10 minutes).
Timeout time.Duration
// Hostname is the hostname for the OAuth redirect listener (default: "127.0.0.1").
Hostname string
// Port fixes the loopback port for the OAuth redirect. 0 tries
// DefaultLoopbackPorts in order.
Port int
}
type Manager ¶
type Manager struct {
Config *config.Config
// AppName is used in hints ("run: <AppName> login").
AppName string
// Keyring is the secure store (default: OS keyring).
Keyring credstore.Store
// FileStore is the opt-in insecure store.
FileStore credstore.Store
// LockDir holds refresh locks.
LockDir string
// HTTPClient is used for token endpoint calls.
HTTPClient *http.Client
// contains filtered or unexported fields
}
Manager owns accounts: resolution, credential storage and token sources.
func (*Manager) Resolve ¶
Resolve picks the account: --account > REEARTH_TOKEN > REEARTH_ACCOUNT > project file > active.
type Resolved ¶
type Resolved struct {
Name string
Account *config.Account
Source string
// Token is set for anonymous token accounts from REEARTH_TOKEN.
Token string
}
Resolved is the account selected for the current command.
func (*Resolved) DisplayName ¶
DisplayName is the account name, or a description for anonymous accounts.
type Source ¶
type Source struct {
// contains filtered or unexported fields
}
Source is an oauth2.TokenSource that refreshes and persists tokens.
func (*Source) Invalidate ¶
Invalidate forces the next Token call to refresh (used after a 401, and to verify a session with the server). It reports false for static tokens, which cannot be refreshed.