auth

package
v0.0.1-testing Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: MIT Imports: 28 Imported by: 1 module, 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 implements login flows (loopback + PKCE, device code), multi-account token management and transparent refresh for Re:Earth's Auth0 tenant.

Index

Constants

View Source
const (
	EnvProd    = "prod"
	EnvStaging = "staging"
)
View Source
const (
	SourceFlag    = "flag"
	SourceEnv     = "env"
	SourceToken   = "REEARTH_TOKEN"
	SourceProject = "project"
	SourceConfig  = "config"
)

Where the resolved account came from.

Variables

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

View Source
var Scopes = []string{"openid", "profile", "email", "offline_access"}

Scopes requested at login. offline_access yields a refresh token.

Functions

func EnvNames

func EnvNames(cfg *config.Config) []string

EnvNames lists the built-in and user-defined environment names.

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 Login

func Login(ctx context.Context, o LoginOptions) (*oauth2.Token, error)

Login runs the selected flow and returns a token that includes a refresh token.

func Revoke

func Revoke(ctx context.Context, client *http.Client, env *Env, refreshToken string) error

Revoke invalidates a refresh token (best effort; used by logout).

func ValidateAccountName

func ValidateAccountName(name string) error

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

func ResolveEnv(cfg *config.Config, name string) (*Env, error)

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.

func (*Env) OAuth2Config

func (e *Env) OAuth2Config(redirectURL string) *oauth2.Config

OAuth2Config returns the oauth2 configuration for this environment.

type Flow

type Flow int
const (
	// FlowLoopback opens a browser and receives the code on 127.0.0.1 (authorization code + PKCE).
	FlowLoopback Flow = iota
	// FlowDevice shows a code to enter on another device (RFC 8628).
	FlowDevice
)

func DetectFlow

func DetectFlow(e FlowEnv, forceWeb, forceDevice bool) Flow

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.

func (Flow) String

func (f Flow) String() string

type FlowEnv

type FlowEnv struct {
	GOOS   string
	Exists func(path string) bool
}

FlowEnv abstracts the platform for DetectFlow (for tests). Environment variables are read through envvar.

func SystemFlowEnv

func SystemFlowEnv() FlowEnv

SystemFlowEnv returns the real platform.

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 NewManager

func NewManager(cfg *config.Config, appName string) *Manager

func (*Manager) Resolve

func (m *Manager) Resolve(flagAccount string, project *config.Project) (*Resolved, error)

Resolve picks the account: --account > REEARTH_TOKEN > REEARTH_ACCOUNT > project file > active.

func (*Manager) SaveLogin

func (m *Manager) SaveLogin(name string, acc *config.Account, env *Env, tok *oauth2.Token) error

SaveLogin stores the credentials of a freshly logged-in oauth account.

func (*Manager) SaveToken

func (m *Manager) SaveToken(name string, acc *config.Account, token string) error

SaveToken stores a static token account.

func (*Manager) Store

func (m *Manager) Store(acc *config.Account) credstore.Store

Store returns the credential store for an account.

func (*Manager) TokenSource

func (m *Manager) TokenSource(r *Resolved) (*Source, error)

TokenSource returns a token source for the resolved account. Access tokens are refreshed transparently and rotated refresh tokens are persisted.

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

func (r *Resolved) DisplayName() string

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

func (s *Source) Invalidate() bool

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.

func (*Source) Static

func (s *Source) Static() bool

Static reports whether the source holds a non-refreshable token.

func (*Source) Token

func (s *Source) Token() (*oauth2.Token, error)

Jump to

Keyboard shortcuts

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