Documentation
¶
Index ¶
- Constants
- Variables
- func DeleteAllTokens(api string) error
- func DeleteRefreshToken(api string) error
- func LoadAccessToken(api, audience, orgID string) (string, error)
- func LoadRefreshToken(api string) (string, error)
- func SaveAccessToken(api, audience, orgID, token string) error
- func SaveRefreshToken(api, token string) error
- type BrowserLoginOptions
- type CallbackPayload
- type Organization
- type TokenSource
- type User
Constants ¶
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 ¶
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.
var ErrNotLoggedIn = errors.New("not logged in — run `norcube login`")
Functions ¶
func DeleteAllTokens ¶
DeleteAllTokens nukes refresh + every cached access token for an API. Used by `norcube logout`.
func DeleteRefreshToken ¶
func LoadAccessToken ¶
func LoadRefreshToken ¶
func SaveAccessToken ¶
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 ¶
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:
- Bind a loopback HTTP server on a random localhost port.
- Open the user's browser to <WebApp>/cli-login?port=<P>&state=<S>.
- Wait for the page to POST {state, tokens...} to http://127.0.0.1:P/callback.
- Validate state, return the payload.
The caller is responsible for persisting the returned tokens.
type Organization ¶
type TokenSource ¶
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 ¶
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.