auth

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AudienceAuth        = "auth"
	AudienceBilling     = "billing"
	AudienceSnapDB      = "snapdb-api"
	AudienceLangsync    = "langsync-api"
	AudienceDomainradar = "domainradar"
	AudiencePrompthub   = "prompthub"
)

Audience names recognized by the auth service. These match the constants in apps/auth/internal/handler/user/user.go.

Variables

View Source
var ErrLoginRequired = errors.New("not signed in or session expired")

ErrLoginRequired marks auth failures that a fresh `norcube login` fixes: no refresh token in the keyring, or the server rejecting the one we have (revoked / fully expired). main() detects it with errors.Is and prints the re-login hint once, instead of every command growing its own.

View Source
var ErrNotLoggedIn = errors.New("not logged in — run `norcube login`")

Functions

func DeleteAllTokens

func DeleteAllTokens(api string) error

DeleteAllTokens nukes refresh + every cached access token for an API. Used by `norcube logout`.

func DeleteRefreshToken

func DeleteRefreshToken(api string) error

func LoadAccessToken

func LoadAccessToken(api, audience, orgID string) (string, error)

func LoadRefreshToken

func LoadRefreshToken(api string) (string, error)

func SaveAccessToken

func SaveAccessToken(api, audience, orgID, token string) error

SaveAccessToken / LoadAccessToken cache short-lived access tokens per (api, audience, organization) so we don't hit /oauth/token on every command invocation. Callers must check expiry before using.

func SaveRefreshToken

func SaveRefreshToken(api, token string) error

Types

type BrowserLoginOptions

type BrowserLoginOptions struct {
	WebApp         string        // base URL of the web app, e.g. https://app.norcube.com
	Timeout        time.Duration // how long to wait for the user to complete the flow
	BrowserOpenURL func(string) error
	OnURLReady     func(string) // called with the URL just before opening the browser, for printing
}

BrowserLoginOptions configures BrowserLogin.

type CallbackPayload

type CallbackPayload struct {
	State            string       `json:"state"`
	AccessToken      string       `json:"accessToken"`
	RefreshToken     string       `json:"refreshToken"`
	AccessExpiresIn  int          `json:"accessExpiresIn"`
	RefreshExpiresIn int          `json:"refreshExpiresIn"`
	User             User         `json:"user"`
	DefaultOrg       Organization `json:"defaultOrganization"`
}

CallbackPayload is what the /cli-login frontend page POSTs to our loopback server. Field names mirror the JSON shape returned by /auth/cli/exchange, passed through unchanged by the page.

func BrowserLogin

func BrowserLogin(ctx context.Context, opts BrowserLoginOptions) (*CallbackPayload, error)

BrowserLogin runs the full browser-based login handshake:

  1. Bind a loopback HTTP server on a random localhost port.
  2. Open the user's browser to <WebApp>/cli-login?port=<P>&state=<S>.
  3. Wait for the page to POST {state, tokens...} to http://127.0.0.1:P/callback.
  4. Validate state, return the payload.

The caller is responsible for persisting the returned tokens.

type Organization

type Organization struct {
	ID   string `json:"id"`
	Name string `json:"name"`
	Slug string `json:"slug"`
}

type TokenSource

type TokenSource struct {
	APIBase  string
	Audience string
	OrgID    string
	HTTP     *http.Client
}

TokenSource returns a valid Bearer access token for a (audience, organization) pair, refreshing it via /oauth/token when necessary. It is the single place the rest of the CLI should ask for "give me a token I can use right now."

func NewTokenSource

func NewTokenSource(apiBase, audience, orgID string) *TokenSource

func (*TokenSource) BearerInjector

func (t *TokenSource) BearerInjector() func(ctx context.Context, req *http.Request) error

BearerInjector returns a request editor that injects a fresh access token into each outgoing request. Every generated API client shares this, so token acquisition lives in exactly one place.

func (*TokenSource) Token

func (t *TokenSource) Token(ctx context.Context) (string, error)

Token returns a valid access token, minting one via /oauth/token if no cached token exists or the cached token is close to expiring.

Side effect: when /oauth/token returns a rotated refresh token, the keyring-stored refresh token is updated in place. This keeps the CLI logged in indefinitely as long as the user runs `nrc` at least once per refresh-token TTL — the rotation pushes the expiry forward on each exchange. The user only sees `nrc login --force` again if they stay completely inactive for the full TTL window.

type User

type User struct {
	ID        string `json:"id"`
	Name      string `json:"name"`
	Email     string `json:"email"`
	AvatarURL string `json:"avatarUrl"`
}

Jump to

Keyboard shortcuts

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