Documentation
¶
Overview ¶
web/handlers.go
web/headers.go
web/markdown.go
web/render.go
Index ¶
- Constants
- Variables
- func Challenge(verifier string) string
- func Markdown(w http.ResponseWriter, r *http.Request)
- func RenderErrorPage(status int, reason, message, user string) ([]byte, error)
- func RenderResultFragment(v ResultView) ([]byte, error)
- func RenderResultPage(v ResultView) ([]byte, error)
- func Serve(w http.ResponseWriter, r *http.Request, ...)
- func SignState(secret []byte, c StateClaims) string
- func WantsMarkdown(r *http.Request) bool
- type Card
- type Config
- type ErrorView
- type GitHub
- func (g GitHub) Authorize(clientID, redirectURI, nonce, challenge string) string
- func (g GitHub) Exchange(ctx context.Context, clientID, secret, code, redirectURI, verifier string) (string, error)
- func (g GitHub) Login(ctx context.Context, token string) (string, error)
- func (g GitHub) Revoke(ctx context.Context, clientID, secret, token string) error
- type Header
- type Link
- type Meter
- type ResultView
- type StateClaims
Constants ¶
const MarkdownAccept = `.*text/markdown.*`
MarkdownAccept is the vercel.json `has` value matched against the Accept header.
const MarkdownRoute = `^/(?!api/|auth/|result(?:/|$))([^.]*)$`
MarkdownRoute is the vercel.json route source (PCRE) for paths that answer Accept: text/markdown: every page path, but not files (a dot in the path) and not the functions, whose HTML has no Markdown twin. $1 is the path without its leading slash. WantsMarkdown applies the same rule in Go; cmd/devserver and the tests rely on it.
const StateCookie = "gha_oauth"
StateCookie carries the signed sign-in state between /auth/login and /api/private.
const StateMaxAge = 10 * time.Minute
StateMaxAge is how long a sign-in may take between GitHub's consent screen and our callback.
Variables ¶
var ( ErrState = errors.New("sign-in state is missing, invalid or expired") ErrExchange = errors.New("GitHub did not accept the sign-in code") )
Functions ¶
func Markdown ¶
func Markdown(w http.ResponseWriter, r *http.Request)
Markdown serves GET /api/markdown?path=<path>, which vercel.json routes every page request with Accept: text/markdown to. An unknown path is a 404 with a Markdown body that points at the sitemap and llms.txt.
func RenderErrorPage ¶
RenderErrorPage renders the check form with one message.
func RenderResultFragment ¶
func RenderResultFragment(v ResultView) ([]byte, error)
RenderResultFragment is the result section alone, for /api/private.
func RenderResultPage ¶
func RenderResultPage(v ResultView) ([]byte, error)
RenderResultPage is the full /result page. Its <h1> is the @login in the result header.
func Serve ¶
func Serve(w http.ResponseWriter, r *http.Request, h func(*Config, http.ResponseWriter, *http.Request))
Serve is what each Vercel function calls.
func SignState ¶
func SignState(secret []byte, c StateClaims) string
SignState encodes claims as payload.signature.
func WantsMarkdown ¶
WantsMarkdown reports whether a request should get the Markdown twin instead of HTML.
Types ¶
type Card ¶
type Card struct {
Slug, Name, HowToEarn, Image, ImageAlt, Initials, TierLabel, Line string
Source, SourceMeaning, BadgeURL, Unlocked, TierText string
Tier int
Earned, Locked bool
Progress *Meter
Notes []string
Milestones []Link
}
Card is one badge on the results page.
type Config ¶
type Config struct {
ClientID, ClientSecret, BotToken string
StateSecret []byte
BaseURL string // https://gitverse.vercel.app or http://localhost:8787
GitHub GitHub
Official report.Official
NewCounts func(token string) (counters.Source, error)
Timeout time.Duration
Now func() time.Time
Log *log.Logger
}
Config is everything the handlers need. Tests replace GitHub, Official, NewCounts and Now.
func (*Config) Login ¶
func (c *Config) Login(w http.ResponseWriter, r *http.Request)
Login serves GET /auth/login: a signed state cookie, then GitHub's consent screen.
type ErrorView ¶
type ErrorView struct {
Status int
Reason, Message string
User string // pre-fills the username box
}
ErrorView is shown instead of a result.
type GitHub ¶
GitHub holds the endpoints, so tests can point them at a fake.
func (GitHub) Exchange ¶
func (g GitHub) Exchange(ctx context.Context, clientID, secret, code, redirectURI, verifier string) (string, error)
Exchange trades the code for a user token. A refresh token, if any, is ignored.
type Header ¶
type Header struct{ Key, Value string }
Header is one response header.
func SecurityHeaders ¶
func SecurityHeaders() []Header
SecurityHeaders apply to every response. cmd/gendocs writes the same list into vercel.json for static files, so functions and static pages cannot drift.
type Link ¶
type Link struct{ Label, URL string }
Link is a milestone row; URL is empty for a private one.
type ResultView ¶
type ResultView struct {
User, Avatar string
Private, Hidden, Authenticated bool
Standalone bool // the full /result page: @login is its <h1>
Earned, EarnableTotal, TierTotal int
Closest *Card
Display []Card // "On display": every earned badge, any kind
Collect []Card // "Still to collect": earnable, not earned
Archive []Card // "The archive": cannot be earned, not earned
Highlights []report.HighlightRow
Verified string
}
ResultView is everything result.html shows.
func NewResultView ¶
func NewResultView(r *report.Report, private bool) ResultView
NewResultView turns a report into the page model. private marks a signed-in result.
type StateClaims ¶
type StateClaims struct {
Nonce string `json:"n"`
Verifier string `json:"v"` // PKCE code_verifier
Issued int64 `json:"t"`
}
StateClaims travel in an HMAC-signed, HttpOnly cookie between /auth/login and /api/private.
func NewState ¶
func NewState(now time.Time) (StateClaims, error)
NewState makes a fresh nonce and PKCE verifier.
func VerifyState ¶
VerifyState checks the signature, that nonce is the one GitHub echoed back, and the age.