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
- Variables
- type FailError
- type LoginResult
- type Minter
- type Prompter
- type ServiceTokenMinter
- type Session
- type Site
- func (s *Site) Login(ctx context.Context, identity, password string) (LoginResult, error)
- func (s *Site) Prime(ctx context.Context) error
- func (s *Site) Remember() (string, bool)
- func (s *Site) SeedCookies(header string) error
- func (s *Site) ServiceToken(ctx context.Context, service string) (string, error)
- func (s *Site) SetRemember(value string)
- func (s *Site) TwoFactor(ctx context.Context, code string) (User, error)
- type Store
- type User
Constants ¶
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).
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.
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.
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 ¶
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.
var ErrNotFound = errors.New("credential not found")
ErrNotFound is returned by Load when no value is stored under a key.
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 ¶
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 ¶
Error implements error with the user-facing message only; the status stays available for callers that branch on it.
type LoginResult ¶
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 ¶
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.
type Session ¶
Session is what a completed login yields: who logged in, and the remember cookie value to persist.
func Login ¶
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 ¶
NewSite builds a Site for baseURL (e.g. https://www.newgrounds.com).
func (*Site) Login ¶
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 ¶
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) SeedCookies ¶
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 ¶
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 ¶
SetRemember seeds the jar with a stored remember cookie so the first service-token call of a run authenticates.
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) Load ¶
Load retrieves a secret, checking the keyring then the fallback file. Returns ErrNotFound when neither has it.
func (Store) Migrate ¶
Migrate removes the v0.1 cookie-header slot. It reports whether one was there so the caller can explain the login prompt that follows.