Files
summercms/modules/wristband/oidc_consent.go
Jakub Zych 659b42aaa8 feat(19.4-01): OIDC flavour of wristband with an end-to-end tracer
- NewOIDCServer/SetOIDCBackend build a second Server on the same engine;
  Authorize and Token dispatch by flavour, Metadata/Register 404 on OIDC
- OIDCBackend/OIDCTx, KeyStore and OIDCHost seams; MCP Backend untouched
- Discovery (PHP field order), JWKS (stdlib JWK encoding), authorize with
  resume and one-shot consent token, token with an RS256 id_token signed
  through golang-jwt map claims, userinfo
- TestOIDCTracerSmoke verifies the id_token against the served JWKS
2026-10-19 18:18:05 +02:00

128 lines
3.9 KiB
Go

// Consent for the OpenID Connect flavour: the one-shot consent token
// (PHP ConsentCsrf) and the decision logic shared by the HTML handlers and
// the JSON operations a host's own consent page uses.
package wristband
import (
"context"
"strconv"
)
// consentDigest binds a consent token to one user: SHA-256 hex of
// token + "|" + user ID, byte-compatible with PHP ConsentCsrf::bind, so a
// digest copied from the PHP tables stays valid (T-19.4-04).
func consentDigest(token string, userID uint) string {
return sha256Hex(token + "|" + strconv.FormatUint(uint64(userID), 10))
}
// prepareConsent mints a new consent token for the request (replacing any
// stored digest) and builds the consent view listing the scopes the user
// has not consented to yet; openid is never listed.
func (s *Server) prepareConsent(ctx context.Context, tx OIDCTx, req *AuthRequestRecord, client *OIDCClientRecord, userID uint, consent *ConsentRecord) (ConsentView, error) {
showing := req.Scopes
if consent != nil {
showing = missingScopes(req.Scopes, consent.Scopes)
}
listed := []string{}
for _, sc := range showing {
if sc != "openid" && !stringSliceContains(listed, sc) {
listed = append(listed, sc)
}
}
token, err := s.randomBytes(32)
if err != nil {
return ConsentView{}, err
}
digest := consentDigest(token, userID)
if err := tx.SetConsentTokenHash(ctx, req.ID, &digest); err != nil {
return ConsentView{}, err
}
return ConsentView{
RequestID: req.RequestID,
ConsentToken: token,
ClientID: client.ClientID,
ClientName: client.Name,
Scopes: listed,
UserID: userID,
RedirectHost: redirectHost(req.RedirectURI),
Incremental: consent != nil,
}, nil
}
// decideConsent verifies and burns the consent token, then denies
// (consume, access_denied redirect) or allows (consume first, merge the
// consent, mint the code) in one OIDC transaction. It returns the redirect
// URL, or an errAuthorizeRender carrying the code to render. The token is
// cleared even when a later check fails: the transaction commits whenever
// the answer is a protocol error.
func (s *Server) decideConsent(ctx context.Context, requestID string, userID uint, token string, allow bool) (string, error) {
if requestID == "" {
return "", errAuthorizeRender(AuthorizeErrorExpiredRequest)
}
var target string
var failure errAuthorizeRender
err := s.oidcBackend.WithinOIDCTx(ctx, func(tx OIDCTx) error {
req, err := tx.AuthRequestByRequestIDForUpdate(ctx, requestID)
if err != nil {
return err
}
if !s.authRequestUsable(req) {
failure = AuthorizeErrorExpiredRequest
return nil
}
if req.ConsentTokenHash == nil || *req.ConsentTokenHash == "" || token == "" ||
!constantEqual(*req.ConsentTokenHash, consentDigest(token, userID)) {
failure = AuthorizeErrorExpiredRequest
return nil
}
if err := tx.SetConsentTokenHash(ctx, req.ID, nil); err != nil {
return err
}
client, code, err := s.usableClient(ctx, tx, req.ClientID)
if err != nil {
return err
}
if code != "" {
failure = code
return nil
}
now := s.now()
if !allow {
if _, err := tx.ConsumeAuthRequest(ctx, req.ID, now); err != nil {
return err
}
state := ""
if req.State != nil {
state = *req.State
}
target = errorRedirectURL(req.RedirectURI, "access_denied", "", state)
return nil
}
granted := intersectScopes(req.Scopes, client.Scopes, s.oidcSupportedScopes())
consumed, err := tx.ConsumeAuthRequest(ctx, req.ID, now)
if err != nil {
return err
}
if !consumed {
failure = AuthorizeErrorExpiredRequest
return nil
}
if err := tx.SaveConsent(ctx, userID, client.ClientID, granted, now); err != nil {
return err
}
raw, err := s.mintCode(ctx, tx, req, userID, granted)
if err != nil {
return err
}
target = codeRedirect(req, raw)
return nil
})
if err != nil {
return "", err
}
if failure != "" {
return "", failure
}
return target, nil
}