auth

package
v1.0.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: MIT Imports: 16 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 handles credential storage, site login, and chat-JWT minting.

The stored credential is the value of the site's long-lived remember cookie — never the account password (ADR 0001, ADR 0003).

Index

Constants

View Source
const (

	// ChatService is the service name the chat JWT is minted for.
	ChatService = "chat"
)

Routes under the site's /api/v1/auth group (docs/site-login-endpoints.md).

View Source
const IdentityHelp = "Use your email address if your account is set to " +
	"log in with email only."

IdentityHelp is shown before the identity prompt. The site answers an email-only account's username with the same 422 as a wrong password, on purpose, so the CLI cannot detect that case and says so up front.

View Source
const PasswordHelp = "Press Enter with no password to change the username."

PasswordHelp is shown before the password prompt. An empty password is never sent: the site would only answer 422 and count it against the login limiter, so the entry doubles as the way back to the identity.

View Source
const RememberKey = "ng_remember"

RememberKey is the credential-store slot for the remember cookie value. It is the only key a current binary writes.

Variables

View Source
var ErrNoChallenge = errors.New("no two-factor challenge in progress")

ErrNoChallenge reports a 403 from two-factor: the login challenge in this session expired or was consumed, so login must start over.

View Source
var ErrNotFound = errors.New("credential not found")

ErrNotFound is returned by Load when no value is stored under a key.

View Source
var ErrSignedOut = errors.New("signed out")

ErrSignedOut reports a 401 from service-token: the remember cookie no longer works (password changed, or `ngchat logout` ran). Nothing the client can do fixes it, so callers stop instead of retrying.

Functions

This section is empty.

Types

type FailError

type FailError struct {
	Status int
	Fields map[string][]string
}

FailError is a JSend "fail" (or non-JSend error) response. Fields follows Laravel's validation shape: field name to a list of messages.

func (*FailError) Error

func (e *FailError) Error() string

Error implements error with the user-facing message only; the status stays available for callers that branch on it.

func (*FailError) Has

func (e *FailError) Has(field string) bool

Has reports whether the response carried a message for field.

func (*FailError) Message

func (e *FailError) Message() string

Message returns the first message of the first known field, else the first message of any field, else the HTTP status text.

type LoginResult

type LoginResult struct {
	TwoFactor       string
	ObfuscatedEmail string
	User            User
}

LoginResult is the outcome of a successful password step. TwoFactor is "" when the login completed, or "email"/"totp" when a code is needed.

type Minter

type Minter interface {
	Mint(ctx context.Context) (string, error)
}

Minter obtains a fresh chat JWT. The token lives 1 hour and the chat server hard-closes the socket when it lapses, so minting recurs for the life of a session.

type Prompter

type Prompter interface {
	// Line reads a visible line, trimmed.
	Line(prompt string) (string, error)
	// Secret reads a line without echo.
	Secret(prompt string) (string, error)
}

Prompter collects login input. The terminal implementation lives in cmd/ngchat so this flow can be driven by a script in tests.

type ServiceTokenMinter

type ServiceTokenMinter struct {
	Site *Site
}

ServiceTokenMinter re-mints through POST /api/v1/auth/service-token with the run's cookie jar (ADR 0003). Callers mint exactly once per connect and once per server revalidate: the site's service_token limiter (10/min per user) counts before auth and CSRF, so a retry loop would lock the user's browser out of chat too. A 401 surfaces as ErrSignedOut and must stop the client rather than reconnect.

func (*ServiceTokenMinter) Mint

func (m *ServiceTokenMinter) Mint(ctx context.Context) (string, error)

Mint implements Minter.

type Session

type Session struct {
	User     User
	Remember string
}

Session is what a completed login yields: who logged in, and the remember cookie value to persist.

func Login

func Login(ctx context.Context, site *Site, p Prompter, out io.Writer) (Session, error)

Login walks the password and two-factor steps until the jar holds a remember cookie. Wrong credentials re-prompt from the password step; three wrong codes or an expired challenge restart there too; an empty password goes back to the identity prompt. Lockout (429) and an undeliverable code (422) end the flow with the site's message, since only waiting or support can fix them.

type Site

type Site struct {
	// contains filtered or unexported fields
}

Site talks to the newgrounds.com JSON auth API through one in-memory cookie jar per run. Only the remember cookie survives the process; the session and CSRF cookies are re-established by priming (ADR 0003).

func NewSite

func NewSite(baseURL string) (*Site, error)

NewSite builds a Site for baseURL (e.g. https://www.newgrounds.com).

func (*Site) Login

func (s *Site) Login(ctx context.Context, identity, password string) (LoginResult, error)

Login runs the password step. A 403 means the jar was already authenticated, which a fresh jar cannot be, so it is reset and the login retried once.

func (*Site) Prime

func (s *Site) Prime(ctx context.Context) error

Prime establishes the session and CSRF cookies with a guest GET of auth/me. The 401 it answers is expected; only the cookies matter.

func (*Site) Remember

func (s *Site) Remember() (string, bool)

Remember returns the remember cookie currently in the jar, if any.

func (*Site) SeedCookies

func (s *Site) SeedCookies(header string) error

SeedCookies adds a raw Cookie header ("a=1; b=2") to the jar for the site host. Used for NGCHAT_NG_COOKIE and the dev proxy routing cookie.

func (*Site) ServiceToken

func (s *Site) ServiceToken(ctx context.Context, service string) (string, error)

ServiceToken mints a JWT for service. Callers own the rate: the site's limiter counts before auth and CSRF, so a retry loop here would also lock the user's browser out. A 401 is ErrSignedOut.

func (*Site) SetRemember

func (s *Site) SetRemember(value string)

SetRemember seeds the jar with a stored remember cookie so the first service-token call of a run authenticates.

func (*Site) TwoFactor

func (s *Site) TwoFactor(ctx context.Context, code string) (User, error)

TwoFactor completes the challenge started by Login. A six-character entry is sent as the emailed or TOTP code, anything else as a TOTP recovery code, so backup codes need no separate command.

type Store

type Store struct{}

Store persists secrets in the OS keyring (macOS Keychain, Windows Credential Manager, Linux Secret Service), falling back to a 0600 JSON file under the user config dir on headless boxes without a keyring.

func (Store) Delete

func (Store) Delete(key string) error

Delete removes a secret from both backends; missing entries are not an error.

func (Store) Load

func (Store) Load(key string) (string, error)

Load retrieves a secret, checking the keyring then the fallback file. Returns ErrNotFound when neither has it.

func (Store) Migrate

func (s Store) Migrate() (bool, error)

Migrate removes the v0.1 cookie-header slot. It reports whether one was there so the caller can explain the login prompt that follows.

func (Store) Save

func (Store) Save(key, value string) error

Save stores a secret under key, preferring the keyring. Whichever backend takes the value, the other is cleared: Load prefers the keyring, so a keyring write that fails while an older entry survives would otherwise keep logging in as the previous account.

type User

type User struct {
	ID       int64  `json:"id"`
	Username string `json:"username"`
}

User identifies the account that logged in.

Jump to

Keyboard shortcuts

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