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
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
c85d25c4b9
commit
7149b98b7b
@@ -0,0 +1,84 @@
|
||||
require "test_helper"
|
||||
|
||||
class OidcDeviceCodeTest < ActiveSupport::TestCase
|
||||
def setup
|
||||
@application = Application.create!(
|
||||
name: "Device Code Model Test",
|
||||
slug: "device-code-model-test",
|
||||
app_type: "oidc",
|
||||
is_public_client: true,
|
||||
active: true
|
||||
)
|
||||
@user = User.create!(email_address: "device_model@example.com", password: "password123")
|
||||
end
|
||||
|
||||
test "generates an opaque device_code stored as HMAC and looked up by plaintext" do
|
||||
dc = OidcDeviceCode.create!(application: @application)
|
||||
|
||||
assert dc.plaintext_device_code.present?
|
||||
assert dc.device_code_hmac.present?
|
||||
assert_not_equal dc.plaintext_device_code, dc.device_code_hmac
|
||||
assert_equal dc, OidcDeviceCode.find_by_plaintext_device_code(dc.plaintext_device_code)
|
||||
assert_nil OidcDeviceCode.find_by_plaintext_device_code("wrong")
|
||||
end
|
||||
|
||||
test "generates a short user_code from the unambiguous alphabet" do
|
||||
dc = OidcDeviceCode.create!(application: @application)
|
||||
|
||||
assert_equal 8, dc.user_code.length
|
||||
# No visually ambiguous characters (0/O, 1/I) and only the allowed alphabet.
|
||||
assert_match(/\A[A-HJ-NP-Z2-9]{8}\z/, dc.user_code)
|
||||
end
|
||||
|
||||
test "find_by_user_code normalizes case, hyphens, and whitespace" do
|
||||
dc = OidcDeviceCode.create!(application: @application)
|
||||
formatted = "#{dc.user_code[0, 4]}-#{dc.user_code[4, 4]}".downcase
|
||||
|
||||
assert_equal dc, OidcDeviceCode.find_by_user_code(formatted)
|
||||
assert_equal dc, OidcDeviceCode.find_by_user_code(" #{dc.user_code} ")
|
||||
assert_nil OidcDeviceCode.find_by_user_code("nope")
|
||||
end
|
||||
|
||||
test "user_code is unique" do
|
||||
dc = OidcDeviceCode.create!(application: @application)
|
||||
dup = OidcDeviceCode.new(application: @application, user_code: dc.user_code)
|
||||
|
||||
assert_not dup.valid?
|
||||
assert_includes dup.errors[:user_code], "has already been taken"
|
||||
end
|
||||
|
||||
test "starts pending and approve! attaches the user and auth context" do
|
||||
dc = OidcDeviceCode.create!(application: @application)
|
||||
assert dc.pending?
|
||||
|
||||
dc.approve!(user: @user, acr: "1", auth_time: 1_700_000_000)
|
||||
|
||||
assert dc.approved?
|
||||
assert_equal @user, dc.user
|
||||
assert_equal "1", dc.acr
|
||||
assert_equal 1_700_000_000, dc.auth_time
|
||||
end
|
||||
|
||||
test "deny! marks the code denied" do
|
||||
dc = OidcDeviceCode.create!(application: @application)
|
||||
dc.deny!
|
||||
assert dc.denied?
|
||||
end
|
||||
|
||||
test "expired? reflects expires_at" do
|
||||
assert OidcDeviceCode.create!(application: @application, expires_at: 1.minute.ago).expired?
|
||||
assert_not OidcDeviceCode.create!(application: @application).expired?
|
||||
end
|
||||
|
||||
test "uses_pkce? and rejects malformed code_challenge" do
|
||||
assert_not OidcDeviceCode.create!(application: @application).uses_pkce?
|
||||
|
||||
valid_challenge = "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM"
|
||||
dc = OidcDeviceCode.create!(application: @application, code_challenge: valid_challenge, code_challenge_method: "S256")
|
||||
assert dc.uses_pkce?
|
||||
|
||||
bad = OidcDeviceCode.new(application: @application, code_challenge: "too-short")
|
||||
assert_not bad.valid?
|
||||
assert_includes bad.errors[:code_challenge], "must be 43-128 characters of base64url encoding"
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user