Files
archie/internal/infrastructure/oidc/flow.go
T

110 lines
3.9 KiB
Go

package oidc
import (
"context"
"fmt"
"strings"
gooidc "github.com/coreos/go-oidc/v3/oidc"
"golang.org/x/oauth2"
"github.com/samcharles93/archie-core/internal/domain/identity"
)
// Flow runs the authorization code flow with PKCE, so a person signs in at the
// provider and the dashboard learns who they are without archie ever seeing a
// password, holding a session, or writing any of the protocol itself: discovery,
// the code exchange, PKCE and token verification are the libraries'.
type Flow struct {
provider *Provider
clientID string
clientSecret string
redirectURL string
}
// NewFlow builds the browser flow over provider. ClientID and the secret are
// the provider's registration for this dashboard; the secret is read from
// wherever the operator keeps it and is never logged or stored.
func NewFlow(provider *Provider, clientID, clientSecret, redirectURL string) (*Flow, error) {
if strings.TrimSpace(clientID) == "" {
return nil, fmt.Errorf("oidc: client id is required for the browser flow")
}
if strings.TrimSpace(redirectURL) == "" {
return nil, fmt.Errorf("oidc: redirect URL is required for the browser flow")
}
return &Flow{
provider: provider,
clientID: clientID, clientSecret: clientSecret, redirectURL: redirectURL,
}, nil
}
func (f *Flow) 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,
ClientSecret: f.clientSecret,
Endpoint: discovered.Endpoint(),
RedirectURL: f.redirectURL,
// openid is what makes the provider return a subject at all; profile
// and email are what make a name available for a 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 *Flow) 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 completes the flow: it trades the code for the provider's tokens and
// verifies the ID token against the browser client and binds its nonce to the
// PKCE verifier. Access tokens are not proof of a browser login.
func (f *Flow) Exchange(ctx context.Context, code, codeVerifier string) (identity.ProviderSession, error) {
config, err := f.oauth(ctx)
if err != nil {
return identity.ProviderSession{}, err
}
token, err := config.Exchange(ctx, strings.TrimSpace(code), oauth2.VerifierOption(codeVerifier))
if err != nil {
return identity.ProviderSession{}, fmt.Errorf("oidc: exchange code: %w", err)
}
rawIDToken, ok := token.Extra("id_token").(string)
if !ok || rawIDToken == "" {
return identity.ProviderSession{}, fmt.Errorf("oidc: provider returned no ID token")
}
discovered, err := f.provider.get(ctx)
if err != nil {
return identity.ProviderSession{}, err
}
verified, err := discovered.Verifier(&gooidc.Config{ClientID: f.clientID}).Verify(ctx, rawIDToken)
if err != nil {
return identity.ProviderSession{}, fmt.Errorf("oidc: verify ID token: %w", err)
}
if verified.Nonce != oauth2.S256ChallengeFromVerifier(codeVerifier) {
return identity.ProviderSession{}, fmt.Errorf("oidc: ID token nonce mismatch")
}
credential, err := NewVerifierForAudience(f.provider, f.clientID).Verify(ctx, rawIDToken)
if err != nil {
return identity.ProviderSession{}, err
}
expires := token.Expiry
if credential.Expires.Before(expires) || expires.IsZero() {
expires = credential.Expires
}
return identity.ProviderSession{Credential: credential, Token: rawIDToken, Expires: expires}, nil
}