The binary serves the account routes the Vue client already calls: status,
setup, password and passkey sign-in, recovery, sign-out, OIDC, account
passkeys, password and recovery codes, personal tokens and capabilities.
The API authenticates a session cookie, a personal token or the bootstrap
admin token, and unsafe requests pass cross-origin protection. The
dashboard serves the Vue app at /.
Copied from internal/webui/{accounts,passkeys,personal_tokens,
api_capabilities,auth}.go, internal/infrastructure/oidc/{flow,oidc}.go and
internal/app/archieui/account_oidc.go, which archie-ui still imports.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
199 lines
6.6 KiB
Go
199 lines
6.6 KiB
Go
package governance
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
|
|
gooidc "github.com/coreos/go-oidc/v3/oidc"
|
|
"golang.org/x/oauth2"
|
|
)
|
|
|
|
// oidcCallbackPath must match the redirect URI registered at the provider:
|
|
// the provider returns the browser to the path it was given.
|
|
const oidcCallbackPath = "/oauth2/callback"
|
|
|
|
// oidcLogin runs the browser sign-in against the provider the authentication
|
|
// settings name. It keeps the provider's discovery and signing-key cache
|
|
// until the settings change, so a change applies without a restart.
|
|
type oidcLogin struct {
|
|
mu sync.Mutex
|
|
current AuthenticationSettings
|
|
provider *oidcProvider
|
|
}
|
|
|
|
// flow returns the authorization code flow for the saved settings, or
|
|
// ErrNoCredential when no provider is configured.
|
|
func (o *oidcLogin) flow(ctx context.Context, settings AuthenticationSettingsStore) (*oidcFlow, error) {
|
|
value, err := settings.AuthenticationSettings(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if value.PublicURL == "" || value.OIDCIssuer == "" {
|
|
return nil, ErrNoCredential
|
|
}
|
|
o.mu.Lock()
|
|
defer o.mu.Unlock()
|
|
if o.provider == nil || o.current != value {
|
|
provider, err := newOIDCProvider(value.OIDCIssuer, value.OIDCAudience)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
o.provider, o.current = provider, value
|
|
}
|
|
return newOIDCFlow(o.provider, value.OIDCClientID, value.PublicURL+oidcCallbackPath)
|
|
}
|
|
|
|
// oidcProvider discovers the identity provider on first use and keeps the
|
|
// result. A provider that is down does not stop the binary: discovery is
|
|
// retried on the next sign-in.
|
|
type oidcProvider struct {
|
|
issuer string
|
|
audience string
|
|
|
|
mu sync.Mutex
|
|
discovered *gooidc.Provider
|
|
}
|
|
|
|
func newOIDCProvider(issuer, audience string) (*oidcProvider, error) {
|
|
issuer, audience = strings.TrimSpace(issuer), strings.TrimSpace(audience)
|
|
if issuer == "" {
|
|
return nil, fmt.Errorf("oidc: issuer is required")
|
|
}
|
|
// Without an audience a token the provider minted for a different
|
|
// service would authenticate here.
|
|
if audience == "" {
|
|
return nil, fmt.Errorf("oidc: audience is required")
|
|
}
|
|
return &oidcProvider{issuer: issuer, audience: audience}, nil
|
|
}
|
|
|
|
func (p *oidcProvider) get(ctx context.Context) (*gooidc.Provider, error) {
|
|
p.mu.Lock()
|
|
defer p.mu.Unlock()
|
|
if p.discovered != nil {
|
|
return p.discovered, nil
|
|
}
|
|
discovered, err := gooidc.NewProvider(ctx, p.issuer)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%w: discover %q: %w", ErrProviderUnavailable, p.issuer, err)
|
|
}
|
|
p.discovered = discovered
|
|
return discovered, nil
|
|
}
|
|
|
|
// oidcFlow runs the authorization code flow with PKCE. Discovery, the code
|
|
// exchange, PKCE and token verification are the libraries'.
|
|
type oidcFlow struct {
|
|
provider *oidcProvider
|
|
clientID string
|
|
redirectURL string
|
|
}
|
|
|
|
func newOIDCFlow(provider *oidcProvider, clientID, redirectURL string) (*oidcFlow, error) {
|
|
if strings.TrimSpace(clientID) == "" {
|
|
return nil, fmt.Errorf("oidc: client id is required for the browser flow")
|
|
}
|
|
return &oidcFlow{provider: provider, clientID: clientID, redirectURL: redirectURL}, nil
|
|
}
|
|
|
|
func (f *oidcFlow) oauth(ctx context.Context) (oauth2.Config, error) {
|
|
discovered, err := f.provider.get(ctx)
|
|
if err != nil {
|
|
return oauth2.Config{}, err
|
|
}
|
|
return oauth2.Config{
|
|
ClientID: f.clientID,
|
|
Endpoint: discovered.Endpoint(),
|
|
RedirectURL: f.redirectURL,
|
|
// openid makes the provider return a subject at all; profile and
|
|
// email make a name available for display.
|
|
Scopes: []string{gooidc.ScopeOpenID, "profile", "email"},
|
|
}, nil
|
|
}
|
|
|
|
// AuthCodeURL returns the provider URL to send a browser to, with the
|
|
// audience requested, and the PKCE verifier.
|
|
func (f *oidcFlow) AuthCodeURL(ctx context.Context, state string) (string, string, error) {
|
|
config, err := f.oauth(ctx)
|
|
if err != nil {
|
|
return "", "", err
|
|
}
|
|
codeVerifier := oauth2.GenerateVerifier()
|
|
url := config.AuthCodeURL(state,
|
|
oauth2.S256ChallengeOption(codeVerifier),
|
|
oauth2.SetAuthURLParam("nonce", oauth2.S256ChallengeFromVerifier(codeVerifier)),
|
|
oauth2.SetAuthURLParam("audience", f.provider.audience),
|
|
)
|
|
return url, codeVerifier, nil
|
|
}
|
|
|
|
// Exchange trades the code for the provider's tokens, verifies the ID token
|
|
// against the browser client and binds its nonce to the PKCE verifier. An
|
|
// access token is not proof of a browser sign-in.
|
|
func (f *oidcFlow) Exchange(ctx context.Context, code, codeVerifier string) (ProviderSession, error) {
|
|
config, err := f.oauth(ctx)
|
|
if err != nil {
|
|
return ProviderSession{}, err
|
|
}
|
|
token, err := config.Exchange(ctx, strings.TrimSpace(code), oauth2.VerifierOption(codeVerifier))
|
|
if err != nil {
|
|
return ProviderSession{}, fmt.Errorf("%w: exchange code: %w", ErrCredentialRejected, err)
|
|
}
|
|
rawIDToken, ok := token.Extra("id_token").(string)
|
|
if !ok || rawIDToken == "" {
|
|
return ProviderSession{}, fmt.Errorf("%w: provider returned no ID token", ErrCredentialRejected)
|
|
}
|
|
discovered, err := f.provider.get(ctx)
|
|
if err != nil {
|
|
return ProviderSession{}, err
|
|
}
|
|
verified, err := discovered.Verifier(&gooidc.Config{ClientID: f.clientID}).Verify(ctx, rawIDToken)
|
|
if err != nil {
|
|
return ProviderSession{}, fmt.Errorf("%w: verify ID token: %w", ErrCredentialRejected, err)
|
|
}
|
|
if verified.Nonce != oauth2.S256ChallengeFromVerifier(codeVerifier) {
|
|
return ProviderSession{}, fmt.Errorf("%w: ID token nonce mismatch", ErrCredentialRejected)
|
|
}
|
|
credential, err := credentialOf(verified)
|
|
if err != nil {
|
|
return ProviderSession{}, err
|
|
}
|
|
expires := token.Expiry
|
|
if credential.Expires.Before(expires) || expires.IsZero() {
|
|
expires = credential.Expires
|
|
}
|
|
return ProviderSession{Credential: credential, Token: rawIDToken, Expires: expires}, nil
|
|
}
|
|
|
|
// credentialOf reads what a verified token proved. Claims are read only after
|
|
// verification: an unverified token selects nothing.
|
|
func credentialOf(token *gooidc.IDToken) (Credential, error) {
|
|
var claims struct {
|
|
Subject string `json:"sub"`
|
|
ClientID string `json:"client_id"`
|
|
Scope string `json:"scope"`
|
|
Scopes []string `json:"scp"`
|
|
Email string `json:"email"`
|
|
Verified bool `json:"email_verified"`
|
|
}
|
|
_ = token.Claims(&claims)
|
|
subject := Subject{Issuer: token.Issuer, Subject: strings.TrimSpace(claims.Subject)}
|
|
if subject.Subject == "" {
|
|
subject.Subject = strings.TrimSpace(claims.ClientID)
|
|
}
|
|
if subject.Subject == "" {
|
|
return Credential{}, fmt.Errorf("%w: token asserts neither sub nor client_id", ErrCredentialRejected)
|
|
}
|
|
email := ""
|
|
if claims.Verified {
|
|
email = strings.TrimSpace(claims.Email)
|
|
}
|
|
scopes := claims.Scopes
|
|
if len(scopes) == 0 {
|
|
scopes = strings.Fields(claims.Scope)
|
|
}
|
|
return Credential{Subject: subject, Email: email, Scopes: scopes, Expires: token.Expiry}, nil
|
|
}
|