Files
zeekayandHanzo Dev 084011934f legal: dual MIT OR Apache-2.0, canonical licence text
This repo is public and its LICENSE called the source "confidential and
proprietary ... All rights reserved". Public visibility contradicts
confidentiality, and HIP-0130 puts `iam` in the OSS core tier. The declaration
now matches both.

LICENSE-APACHE and LICENSE-MIT carry the canonical texts, unedited —
LICENSE-APACHE is byte-identical to apache.org/licenses/LICENSE-2.0.txt
(sha256 cfc7749b…, 11358 bytes, blob d645695). LICENSE declares the pair.
LICENSE-MIT carries the copyright line the notice requires.

249 Go files led with `// Copyright 2026 Hanzo AI, Inc. All rights reserved.`
— the classic proprietary reservation, and a per-file contradiction of the new
grant. Each now carries `// SPDX-License-Identifier: MIT OR Apache-2.0`. The
word "confidential" survives untouched in the OIDC sense (confidential client),
which is protocol vocabulary, not a licence claim.

There is nothing else to declare: `go.mod` has no licence field and this repo
ships no Cargo/npm/PyPI manifest.

Relicensing is ours alone to do. The tree is original work, not a fork:
`fork: false` with no parent, its own root commit, and no Casdoor-lineage tag
is an ancestor of `main`. The retired Casdoor fork is `hanzoai/iam-v1`; the
provenance note in LICENSE names it, restoring the vendor name a clean-room
assertion needs in order to say what it is clean of.

Build, vet and all 29 test packages green.

Co-authored-by: Hanzo Dev <[email protected]>
2026-08-04 01:09:51 -07:00

98 lines
4.1 KiB
Go

// Copyright 2026 Hanzo AI, Inc.
// SPDX-License-Identifier: MIT OR Apache-2.0
package oidc
import (
"context"
"errors"
"github.com/golang-jwt/jwt/v5"
"github.com/hanzoai/orm"
"github.com/hanzoai/iam/pkg/store"
)
// Token verification is a pure reduction of a signed value: read the `kid`,
// resolve the matching signing Cert, check the signature under an explicit
// algorithm allowlist (never alg:none, never an unexpected method), and validate
// the standard time claims. Every protected route reduces a bearer the same way,
// so a token is trusted for exactly what it cryptographically is — no more.
// acceptedAlgs is the closed set of signing algorithms a bearer may carry. It
// mirrors the JWKS: the classical interop algorithms plus post-quantum ML-DSA.
// alg:none and any HMAC family are absent, so a forged header cannot select a
// verification path that trusts attacker-controlled material.
var acceptedAlgs = []string{"RS256", "RS512", "ES256", "ES384", "ES512", algMLDSA65}
// VerifyToken is the exported bearer-verification primitive the authz layer
// reuses to gate the CRUD surface: it is verifyToken, so a bearer presented to a
// protected route is trusted under the exact same closed algorithm allowlist,
// trusted signing-cert kid resolution, and time validation as every OIDC route.
// One verification path, one trust model — no second, weaker check.
func VerifyToken(ctx context.Context, db orm.DB, tokenStr string) (*Claims, error) {
return verifyToken(ctx, db, tokenStr)
}
// verifyToken parses tokenStr, verifies its signature against the Cert named by
// the token's kid, and returns the validated claims. It fails closed on an
// unknown kid, a disallowed algorithm, a bad signature, or an expired token.
func verifyToken(ctx context.Context, db orm.DB, tokenStr string) (*Claims, error) {
return parseToken(ctx, db, tokenStr)
}
// verifyHint verifies an id_token_hint: the SAME signature check, the SAME
// closed algorithm allowlist and the SAME trusted-kid resolution as any bearer,
// with the expiry deliberately not enforced.
//
// A hint is not a credential and grants nothing — it only NARROWS what the
// caller may receive, by naming the person the client believes is signed in. And
// it is expired by construction: a relying party sends its LAST id_token to ask
// "is this same person still signed in?", which is a question one asks precisely
// when the token has run out (OIDC Core §3.1.2.1 asks authorization servers to
// accept it anyway). Refusing the expired hint would not be strict, it would be
// backwards: the hint would be dropped, the check it exists to perform would not
// run, and the client would silently receive a grant for whoever else happened
// to be signed in on that browser.
//
// The signature is still mandatory, so nobody can invent a hint.
func verifyHint(ctx context.Context, db orm.DB, tokenStr string) (*Claims, error) {
return parseToken(ctx, db, tokenStr, jwt.WithoutClaimsValidation())
}
// parseToken is the one verification path: trusted-kid key resolution, a closed
// algorithm allowlist, and the caller's choice of claim validation. The only
// thing any caller may vary is `extra` — never the key lookup, never the
// allowlist.
func parseToken(ctx context.Context, db orm.DB, tokenStr string, extra ...jwt.ParserOption) (*Claims, error) {
claims := &Claims{}
keyFunc := func(t *jwt.Token) (any, error) {
kid, _ := t.Header["kid"].(string)
if kid == "" {
return nil, errors.New("verify: token has no kid")
}
// Resolve the kid ONLY among trusted platform signing certs, so a token
// signed by a tenant-created cert with a colliding name never verifies.
cert, err := store.GetSigningCert(ctx, db, kid)
if err != nil {
return nil, err
}
if cert == nil {
return nil, errors.New("verify: unknown signing key")
}
pub, _, _, err := certPublicKey(cert)
if err != nil {
return nil, err
}
return pub, nil
}
opts := append([]jwt.ParserOption{
jwt.WithValidMethods(acceptedAlgs),
jwt.WithTimeFunc(nowFunc),
}, extra...)
if _, err := jwt.ParseWithClaims(tokenStr, claims, keyFunc, opts...); err != nil {
return nil, err
}
return claims, nil
}