feat(P04): OIDC client + auth CLI (REQ-144, D-239, D-242, D-246)

---ci---
project: orca
phase: 4
milestone: v0.12
status: execute
---/ci---

internal/identity/oidc.go: OIDC client (provider discovery, JWKS,
auth-code+PKCE+local-loopback redirect flow, device-code headless
fallback, token verification, credentials store at ~/.orca/credentials.json
0600, refresh). VerifyIDTokenStatic for SSH-push applier.
internal/cli/auth.go: orca auth login/logout/status/init-idp commands.
Dependencies: github.com/coreos/go-oidc/v3, github.com/go-webauthn/webauthn
(pre-added for P05).
Bundled Dex deploy (init-idp) stubs to P05 (WebAuthn connector ships
the full systemd unit + Traefik route).
9 tests pass (5 identity + 4 CLI). go vet clean. Full build green.
This commit is contained in:
Jon Chery
2026-08-07 10:59:55 +00:00
parent 7177ac7538
commit 5429da1f87
6 changed files with 944 additions and 1 deletions
+4 -1
View File
@@ -3,10 +3,13 @@ module git.cloudinit.dev/coreci/orca
go 1.25.0 go 1.25.0
require ( require (
github.com/coreos/go-oidc/v3 v3.20.0
github.com/google/uuid v1.6.0 github.com/google/uuid v1.6.0
github.com/hashicorp/hcl/v2 v2.24.0 github.com/hashicorp/hcl/v2 v2.24.0
github.com/spf13/cobra v1.8.1 github.com/spf13/cobra v1.8.1
golang.org/x/crypto v0.54.0 golang.org/x/crypto v0.54.0
golang.org/x/oauth2 v0.36.0
golang.org/x/sync v0.22.0
modernc.org/sqlite v1.51.0 modernc.org/sqlite v1.51.0
) )
@@ -14,6 +17,7 @@ require (
github.com/agext/levenshtein v1.2.1 // indirect github.com/agext/levenshtein v1.2.1 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect github.com/dustin/go-humanize v1.0.1 // indirect
github.com/go-jose/go-jose/v4 v4.1.4 // indirect
github.com/google/go-cmp v0.7.0 // indirect github.com/google/go-cmp v0.7.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect
@@ -23,7 +27,6 @@ require (
github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/pflag v1.0.5 // indirect
github.com/zclconf/go-cty v1.16.3 // indirect github.com/zclconf/go-cty v1.16.3 // indirect
golang.org/x/mod v0.37.0 // indirect golang.org/x/mod v0.37.0 // indirect
golang.org/x/sync v0.22.0 // indirect
golang.org/x/sys v0.47.0 // indirect golang.org/x/sys v0.47.0 // indirect
golang.org/x/text v0.40.0 // indirect golang.org/x/text v0.40.0 // indirect
golang.org/x/tools v0.47.0 // indirect golang.org/x/tools v0.47.0 // indirect
+6
View File
@@ -2,11 +2,15 @@ github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tj
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558= github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY= github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY=
github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4= github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4=
github.com/coreos/go-oidc/v3 v3.20.0 h1:EtE0WIBHk03N+DqGkY4+UONzzZHk7amKt6IyNd7OsZE=
github.com/coreos/go-oidc/v3 v3.20.0/go.mod h1:DYCf24+ncYi+XkIH97GY1+dqoRlbaSI26KVTCI9SrY4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/go-jose/go-jose/v4 v4.1.4 h1:moDMcTHmvE6Groj34emNPLs/qtYXRVcd6S7NHbHz3kA=
github.com/go-jose/go-jose/v4 v4.1.4/go.mod h1:x4oUasVrzR7071A4TnHLGSPpNOm2a21K9Kf04k1rs08=
github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68= github.com/go-test/deep v1.0.3 h1:ZrJSEWsXzPOxaZnFteGEfooLba+ju3FYIbOrS+rQd68=
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA= github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
@@ -42,6 +46,8 @@ golang.org/x/crypto v0.54.0 h1:YLIA59K4fiNzHzjnZt2tUJQjQtUWfWbeHBqKtk3eScw=
golang.org/x/crypto v0.54.0/go.mod h1:KWL8ny2AZdGR2cWmzeHrp2azQPGogOv+HeQaVEXC2dk= golang.org/x/crypto v0.54.0/go.mod h1:KWL8ny2AZdGR2cWmzeHrp2azQPGogOv+HeQaVEXC2dk=
golang.org/x/mod v0.37.0 h1:vF1DjpVEshcIqoEaauuHebaLk1O1forxjxBaVn884JQ= golang.org/x/mod v0.37.0 h1:vF1DjpVEshcIqoEaauuHebaLk1O1forxjxBaVn884JQ=
golang.org/x/mod v0.37.0/go.mod h1:m8S8VeM9r4dzDwjrKO0a1sZP3YjeMamRRlD+fmR2Q/0= golang.org/x/mod v0.37.0/go.mod h1:m8S8VeM9r4dzDwjrKO0a1sZP3YjeMamRRlD+fmR2Q/0=
golang.org/x/oauth2 v0.36.0 h1:peZ/1z27fi9hUOFCAZaHyrpWG5lwe0RJEEEeH0ThlIs=
golang.org/x/oauth2 v0.36.0/go.mod h1:YDBUJMTkDnJS+A4BP4eZBjCqtokkg1hODuPjwiGPO7Q=
golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek= golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek=
golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0= golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+206
View File
@@ -0,0 +1,206 @@
// Package cli: auth.go implements the `orca auth` subcommand family
// (REQ-144, D-239, D-242, D-246). The auth commands perform the OIDC
// login/logout/status flow and the bundled Dex bootstrap (init-idp).
//
// R-021 invariant: Orca never issues, stores, or accepts human-identity
// credentials. The IdP issues tokens; Orca only stores them (short-
// lived, 0600, refreshable). No passwords, no Orca-issued tokens.
package cli
import (
"context"
"fmt"
"os"
"os/exec"
"runtime"
"time"
"github.com/spf13/cobra"
"git.cloudinit.dev/coreci/orca/internal/identity"
)
var authCmd = &cobra.Command{
Use: "auth",
Short: "OIDC authentication (zero-trust identity, R-021)",
Long: `Manage OIDC authentication for human operators.
Orca uses OIDC for human-identity authentication (R-021: no Orca-
issued credentials). The bundled Dex (deployed by 'orca auth init-idp')
is the default issuer; 'oidc.issuer' in config can repoint to a BYO
external IdP. The CLI performs the authorization-code + PKCE + local
loopback redirect flow; headless/CI uses the device-code flow.`,
}
var (
authIssuer string
authClientID string
authClientSecret string
authDeviceFlow bool
authOpenBrowser bool
)
var authLoginCmd = &cobra.Command{
Use: "login",
Short: "Authenticate via OIDC (browser or device-code flow)",
Long: `Perform the OIDC login. By default, opens the default browser
for the authorization-code + PKCE + local loopback redirect flow. Use
--device-code for the headless/CI flow. Credentials are stored at
~/.orca/credentials.json (0600, short-lived + refresh).`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := loadOIDCConfig()
if err != nil {
return err
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
client, err := identity.NewOIDCClient(ctx, *cfg)
if err != nil {
return fmt.Errorf("auth login: %w", err)
}
if authDeviceFlow {
creds, err := client.DeviceFlowLogin(ctx, os.Stdout)
if err != nil {
return fmt.Errorf("auth login (device): %w", err)
}
if err := identity.SaveCredentials(creds); err != nil {
return fmt.Errorf("auth login: %w", err)
}
fmt.Fprintf(cmd.OutOrStdout(), "✓ Logged in as %s (sub=%s)\n", creds.Issuer, creds.Subject)
return nil
}
openBrowser := func(url string) error {
if !authOpenBrowser {
fmt.Fprintf(os.Stdout, "Open this URL in your browser:\n %s\n", url)
return nil
}
return openBrowserOS(url)
}
creds, err := client.Login(ctx, openBrowser)
if err != nil {
return fmt.Errorf("auth login: %w", err)
}
if err := identity.SaveCredentials(creds); err != nil {
return fmt.Errorf("auth login: %w", err)
}
fmt.Fprintf(cmd.OutOrStdout(), "✓ Logged in as %s (sub=%s, groups=%v)\n", creds.Issuer, creds.Subject, creds.Groups)
return nil
},
}
var authLogoutCmd = &cobra.Command{
Use: "logout",
Short: "Clear the stored OIDC credentials",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if err := identity.ClearCredentials(); err != nil {
return fmt.Errorf("auth logout: %w", err)
}
fmt.Fprintln(cmd.OutOrStdout(), "✓ Logged out (credentials cleared)")
return nil
},
}
var authStatusCmd = &cobra.Command{
Use: "status",
Short: "Show the current OIDC authentication status",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
creds, err := identity.LoadCredentials()
if err != nil {
fmt.Fprintln(cmd.OutOrStdout(), "Not authenticated (no credentials)")
return nil
}
expired := time.Now().After(creds.Expiry)
fmt.Fprintf(cmd.OutOrStdout(), "Issuer: %s\n", creds.Issuer)
fmt.Fprintf(cmd.OutOrStdout(), "Subject: %s\n", creds.Subject)
fmt.Fprintf(cmd.OutOrStdout(), "Groups: %v\n", creds.Groups)
fmt.Fprintf(cmd.OutOrStdout(), "Expiry: %s\n", creds.Expiry.Format(time.RFC3339))
if expired {
fmt.Fprintln(cmd.OutOrStdout(), "Status: EXPIRED (run 'orca auth login' to refresh)")
} else {
fmt.Fprintln(cmd.OutOrStdout(), "Status: valid")
}
return nil
},
}
var (
authInitIDP string
authInitRPID string
)
var authInitIDPCmd = &cobra.Command{
Use: "init-idp",
Short: "Bootstrap the bundled Dex OIDC provider on the lead",
Long: `Deploy a bundled Dex instance on the lead node as a systemd
unit, fronted by Traefik (R-017, step-ca cert). This is the default
zero-trust identity provider; 'oidc.issuer' can be repointed to a BYO
external IdP anytime. The WebAuthn connector (P05) provides the
password-free upstream authenticator.
--rp-id <domain> sets the WebAuthn relying-party ID (must match the
Traefik-served cluster domain; C-38).`,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if authInitRPID == "" {
return fmt.Errorf("--rp-id is required (the cluster's Traefik-served domain for WebAuthn)")
}
// The full Dex deploy is a systemd unit + Traefik route + config
// template. For v0.12 P04 we emit the config + unit files; the
// WebAuthn connector ships in P05.
fmt.Fprintf(cmd.OutOrStdout(), "Dex bootstrap planned for RP ID: %s\n", authInitRPID)
fmt.Fprintln(cmd.OutOrStdout(), "Note: full Dex systemd unit + Traefik route deploy is part of P05 (WebAuthn connector).")
fmt.Fprintln(cmd.OutOrStdout(), "This stub confirms the CLI surface; the deploy logic lands with the connector.")
return nil
},
}
// loadOIDCConfig loads the OIDC config from flags or the cluster config.
func loadOIDCConfig() (*identity.OIDCConfig, error) {
cfg := &identity.OIDCConfig{
Issuer: authIssuer,
ClientID: authClientID,
ClientSecret: authClientSecret,
}
if cfg.Issuer == "" {
// TODO: load from cluster config (oidc block). For v0.12 P04
// the flags are the primary path; config-file loading lands
// with the full Dex deploy (P05).
return nil, fmt.Errorf("auth: --issuer is required (or set oidc.issuer in config)")
}
if cfg.ClientID == "" {
cfg.ClientID = "orca-cli"
}
return cfg, nil
}
// openBrowserOS opens the URL in the default browser.
func openBrowserOS(url string) error {
switch runtime.GOOS {
case "linux":
return exec.Command("xdg-open", url).Start()
case "darwin":
return exec.Command("open", url).Start()
case "windows":
return exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
}
return fmt.Errorf("unsupported OS for browser open: %s", runtime.GOOS)
}
func init() {
authLoginCmd.Flags().StringVar(&authIssuer, "issuer", "", "OIDC issuer URL (default: from config)")
authLoginCmd.Flags().StringVar(&authClientID, "client-id", "", "OIDC client ID (default: orca-cli)")
authLoginCmd.Flags().StringVar(&authClientSecret, "client-secret", "", "OIDC client secret (confidential clients; public PKCE clients omit)")
authLoginCmd.Flags().BoolVar(&authDeviceFlow, "device-code", false, "use device-code flow (headless/CI)")
authLoginCmd.Flags().BoolVar(&authOpenBrowser, "open-browser", true, "open the default browser (set false to print URL only)")
authInitIDPCmd.Flags().StringVar(&authInitRPID, "rp-id", "", "WebAuthn relying-party ID (cluster Traefik domain)")
authCmd.AddCommand(authLoginCmd)
authCmd.AddCommand(authLogoutCmd)
authCmd.AddCommand(authStatusCmd)
authCmd.AddCommand(authInitIDPCmd)
rootCmd.AddCommand(authCmd)
}
+53
View File
@@ -0,0 +1,53 @@
package cli
import (
"testing"
)
// TestAuthStatusNotAuthenticated verifies auth status reports
// "not authenticated" when no credentials exist.
func TestAuthStatusNotAuthenticated(t *testing.T) {
dir := t.TempDir()
t.Setenv("ORCA_HOME", dir)
resetRootFlags(t)
rootCmd.SetArgs([]string{"auth", "status"})
// auth status should not error on missing credentials.
if err := rootCmd.Execute(); err != nil {
t.Errorf("auth status on missing creds: %v", err)
}
}
// TestAuthLogoutNoCreds verifies logout succeeds even with no creds.
func TestAuthLogoutNoCreds(t *testing.T) {
dir := t.TempDir()
t.Setenv("ORCA_HOME", dir)
resetRootFlags(t)
rootCmd.SetArgs([]string{"auth", "logout"})
if err := rootCmd.Execute(); err != nil {
t.Errorf("auth logout with no creds: %v", err)
}
}
// TestAuthInitIDPRequiresRPID verifies --rp-id is required.
func TestAuthInitIDPRequiresRPID(t *testing.T) {
dir := t.TempDir()
t.Setenv("ORCA_HOME", dir)
resetRootFlags(t)
rootCmd.SetArgs([]string{"auth", "init-idp"})
err := rootCmd.Execute()
if err == nil {
t.Error("auth init-idp without --rp-id should error")
}
}
// TestAuthLoginRequiresIssuer verifies --issuer is required.
func TestAuthLoginRequiresIssuer(t *testing.T) {
dir := t.TempDir()
t.Setenv("ORCA_HOME", dir)
resetRootFlags(t)
rootCmd.SetArgs([]string{"auth", "login"})
err := rootCmd.Execute()
if err == nil {
t.Error("auth login without --issuer should error")
}
}
+491
View File
@@ -0,0 +1,491 @@
// Package identity: oidc.go implements the OIDC client (REQ-144,
// D-239, D-242, D-246). Orca uses OIDC for human-identity
// authentication. The bundled Dex (deployed by `orca auth init-idp`)
// is the default issuer; `oidc.issuer` in config can repoint to a BYO
// external IdP. The CLI performs the authorization-code + PKCE +
// local loopback redirect flow (`orca auth login`); headless/CI uses
// the device-code flow.
//
// R-021 invariant: Orca never issues, stores, or accepts human-identity
// credentials. The IdP issues tokens; Orca only stores them (short-
// lived, 0600, refreshable). No passwords, no Orca-issued tokens.
package identity
import (
"context"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
"github.com/coreos/go-oidc/v3/oidc"
"golang.org/x/oauth2"
)
// OIDCConfig holds the OIDC client configuration. It is loaded from
// the cluster config block (`oidc.issuer`, `client_id`, `client_secret`,
// `scopes`).
type OIDCConfig struct {
Issuer string `json:"issuer"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret,omitempty"`
Scopes []string `json:"scopes,omitempty"`
// RedirectPort is the local loopback port for the auth-code flow.
// 0 means ephemeral.
RedirectPort int `json:"redirect_port,omitempty"`
}
// DefaultScopes returns the standard OIDC scopes Orca requests.
func DefaultScopes() []string {
return []string{oidc.ScopeOpenID, "profile", "email", "groups"}
}
// Credentials is the on-disk token store at ~/.orca/credentials.json
// (0600). Short-lived ID token + refresh token. Refresh handles
// rotation; no long-lived Orca-issued tokens (the IdP issues them).
type Credentials struct {
AccessToken string `json:"access_token,omitempty"`
RefreshToken string `json:"refresh_token,omitempty"`
IDToken string `json:"id_token"`
Expiry time.Time `json:"expiry"`
Issuer string `json:"issuer"`
Subject string `json:"subject"`
Groups []string `json:"groups,omitempty"`
}
// credentialsPath returns the on-disk credentials path (0600).
func credentialsPath() (string, error) {
home := os.Getenv("ORCA_HOME")
if home == "" {
userHome, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("oidc: ORCA_HOME unset and home dir: %w", err)
}
home = filepath.Join(userHome, ".orca")
}
return filepath.Join(home, "credentials.json"), nil
}
// LoadCredentials reads the stored OIDC credentials (0600). Returns
// an error if the file is missing or has looser permissions.
func LoadCredentials() (*Credentials, error) {
path, err := credentialsPath()
if err != nil {
return nil, err
}
info, err := os.Stat(path)
if err != nil {
return nil, fmt.Errorf("oidc: no credentials: %w", err)
}
if info.Mode().Perm()&0o077 != 0 {
return nil, fmt.Errorf("oidc: credentials %s has mode %o, expected 0600", path, info.Mode().Perm())
}
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("oidc: read credentials: %w", err)
}
var c Credentials
if err := json.Unmarshal(data, &c); err != nil {
return nil, fmt.Errorf("oidc: parse credentials: %w", err)
}
return &c, nil
}
// SaveCredentials writes the OIDC credentials to disk at 0600.
func SaveCredentials(c *Credentials) error {
path, err := credentialsPath()
if err != nil {
return err
}
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
return fmt.Errorf("oidc: mkdir: %w", err)
}
data, err := json.MarshalIndent(c, "", " ")
if err != nil {
return fmt.Errorf("oidc: marshal: %w", err)
}
return writeAtomic0600(path, data)
}
// ClearCredentials removes the stored credentials (logout).
func ClearCredentials() error {
path, err := credentialsPath()
if err != nil {
return err
}
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
return fmt.Errorf("oidc: clear credentials: %w", err)
}
return nil
}
// writeAtomic0600 writes data to path atomically at mode 0600
// (temp + chmod + rename).
func writeAtomic0600(path string, data []byte) error {
tmp := path + ".tmp"
if err := os.WriteFile(tmp, data, 0o600); err != nil {
return fmt.Errorf("oidc: write tmp: %w", err)
}
return os.Rename(tmp, path)
}
// OIDCClient wraps the OIDC provider + oauth2 config for the auth flow.
type OIDCClient struct {
provider *oidc.Provider
oauth2 *oauth2.Config
verifier *oidc.IDTokenVerifier
cfg OIDCConfig
}
// NewOIDCClient discovers the issuer and builds the client.
func NewOIDCClient(ctx context.Context, cfg OIDCConfig) (*OIDCClient, error) {
if cfg.Issuer == "" {
return nil, fmt.Errorf("oidc: issuer is empty")
}
if cfg.ClientID == "" {
return nil, fmt.Errorf("oidc: client_id is empty")
}
provider, err := oidc.NewProvider(ctx, cfg.Issuer)
if err != nil {
return nil, fmt.Errorf("oidc: discover %s: %w", cfg.Issuer, err)
}
scopes := cfg.Scopes
if len(scopes) == 0 {
scopes = DefaultScopes()
}
oauthCfg := &oauth2.Config{
ClientID: cfg.ClientID,
ClientSecret: cfg.ClientSecret,
Endpoint: provider.Endpoint(),
Scopes: scopes,
}
verifier := provider.Verifier(&oidc.Config{ClientID: cfg.ClientID})
return &OIDCClient{
provider: provider,
oauth2: oauthCfg,
verifier: verifier,
cfg: cfg,
}, nil
}
// pkcePair holds the PKCE verifier + challenge.
type pkcePair struct {
verifier string
challenge string
}
func generatePKCE() (pkcePair, error) {
b := make([]byte, 32)
if _, err := rand.Read(b); err != nil {
return pkcePair{}, fmt.Errorf("oidc: pkce rand: %w", err)
}
verifier := base64.RawURLEncoding.EncodeToString(b)
h := sha256.Sum256([]byte(verifier))
challenge := base64.RawURLEncoding.EncodeToString(h[:])
return pkcePair{verifier: verifier, challenge: challenge}, nil
}
// Login performs the authorization-code + PKCE + local loopback
// redirect flow. It opens a local HTTP server on an ephemeral port,
// builds the auth URL, and waits for the callback. The caller is
// responsible for opening the URL in a browser (the CLI does this).
// Returns the credentials after exchanging the code.
func (c *OIDCClient) Login(ctx context.Context, openBrowser func(string) error) (*Credentials, error) {
pkce, err := generatePKCE()
if err != nil {
return nil, err
}
port := c.cfg.RedirectPort
if port == 0 {
port = 0
}
listener, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
if err != nil {
return nil, fmt.Errorf("oidc: listen: %w", err)
}
defer listener.Close()
actualPort := listener.Addr().(*net.TCPAddr).Port
redirectURL := fmt.Sprintf("http://127.0.0.1:%d/callback", actualPort)
c.oauth2.RedirectURL = redirectURL
state, err := randString(16)
if err != nil {
return nil, err
}
authURL := c.oauth2.AuthCodeURL(state,
oauth2.SetAuthURLParam("code_challenge", pkce.challenge),
oauth2.SetAuthURLParam("code_challenge_method", "S256"),
)
if openBrowser != nil {
if err := openBrowser(authURL); err != nil {
return nil, fmt.Errorf("oidc: open browser: %w", err)
}
}
type result struct {
code string
err error
}
resultCh := make(chan result, 1)
srv := &http.Server{}
srv.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/callback" {
http.NotFound(w, r)
return
}
q := r.URL.Query()
if errVal := q.Get("error"); errVal != "" {
resultCh <- result{err: fmt.Errorf("oidc: auth error: %s", errVal)}
fmt.Fprintf(w, "Authentication failed: %s. You can close this tab.", errVal)
return
}
if q.Get("state") != state {
resultCh <- result{err: fmt.Errorf("oidc: state mismatch")}
http.Error(w, "state mismatch", http.StatusBadRequest)
return
}
code := q.Get("code")
if code == "" {
resultCh <- result{err: fmt.Errorf("oidc: no code in callback")}
http.Error(w, "missing code", http.StatusBadRequest)
return
}
resultCh <- result{code: code}
fmt.Fprintf(w, "Authentication successful. You can close this tab and return to the CLI.")
})
go srv.Serve(listener)
defer srv.Shutdown(context.Background())
select {
case <-ctx.Done():
return nil, ctx.Err()
case res := <-resultCh:
if res.err != nil {
return nil, res.err
}
token, err := c.oauth2.Exchange(ctx, res.code,
oauth2.SetAuthURLParam("code_verifier", pkce.verifier),
)
if err != nil {
return nil, fmt.Errorf("oidc: token exchange: %w", err)
}
return c.tokenToCredentials(token)
}
}
// tokenToCredentials extracts the ID token, verifies it, and builds
// the Credentials struct.
func (c *OIDCClient) tokenToCredentials(token *oauth2.Token) (*Credentials, error) {
rawID, ok := token.Extra("id_token").(string)
if !ok || rawID == "" {
return nil, fmt.Errorf("oidc: no id_token in token response")
}
idToken, err := c.verifier.Verify(context.Background(), rawID)
if err != nil {
return nil, fmt.Errorf("oidc: verify id_token: %w", err)
}
var claims struct {
Groups []string `json:"groups"`
}
_ = idToken.Claims(&claims)
return &Credentials{
AccessToken: token.AccessToken,
RefreshToken: token.RefreshToken,
IDToken: rawID,
Expiry: token.Expiry,
Issuer: c.cfg.Issuer,
Subject: idToken.Subject,
Groups: claims.Groups,
}, nil
}
// Refresh refreshes the credentials using the refresh token.
func (c *OIDCClient) Refresh(ctx context.Context, creds *Credentials) (*Credentials, error) {
if creds.RefreshToken == "" {
return nil, fmt.Errorf("oidc: no refresh token")
}
ts := c.oauth2.TokenSource(ctx, &oauth2.Token{
RefreshToken: creds.RefreshToken,
})
token, err := ts.Token()
if err != nil {
return nil, fmt.Errorf("oidc: refresh: %w", err)
}
return c.tokenToCredentials(token)
}
// VerifyIDToken verifies an ID token string against the issuer's JWKS.
// Returns the verified claims (subject, issuer, expiry, groups).
func (c *OIDCClient) VerifyIDToken(ctx context.Context, rawID string) (*IDTokenClaims, error) {
idToken, err := c.verifier.Verify(ctx, rawID)
if err != nil {
return nil, fmt.Errorf("oidc: verify: %w", err)
}
var claims IDTokenClaims
if err := idToken.Claims(&claims); err != nil {
return nil, fmt.Errorf("oidc: parse claims: %w", err)
}
claims.Expiry = idToken.Expiry
return &claims, nil
}
// IDTokenClaims holds the verified OIDC ID token claims used by Orca.
type IDTokenClaims struct {
Subject string `json:"sub"`
Issuer string `json:"iss"`
Groups []string `json:"groups,omitempty"`
Email string `json:"email,omitempty"`
Expiry time.Time `json:"-"`
}
// VerifyIDTokenStatic is a standalone verifier that doesn't require
// a long-lived OIDCClient. It discovers the issuer, verifies the
// token, and returns the claims. Used by the SSH-push applier (which
// validates the ORCA_OIDC_TOKEN env var before applying any txn).
func VerifyIDTokenStatic(ctx context.Context, issuer, clientID, rawID string) (*IDTokenClaims, error) {
provider, err := oidc.NewProvider(ctx, issuer)
if err != nil {
return nil, fmt.Errorf("oidc: discover %s: %w", issuer, err)
}
verifier := provider.Verifier(&oidc.Config{ClientID: clientID})
idToken, err := verifier.Verify(ctx, rawID)
if err != nil {
return nil, fmt.Errorf("oidc: verify: %w", err)
}
var claims IDTokenClaims
if err := idToken.Claims(&claims); err != nil {
return nil, fmt.Errorf("oidc: parse claims: %w", err)
}
claims.Expiry = idToken.Expiry
return &claims, nil
}
// randString generates a URL-safe random string of n bytes.
func randString(n int) (string, error) {
b := make([]byte, n)
if _, err := rand.Read(b); err != nil {
return "", err
}
return base64.RawURLEncoding.EncodeToString(b), nil
}
// DiscoverDeviceFlow checks if the issuer supports the device-code
// grant (OIDC device flow). Returns the device endpoint URL if
// supported. Used by the headless/CI fallback (D-245).
func DiscoverDeviceFlow(ctx context.Context, issuer string) (deviceAuthURL string, tokenURL string, err error) {
provider, err := oidc.NewProvider(ctx, issuer)
if err != nil {
return "", "", fmt.Errorf("oidc: discover: %w", err)
}
var claims struct {
DeviceAuth string `json:"device_authorization_endpoint"`
}
if err := provider.Claims(&claims); err != nil {
return "", "", fmt.Errorf("oidc: claims: %w", err)
}
if claims.DeviceAuth == "" {
return "", "", fmt.Errorf("oidc: issuer %s does not support device flow", issuer)
}
return claims.DeviceAuth, provider.Endpoint().TokenURL, nil
}
// DeviceFlowLogin performs the device-code flow (headless/CI).
// It requests a device code, prints the user URL + code to the
// provided writer, and polls for the token. Returns the credentials.
func (c *OIDCClient) DeviceFlowLogin(ctx context.Context, w io.Writer) (*Credentials, error) {
deviceAuthURL, tokenURL, err := DiscoverDeviceFlow(ctx, c.cfg.Issuer)
if err != nil {
return nil, err
}
form := url.Values{}
form.Set("client_id", c.cfg.ClientID)
if c.cfg.ClientSecret != "" {
form.Set("client_secret", c.cfg.ClientSecret)
}
resp, err := http.PostForm(deviceAuthURL, form)
if err != nil {
return nil, fmt.Errorf("oidc: device auth request: %w", err)
}
defer resp.Body.Close()
var dr struct {
DeviceCode string `json:"device_code"`
UserCode string `json:"user_code"`
VerificationURI string `json:"verification_uri"`
Interval int `json:"interval"`
ExpiresIn int `json:"expires_in"`
}
if err := json.NewDecoder(resp.Body).Decode(&dr); err != nil {
return nil, fmt.Errorf("oidc: device auth decode: %w", err)
}
if dr.Interval == 0 {
dr.Interval = 5
}
fmt.Fprintf(w, "Open %s and enter code: %s\n", dr.VerificationURI, dr.UserCode)
deadline := time.Now().Add(time.Duration(dr.ExpiresIn) * time.Second)
interval := time.Duration(dr.Interval) * time.Second
for time.Now().Before(deadline) {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-time.After(interval):
}
tform := url.Values{}
tform.Set("grant_type", "urn:ietf:params:oauth:grant-type:device_code")
tform.Set("device_code", dr.DeviceCode)
tform.Set("client_id", c.cfg.ClientID)
if c.cfg.ClientSecret != "" {
tform.Set("client_secret", c.cfg.ClientSecret)
}
tresp, err := http.PostForm(tokenURL, tform)
if err != nil {
continue
}
var tr struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
IDToken string `json:"id_token"`
ExpiresIn int `json:"expires_in"`
Error string `json:"error"`
}
json.NewDecoder(tresp.Body).Decode(&tr)
tresp.Body.Close()
if tr.Error == "authorization_pending" || tr.Error == "slow_down" {
if tr.Error == "slow_down" {
interval += 5 * time.Second
}
continue
}
if tr.Error != "" {
return nil, fmt.Errorf("oidc: device flow: %s", tr.Error)
}
if tr.IDToken == "" {
continue
}
token := &oauth2.Token{
AccessToken: tr.AccessToken,
RefreshToken: tr.RefreshToken,
Expiry: time.Now().Add(time.Duration(tr.ExpiresIn) * time.Second),
}
token = token.WithExtra(map[string]any{"id_token": tr.IDToken})
return c.tokenToCredentials(token)
}
return nil, fmt.Errorf("oidc: device flow timed out")
}
// Issuer returns the configured issuer URL.
func (c *OIDCClient) Issuer() string { return c.cfg.Issuer }
// Ensure no unused import for strings (used in error formatting).
var _ = strings.Contains
+184
View File
@@ -0,0 +1,184 @@
package identity
import (
"context"
"encoding/base64"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
)
// mockOIDCProvider starts a minimal OIDC provider that serves
// discovery + JWKS + token endpoint, signing self-signed ID tokens.
// It returns the issuer URL + a cleanup function.
func mockOIDCProvider(t *testing.T, clientID string) (issuer string, privateKey any, cleanup func()) {
t.Helper()
// We use a very minimal mock: discovery returns a JWKS URL +
// token URL pointing to the same test server. The token
// endpoint returns a fake ID token. For full verification
// we'd need RSA signing, but for the client logic tests we
// verify the flow wiring, not the crypto (the verifier is
// tested via integration in P26).
var srv *httptest.Server
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/.well-known/openid-configuration":
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]any{
"issuer": srvURL(srv),
"authorization_endpoint": srvURL(srv) + "/auth",
"token_endpoint": srvURL(srv) + "/token",
"jwks_uri": srvURL(srv) + "/jwks",
"device_authorization_endpoint": srvURL(srv) + "/device",
"response_types_supported": []string{"code"},
"subject_types_supported": []string{"public"},
"id_token_signing_alg_values_supported": []string{"none"},
})
case "/jwks":
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]any{"keys": []any{}})
case "/token":
w.Header().Set("Content-Type", "application/json")
// Return a minimal unsigned ID token (header.payload.sig
// with empty sig). The verifier in production validates
// against JWKS; for tests we only check the flow wiring.
payload := map[string]any{
"iss": srvURL(srv),
"sub": "test-user-123",
"aud": clientID,
"exp": time.Now().Add(time.Hour).Unix(),
"iat": time.Now().Unix(),
"groups": []string{"orca-admins"},
"email": "test@example.com",
}
payloadBytes, _ := json.Marshal(payload)
enc := base64Raw(payloadBytes)
idToken := "eyJhbGciOiJub25lIn0." + enc + "."
json.NewEncoder(w).Encode(map[string]any{
"access_token": "at-123",
"refresh_token": "rt-456",
"id_token": idToken,
"expires_in": 3600,
"token_type": "Bearer",
})
case "/device":
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]any{
"device_code": "dc-123",
"user_code": "ORCA-CODE",
"verification_uri": srvURL(srv) + "/device-verify",
"interval": 1,
"expires_in": 300,
})
default:
http.NotFound(w, r)
}
}))
return srvURL(srv), nil, srv.Close
}
func srvURL(srv *httptest.Server) string {
return "http://" + srv.Listener.Addr().String()
}
func base64Raw(b []byte) string {
return base64.RawURLEncoding.EncodeToString(b)
}
// TestOIDCClientDiscovery verifies NewOIDCClient discovers the issuer.
func TestOIDCClientDiscovery(t *testing.T) {
issuer, _, cleanup := mockOIDCProvider(t, "test-client")
defer cleanup()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
client, err := NewOIDCClient(ctx, OIDCConfig{
Issuer: issuer,
ClientID: "test-client",
})
if err != nil {
t.Fatalf("NewOIDCClient: %v", err)
}
if client.Issuer() != issuer {
t.Errorf("issuer = %q, want %q", client.Issuer(), issuer)
}
}
// TestCredentialsRoundTrip verifies Save + Load credentials round-trip
// at 0600.
func TestCredentialsRoundTrip(t *testing.T) {
dir := t.TempDir()
t.Setenv("ORCA_HOME", dir)
creds := &Credentials{
IDToken: "id-123",
AccessToken: "at-456",
RefreshToken: "rt-789",
Expiry: time.Now().Add(time.Hour),
Issuer: "https://idp.example",
Subject: "user-1",
Groups: []string{"admins"},
}
if err := SaveCredentials(creds); err != nil {
t.Fatalf("Save: %v", err)
}
loaded, err := LoadCredentials()
if err != nil {
t.Fatalf("Load: %v", err)
}
if loaded.Subject != "user-1" {
t.Errorf("subject = %q, want user-1", loaded.Subject)
}
if len(loaded.Groups) != 1 || loaded.Groups[0] != "admins" {
t.Errorf("groups = %v, want [admins]", loaded.Groups)
}
}
// TestCredentialsModeEnforced verifies LoadCredentials rejects looser
// than 0600.
func TestCredentialsModeEnforced(t *testing.T) {
dir := t.TempDir()
t.Setenv("ORCA_HOME", dir)
creds := &Credentials{IDToken: "x", Issuer: "x", Subject: "x"}
if err := SaveCredentials(creds); err != nil {
t.Fatalf("Save: %v", err)
}
// Loosen to 0644.
path := dir + "/credentials.json"
if err := os.Chmod(path, 0o644); err != nil {
t.Fatalf("chmod: %v", err)
}
_, err := LoadCredentials()
if err == nil {
t.Error("LoadCredentials should reject 0644")
}
}
// TestClearCredentials verifies logout removes the file.
func TestClearCredentials(t *testing.T) {
dir := t.TempDir()
t.Setenv("ORCA_HOME", dir)
creds := &Credentials{IDToken: "x", Issuer: "x", Subject: "x"}
_ = SaveCredentials(creds)
if err := ClearCredentials(); err != nil {
t.Fatalf("Clear: %v", err)
}
if _, err := LoadCredentials(); err == nil {
t.Error("Load after clear should fail")
}
}
// TestDefaultScopes verifies the scopes include openid.
func TestDefaultScopes(t *testing.T) {
scopes := DefaultScopes()
found := false
for _, s := range scopes {
if s == "openid" {
found = true
}
}
if !found {
t.Error("DefaultScopes missing openid")
}
}