mirror of
https://github.com/hanzoai/iam.git
synced 2026-11-19 18:37:16 +00:00
An application serves every org when it is shared or offers an org choice (orgChoiceMode other than "" and "None"). Create and Update refuse a non-SuperAdmin any such write -- making one, editing one, or confining one -- through one gate, authorizeSpan, on schema.Application.Spans. Tenant isolation reads ServesAnyOrg, which also requires the platform to own the row (Owner "admin"). A row any other org owns serves that org alone, whatever it declares, so the code mint, the password grant, impersonation, device approval and federation cross tenants only through a platform application. The seed files an owner-less application under the owner its key names. Regenerates the internal/tokens zipdoc. Co-authored-by: Hanzo Dev <[email protected]>
129 lines
5.3 KiB
Go
129 lines
5.3 KiB
Go
// Copyright 2026 Hanzo AI, Inc.
|
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
|
|
|
package oidc
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
|
|
policy "github.com/hanzoai/authz"
|
|
"github.com/hanzoai/orm"
|
|
|
|
"github.com/hanzoai/iam/pkg/schema"
|
|
"github.com/hanzoai/iam/pkg/store"
|
|
)
|
|
|
|
// The shared tail of every interactive authentication: given a user who has
|
|
// ALREADY proven who they are — by password, by wallet signature, by any future
|
|
// factor — decide what the SDK reads back. One place, so a new endpoint
|
|
// inherits the redirect/PKCE/tenant rules instead of restating them.
|
|
|
|
// Mint is the authorize passthrough an interactive login carries. Type selects
|
|
// the shape: "code" mints a PKCE-bound authorization code for the OAuth flow,
|
|
// anything else is a bare portal sign-in.
|
|
type Mint struct {
|
|
Type string
|
|
RedirectUri string
|
|
State string
|
|
Scope string
|
|
Nonce string
|
|
CodeChallenge string
|
|
CodeChallengeMethod string
|
|
Resource string
|
|
// Actor and Reason are set only by an impersonation (impersonate.go): the
|
|
// SuperAdmin the code is minted for the user BY, and why. Every sign-in a
|
|
// person performs leaves both empty.
|
|
Actor string
|
|
Reason string
|
|
}
|
|
|
|
// MintFor resolves what a successful authentication returns to the SDK: the
|
|
// user id for a bare portal sign-in, or a fresh PKCE-bound authorization code
|
|
// (persisted) for the OAuth flow. userID is "<org>/<name>" — the caller's own
|
|
// verified identity, never a client-supplied value.
|
|
//
|
|
// This is the ONE mint path. Every rule below is a security invariant, so it
|
|
// lives here rather than in each endpoint:
|
|
// - Tenant isolation: the user's org must be permitted for this application —
|
|
// its own org, or any org through an application that serves every org
|
|
// (schema.Application.ServesAnyOrg: it declares so AND the platform owns it).
|
|
// Without it a user in one tenant could obtain a token naming another.
|
|
// - Redirect binding: an exactly-registered redirect_uri (RFC 6749 §3.1.2.3);
|
|
// the token endpoint re-checks it. A supplied-but-unregistered URI is never
|
|
// minted against.
|
|
// - PKCE: S256 only (never "plain"), and a public client must present a
|
|
// challenge — no downgrade.
|
|
// - Reserved-org confinement: a built-in/SuperAdmin principal is grantable
|
|
// ONLY through an application that serves its own reserved org.
|
|
func MintFor(ctx context.Context, db orm.DB, app *schema.Application, userID string, p Mint) (string, error) {
|
|
// The user's org is the owner half of its own id, set server-side at
|
|
// authentication — never read from the request.
|
|
org, _, _ := strings.Cut(userID, "/")
|
|
|
|
// Reserved-org confinement, ahead of the type split so EVERY grant shape is
|
|
// bound identically — a bare session, an authorization code, a wallet
|
|
// sign-in, and the silent SSO grant alike. A RESERVED-org principal (a
|
|
// SuperAdmin under "admin", a built-in service identity) may be granted only
|
|
// through an application that itself serves that reserved org — the dedicated
|
|
// console. Otherwise any SHARED application (whose tenant rule below accepts
|
|
// every org by design) would mint a real SuperAdmin grant, and silent SSO
|
|
// would mint it with no interaction at all.
|
|
//
|
|
// It lived in login.go, which meant it held for a typed password and for
|
|
// nothing else. Here it holds for every endpoint, including the ones not
|
|
// written yet.
|
|
if policy.IsReservedOrg(org) && (app == nil || org != app.Organization) {
|
|
return "", errors.New("the user is not permitted to sign in to this application")
|
|
}
|
|
|
|
// A bare sign-in needs no application: report the identity and stop.
|
|
if p.Type != "code" {
|
|
return userID, nil
|
|
}
|
|
if app == nil {
|
|
return "", errors.New("the application does not exist")
|
|
}
|
|
if org != app.Organization && !app.ServesAnyOrg() {
|
|
return "", errors.New("the user is not permitted to sign in to this application")
|
|
}
|
|
if p.RedirectUri != "" && !app.IsRedirectUriValid(p.RedirectUri) {
|
|
return "", errors.New("invalid redirect_uri")
|
|
}
|
|
method := normalizeChallengeMethod(p.CodeChallenge, p.CodeChallengeMethod)
|
|
if p.CodeChallenge != "" && method != "S256" {
|
|
return "", errors.New("only S256 PKCE is supported")
|
|
}
|
|
if app.ClientSecret == "" && p.CodeChallenge == "" {
|
|
return "", errors.New("PKCE is required for public clients")
|
|
}
|
|
code, err := MintCode(app, userID, p.Scope, p.CodeChallenge, method, p.Resource, nowFunc())
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
// Bind the redirect_uri and nonce onto the code so the token exchange can
|
|
// re-verify the redirect and echo the nonce into the id_token.
|
|
code.RedirectUri = p.RedirectUri
|
|
code.Nonce = p.Nonce
|
|
code.Actor, code.Reason = p.Actor, p.Reason
|
|
if err := store.PersistToken(ctx, db, code); err != nil {
|
|
return "", err
|
|
}
|
|
return code.Code, nil
|
|
}
|
|
|
|
// ResolveApp resolves the OAuth application an endpoint names: by clientId when
|
|
// present, else by name under the "admin" registry owner. Returns (nil, nil)
|
|
// when the request names no application. Shared by every interactive login so
|
|
// they all resolve the same app from the same fields.
|
|
func ResolveApp(ctx context.Context, db orm.DB, clientId, name string) (*schema.Application, error) {
|
|
if clientId != "" {
|
|
return store.GetApplicationByClientId(ctx, db, clientId)
|
|
}
|
|
if name != "" {
|
|
return store.GetApplicationByName(ctx, db, "admin", name)
|
|
}
|
|
return nil, nil
|
|
}
|