Documentation
¶
Overview ¶
Package run is the one "prepare a run" step shared by cmd/ngchat and cmd/smoke: the environment both read, the site client with its routing cookie, the credential the jar is seeded with, the chat-URL check, and the client.Config that follows. The two binaries used to hand-write this sequence and drifted (different variable names, one skipping the store migration); keeping it here means a change to the order or the messages happens once and is tested once, through narrowed interfaces rather than a keyring or a network.
Index ¶
Constants ¶
const ( // DefaultWSURL and DefaultSiteURL are production, the only endpoints // that exist outside Newgrounds; the variables below override them // for the internal dev stack. DefaultWSURL = "wss://chat.newgrounds.com/ws" DefaultSiteURL = "https://www.newgrounds.com" // Channel is the one room a run joins. The server has one channel // today and switching is out of scope (docs/roadmap.md). Channel = "general" )
Variables ¶
var ErrNoCredential = errors.New(
"no NGCHAT_NG_COOKIE and no usable stored login")
ErrNoCredential is returned by Prepare when nothing usable is stored and Options.Login is nil. It wraps the store's own answer, so errors.Is still tells auth.ErrNotFound (log in) from auth.ErrKeyringUnavailable (unlock the keyring, or log in and the value lands in the file).
Functions ¶
func ClientConfig ¶
ClientConfig is the client.Config both binaries run: the chat URL and routing cookie from Env, the one Channel, and a minter over the site Prepare just seeded. log is nil for no logging.
func NewSite ¶
NewSite builds the site client for the run with the routing cookie seeded, so it rides on every request the way it does in a browser. It is separate from Prepare because login and logout need the site too, and neither must be blocked by a stale chat URL or a missing credential. Errors name the variable at fault.
Types ¶
type CredentialStore ¶
CredentialStore is what Prepare needs from auth.Store, narrowed so the outcome matrix can be scripted without a keyring.
type Env ¶
type Env struct {
// SiteURL is NGCHAT_SITE_URL, the origin whose cookie jar mints the
// chat JWT.
SiteURL string
// WSURL is NGCHAT_WS_URL, the chat endpoint the JWT is sent to.
WSURL string
// Routing is NGCHAT_ROUTING_COOKIE: the dev proxy's routing cookie,
// which must ride on every request, site and chat alike. Empty in
// production.
Routing string
// CookieHeader is NGCHAT_NG_COOKIE, a raw cookie header that bypasses
// the stored login. Development and the smoke harness only.
CookieHeader string
}
Env is what a run reads from the environment, held as a value so the step can be tested without os.Getenv and so a caller can see, in one place, every variable that steers a run.
func FromEnv ¶
func FromEnv() Env
FromEnv reads Env from the process environment, defaulting the URLs to production so a run with nothing set is a run against the real site, as README.md promises.
type Options ¶
type Options struct {
Env Env
Store CredentialStore
Site Site
// Login runs when the store has nothing usable: not found, or a
// keyring that gave no answer. It must leave the jar authenticated
// (auth.Login does), since the first mint follows with no further
// setup. nil means the caller cannot prompt, and Prepare returns
// ErrNoCredential instead; that is how the headless smoke harness
// stays headless (ADR 0008).
Login func(ctx context.Context) error
// Notices receives the lines a user should see on the way: a
// removed v0.1 slot, a keyring that could not be read. nil discards
// them. Never a secret.
Notices io.Writer
}
Options is what Prepare works with. Store and Site are interfaces rather than the auth types so the decision table is tested with fakes; Env is a value so no test reads the real environment.
type Site ¶
type Site interface {
CredentialKey() string
SeedCookies(header string) error
SetRemember(value string)
CheckChatURL(raw string) error
}
Site is what Prepare needs from *auth.Site, narrowed so the jar can be a fake that records what it was seeded with.
type Source ¶
type Source int
Source says where the credential a run minted with came from. The smoke harness prints it because an exported NGCHAT_NG_COOKIE silently wins over a fresh `ngchat login`, which is the one thing an operator verifying the login flow needs to know.
func Prepare ¶
Prepare gets the jar into a state that can mint, in this order:
- The chat URL is checked, before anything is minted, because the token minted from the site's cookie goes to it as the first frame.
- NGCHAT_NG_COOKIE, when set, is seeded as-is and nothing stored is read.
- Otherwise the v0.1 cookie-header slot is removed (it cannot be converted, ADR 0003), the stored remember cookie is loaded, and when there is none Options.Login runs.
A keyring that gave no answer counts as "log in": whatever it holds cannot be used this run, and a fresh login lands in the file, which the store reads first from then on. Any other store error propagates.