Files
clinch/db/migrate/20260719000001_create_oidc_device_codes.rb
Dan MilneandClaude Opus 4.8 7149b98b7b Add OAuth device flow, token introspection, and dynamic client registration
Adds three OAuth surfaces to the OIDC provider so CLIs, terminal agents, and
MCP connectors can authenticate as real users instead of using static API keys.

Device Authorization Grant (RFC 8628):
- OidcDeviceCode model (HMAC device_code, short plaintext user_code, nullable
  user until approval, slow_down polling state), mirroring OidcAuthorizationCode
- POST /oauth/device_authorization issues the code pair + verification URIs
- device_code grant on /oauth/token returns authorization_pending / slow_down /
  access_denied / expired_token, then the standard token triple; single-use
- Authenticated /device approval page, gated by Application#user_allowed?

Token Introspection (RFC 7662):
- POST /oauth/introspect: confidential-caller-authenticated; returns active,
  scope, and the user's groups so resource servers can authorize on membership

Dynamic Client Registration (RFC 7591):
- POST /oauth/register creates public/confidential clients (PKCE required)
- Runtime toggle via a new Setting store + admin switch on the Applications page;
  off by default, env var CLINCH_DCR_ENABLED as bootstrap fallback
- New clients are default-deny (no allowed_groups) until an admin grants access
- RFC 8414 metadata alias at /.well-known/oauth-authorization-server;
  registration_endpoint advertised only while the window is open

Discovery advertises all three grants/endpoints. Seeds add a clinch-cli public
client and a c2a2-introspection confidential client. ADRs in docs/decisions
record the opaque-vs-JWT, device-flow, and DCR-security decisions.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F7cwhwDJp3MJJDoNPVE6zq
2026-07-19 12:30:02 +10:00

44 lines
1.7 KiB
Ruby

class CreateOidcDeviceCodes < ActiveRecord::Migration[8.1]
def change
create_table :oidc_device_codes do |t|
t.references :application, null: false, foreign_key: true
# user_id is nullable: it stays blank while the code is pending and is
# filled in when the user approves the request on the verification page.
t.references :user, null: true, foreign_key: true
# Opaque device_code, stored as an HMAC (never plaintext) — matches the
# OidcAuthorizationCode pattern.
t.string :device_code_hmac, null: false
# Short, human-typable code the user enters on the verification page.
# Stored in plaintext because the user reads it off one screen and types
# it into another; kept safe by short expiry + single use + rate limiting.
t.string :user_code, null: false
t.string :status, null: false, default: "pending" # pending / approved / denied
t.string :scope
t.string :nonce
# PKCE (RFC 8628 permits and recommends PKCE for public clients).
t.string :code_challenge
t.string :code_challenge_method
# Captured at approval time from the approving user's session.
t.string :acr
t.integer :auth_time
t.datetime :expires_at, null: false
# Timestamp of the last token-endpoint poll, used to enforce the polling
# interval and emit slow_down (RFC 8628 §3.5).
t.datetime :last_polled_at
t.integer :interval, null: false, default: 5
t.timestamps
end
add_index :oidc_device_codes, :device_code_hmac, unique: true
add_index :oidc_device_codes, :user_code, unique: true
add_index :oidc_device_codes, :expires_at
end
end