Files
iam/internal/registry/registry.go
zeekay 2bbb0db654 A brand org is no tenant of its plain members, and a key dies with its holder
A key resolves only while its holder may sign in: a suspended or deleted
account's keys answer key_holder_disabled, its org's default key included. An
org the platform runs (authz.IsPlatformOrg) is a tenant only of its owners and
admins, its own machines and a SuperAdmin, and a brand's directory of no one:
store.Acts is that rule asked of one row, and the key resolver
(key_not_tenant), the user-key mint (403, no key filed that acts for no one) and
the delegation read it. principal.Scope and ScopeRead require MemberOf, the org
listing filters by it, and the registry gives a person a token only as an admin
of a candidate org.

A delegated token (as(), naming its actor in act) founds no org and writes no
account. A suspended account resolves nobody on its session or its bearer, and
founds nothing. An act key names its holder and mints only while that holder
runs the key's org. A machine signs in as an application or with its key: it is
refused a password grant, a form sign-in and an invitation. The groups claim
carries the orgs a person is a tenant of, so a relying party that maps groups
onto access sees no directory and no brand org held as a plain member. A name a
removed org still holds draws the answer a standing one does.

authz v1.10.47.
2026-10-17 04:07:47 -07:00

576 lines
26 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
// Package registry serves the Docker Registry v2 token endpoint the self-hosted
// OCI registry (registry:2 at oci.hanzo.ai) authenticates docker push/pull
// against. registry:2 is configured with
//
// REGISTRY_AUTH_TOKEN_REALM = https://hanzo.id/v1/iam/registry/token
// REGISTRY_AUTH_TOKEN_SERVICE = oci.hanzo.ai (the `aud` a token must carry)
// REGISTRY_AUTH_TOKEN_ROOTCERTBUNDLE = <the registry signing key's public half>
//
// and verifies every token against the JWKS this package publishes at
// /v1/iam/registry/jwks. Two routes, both PUBLIC (a docker client holds no IAM
// bearer — it authenticates with a Basic credential the token endpoint checks):
//
// GET;POST /v1/iam/registry/token — Docker Registry v2 token auth
// GET /v1/iam/registry/jwks — the verifying key (ROOTCERTBUNDLE trust set)
//
// Authentication accepts three credential shapes, all fail-closed. Each resolves a
// principal that carries its OWNER org, and authenticate binds that owner to the
// platform candidateOrgs {admin, hanzo} in ONE authoritative gate — the v1 trust
// boundary (v1 only ever authenticated users in those two orgs). A principal in any
// other tenant org gets no token. ALL THREE shapes pass through the same gate, so a
// foreign tenant is denied whether it presents:
// - a user password (resolved within the NON-RESERVED candidateOrgs — a reserved-
// org/SuperAdmin password is not a registry credential; see userByPassword —
// verified through the SAME lockout choke point login uses),
// - a Hanzo SECRET API key (sk-, resolved via store.UserByAccessKey — which
// resolves the key's OWNER, any org — then gated; a public pk- is write-only and
// authenticates nothing here),
// - a confidential application's clientId:clientSecret (store.GetApplicationBy
// ClientId resolves GLOBALLY, so the gate on app.Owner is what stops a tenant
// app minted in its OWN org from becoming a privileged push identity).
//
// Authorization: push (privileged) is DECLARED, never inferred from the shape of a
// credential. An APPLICATION pushes only when it is named on the capPush allowlist
// and owned by a reserved platform signing org (appPrivileged); a USER pushes only
// as an admin/SuperAdmin of a platform org (userPrivileged). A tenant/platform
// org-admin is NEVER privileged — IsAdmin is set on every org creator, so it is not,
// alone, a push signal. Any other authenticated principal is restricted to `pull`.
// An action not authorized is dropped, and a scope left with no authorized action is
// omitted entirely — never a silent grant.
//
// POLICY (owner decision, deliberately preserved — do NOT silently change):
// "any authenticated identity may `pull` any repository" is the EXISTING the legacy surface
// (iam-v1) behavior this port reproduces so the identity cutover is pure parity,
// not a policy shift — BUT "authenticated" is now bounded to candidateOrgs, so a
// foreign tenant can neither push nor pull. Pulls remain NOT per-repo-org-scoped
// within {admin, hanzo}. Tightening pull to per-org authorization is a legitimate
// future hardening, but an OWNER call, not a port detail: CI and cluster nodes
// pull cross-org base images through this realm, so narrowing pull scope here can
// break the image supply chain. If made, do it as an explicit, tested policy
// decision — never as an incidental edit to scopeAccess/authorizeActions.
//
// OWNER DECISION 2026-10-16: an org the platform runs is no tenant of its plain
// members (authz.IsPlatformOrg). A PERSON authenticates here only as an admin of a
// candidate org or a SuperAdmin; a plain member of hanzo — a customer who landed
// there — gets no token, by password or by key (store.KeyNotTenant). The candidate
// orgs' machines and the platform's applications pull exactly as before, so the
// image supply chain is untouched.
package registry
import (
"context"
"fmt"
"net/http"
"os"
"strings"
"time"
policy "github.com/hanzoai/authz"
"github.com/hanzoai/orm"
"github.com/valyala/fasthttp"
"github.com/zap-proto/zip"
"github.com/hanzoai/iam/internal/httpx"
"github.com/hanzoai/iam/internal/users"
"github.com/hanzoai/iam/pkg/schema"
"github.com/hanzoai/iam/pkg/store"
)
// Canonical routes. There is one shape per endpoint; anything else is 404.
const (
PathToken = "/v1/iam/registry/token"
PathJWKS = "/v1/iam/registry/jwks"
)
// envService names the registry these tokens are for. It is the `aud` every
// minted token carries, and the realm every challenge quotes.
const envService = "REGISTRY_AUTH_TOKEN_SERVICE"
// defaultService is the one registry this realm serves. There is exactly one;
// the env exists so a second deployment can name its own rather than so this
// one can be reconfigured.
const defaultService = "oci.hanzo.ai"
// envPushApps names the applications allowed to push, comma or space separated.
const envPushApps = "IAM_REGISTRY_PUSH_APPS"
// capPush is the authority to push to the shared registry. An application's whole
// authority is the set of capabilities its name is allowlisted for, so push is one
// more member of that set rather than a second kind of grant. Fail-secure by
// construction: policy.Holds reads an unset or empty list as naming nobody.
var capPush = policy.Cap{Name: "registry-push", Env: envPushApps}
// audience answers which registry a token may be minted for, and it is the ONLY
// source of that string — never the request.
//
// A caller asking for a different service is asking us to sign a token an
// unrelated verifier would accept, and every verifier trusting our JWKS reads
// `aud` to decide whether a token is addressed to it. So a mismatch is refused
// rather than honoured: the requested value is compared, never copied. An absent
// `service` is not a mismatch — some clients omit it, and there is only one
// answer it could have meant.
func audience(requested string) (string, bool) {
ours := strings.TrimSpace(os.Getenv(envService))
if ours == "" {
ours = defaultService
}
requested = strings.TrimSpace(requested)
return ours, requested == "" || requested == ours
}
//go:generate go run github.com/zap-proto/zip/cmd/zipdoc
// Route registers the registry token + JWKS endpoints on r (the PUBLIC group),
// backed by db and the process signing keyring. Called once from routes.Route.
// The keyring is passed as a lazy resolver (processKeyring): registering never forces
// key resolution, so every host that registers the full IAM surface (routes.Route)
// comes up even with no registry key configured; only an actual registry request
// resolves, and a fail-closed resolution answers 503 (never an untrusted token).
func Route(r *zip.Group, db orm.DB) {
route(r, db, processKeyring)
}
// route is the registration seam Route and the tests share: it binds a keyring
// resolver into a handler and registers the routes, so a test drives the SAME
// handlers with an injected key and never touches process/env state.
func route(r *zip.Group, db orm.DB, key func() (*keyring, error)) {
h := &handler{db: db, key: key}
r.Raw(http.MethodGet, PathToken, h.token)
r.Raw(http.MethodPost, PathToken, h.token)
r.Raw(http.MethodGet, PathJWKS, h.jwks)
}
// handler holds the store and the lazy signing-keyring resolver.
type handler struct {
db orm.DB
key func() (*keyring, error)
}
// tokenResponse is the Docker registry v2 token JSON. `token` is what the GET flow
// (docker, crane, kaniko) reads; `access_token` mirrors it for the containerd /
// BuildKit OAuth2 POST flow — emitting both lets buildx push+pull, not only the
// GET-flow clients.
type tokenResponse struct {
Token string `json:"token"`
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
IssuedAt string `json:"issued_at"`
}
// access is one entry in the token's `access` array (type:name:actions).
type access struct {
Type string `json:"type"`
Name string `json:"name"`
Actions []string `json:"actions"`
}
// principal is the authenticated identity the token is minted for: `subject` is
// the `sub` claim (owner/name for a user, clientId for a service account),
// `owner` is the tenant org that credential belongs to (the user's org, or the
// application's Owner), and `privileged` decides whether push is granted. `owner`
// is what the ONE candidateOrgs gate in authenticate enforces, so EVERY credential
// path — key, password, service account — is bound by construction.
type principal struct {
subject string
owner string
privileged bool
}
// token signs a container client in to your registry. `docker login`, and every
// build tool that pushes or pulls images, lands here: it exchanges the
// credential for a short-lived token scoped to exactly the repositories that
// credential may touch.
//
// Both of the shapes container tooling uses are accepted, so the same login works
// whichever client your pipeline runs.
func (h *handler) token(c *zip.Ctx) error {
kr, err := h.key()
if err != nil {
// Fail closed: no trusted signing key ⇒ no token. Never mint under an
// untrusted (e.g. ephemeral) key the registry's ROOTCERTBUNDLE rejects.
return c.JSON(503, map[string]string{"error": "registry signing key unavailable"})
}
req := c.Fiber().Request()
service, addressed := audience(formOrQuery(req, "service"))
if !addressed {
return challenge(c, service, "unknown service")
}
id, secret := credentials(c, req)
if id == "" {
return challenge(c, service, "authentication required")
}
p := h.authenticate(c.Context(), id, secret)
if p == nil {
return challenge(c, service, "invalid credentials")
}
acc := scopeAccess(scopes(req), p.privileged)
now := time.Now()
tok, err := kr.sign(p.subject, service, acc, now)
if err != nil {
return c.JSON(500, map[string]string{"error": "failed to sign token"})
}
return c.JSON(200, tokenResponse{
Token: tok,
AccessToken: tok,
ExpiresIn: int(tokenTTL / time.Second),
IssuedAt: now.Format(time.RFC3339),
})
}
// jwks publishes the public key your registry uses to verify the tokens issued
// above — the one URL to configure so the registry trusts logins without holding
// any secret of its own.
//
// If no signing key is available it refuses rather than publishing an empty set,
// because a registry that trusts nothing looks identical to one that trusts
// everything until somebody tries to push.
func (h *handler) jwks(c *zip.Ctx) error {
kr, err := h.key()
if err != nil {
return c.JSON(503, map[string]string{"error": "registry signing key unavailable"})
}
return c.JSON(200, kr.publicJWKS())
}
// challenge writes the 401 a docker client expects: a WWW-Authenticate Basic
// challenge naming the service realm, and NO token.
//
// The realm is always OUR service name, resolved by audience — a challenge is
// the one place a rejected request could otherwise see its own input quoted back
// inside a header.
func challenge(c *zip.Ctx, service, msg string) error {
c.SetHeader("WWW-Authenticate", fmt.Sprintf(`Basic realm="%s"`, service))
return c.JSON(401, map[string]string{"error": msg})
}
// credentials extracts (id, secret) two ways: the Authorization: Basic header
// (the docker GET flow) first, else the form username/password (the OAuth2 POST
// flow, which sends no Basic header). An empty Basic id falls through to the form,
// matching the docker client's own precedence.
func credentials(c *zip.Ctx, req *fasthttp.Request) (id, secret string) {
if bid, bsecret, ok := httpx.Basic(c); ok && bid != "" {
return bid, bsecret
}
return formOrQuery(req, "username"), formOrQuery(req, "password")
}
// authenticate resolves (id, secret) to a registry principal, or nil. It is the
// ONE trust boundary: resolve() finds WHO the credential is (any org), and this
// function then binds that principal to the platform candidateOrgs {admin, hanzo}
// in a SINGLE authoritative gate. This is the v1 boundary — v1 only ever
// authenticated users in those two orgs — and, crucially, it is enforced ONCE over
// whatever principal any credential path returns, so a NEW credential path can
// never silently skip it (the class of bug that let the key AND the service-account
// paths reach privileged push from a foreign tenant). A principal whose owner is
// any other tenant gets no token.
func (h *handler) authenticate(ctx context.Context, id, secret string) *principal {
p := h.resolve(ctx, id, secret)
if p == nil || !inCandidateOrg(p.owner) {
return nil // no credential matched, or a foreign-tenant principal — denied
}
return p
}
// resolve finds the principal a credential authenticates as — WITHOUT the org
// bound (authenticate applies that once). Ordered, each fail-closed; first match
// wins:
//
// 1. API key — a SECRET sk- value (in the secret, or the username for the
// token-as-username clients) resolved through store.UserByAccessKey. Keyed by
// an unambiguous prefix, so it never captures a password or clientId. A public
// pk- is write-only: it authenticates nothing (store.UserByAccessKey refuses it),
// and no key speaks for a SuperAdmin (store.SuperAdminKey).
// 2. User password — resolved within the NON-RESERVED candidate org(s) and
// verified through the SAME lockout choke point login uses. A reserved-org
// (SuperAdmin) password is NOT a registry credential (see userByPassword);
// a platform push identity is a service account (path 3).
// 3. Service account — a confidential application's clientId:clientSecret,
// compared in constant time. This is the CI/machine push identity.
func (h *handler) resolve(ctx context.Context, id, secret string) *principal {
for _, cand := range []string{secret, id} {
if u := h.userByKey(ctx, cand); u != nil {
return userPrincipal(u)
}
}
// A plain member of a platform org is not its tenant (authz.IsPlatformOrg): a
// customer who landed in hanzo pulls none of its images. Its admins and its
// machines do; a key of a plain member resolves nowhere (store.KeyNotTenant).
if u := h.userByPassword(ctx, id, secret); u != nil && (u.Machine() || userPrivileged(u)) {
return userPrincipal(u)
}
if p := h.serviceAccount(ctx, id, secret); p != nil {
return p
}
return nil
}
// userByKey resolves a Hanzo API key to its user, fail-closed: an empty, unknown,
// or non-key value (any error, incl. orm.ErrNotFound) yields nil — never a wrong
// or fallback principal. It reuses the ONE key resolver (store.UserAndScopeByAccessKey);
// there is no second key path. The key resolves its OWNER's org (any tenant); the
// candidateOrgs bound is applied ONCE in authenticate, not here. A key limited
// in any way (a reach, a budget, a rate) is not a registry credential: the
// registry enforces none of those, so it would act as its whole holder.
func (h *handler) userByKey(ctx context.Context, key string) *schema.User {
if strings.TrimSpace(key) == "" {
return nil
}
u, scope, err := store.UserAndScopeByAccessKey(ctx, h.db, key)
if err != nil || u == nil || strings.TrimSpace(scope) != "" {
return nil
}
return u
}
// userByPassword resolves a user by username (or email) within the NON-RESERVED
// platform candidate org(s) and verifies the password through users.Authenticate —
// the SAME lockout-enforcing choke point the interactive login and the ROPC grant
// use, so this PUBLIC endpoint throttles a password guess exactly like login: a run
// of wrong passwords locks THAT ONE row, a locked account verifies as no-match
// (docker gets the same opaque 401 as a wrong password — no lockout oracle). Returns
// nil on no match, wrong password, or a locked account.
//
// A RESERVED-org (SuperAdmin/built-in/service) principal is DELIBERATELY NOT
// authenticated by password here — the reserved candidate is skipped. This closes
// two holes the earlier multi-org walk opened on a PUBLIC unauthenticated endpoint:
// - unauth account-lock DoS: verifying the reserved row drove its shared lockout
// counter, so five wrong `docker login`s locked the platform SuperAdmin out of
// every password endpoint (login/ROPC/registry share the one row counter). A public
// endpoint must never let an anonymous caller trip a hard lock on the super.
// - a low-throttle brute-force oracle for the SuperAdmin's password on a public
// realm.
//
// Skipping the reserved org ALSO collapses the walk to the single non-reserved
// candidate, so a wrong attempt drives at most ONE row's counter (login-parity, no
// double-speed lock) and a correct hanzo/<name> password can never touch
// admin/<name>'s counter (no cross-org coupling — the F-2 bug where [email protected]
// collided across admin and hanzo). A platform push identity is a HIGH-ENTROPY
// machine credential — a service account (serviceAccount), the documented CI push
// identity — never a guessable web password on a public endpoint, and never a
// SuperAdmin's API key, which resolves to nobody (store.SuperAdminKey).
//
// PARITY NOTE: legacy's registry token path resolved {admin, hanzo} passwords with
// NO lockout at all, so this per-account lock on the registry endpoint is NEW surface
// (added by F-D1). Narrowing the PASSWORD path to non-reserved orgs is a deliberate,
// tested hardening of that new surface — not an incidental edit — and it aligns the
// registry with the ROPC grant, which already refuses reserved-org password grants
// outright (token.go). It does not narrow the service-account path, which is the
// platform's machine push identity.
func (h *handler) userByPassword(ctx context.Context, id, secret string) *schema.User {
for _, org := range candidateOrgs() {
if policy.IsReservedOrg(org) {
continue
}
u := resolveUser(ctx, h.db, org, id)
if u == nil {
continue
}
if ok, _ := users.Authenticate(ctx, h.db, u, secret, orgPasswordType(ctx, h.db, org), time.Now()); ok {
return u
}
}
return nil
}
// serviceAccount authenticates a confidential application by clientId:clientSecret
// in constant time — the machine identity a build or a deploy signs in as. A
// secret-less application is never a valid credential.
//
// Authenticating is all this does. What the application may then DO is appPrivileged,
// because holding a valid credential says who you are and nothing about what you may
// write: the credential a fleet mounts to PULL images authenticates exactly as well
// as the one CI pushes with.
//
// The principal carries the application's OWNER, so the authenticate gate binds it
// to candidateOrgs exactly like the user paths. store.GetApplicationByClientId
// resolves GLOBALLY across every org, so without that bound an app a tenant created
// in its OWN org (Owner="evil") would authenticate on the shared registry at all.
func (h *handler) serviceAccount(ctx context.Context, id, secret string) *principal {
app, err := store.GetApplicationByClientId(ctx, h.db, id)
if err != nil || app == nil || app.ClientSecret == "" {
return nil
}
if !app.Proves(secret) {
return nil
}
return &principal{subject: id, owner: app.Owner, privileged: appPrivileged(app)}
}
// appPrivileged decides whether an APPLICATION may push to the shared registry —
// the service-account twin of userPrivileged, reading a declaration rather than
// assuming one.
//
// Two conditions, both required. The application must be named on capPush's
// allowlist, which is operator config, so an application row can never grant itself
// push. And its OWNING org must be a reserved platform signing org, which reserves a
// listed name to the platform's own app: the registry authenticates the hanzo org as
// well as admin, so without the owner pin a hanzo-owned row carrying a listed name
// would inherit push from the name alone.
//
// The allowlist is written in application NAMES. Every provisioned app is registered
// with clientId == name (HIP-0111, <org>-<app>), which is the string a docker login
// presents and the `sub` of the token minted from it, so the name an operator writes
// here is the identity they see in the credential and in the log.
func appPrivileged(app *schema.Application) bool {
p := policy.Principal{App: &policy.App{Name: app.Name, Owner: app.Owner}}
return p.Holds(capPush, os.Getenv)
}
// userPrincipal projects a user into a token principal: `sub` is owner/name,
// `owner` is the user's org (bound to candidateOrgs by authenticate), and push
// (privileged) is gated by userPrivileged.
func userPrincipal(u *schema.User) *principal {
return &principal{
subject: u.Owner + "/" + u.Name,
owner: u.Owner,
privileged: userPrivileged(u),
}
}
// userPrivileged decides whether a USER may push to the shared registry. This is
// the v1-PARITY gate: legacy (registry_token.go) authenticated users within
// {admin, hanzo} and granted push to any IsAdmin/SuperAdmin among them. We
// reproduce that EXACTLY so the identity cutover is a faithful drop-in with zero
// behavior change — a hanzo-org admin keeps push, as it does on legacy today.
//
// Push requires the user to be in a candidateOrg AND be an admin/SuperAdmin there.
// The candidateOrgs bound (enforced at authentication, registry.go ~:241) is what
// makes IsAdmin safe to trust here: a FOREIGN-tenant org-admin never authenticates
// to the registry at all (TestToken_ForeignTenantKey_Denied), so its IsAdmin can
// never reach this gate — that foreign-org denial is the actual cross-tenant close,
// independent of this predicate. CI pushes through the service account (privileged
// by its own path).
//
// DEFERRED OWNER POLICY (do NOT tighten during the migration): whether a hanzo-org
// HUMAN admin should keep registry push — vs. restricting push to the admin org
// (SuperAdmins) / signing-trust orgs only — is a post-cutover hardening decision,
// not a port detail. Narrowing it here would be a behavior change vs legacy. If
// made, do it as an explicit, tested policy change (alongside the pull org-scoping
// decision documented at the top of this file), never as an incidental edit.
func userPrivileged(u *schema.User) bool {
return inCandidateOrg(u.Owner) && (u.IsAdmin || u.SuperAdmin())
}
// candidateOrgs are the platform organizations a docker credential is resolved
// within: the reserved admin org (home of the SuperAdmin) and the hanzo org (home
// of platform users). oci.hanzo.ai is Hanzo infrastructure, so these are the
// two tenants its push/pull identities live in — matching the beego source's
// {AdminOrg, "hanzo"} resolution. This is the trust boundary EVERY user credential
// shape (password AND API key) is bound to.
func candidateOrgs() []string { return []string{"admin", "hanzo"} }
// inCandidateOrg reports whether owner is one of the platform candidateOrgs — the
// ONE membership predicate the key path and any future bound share.
func inCandidateOrg(owner string) bool {
for _, o := range candidateOrgs() {
if o == owner {
return true
}
}
return false
}
// resolveUser looks a user up by email (identifier contains "@") or by name,
// scoped to org — the same email-or-name resolution the login endpoint uses.
func resolveUser(ctx context.Context, db orm.DB, org, identifier string) *schema.User {
if strings.Contains(identifier, "@") {
if u, err := store.GetUserByEmail(ctx, db, org, identifier); err == nil && u != nil {
return u
}
// Fall through: some accounts set name = email.
}
u, err := store.GetUserByName(ctx, db, org, identifier)
if err != nil {
return nil
}
return u
}
// orgPasswordType returns the organization's PasswordType — the fallback scheme
// when a user row carries none, exactly as the login path resolves it. A missing
// org yields "" (the user's own type then decides; cred.Verify fails closed if
// neither is set, never guessing an algorithm).
func orgPasswordType(ctx context.Context, db orm.DB, org string) string {
o, err := store.GetOrganizationByName(ctx, db, org)
if err != nil || o == nil {
return ""
}
return o.PasswordType
}
// scopeAccess parses the requested Docker scope strings ("type:name:a,b") into
// access entries, authorizing each against `privileged`. Both request shapes are
// flattened (space-separated scopes in one param, and repeated scope params), so
// no requested repository is silently dropped by shape. A non-privileged principal
// keeps only the `pull` action; a scope left with no authorized action is omitted
// entirely — so an identity gets NO access entry for an action it cannot perform,
// never an empty-action grant.
func scopeAccess(rawScopes []string, privileged bool) []access {
out := make([]access, 0, len(rawScopes))
for _, raw := range rawScopes {
for _, s := range strings.Fields(raw) {
parts := strings.SplitN(s, ":", 3)
if len(parts) != 3 {
continue
}
actions := authorizeActions(strings.Split(parts[2], ","), privileged)
if len(actions) == 0 {
continue
}
out = append(out, access{Type: parts[0], Name: parts[1], Actions: actions})
}
}
return out
}
// authorizeActions returns the actions a principal is authorized for: all of them
// when privileged, else only `pull`. Empties (from a malformed "a,,b") are dropped.
func authorizeActions(actions []string, privileged bool) []string {
out := make([]string, 0, len(actions))
for _, a := range actions {
a = strings.TrimSpace(a)
if a == "" {
continue
}
if privileged || a == "pull" {
out = append(out, a)
}
}
return out
}
// scopes collects EVERY requested scope from both the query and the form body —
// the merge the docker GET flow (query) and the OAuth2 POST flow (body) each need,
// and the multi-scope shape buildx sends as repeated scope params. Reading only
// the first value would silently drop the rest.
func scopes(req *fasthttp.Request) []string {
var out []string
for _, v := range req.URI().QueryArgs().PeekMulti("scope") {
out = append(out, string(v))
}
for _, v := range req.PostArgs().PeekMulti("scope") {
out = append(out, string(v))
}
return out
}
// formOrQuery reads a single value from the query string, else the form body —
// the docker GET flow puts service/username in the query, the OAuth2 POST flow in
// the body.
func formOrQuery(req *fasthttp.Request, key string) string {
if v := req.URI().QueryArgs().Peek(key); len(v) > 0 {
return string(v)
}
if v := req.PostArgs().Peek(key); len(v) > 0 {
return string(v)
}
return ""
}