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

91 lines
3.3 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 {
verifier *Verifier
oauth oauth2.Config
audience string
}
// NewFlow discovers the provider and builds the browser flow. 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(ctx context.Context, cfg Config, clientID, clientSecret, redirectURL string) (*Flow, error) {
issuer := strings.TrimSpace(cfg.Issuer)
audience := strings.TrimSpace(cfg.Audience)
if issuer == "" {
return nil, fmt.Errorf("oidc: issuer is required")
}
if audience == "" {
return nil, fmt.Errorf("oidc: audience is required")
}
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")
}
provider, err := gooidc.NewProvider(ctx, issuer)
if err != nil {
return nil, fmt.Errorf("oidc: discover %q: %w", issuer, err)
}
return &Flow{
verifier: &Verifier{verifier: provider.Verifier(&gooidc.Config{ClientID: audience})},
oauth: oauth2.Config{
ClientID: clientID,
ClientSecret: clientSecret,
Endpoint: provider.Endpoint(),
RedirectURL: 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"},
},
audience: audience,
}, nil
}
// AuthCodeURL returns the provider URL to send a browser to, with the
// audience requested, and the PKCE verifier.
func (f *Flow) AuthCodeURL(state string) (string, string) {
codeVerifier := oauth2.GenerateVerifier()
url := f.oauth.AuthCodeURL(state,
oauth2.S256ChallengeOption(codeVerifier),
oauth2.SetAuthURLParam("audience", f.audience),
)
return url, codeVerifier
}
// Exchange completes the flow: it trades the code for the provider's tokens and
// verifies the access token through the same Verifier a presented token goes
// through. There is no second verification path here, so a token obtained by the
// browser cannot be accepted on terms a presented token would be refused on.
func (f *Flow) Exchange(ctx context.Context, code, codeVerifier string) (identity.ProviderSession, error) {
token, err := f.oauth.Exchange(ctx, strings.TrimSpace(code), oauth2.VerifierOption(codeVerifier))
if err != nil {
return identity.ProviderSession{}, fmt.Errorf("oidc: exchange code: %w", err)
}
credential, err := f.verifier.Verify(ctx, token.AccessToken)
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: token.AccessToken, Expires: expires}, nil
}