Files
iam/internal/oidc/password.go
zeekayandHanzo Dev 6d9f08eebb Sign-out ends the session from every page that signs a person out, and an old browser is judged by Origin
v1.34.158 judged sign-out by the rule for every other cookie write, which
asks for a JSON body. @hanzo/iam end() and iam-sdk logout() post a form, or
nothing, from the issuer's own page, so sign-out answered 403 and the
session stayed alive. A relying party's POST sign-out form (RP-Initiated
Logout 1.0 §2) was refused the same way.

Sign-out now has its own rule, Request.ends: it refuses only what the
browser says another page sent without navigating here (an image, a frame,
a script's fetch). Everything the issuer's pages send ends the sessions,
whatever the body, and so does a GET or POST navigation. A browser with no
Fetch Metadata is believed.

Every other cookie write still needs the issuer's page and a JSON body. A
browser with no Fetch Metadata (Safari before 16.4, Firefox before 90, a
proxy that strips it) was refused on every write. Its Origin is now
compared with the Host header: the same host over https counts as the
issuer's page, and any other origin counts as cross-site. A request with
neither header came from no page. The checks read sessions.Request, a value
holding the method, Fetch Metadata, Origin, Host and Content-Type. The typed
handlers bind Origin and Host next to Sec-Fetch-Site.

Co-authored-by: Hanzo Dev <[email protected]>
2026-10-17 21:27:31 -07:00

251 lines
11 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package oidc
import (
"context"
"net/http"
"github.com/hanzoai/orm"
"github.com/zap-proto/zip"
"github.com/hanzoai/iam/internal/cred"
"github.com/hanzoai/iam/internal/httpx"
"github.com/hanzoai/iam/internal/otp"
"github.com/hanzoai/iam/internal/sessions"
"github.com/hanzoai/iam/internal/users"
"github.com/hanzoai/iam/pkg/schema"
"github.com/hanzoai/iam/pkg/store"
)
// PUT /v1/iam/password — the ONE place a person's own password is written.
//
// It closes a hole with no bottom: nothing anywhere consumed a verification code
// and set a credential, so /v1/iam/verification-codes minted and delivered a code
// that no endpoint could redeem into a password. A locked-out person could not
// finish a reset by any route, and a signed-in person could not rotate their own
// password either — every writer (users update, admin upsert, SCIM) is
// admin-scoped, because internal/authz deliberately gates the self clause to GET
// so a regular user cannot carry isAdmin on a full-row write. Widening that
// clause is the privilege escalation the comment there guards against, so the
// self path belongs here instead: one endpoint, self-scoped, with the ONE field
// it may write.
//
// Two proofs of the same person, one write site:
//
// - A SIGNED-IN caller proves it with the password being replaced. The subject
// is ALWAYS the caller (callerOf), exactly as PUT /v1/iam/consent does it —
// the body never names a target on this arm, so there is no shape in which
// this writes somebody else's credential.
// - A LOCKED-OUT caller has no session, which is the whole point, so it names
// the account and proves it with a code delivered to that account's own
// address. The code is spent by [otp.Consume], which is bound to the account
// it was minted for.
//
// It is not a second authentication scheme and must never become one. Nothing
// here mints a session, a token or an authorization code: a person who lands a
// reset then signs in through /v1/iam/login like anybody else, second factor
// included. The old-password proof runs through users.Authenticate — the single
// human-credential check — so the account lockout applies to a rotation exactly
// as it applies to a login.
const PathPassword = "/v1/iam/password"
// errPasswordCode is the ONE refusal for every way the code arm can fail:
// unknown account, wrong or expired or spent or foreign code, nothing able to
// deliver. They collapse to one answer because telling them apart answers "does
// this address have an account" to anyone who asks.
const errPasswordCode = "the code is incorrect or has expired"
// passwordBody is the wire shape. Exactly one proof may be present, and which
// one it is selects the arm.
type passwordBody struct {
// The account being recovered, for a caller who cannot be signed in. Read on
// the CODE arm only — a signed-in caller is resolved from its own session or
// token, never from these.
Organization string `json:"organization"`
Username string `json:"username" url:"-"` // email, username OR phone
// The three fields that decide the write, each `url:"-"` so the BODY is the
// only place it can be stated. A scalar without that tag binds from the query
// on every method and binds AFTER the body, so it does not merely offer a
// second source — it overrides the first. A URL is also the part of a request
// that gets written down: access and proxy logs, browser history, the Referer
// of whatever the answer links to. A credential belongs in none of those.
// Code is the one-time code delivered to the account's own address.
Code string `json:"code" url:"-"`
// OldPassword is the credential being replaced, the proof a signed-in caller
// gives instead of a code.
OldPassword string `json:"oldPassword" url:"-"`
// Password is the new credential. It must satisfy the platform floor and the
// organization's own complexity options.
Password string `json:"password" url:"-"`
// The two credentials a signed-in caller can present, bound from the request
// headers so this stays a typed op. `json:"-"` keeps them off the body and out
// of the query string, so the header is the only way to present either.
//
// Both, because the two arms of this endpoint serve two callers: the portal
// holds a session cookie, an API client holds a bearer. Resolving only one of
// them would silently take password change away from the other. Site, Origin,
// Host and Type are what the cookie is judged by (sessions.Forgeable).
Cookie string `json:"-" header:"Cookie"`
Auth string `json:"-" header:"Authorization"`
Site string `json:"-" header:"Sec-Fetch-Site"`
Origin string `json:"-" header:"Origin"`
Host string `json:"-" header:"Host"`
Type string `json:"-" header:"Content-Type"`
}
// asker is what this PUT presents to say who is asking.
func (in *passwordBody) asker() asker {
return asker{Method: http.MethodPut, Cookie: sessionCookie(in.Cookie), Site: in.Site, Origin: in.Origin, Host: in.Host, Type: in.Type, Bearer: httpx.BearerValue(in.Auth)}
}
// sessionCookie is the session value carried by a raw `Cookie` header, or "".
// A typed op is handed the header rather than the request, so the parse that
// fiber's c.Cookies does lives here instead.
func sessionCookie(header string) string {
for _, c := range readCookies(header) {
if c.Name == sessions.CookieName {
return c.Value
}
}
return ""
}
// readCookies parses a Cookie header with the standard library's own rules, so a
// quoted value or an unusual separator is read exactly as net/http reads it.
func readCookies(header string) []*http.Cookie {
if header == "" {
return nil
}
return (&http.Request{Header: http.Header{"Cookie": []string{header}}}).Cookies()
}
// putPasswordHandler replaces the calling person's password. Only their own —
// there is no shape of this request that writes somebody else's.
//
// Prove who you are with the password you are replacing, or — when you cannot
// sign in at all — with a code sent to the address the account already holds.
// Exactly one of the two: a request carrying both proves nothing more than
// either, and answering it would mean deciding which one mattered.
//
// A reset also clears the account lockout, in the SAME transaction as the
// digest. Replacing a credential retires the run of guesses against the old one,
// and without this a person who reset a forgotten password was still refused for
// up to fifteen more minutes — with the brand-new password they had just chosen.
func putPasswordHandler(db orm.DB) zip.TypedHandler[passwordBody, httpx.Answer] {
return func(ctx context.Context, in *passwordBody) (*httpx.Answer, error) {
if in.Password == "" {
return httpx.Bad(400, "password cannot be empty", ""), nil
}
if (in.Code == "") == (in.OldPassword == "") {
return httpx.Bad(400, "send either the current password or a verification code, not both", ""), nil
}
var user *schema.User
if in.Code != "" {
var err error
if user, err = codeSubject(ctx, db, *in); err != nil {
return httpx.Bad(400, err.Error(), ""), nil
}
if user == nil {
return httpx.Bad(400, errPasswordCode, ""), nil
}
} else {
owner, name, err := callerFrom(ctx, db, in.asker())
if err != nil {
return callerAnswer(err), nil
}
if user, err = store.GetUserByName(ctx, db, owner, name); err != nil {
return httpx.Bad(400, err.Error(), ""), nil
}
if user == nil {
return httpx.Bad(400, "the user does not exist", ""), nil
}
// The one human-credential check, so a rotation is subject to the same
// lockout as a login and a correct old password clears the running count.
ok, locked := users.Authenticate(ctx, db, user, in.OldPassword,
loginOrgPasswordType(ctx, db, user.Owner), nowFunc())
if locked {
return httpx.Bad(400, "too many failed attempts; the account is temporarily locked", ""), nil
}
if !ok {
return httpx.Bad(400, "the current password is incorrect", ""), nil
}
}
// Complexity is the org's, floored by the platform's — the same rule signup
// enforces, from the same function, so a password that could not have been
// registered cannot be arrived at by resetting either.
org, err := store.GetOrganizationByName(ctx, db, user.Owner)
if err != nil {
return httpx.Bad(400, err.Error(), ""), nil
}
var options []string
if org != nil {
options = org.PasswordOptions
}
if msg := passwordPolicyError(options, in.Password); msg != "" {
return httpx.Bad(400, msg, ""), nil
}
hash, err := cred.Hash(in.Password)
if err != nil {
return httpx.Bad(400, "server_error", ""), nil
}
// One row-locked write for the digest AND the lockout, through the same seam
// every other pre-authentication user-row write uses. Clearing the counter
// here rather than in users.Update is deliberate: a full-row admin profile
// edit must keep carrying it forward (F-6), because an omitted counter there
// would silently unlock a locked account mid-attack. What retires the run of
// guesses is replacing the credential they were aimed at, which is this.
//
// A code sent to the account's own address and spent here proves that address,
// and this write replaces the only password anybody holds — so whoever set the
// old one can no longer sign in with it, and the proof is recorded with the
// credential it belongs to.
if _, err := updateUser(ctx, db, user.Owner, user.Name, func(_ orm.DB, u *schema.User) error {
u.PasswordHash = hash
u.PasswordType = cred.TypeArgon2id
u.PasswordSalt = ""
u.SigninWrongTimes = 0
u.LastSigninWrongTime = ""
if in.Code != "" && u.Email != "" && store.NormalizeEmail(in.Username) == store.NormalizeEmail(u.Email) {
u.EmailVerified = true
}
u.UpdatedTime = provisionNow()
return nil
}); err != nil {
return httpx.Bad(400, err.Error(), ""), nil
}
return httpx.Good(nil), nil
}
}
// codeSubject resolves the account a code-proved reset is for, or nil when the
// code does not prove one. Every refusal is nil, never a distinguishable error.
//
// Delivery must be configured, for the same reason codeLogin requires it: a
// process that cannot send a code cannot have sent the one it is being asked to
// trust.
func codeSubject(ctx context.Context, db orm.DB, in passwordBody) (*schema.User, error) {
if !otp.DeliveryConfigured() || in.Organization == "" || in.Username == "" {
return nil, nil
}
// The SAME resolution login uses, so the account a code recovers is the account
// that identifier signs in as — name first, then email, then phone, then the
// accounts registered in the org the caller names, which is where a person in an
// org of their own is found.
user, err := resolveLoginUser(ctx, db, in.Organization, in.Username, false)
if err != nil {
return nil, err
}
ok, err := otp.Consume(ctx, db, user, in.Username, in.Code, nowFunc())
if err != nil || !ok {
return nil, err
}
return user, nil
}