activeadmin-device_authorization 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6edcb0a43663f2ee55e360f2571a9d980265076434205a603dbe4b41a785d351
4
- data.tar.gz: 438551d1316c58ace7c9b6a81aec3f388a8a261dffdb29d40590348eb9da33e0
3
+ metadata.gz: 72adec60f28b252c649bee7dbf63751b0a2816839c4db22432cd713b3268dc90
4
+ data.tar.gz: 86a971e70328d02e98ef954f5043b085627d5157003425be94adda729cbf47ec
5
5
  SHA512:
6
- metadata.gz: ae06adfc6827bd0c6be364d76716bc0771c4f0012b21a4a6c23cdee93ed395c29b02ad8c60cac0ba0c3bef4bf950767b0bc6ad9b10611da849500810a70dca39
7
- data.tar.gz: da1d891cadd3eb99c7fa0391d6dfddb306baac964cc66599d932fb12ca6acd742d9ab2766d2a51466ef88e9f5d839ec3aee046f7ae995282916008c7537fddbf
6
+ metadata.gz: b4cdc2f8bf7811ba4767b737da76845e173e033de9dbd0612ba1e010412bb6a4d53235ae489f0ca0e1a10cb3179b5d10d73c029c1453448ebae48575c92cb4a8
7
+ data.tar.gz: feebea0eca1d77bfa901886a9b61ca23a2027e656f16dc79bf89f1e1ad3dab401df029707f61b06f0b874cfc9aa2cf98d652b29c4bf6d0bae357ac519406176f
data/CHANGELOG.md CHANGED
@@ -1,11 +1,21 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 0.3.0 (2026-11-16)
4
+
5
+ - BREAKING: Consent UI is only the ActiveAdmin Device Auth page (`ActiveAdmin.routes`); remove `consent: :controller` and ConsentsController
6
+ - BREAKING: `Routes.draw` draws public OAuth endpoints only and applies `path_namespace` itself (one line at routes root; do not wrap in `namespace :admin`)
7
+ - BREAKING: `verification_uri_complete` uses a `user_code` query parameter instead of a path suffix
8
+ - Register the Device Auth page from `ActiveAdmin.after_load` when `register_active_admin_page` is true
9
+ - Allow manual `Page.register!` when `register_active_admin_page` is false (engine auto-register stays gated)
10
+ - Show the enter-code title on Device Auth when no grant is loaded
11
+ - Document the ActiveAdmin consent vs explicit OAuth split as RFC-0002
12
+
3
13
  ## 0.2.0 (2026-10-28)
4
14
 
5
15
  - BREAKING: Require ActiveAdmin 4 (`>= 4.0.0.beta19`); drop ActiveAdmin 3 support
6
16
  - Ship ActiveAdmin 4 consent markup: `.attributes-table`, self-contained verify prompt, Approve/Deny submits inside `.formtastic`
7
- - Resolve `current_resource_owner` with `instance_exec` for zero-arity procs so private Devise helpers work
8
- - Document ActionPolicy `approve?` / `deny?` / `step_up?` page policy hooks
17
+ - Resolve `current_resource_owner` with `instance_exec` for zero-arity procs so private authentication helpers work
18
+ - Document page policy hooks `approve?` / `deny?` / `step_up?`
9
19
 
10
20
  ## 0.1.3 (2026-10-26)
11
21
 
data/README.md CHANGED
@@ -30,7 +30,7 @@ Maintainers publish with `make release` (`usr/bin/release.rb`), which builds, pu
30
30
 
31
31
  ## Why these settings are configurable
32
32
 
33
- - `challenge_expires_in` (default 600s): RFC 8628 requires `expires_in` for device/user codes. The RFC example is 1800s; Doorkeeper device grant defaults to 300s. Always required; device codes must expire.
33
+ - `challenge_expires_in` (default 600s): RFC 8628 requires `expires_in` for device/user codes. The RFC example is 1800s; this gem defaults to 600s. Always required; device codes must expire.
34
34
  - `poll_interval` (default 5s): RFC 8628 `interval`; clients must use 5 when omitted.
35
35
  - `slow_down_increment` (default 5s): RFC 8628 `slow_down` requires increasing the interval by 5s. Keep 5 unless you have a documented reason.
36
36
  - `access_token_absolute_ttl` (default 4h): Absolute bearer cap (RFC 6819 short-lived access tokens). Set to `nil` to disable absolute expiry.
@@ -53,7 +53,7 @@ ActiveAdmin::DeviceAuthorization.configure do |config|
53
53
  config.grant_class_name = "DeviceAuthorizationGrant"
54
54
  config.access_token_class_name = "DeviceAuthorizationAccessToken"
55
55
  # Prefer zero-arity: the gem runs this with instance_exec on the controller.
56
- # Devise helpers are often private; do not call them with an explicit receiver.
56
+ # Auth helpers are often private; do not call them with an explicit receiver.
57
57
  config.current_resource_owner = -> { current_admin_user }
58
58
  # Arity-1 form still works; use send when the helper is private:
59
59
  # config.current_resource_owner = ->(controller) { controller.send(:current_admin_user) }
@@ -87,15 +87,19 @@ end
87
87
  ```
88
88
 
89
89
  ```ruby
90
- # config/routes.rb (inside the admin namespace)
91
- namespace :admin do
92
- ActiveAdmin::DeviceAuthorization::Routes.draw(self)
93
- # Optional: Routes.draw(self, consent: :controller) serves the code-entry and
94
- # consent pages from the gem instead of ActiveAdmin page actions.
95
- end
90
+ # config/routes.rb (at the mapper root; draws /admin/oauth/... only)
91
+ ActiveAdmin.routes(self)
92
+ ActiveAdmin::DeviceAuthorization::Routes.draw(self)
96
93
  ```
97
94
 
98
- OAuth controller paths are absolute (`/active_admin/device_authorization/...`) so nesting under `namespace :admin` does not invent `Admin::ActiveAdmin::...` constants. Consent defaults to the ActiveAdmin page (`device_auth#...`).
95
+ Consent UI is the ActiveAdmin "Device Auth" page (index plus `approve` / `deny` /
96
+ `step_up` page_actions). It is drawn by `ActiveAdmin.routes` and authenticated
97
+ through `ActiveAdmin::BaseController`. Do not nest `Routes.draw` under another
98
+ `namespace :admin` (that double-prefixes the OAuth path). OAuth controller paths
99
+ are absolute (`/active_admin/device_authorization/...`) so the admin namespace
100
+ does not invent `Admin::ActiveAdmin::...` constants. OAuth endpoints stay public
101
+ protocol surfaces (rate limits plus `client_id` / `device_code`); they do not use
102
+ the admin session. See RFC-0002.
99
103
 
100
104
  `access_token_absolute_ttl` is the hard ceiling. Consent `min` / `max` / `default` / `choices` must sit inside that ceiling; `clamp_absolute_ttl` uses `min(access_token_max_absolute_ttl, access_token_absolute_ttl)`.
101
105
 
@@ -112,10 +116,10 @@ The gem does not replace host security controls. Before production traffic:
112
116
  - Schedule `bundle exec rake active_admin:device_authorization:retention` at the cadence required by the host retention policy. Token retention deletes rows past `token_retention_period` after absolute expiry, revoke, or idle expiry (when idle TTL is enabled). Do not rely on absolute expiry alone when absolute TTL is nil (far-future sentinel).
113
117
  - Implement host models and indexes that match the contract below, including uniqueness and foreign keys.
114
118
  - Authenticate bearer tokens in the host API (see Bearer validation contract). The gem issues and expires tokens; it does not install API authentication middleware.
115
- - When the host authorizes ActiveAdmin pages with ActionPolicy (or a similar adapter that maps actions to `action_name?` methods), define `approve?`, `deny?`, and `step_up?` on the page policy. Missing methods fail closed and Approve/Deny/step-up return unauthorized. `step_up?` is needed only when `on_approve_step_up` is configured.
119
+ - When the host authorizes ActiveAdmin pages with an adapter that maps actions to `action_name?` methods, define `approve?`, `deny?`, and `step_up?` on the page policy. Missing methods fail closed and Approve/Deny/step-up return unauthorized. `step_up?` is needed only when `on_approve_step_up` is configured.
116
120
 
117
121
  ```ruby
118
- # Example ActionPolicy page policy hooks for Device Auth
122
+ # Example page policy hooks for Device Auth
119
123
  class ActiveAdmin::PagePolicy < ApplicationPolicy
120
124
  def show?
121
125
  true
@@ -149,16 +153,15 @@ end
149
153
 
150
154
  The gem issues and expires tokens, but it does not install authentication in the host API. For every protected request, the host must digest the presented bearer value with `ActiveAdmin::DeviceAuthorization.config.digest`, find the matching token through the allowed client or resource-owner relation, reject it when `AccessTokenExpiry#expired?` is true, enforce its scopes, and update `last_used_at` only after successful authentication. A missing, revoked, expired, or out-of-scope token must fail closed.
151
155
 
152
- Register the ActiveAdmin page after configuration:
153
-
154
- ```ruby
155
- ActiveAdmin::DeviceAuthorization::Page.register!
156
- ```
156
+ When `register_active_admin_page` is true (default), the engine registers the
157
+ Device Auth page from `ActiveAdmin.after_load`. Set it to false and call
158
+ `ActiveAdmin::DeviceAuthorization::Page.register!` yourself only when you need
159
+ to own registration timing.
157
160
 
158
161
  ## Protocol
159
162
 
160
163
  1. `POST /admin/oauth/device_authorization` with `client_id` and `scope`
161
- 2. Open `verification_uri` / `verification_uri_complete` while signed in as an admin user
164
+ 2. Open `verification_uri` / `verification_uri_complete` (`?user_code=...`) while signed in as an admin user
162
165
  3. Approve or deny (optional host `before_approve` / `on_approve_step_up` for step-up second factor before Approve; Deny is ungated)
163
166
  4. Poll `POST /admin/oauth/token` with `grant_type=urn:ietf:params:oauth:grant-type:device_code`
164
167
 
@@ -167,13 +170,17 @@ ActiveAdmin::DeviceAuthorization::Page.register!
167
170
  ```bash
168
171
  bundle install
169
172
  bundle exec appraisal install
173
+ npm install && npx playwright install chromium
170
174
  make lint
171
175
  make test
172
176
  ```
173
177
 
174
178
  Matrixed Rails versions use [Appraisal](https://github.com/thoughtbot/appraisal): `gemfiles/rails72.gemfile`, `rails8ruby34.gemfile`, and `rails8ruby4.gemfile`, each pinned to ActiveAdmin 4.0.0.beta23. Run `bundle exec appraisal rspec` to execute RSpec in each gemfile context.
175
179
 
176
- [Trunk](https://docs.trunk.io) config lives in `.trunk/`; CI can run `trunk` via `.github/workflows/_trunk_check.yml`. Releases: `make release` or `usr/bin/release.rb`.
180
+ Browser e2e uses Capybara + Playwright against `spec/dummy` (ActiveAdmin session,
181
+ device authorization consent, and bearer-protected API). See `spec/README.md`.
182
+
183
+ Trunk config lives in `.trunk/`; CI can run `trunk` via `.github/workflows/_trunk_check.yml`. Releases: `make release` or `usr/bin/release.rb`.
177
184
 
178
185
  ## Security
179
186
 
@@ -47,14 +47,25 @@ Gem::Specification.new do |spec|
47
47
  spec.add_dependency "rails", ">= 7.2.3.2"
48
48
 
49
49
  spec.add_development_dependency "appraisal", "~> 2"
50
+ spec.add_development_dependency "activeadmin-graphql", ">= 0.4.1"
50
51
  spec.add_development_dependency "bundler", ">= 2"
51
52
  spec.add_development_dependency "bundler-audit", "~> 0.9"
52
- spec.add_development_dependency "polyrun", ">= 2.2.0"
53
+ spec.add_development_dependency "capybara", "~> 3.40"
54
+ spec.add_development_dependency "capybara-playwright-driver", "~> 0.5"
55
+ spec.add_development_dependency "cssbundling-rails"
56
+ spec.add_development_dependency "database_cleaner-active_record", "~> 2.2"
57
+ spec.add_development_dependency "devise", ">= 4.9"
58
+ spec.add_development_dependency "importmap-rails"
59
+ spec.add_development_dependency "polyrun", ">= 2.2.9"
60
+ spec.add_development_dependency "puma", ">= 5.0"
53
61
  spec.add_development_dependency "rake", "~> 13"
54
62
  spec.add_development_dependency "rbs", "~> 4"
55
63
  spec.add_development_dependency "rspec", "~> 3"
64
+ spec.add_development_dependency "rspec-rails", ">= 6"
56
65
  spec.add_development_dependency "rubocop-rspec", "~> 3.8"
57
66
  spec.add_development_dependency "rubocop-thread_safety", "~> 0.7"
67
+ spec.add_development_dependency "sprockets-rails"
68
+ spec.add_development_dependency "sqlite3", ">= 2.1"
58
69
  spec.add_development_dependency "standard", "~> 1.52"
59
70
  spec.add_development_dependency "standard-custom", "~> 1.0"
60
71
  spec.add_development_dependency "standard-performance", "~> 1.8"
@@ -71,7 +71,8 @@
71
71
 
72
72
  <% if grant.pending? %>
73
73
  <div class="device-auth-actions">
74
- <%= form_with url: send(:"#{config.path_namespace}_approve_device_auth_path", grant.user_code), method: :post, id: "device-auth-approve", data: {turbo: false}, class: "formtastic" do %>
74
+ <%= form_with url: url_for(action: :approve), method: :post, id: "device-auth-approve", data: {turbo: false}, class: "formtastic" do %>
75
+ <%= hidden_field_tag :user_code, grant.user_code %>
75
76
  <% if available_scopes.any? %>
76
77
  <fieldset class="inputs">
77
78
  <legend><%= t("active_admin.device_authorization.scopes") %></legend>
@@ -109,7 +110,8 @@
109
110
  </fieldset>
110
111
  <% end %>
111
112
 
112
- <%= form_with url: send(:"#{config.path_namespace}_deny_device_auth_path", grant.user_code), method: :post, data: {turbo: false}, class: "formtastic" do %>
113
+ <%= form_with url: url_for(action: :deny), method: :post, data: {turbo: false}, class: "formtastic" do %>
114
+ <%= hidden_field_tag :user_code, grant.user_code %>
113
115
  <fieldset class="actions">
114
116
  <ol>
115
117
  <li class="action input_action">
@@ -62,12 +62,11 @@ module ActiveAdmin
62
62
  @client_class_name = "DeviceAuthorizationClient"
63
63
  @grant_class_name = "DeviceAuthorizationGrant"
64
64
  @access_token_class_name = "DeviceAuthorizationAccessToken"
65
- # Zero-arity: resolved with instance_exec so Devise helpers work without
66
- # an explicit receiver (current_admin_user / current_user are often private).
65
+ # Zero-arity: resolved with instance_exec so private auth helpers work
66
+ # without an explicit receiver (current_admin_user / current_user).
67
67
  @current_resource_owner = -> { current_admin_user }
68
68
 
69
- # RFC 8628 example uses 1800s; Doorkeeper device grant defaults to 300s.
70
- # Ten minutes is a practical admin-consent window.
69
+ # RFC 8628 example uses 1800s; ten minutes is a practical admin-consent window.
71
70
  @challenge_expires_in = 10 * 60
72
71
  @poll_interval = 5
73
72
  @slow_down_increment = 5
@@ -150,7 +149,7 @@ module ActiveAdmin
150
149
 
151
150
  # Resolves the signed-in resource owner for consent actions.
152
151
  # Zero-arity callables run via instance_exec on the controller so private
153
- # Devise helpers (current_admin_user, current_user) are reachable.
152
+ # auth helpers (current_admin_user, current_user) are reachable.
154
153
  # Arity-1 callables still receive the controller; use send(:current_user)
155
154
  # when calling with an explicit receiver.
156
155
  def resolve_current_resource_owner(controller)
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "cgi"
4
+
3
5
  module ActiveAdmin
4
6
  module DeviceAuthorization
5
7
  class DeviceAuthorizationRequest
@@ -48,7 +50,7 @@ module ActiveAdmin
48
50
  device_code: device_code,
49
51
  user_code: user_code,
50
52
  verification_uri: verification_uri,
51
- verification_uri_complete: "#{verification_uri}/#{user_code}",
53
+ verification_uri_complete: "#{verification_uri}?user_code=#{CGI.escape(user_code)}",
52
54
  expires_in: @config.challenge_expires_in.to_i,
53
55
  interval: @config.poll_interval.to_i
54
56
  )
@@ -12,6 +12,24 @@ module ActiveAdmin
12
12
  initializer "active_admin.device_authorization.filter_parameters" do |app|
13
13
  app.config.filter_parameters |= [:device_code, :user_code, :access_token]
14
14
  end
15
+
16
+ # Register the consent page after ActiveAdmin loads admin files (RFC-0002).
17
+ # Auto-register only when register_active_admin_page is true; hosts may still
18
+ # call Page.register! manually when that flag is false.
19
+ initializer "active_admin.device_authorization.page", after: :load_config_initializers do
20
+ ActiveAdmin.after_load do
21
+ if ActiveAdmin::DeviceAuthorization.config.register_active_admin_page
22
+ ActiveAdmin::DeviceAuthorization::Page.register!
23
+ end
24
+ end
25
+
26
+ if defined?(ActiveAdmin) &&
27
+ ActiveAdmin.application.respond_to?(:loaded?) &&
28
+ ActiveAdmin.application.loaded? &&
29
+ ActiveAdmin::DeviceAuthorization.config.register_active_admin_page
30
+ ActiveAdmin::DeviceAuthorization::Page.register!
31
+ end
32
+ end
15
33
  end
16
34
  end
17
35
  end
@@ -2,18 +2,20 @@
2
2
 
3
3
  module ActiveAdmin
4
4
  module DeviceAuthorization
5
- # Registers the ActiveAdmin consent page. Call after ActiveAdmin.load! setup,
6
- # typically from config/initializers/active_admin_device_authorization.rb once
7
- # host scopes and model class names are configured.
5
+ # Registers the ActiveAdmin consent page (RFC-0002). Always registers when
6
+ # called. The engine auto-calls this from `ActiveAdmin.after_load` only when
7
+ # `register_active_admin_page` is true; hosts may call `register!` themselves
8
+ # when that flag is false.
9
+ #
10
+ # Consent auth and authorization stay on ActiveAdmin::BaseController via
11
+ # `ActiveAdmin.routes`. Approve and deny are page_actions; index renders
12
+ # consent when `user_code` is present.
8
13
  module Page
9
14
  module_function
10
15
 
11
16
  def register!
12
17
  config = ActiveAdmin::DeviceAuthorization.config
13
- return unless config.register_active_admin_page
14
-
15
18
  menu_label = config.menu_label || -> { I18n.t("active_admin.device_authorization.menu") }
16
- verification = config.verification_path
17
19
 
18
20
  ActiveAdmin.register_page "Device Auth" do
19
21
  menu parent: config.menu_parent,
@@ -26,77 +28,73 @@ module ActiveAdmin
26
28
  define_method(:index) do
27
29
  user_code = ActiveAdmin::DeviceAuthorization::UserCode.normalize(params[:user_code])
28
30
  if user_code.present?
29
- redirect_to send(:"#{config.path_namespace}_device_auth_grant_path", user_code)
30
- return
31
+ load_grant!
32
+ @grant.mark_expired_if_needed!
33
+ @page_title = I18n.t("active_admin.device_authorization.title")
34
+ else
35
+ @page_title = I18n.t("active_admin.device_authorization.enter_code")
31
36
  end
32
-
33
- @page_title = I18n.t("active_admin.device_authorization.enter_code")
34
37
  super()
35
38
  end
36
39
 
37
- define_method(:show) do
38
- load_grant!
39
- @grant.mark_expired_if_needed!
40
- @page_title = I18n.t("active_admin.device_authorization.title")
41
- render "active_admin/page/index"
42
- end
40
+ define_method(:device_auth_grant) { @grant }
43
41
 
44
- define_method(:approve) do
45
- load_grant!
46
- if config.before_approve && !config.before_approve.call(self)
47
- return
48
- end
42
+ private
49
43
 
50
- ActiveAdmin::DeviceAuthorization::Consent.new(
51
- grant: @grant,
52
- resource_owner: config.resolve_current_resource_owner(self),
53
- scopes: Array(params[:scopes]),
54
- absolute_ttl_seconds: params[:absolute_ttl_seconds]
55
- ).approve!
56
- redirect_to send(:"#{config.path_namespace}_device_auth_grant_path", @grant.user_code),
57
- notice: I18n.t("active_admin.device_authorization.approved")
58
- rescue ActiveAdmin::DeviceAuthorization::Consent::Error => error
59
- redirect_to send(:"#{config.path_namespace}_device_auth_grant_path", @grant.user_code),
60
- alert: I18n.t(
61
- "active_admin.device_authorization.error.#{error.message}",
62
- default: error.message
63
- )
44
+ define_method(:load_grant!) do
45
+ user_code = ActiveAdmin::DeviceAuthorization::UserCode.normalize(params[:user_code])
46
+ @grant = ActiveAdmin::DeviceAuthorization.config.grant_class
47
+ .find_by!(user_code: user_code)
64
48
  end
65
49
 
66
- define_method(:deny) do
67
- load_grant!
68
- ActiveAdmin::DeviceAuthorization::Consent.new(
69
- grant: @grant,
70
- resource_owner: config.resolve_current_resource_owner(self),
71
- scopes: @grant.requested_scopes,
72
- absolute_ttl_seconds: config.access_token_default_absolute_ttl
73
- ).deny!
74
- redirect_to send(:"#{config.path_namespace}_device_auth_grant_path", @grant.user_code),
75
- notice: I18n.t("active_admin.device_authorization.denied")
76
- rescue ActiveAdmin::DeviceAuthorization::Consent::Error => error
77
- redirect_to send(:"#{config.path_namespace}_device_auth_grant_path", @grant.user_code),
78
- alert: I18n.t(
79
- "active_admin.device_authorization.error.#{error.message}",
80
- default: error.message
81
- )
50
+ define_method(:consent_redirect_path) do
51
+ send(
52
+ :"#{ActiveAdmin::DeviceAuthorization.config.path_namespace}_device_auth_path",
53
+ user_code: @grant.user_code
54
+ )
82
55
  end
56
+ end
83
57
 
84
- define_method(:device_auth_grant) { @grant }
85
-
86
- define_method(:page_presenter) do
87
- if action_name == "show"
88
- active_admin_config.get_page_presenter(:index)
89
- else
90
- super()
91
- end
58
+ page_action :approve, method: :post do
59
+ load_grant!
60
+ config = ActiveAdmin::DeviceAuthorization.config
61
+ if config.before_approve && !config.before_approve.call(self)
62
+ return
92
63
  end
93
64
 
94
- private
65
+ ActiveAdmin::DeviceAuthorization::Consent.new(
66
+ grant: @grant,
67
+ resource_owner: config.resolve_current_resource_owner(self),
68
+ scopes: Array(params[:scopes]),
69
+ absolute_ttl_seconds: params[:absolute_ttl_seconds]
70
+ ).approve!
71
+ redirect_to consent_redirect_path,
72
+ notice: I18n.t("active_admin.device_authorization.approved")
73
+ rescue ActiveAdmin::DeviceAuthorization::Consent::Error => error
74
+ redirect_to consent_redirect_path,
75
+ alert: I18n.t(
76
+ "active_admin.device_authorization.error.#{error.message}",
77
+ default: error.message
78
+ )
79
+ end
95
80
 
96
- define_method(:load_grant!) do
97
- user_code = ActiveAdmin::DeviceAuthorization::UserCode.normalize(params[:user_code])
98
- @grant = config.grant_class.find_by!(user_code: user_code)
99
- end
81
+ page_action :deny, method: :post do
82
+ load_grant!
83
+ config = ActiveAdmin::DeviceAuthorization.config
84
+ ActiveAdmin::DeviceAuthorization::Consent.new(
85
+ grant: @grant,
86
+ resource_owner: config.resolve_current_resource_owner(self),
87
+ scopes: @grant.requested_scopes,
88
+ absolute_ttl_seconds: config.access_token_default_absolute_ttl
89
+ ).deny!
90
+ redirect_to consent_redirect_path,
91
+ notice: I18n.t("active_admin.device_authorization.denied")
92
+ rescue ActiveAdmin::DeviceAuthorization::Consent::Error => error
93
+ redirect_to consent_redirect_path,
94
+ alert: I18n.t(
95
+ "active_admin.device_authorization.error.#{error.message}",
96
+ default: error.message
97
+ )
100
98
  end
101
99
 
102
100
  page_action :step_up, method: [:get, :post] do
@@ -108,7 +106,13 @@ module ActiveAdmin
108
106
  end
109
107
  end
110
108
 
111
- content title: proc { I18n.t("active_admin.device_authorization.title") } do
109
+ content title: proc {
110
+ if device_auth_grant
111
+ I18n.t("active_admin.device_authorization.title")
112
+ else
113
+ I18n.t("active_admin.device_authorization.enter_code")
114
+ end
115
+ } do
112
116
  if device_auth_grant
113
117
  render partial: "active_admin/device_authorization/consent",
114
118
  locals: {grant: device_auth_grant}
@@ -117,9 +121,6 @@ module ActiveAdmin
117
121
  end
118
122
  end
119
123
  end
120
-
121
- # Host must also route show/approve/deny; Page only registers the AA index shell.
122
- verification
123
124
  end
124
125
  end
125
126
  end
@@ -3,56 +3,30 @@
3
3
  module ActiveAdmin
4
4
  module DeviceAuthorization
5
5
  module Routes
6
- # Embed under the host admin namespace, for example:
6
+ # Draw public OAuth JSON endpoints only (RFC-0002).
7
7
  #
8
- # namespace :admin do
9
- # ActiveAdmin::DeviceAuthorization::Routes.draw(self)
10
- # end
8
+ # ActiveAdmin.routes(self)
9
+ # ActiveAdmin::DeviceAuthorization::Routes.draw(self)
11
10
  #
12
- # OAuth JSON endpoints always use absolute gem controller paths so nesting
13
- # under `namespace :admin` does not invent `Admin::ActiveAdmin::...`.
11
+ # Call at the routes root. `draw` applies `config.path_namespace` itself;
12
+ # nesting under another `namespace :admin` double-prefixes the path.
13
+ # Consent UI is owned by the ActiveAdmin page (`Page.register!` /
14
+ # `ActiveAdmin.routes`); do not declare consent routes here.
14
15
  #
15
- # consent: :active_admin_page (default) routes show/approve/deny to the
16
- # ActiveAdmin "Device Auth" page controller (`device_auth#...`).
17
- # consent: :controller routes them to the gem ConsentsController instead.
18
- def self.draw(mapper, config: ActiveAdmin::DeviceAuthorization.config, consent: :active_admin_page)
16
+ # OAuth controllers use absolute gem paths so the admin namespace does not
17
+ # invent `Admin::ActiveAdmin::...` constants. They do not use the admin
18
+ # session; rate-limit procs and protocol parameters gate access.
19
+ def self.draw(mapper, config: ActiveAdmin::DeviceAuthorization.config)
19
20
  config.validate_rate_limits!
20
21
  oauth = config.oauth_path_prefix.to_s
21
- verification = config.verification_path.to_s
22
22
 
23
- mapper.namespace :oauth, path: oauth do
24
- mapper.post :device_authorization,
25
- to: "/active_admin/device_authorization/device_authorizations#create"
26
- mapper.post :token,
27
- to: "/active_admin/device_authorization/tokens#create"
28
- end
29
-
30
- case consent
31
- when :active_admin_page
32
- mapper.get "#{verification}/:user_code",
33
- to: "device_auth#show",
34
- as: :device_auth_grant
35
- mapper.post "#{verification}/:user_code/approve",
36
- to: "device_auth#approve",
37
- as: :approve_device_auth
38
- mapper.post "#{verification}/:user_code/deny",
39
- to: "device_auth#deny",
40
- as: :deny_device_auth
41
- when :controller
42
- mapper.get verification,
43
- to: "/active_admin/device_authorization/consents#index",
44
- as: :device_auth
45
- mapper.get "#{verification}/:user_code",
46
- to: "/active_admin/device_authorization/consents#show",
47
- as: :device_auth_grant
48
- mapper.post "#{verification}/:user_code/approve",
49
- to: "/active_admin/device_authorization/consents#approve",
50
- as: :approve_device_auth
51
- mapper.post "#{verification}/:user_code/deny",
52
- to: "/active_admin/device_authorization/consents#deny",
53
- as: :deny_device_auth
54
- else
55
- raise ArgumentError, "consent must be :active_admin_page or :controller (got #{consent.inspect})"
23
+ mapper.namespace config.path_namespace do
24
+ mapper.namespace :oauth, path: oauth do
25
+ mapper.post :device_authorization,
26
+ to: "/active_admin/device_authorization/device_authorizations#create"
27
+ mapper.post :token,
28
+ to: "/active_admin/device_authorization/tokens#create"
29
+ end
56
30
  end
57
31
  end
58
32
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module ActiveAdmin
4
4
  module DeviceAuthorization
5
- VERSION = "0.2.0"
5
+ VERSION = "0.3.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activeadmin-device_authorization
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Makarov
@@ -57,6 +57,20 @@ dependencies:
57
57
  - - "~>"
58
58
  - !ruby/object:Gem::Version
59
59
  version: '2'
60
+ - !ruby/object:Gem::Dependency
61
+ name: activeadmin-graphql
62
+ requirement: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 0.4.1
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 0.4.1
60
74
  - !ruby/object:Gem::Dependency
61
75
  name: bundler
62
76
  requirement: !ruby/object:Gem::Requirement
@@ -85,20 +99,118 @@ dependencies:
85
99
  - - "~>"
86
100
  - !ruby/object:Gem::Version
87
101
  version: '0.9'
102
+ - !ruby/object:Gem::Dependency
103
+ name: capybara
104
+ requirement: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: '3.40'
109
+ type: :development
110
+ prerelease: false
111
+ version_requirements: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - "~>"
114
+ - !ruby/object:Gem::Version
115
+ version: '3.40'
116
+ - !ruby/object:Gem::Dependency
117
+ name: capybara-playwright-driver
118
+ requirement: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - "~>"
121
+ - !ruby/object:Gem::Version
122
+ version: '0.5'
123
+ type: :development
124
+ prerelease: false
125
+ version_requirements: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - "~>"
128
+ - !ruby/object:Gem::Version
129
+ version: '0.5'
130
+ - !ruby/object:Gem::Dependency
131
+ name: cssbundling-rails
132
+ requirement: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ type: :development
138
+ prerelease: false
139
+ version_requirements: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ - !ruby/object:Gem::Dependency
145
+ name: database_cleaner-active_record
146
+ requirement: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - "~>"
149
+ - !ruby/object:Gem::Version
150
+ version: '2.2'
151
+ type: :development
152
+ prerelease: false
153
+ version_requirements: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - "~>"
156
+ - !ruby/object:Gem::Version
157
+ version: '2.2'
158
+ - !ruby/object:Gem::Dependency
159
+ name: devise
160
+ requirement: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '4.9'
165
+ type: :development
166
+ prerelease: false
167
+ version_requirements: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - ">="
170
+ - !ruby/object:Gem::Version
171
+ version: '4.9'
172
+ - !ruby/object:Gem::Dependency
173
+ name: importmap-rails
174
+ requirement: !ruby/object:Gem::Requirement
175
+ requirements:
176
+ - - ">="
177
+ - !ruby/object:Gem::Version
178
+ version: '0'
179
+ type: :development
180
+ prerelease: false
181
+ version_requirements: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - ">="
184
+ - !ruby/object:Gem::Version
185
+ version: '0'
88
186
  - !ruby/object:Gem::Dependency
89
187
  name: polyrun
90
188
  requirement: !ruby/object:Gem::Requirement
91
189
  requirements:
92
190
  - - ">="
93
191
  - !ruby/object:Gem::Version
94
- version: 2.2.0
192
+ version: 2.2.9
95
193
  type: :development
96
194
  prerelease: false
97
195
  version_requirements: !ruby/object:Gem::Requirement
98
196
  requirements:
99
197
  - - ">="
100
198
  - !ruby/object:Gem::Version
101
- version: 2.2.0
199
+ version: 2.2.9
200
+ - !ruby/object:Gem::Dependency
201
+ name: puma
202
+ requirement: !ruby/object:Gem::Requirement
203
+ requirements:
204
+ - - ">="
205
+ - !ruby/object:Gem::Version
206
+ version: '5.0'
207
+ type: :development
208
+ prerelease: false
209
+ version_requirements: !ruby/object:Gem::Requirement
210
+ requirements:
211
+ - - ">="
212
+ - !ruby/object:Gem::Version
213
+ version: '5.0'
102
214
  - !ruby/object:Gem::Dependency
103
215
  name: rake
104
216
  requirement: !ruby/object:Gem::Requirement
@@ -141,6 +253,20 @@ dependencies:
141
253
  - - "~>"
142
254
  - !ruby/object:Gem::Version
143
255
  version: '3'
256
+ - !ruby/object:Gem::Dependency
257
+ name: rspec-rails
258
+ requirement: !ruby/object:Gem::Requirement
259
+ requirements:
260
+ - - ">="
261
+ - !ruby/object:Gem::Version
262
+ version: '6'
263
+ type: :development
264
+ prerelease: false
265
+ version_requirements: !ruby/object:Gem::Requirement
266
+ requirements:
267
+ - - ">="
268
+ - !ruby/object:Gem::Version
269
+ version: '6'
144
270
  - !ruby/object:Gem::Dependency
145
271
  name: rubocop-rspec
146
272
  requirement: !ruby/object:Gem::Requirement
@@ -169,6 +295,34 @@ dependencies:
169
295
  - - "~>"
170
296
  - !ruby/object:Gem::Version
171
297
  version: '0.7'
298
+ - !ruby/object:Gem::Dependency
299
+ name: sprockets-rails
300
+ requirement: !ruby/object:Gem::Requirement
301
+ requirements:
302
+ - - ">="
303
+ - !ruby/object:Gem::Version
304
+ version: '0'
305
+ type: :development
306
+ prerelease: false
307
+ version_requirements: !ruby/object:Gem::Requirement
308
+ requirements:
309
+ - - ">="
310
+ - !ruby/object:Gem::Version
311
+ version: '0'
312
+ - !ruby/object:Gem::Dependency
313
+ name: sqlite3
314
+ requirement: !ruby/object:Gem::Requirement
315
+ requirements:
316
+ - - ">="
317
+ - !ruby/object:Gem::Version
318
+ version: '2.1'
319
+ type: :development
320
+ prerelease: false
321
+ version_requirements: !ruby/object:Gem::Requirement
322
+ requirements:
323
+ - - ">="
324
+ - !ruby/object:Gem::Version
325
+ version: '2.1'
172
326
  - !ruby/object:Gem::Dependency
173
327
  name: standard
174
328
  requirement: !ruby/object:Gem::Requirement
@@ -242,15 +396,11 @@ files:
242
396
  - SECURITY.md
243
397
  - activeadmin-device_authorization.gemspec
244
398
  - app/controllers/active_admin/device_authorization/application_controller.rb
245
- - app/controllers/active_admin/device_authorization/consents_controller.rb
246
399
  - app/controllers/active_admin/device_authorization/device_authorizations_controller.rb
247
400
  - app/controllers/active_admin/device_authorization/json_response.rb
248
401
  - app/controllers/active_admin/device_authorization/tokens_controller.rb
249
402
  - app/views/active_admin/device_authorization/_consent.html.erb
250
403
  - app/views/active_admin/device_authorization/_enter_code.html.erb
251
- - app/views/active_admin/device_authorization/_standalone_styles.html.erb
252
- - app/views/active_admin/device_authorization/consents/index.html.erb
253
- - app/views/active_admin/device_authorization/consents/show.html.erb
254
404
  - config/locales/en.yml
255
405
  - config/locales/fi.yml
256
406
  - config/locales/sv.yml
@@ -1,83 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module ActiveAdmin
4
- module DeviceAuthorization
5
- # Thin HTML consent actions when the host does not use the ActiveAdmin page.
6
- # Prefer ActiveAdmin::DeviceAuthorization::Page.register! for the AA layout.
7
- class ConsentsController < ApplicationController
8
- before_action :authenticate_resource_owner!
9
- before_action :load_grant!, except: :index
10
-
11
- def index
12
- user_code = UserCode.normalize(params[:user_code])
13
- if user_code.present?
14
- redirect_to send(
15
- :"#{ActiveAdmin::DeviceAuthorization.config.path_namespace}_device_auth_grant_path",
16
- user_code
17
- )
18
- return
19
- end
20
-
21
- render layout: false
22
- end
23
-
24
- def show
25
- @grant.mark_expired_if_needed!
26
- render layout: false
27
- end
28
-
29
- def approve
30
- config = ActiveAdmin::DeviceAuthorization.config
31
- if config.before_approve && !config.before_approve.call(self)
32
- return
33
- end
34
-
35
- Consent.new(
36
- grant: @grant,
37
- resource_owner: current_resource_owner,
38
- scopes: Array(params[:scopes]),
39
- absolute_ttl_seconds: params[:absolute_ttl_seconds]
40
- ).approve!
41
- redirect_to after_consent_path, notice: I18n.t("active_admin.device_authorization.approved")
42
- rescue Consent::Error => error
43
- redirect_to after_consent_path,
44
- alert: I18n.t("active_admin.device_authorization.error.#{error.message}", default: error.message)
45
- end
46
-
47
- def deny
48
- Consent.new(
49
- grant: @grant,
50
- resource_owner: current_resource_owner,
51
- scopes: @grant.requested_scopes,
52
- absolute_ttl_seconds: ActiveAdmin::DeviceAuthorization.config.access_token_default_absolute_ttl
53
- ).deny!
54
- redirect_to after_consent_path, notice: I18n.t("active_admin.device_authorization.denied")
55
- rescue Consent::Error => error
56
- redirect_to after_consent_path,
57
- alert: I18n.t("active_admin.device_authorization.error.#{error.message}", default: error.message)
58
- end
59
-
60
- private
61
-
62
- def authenticate_resource_owner!
63
- return if current_resource_owner
64
-
65
- head :unauthorized
66
- end
67
-
68
- def current_resource_owner
69
- @current_resource_owner ||= ActiveAdmin::DeviceAuthorization.config.resolve_current_resource_owner(self)
70
- end
71
-
72
- def load_grant!
73
- @grant = ActiveAdmin::DeviceAuthorization.config.grant_class
74
- .find_by!(user_code: UserCode.normalize(params[:user_code]))
75
- end
76
-
77
- def after_consent_path
78
- config = ActiveAdmin::DeviceAuthorization.config
79
- "/#{config.path_namespace}/#{config.verification_path}/#{@grant.user_code}"
80
- end
81
- end
82
- end
83
- end
@@ -1,151 +0,0 @@
1
- <style>
2
- .device-auth-standalone {
3
- box-sizing: border-box;
4
- color: #1a1a1a;
5
- font-family: Georgia, "Times New Roman", serif;
6
- line-height: 1.5;
7
- margin: 0 auto;
8
- max-width: 40rem;
9
- padding: 1.5rem 1rem 3rem;
10
- }
11
- .device-auth-standalone *,
12
- .device-auth-standalone *::before,
13
- .device-auth-standalone *::after {
14
- box-sizing: border-box;
15
- }
16
- .device-auth-standalone h1,
17
- .device-auth-standalone .panel-title {
18
- color: #111;
19
- font-family: Georgia, "Times New Roman", serif;
20
- font-size: 1.75rem;
21
- font-weight: 700;
22
- line-height: 1.25;
23
- margin: 0 0 1rem;
24
- }
25
- .device-auth-standalone .panel {
26
- background: #fff;
27
- border: 1px solid #c8c8c8;
28
- margin: 0 0 1.25rem;
29
- padding: 1rem 1.1rem;
30
- }
31
- .device-auth-standalone .panel-body > *:first-child {
32
- margin-top: 0;
33
- }
34
- .device-auth-standalone .panel-body > *:last-child {
35
- margin-bottom: 0;
36
- }
37
- .device-auth-standalone .attributes-table {
38
- margin: 0 0 1rem;
39
- width: 100%;
40
- }
41
- .device-auth-standalone .attributes-table table {
42
- border-collapse: collapse;
43
- width: 100%;
44
- }
45
- .device-auth-standalone .attributes-table th,
46
- .device-auth-standalone .attributes-table td {
47
- border-bottom: 1px solid #ddd;
48
- padding: 0.55rem 0.35rem;
49
- text-align: left;
50
- vertical-align: top;
51
- }
52
- .device-auth-standalone .attributes-table th {
53
- color: #444;
54
- font-weight: 600;
55
- width: 8.5rem;
56
- }
57
- .device-auth-standalone .device-auth-user-code {
58
- font-family: ui-monospace, "Cascadia Code", "Source Code Pro", Menlo, Consolas, monospace;
59
- font-size: 1.15rem;
60
- letter-spacing: 0.08em;
61
- }
62
- .device-auth-standalone .device-auth-verify {
63
- border: 1px solid #8a2f1d;
64
- border-radius: 2px;
65
- background: #f8e8e4;
66
- color: #5c1f14;
67
- margin: 0 0 1rem;
68
- padding: 0.75rem 0.9rem;
69
- }
70
- .device-auth-standalone .inline-hints {
71
- color: #555;
72
- margin: 0 0 1rem;
73
- }
74
- .device-auth-standalone fieldset {
75
- border: 0;
76
- margin: 0 0 1rem;
77
- min-width: 0;
78
- padding: 0;
79
- }
80
- .device-auth-standalone legend,
81
- .device-auth-standalone .label {
82
- color: #222;
83
- display: block;
84
- font-weight: 600;
85
- margin: 0 0 0.4rem;
86
- }
87
- .device-auth-standalone .choice {
88
- list-style: none;
89
- margin: 0 0 0.45rem;
90
- }
91
- .device-auth-standalone .choice label {
92
- display: inline-flex;
93
- gap: 0.45rem;
94
- align-items: flex-start;
95
- }
96
- .device-auth-standalone input[type="text"],
97
- .device-auth-standalone select {
98
- border: 1px solid #888;
99
- border-radius: 2px;
100
- display: block;
101
- font: inherit;
102
- max-width: 100%;
103
- padding: 0.5rem 0.6rem;
104
- width: 100%;
105
- }
106
- .device-auth-standalone input[type="text"]:focus,
107
- .device-auth-standalone select:focus,
108
- .device-auth-standalone input[type="checkbox"]:focus,
109
- .device-auth-standalone input[type="submit"]:focus {
110
- outline: 3px solid #1d4f91;
111
- outline-offset: 2px;
112
- }
113
- .device-auth-standalone .device-auth-actions {
114
- display: flex;
115
- flex-direction: column;
116
- gap: 0.65rem;
117
- }
118
- .device-auth-standalone .device-auth-actions ol {
119
- list-style: none;
120
- margin: 0;
121
- padding: 0;
122
- }
123
- .device-auth-standalone .device-auth-actions input[type="submit"],
124
- .device-auth-standalone fieldset.actions input[type="submit"] {
125
- background: #1d4f91;
126
- border: 1px solid #163d70;
127
- border-radius: 2px;
128
- color: #fff;
129
- cursor: pointer;
130
- font: inherit;
131
- min-height: 2.5rem;
132
- padding: 0.45rem 1rem;
133
- width: 100%;
134
- }
135
- .device-auth-standalone .device-auth-actions .formtastic:last-child input[type="submit"] {
136
- background: #fff;
137
- border-color: #888;
138
- color: #222;
139
- }
140
- @media (min-width: 40rem) {
141
- .device-auth-standalone .device-auth-actions {
142
- flex-direction: row;
143
- flex-wrap: wrap;
144
- align-items: flex-end;
145
- }
146
- .device-auth-standalone .device-auth-actions input[type="submit"],
147
- .device-auth-standalone fieldset.actions input[type="submit"] {
148
- width: auto;
149
- }
150
- }
151
- </style>
@@ -1,19 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="<%= I18n.locale %>">
3
- <head>
4
- <meta charset="utf-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1">
6
- <title><%= t("active_admin.device_authorization.enter_code") %></title>
7
- <%= render partial: "active_admin/device_authorization/standalone_styles" %>
8
- </head>
9
- <body>
10
- <main class="device-auth-standalone">
11
- <h1><%= t("active_admin.device_authorization.enter_code") %></h1>
12
- <div class="panel">
13
- <div class="panel-body">
14
- <%= render partial: "active_admin/device_authorization/enter_code" %>
15
- </div>
16
- </div>
17
- </main>
18
- </body>
19
- </html>
@@ -1,14 +0,0 @@
1
- <!DOCTYPE html>
2
- <html lang="<%= I18n.locale %>">
3
- <head>
4
- <meta charset="utf-8">
5
- <meta name="viewport" content="width=device-width, initial-scale=1">
6
- <title><%= t("active_admin.device_authorization.title") %></title>
7
- <%= render partial: "active_admin/device_authorization/standalone_styles" %>
8
- </head>
9
- <body>
10
- <main class="device-auth-standalone">
11
- <%= render partial: "active_admin/device_authorization/consent", locals: {grant: @grant, heading_level: :h1} %>
12
- </main>
13
- </body>
14
- </html>