maquina-generators 0.10.0 → 0.11.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 +4 -4
- data/README.md +26 -17
- data/lib/generators/maquina/app/app_generator.rb +12 -7
- data/lib/generators/maquina/auth_flavor.rb +59 -0
- data/lib/generators/maquina/backstage_signin/backstage_signin.rb +94 -19
- data/lib/generators/maquina/backstage_signin/templates/test/integration/{backstage_access_test.rb → backstage_access_test.rb.tt} +26 -0
- data/lib/generators/maquina/error_context/USAGE +22 -0
- data/lib/generators/maquina/error_context/error_context_generator.rb +51 -0
- data/lib/generators/maquina/mcp/USAGE +4 -3
- data/lib/generators/maquina/mcp/mcp_generator.rb +14 -8
- data/lib/generators/maquina/mcp/mcp_sibling_tools.rb +19 -1
- data/lib/generators/maquina/mcp/templates/test/test_helpers/{mcp_test_helper.rb → mcp_test_helper.rb.tt} +14 -0
- data/lib/generators/maquina/mission_control_jobs/USAGE +5 -3
- data/lib/generators/maquina/mission_control_jobs/mission_control_jobs_generator.rb +2 -0
- data/lib/generators/maquina/oauth/USAGE +5 -2
- data/lib/generators/maquina/oauth/oauth_generator.rb +20 -13
- data/lib/generators/maquina/oauth/templates/app/models/access_token.rb +3 -3
- data/lib/generators/maquina/oauth/templates/app/models/mcp.rb.tt +12 -0
- data/lib/generators/maquina/oauth/templates/app/models/oauth_authorization_code.rb +1 -1
- data/lib/generators/maquina/oauth/templates/test/models/{access_token_test.rb → access_token_test.rb.tt} +2 -0
- data/lib/generators/maquina/passkeys/USAGE +4 -2
- data/lib/generators/maquina/passkeys/passkeys_generator.rb +16 -11
- data/lib/generators/maquina/passkeys/templates/app/controllers/session/{passkeys_controller.rb → passkeys_controller.rb.tt} +6 -2
- data/lib/generators/maquina/passkeys/templates/config/locales/{passkeys.en.yml → passkeys.en.yml.tt} +15 -0
- data/lib/generators/maquina/passkeys/templates/config/locales/{passkeys.es.yml → passkeys.es.yml.tt} +15 -0
- data/lib/generators/maquina/passkeys/templates/test/integration/{passkeys_test.rb → passkeys_test.rb.tt} +6 -0
- data/lib/generators/maquina/registration/registration_generator.rb +32 -0
- data/lib/generators/maquina/registration/templates/app/views/registrations/new.html.erb.tt +4 -0
- data/lib/generators/maquina/registration/templates/app/views/sessions/new.html.erb.tt +5 -0
- data/lib/generators/maquina/security/USAGE +5 -3
- data/lib/generators/maquina/security/security_generator.rb +2 -0
- data/lib/generators/maquina/solid_errors/USAGE +9 -4
- data/lib/generators/maquina/solid_errors/error_context_files.rb +82 -0
- data/lib/generators/maquina/solid_errors/solid_errors_generator.rb +12 -0
- data/lib/generators/maquina/solid_errors/templates/app/agents/errors_query.rb +40 -4
- data/lib/generators/maquina/solid_errors/templates/app/agents/failure_triage.rb +4 -2
- data/lib/generators/maquina/solid_errors/templates/app/agents/list_failures_tool.rb +9 -4
- data/lib/generators/maquina/solid_errors/templates/app/models/mcp/tools/solid_errors.rb.tt +24 -6
- data/lib/generators/maquina/solid_errors/templates/config/initializers/error_context.rb +33 -0
- data/lib/generators/maquina/solid_errors/templates/test/models/mcp/tools/solid_errors_filters_test.rb +68 -0
- data/lib/maquina_generators/version.rb +1 -1
- data/test/fixtures/error_context/0.10.0/app/agents/errors_query.rb +105 -0
- data/test/fixtures/error_context/0.10.0/app/agents/failure_triage.rb +21 -0
- data/test/fixtures/error_context/0.10.0/app/agents/list_failures_tool.rb +20 -0
- data/test/fixtures/error_context/0.10.0/app/models/mcp/tools/solid_errors.rb +81 -0
- data/test/fixtures/error_context/0.10.0/app/models/mcp/tools/solid_errors_write.rb +101 -0
- data/test/generators/maquina/app_generator_test.rb +14 -10
- data/test/generators/maquina/backstage_signin_test.rb +89 -2
- data/test/generators/maquina/error_context_generator_test.rb +188 -0
- data/test/generators/maquina/mcp_generator_test.rb +3 -0
- data/test/generators/maquina/oauth_generator_test.rb +51 -3
- data/test/generators/maquina/passkeys_generator_test.rb +62 -2
- data/test/generators/maquina/registration_generator_test.rb +62 -0
- data/test/generators/maquina/solid_errors_generator_test.rb +56 -0
- data/test/support/rails_auth_app.rb +48 -0
- metadata +25 -11
|
@@ -66,7 +66,7 @@ module Maquina
|
|
|
66
66
|
end
|
|
67
67
|
|
|
68
68
|
# 5b. Who sees the admin-only tools: User#backstage_access?, the same
|
|
69
|
-
#
|
|
69
|
+
# gate that opens the backstage (BackstageSignin).
|
|
70
70
|
def add_backstage_access
|
|
71
71
|
add_backstage_access_to_user
|
|
72
72
|
end
|
|
@@ -91,7 +91,7 @@ module Maquina
|
|
|
91
91
|
|
|
92
92
|
# 8. Test helper and the generated tests
|
|
93
93
|
def create_tests
|
|
94
|
-
|
|
94
|
+
template "test/test_helpers/mcp_test_helper.rb.tt", "test/test_helpers/mcp_test_helper.rb"
|
|
95
95
|
template "test/integration/mcp_flow_test.rb.tt", "test/integration/mcp_flow_test.rb"
|
|
96
96
|
copy_file "test/integration/mcp_conformance_test.rb", "test/integration/mcp_conformance_test.rb"
|
|
97
97
|
template "test/models/mcp/tools_test.rb.tt", "test/models/mcp/tools_test.rb"
|
|
@@ -126,14 +126,20 @@ module Maquina
|
|
|
126
126
|
say "Installed MCP tools for: #{@sibling_tools.join(", ")} (admin only)", :yellow
|
|
127
127
|
say ""
|
|
128
128
|
end
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
129
|
+
if rails_auth?
|
|
130
|
+
say "Admin-only tools are for admins of an account with the backstage on (User#backstage_access?):", :yellow
|
|
131
|
+
say " bin/rails db:migrate adds accounts.allow_backstage (off). Turn it on for your account:"
|
|
132
|
+
say " Account.find_by!(name: \"...\").update!(allow_backstage: true)"
|
|
133
|
+
else
|
|
134
|
+
say "Admin-only tools are for people on the backstage list (User#backstage_access?):", :yellow
|
|
135
|
+
say " backstage:"
|
|
136
|
+
say " emails:"
|
|
137
|
+
say " - you@example.com"
|
|
138
|
+
say " in bin/rails credentials:edit. An account's admin role is not enough."
|
|
139
|
+
end
|
|
134
140
|
say ""
|
|
135
141
|
if backstage_signin_pending.any?
|
|
136
|
-
say "Add User
|
|
142
|
+
say "Add User#backstage_access? (and, with Clave, User.backstage_emails) to app/models/user.rb by hand:", :yellow
|
|
137
143
|
say backstage_access_methods
|
|
138
144
|
end
|
|
139
145
|
if options[:flag]
|
|
@@ -40,7 +40,25 @@ module Maquina
|
|
|
40
40
|
[sibling, "test/models/mcp/tools/#{sibling}_test.rb"]
|
|
41
41
|
]
|
|
42
42
|
|
|
43
|
-
files.map { |owner, path| install_mcp_sibling_file(owner, path, write: write) }
|
|
43
|
+
written = files.map { |owner, path| install_mcp_sibling_file(owner, path, write: write) }
|
|
44
|
+
(written + install_mcp_feature_tests(sibling)).any?
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Host-app tests for a feature a sibling's tool file gained after it
|
|
48
|
+
# first shipped, as [test path, text the tool file has once it has the
|
|
49
|
+
# feature]. An app whose tool file predates the feature gets no test that
|
|
50
|
+
# would fail against it.
|
|
51
|
+
FEATURE_TESTS = {
|
|
52
|
+
solid_errors: [["test/models/mcp/tools/solid_errors_filters_test.rb", "def self.list_failures(scope:, limit: 20, user_id: nil"]]
|
|
53
|
+
}.freeze
|
|
54
|
+
|
|
55
|
+
def install_mcp_feature_tests(sibling)
|
|
56
|
+
tool = File.join(destination_root, "app/models/mcp/tools/#{sibling}.rb")
|
|
57
|
+
return [] unless File.exist?(tool)
|
|
58
|
+
|
|
59
|
+
FEATURE_TESTS.fetch(sibling.to_sym, []).map do |path, marker|
|
|
60
|
+
File.read(tool).include?(marker) && install_mcp_sibling_file(sibling, path, write: false)
|
|
61
|
+
end
|
|
44
62
|
end
|
|
45
63
|
|
|
46
64
|
# The write tools each sibling's --mcp-write adds.
|
|
@@ -14,25 +14,39 @@
|
|
|
14
14
|
# mcp_flag(false) to test it off. Both go through the ENV fallback in
|
|
15
15
|
# Mcp.enabled_for?: if the app moves its flag elsewhere, change mcp_flag.
|
|
16
16
|
#
|
|
17
|
+
<% unless rails_auth? -%>
|
|
17
18
|
# The admin-only tools are for people on the backstage list
|
|
18
19
|
# (User#backstage_access?). Every test starts with the mcp_admin fixture on it,
|
|
19
20
|
# through config.x.backstage_emails, which User.backstage_emails reads first.
|
|
21
|
+
<% else -%>
|
|
22
|
+
# The admin-only tools are for admins of an account with the backstage on
|
|
23
|
+
# (User#backstage_access?). Every test starts with the mcp account's
|
|
24
|
+
# allow_backstage on, so mcp_admin has them and mcp_member does not.
|
|
25
|
+
<% end -%>
|
|
20
26
|
module McpTestHelper
|
|
21
27
|
MCP_PROTOCOL_VERSION_META = "io.modelcontextprotocol/protocolVersion"
|
|
22
28
|
MCP_CLIENT_CAPABILITIES_META = "io.modelcontextprotocol/clientCapabilities"
|
|
23
29
|
# Integration tests run on www.example.com, so tokens are issued for it.
|
|
24
30
|
MCP_RESOURCE = "http://www.example.com/mcp"
|
|
31
|
+
<% unless rails_auth? -%>
|
|
25
32
|
MCP_BACKSTAGE_EMAILS = %w[mcp-admin@example.com].freeze
|
|
33
|
+
<% end -%>
|
|
26
34
|
|
|
27
35
|
def self.included(base)
|
|
28
36
|
base.setup do
|
|
29
37
|
mcp_flag(true)
|
|
38
|
+
<% unless rails_auth? -%>
|
|
30
39
|
@backstage_emails_before = Rails.configuration.x.backstage_emails
|
|
31
40
|
Rails.configuration.x.backstage_emails = MCP_BACKSTAGE_EMAILS.dup
|
|
41
|
+
<% else -%>
|
|
42
|
+
accounts(:mcp).update_column(:allow_backstage, true) if respond_to?(:accounts, true) # fixture accessors are private
|
|
43
|
+
<% end -%>
|
|
32
44
|
end
|
|
33
45
|
base.teardown do
|
|
34
46
|
restore_mcp_flag
|
|
47
|
+
<% unless rails_auth? -%>
|
|
35
48
|
Rails.configuration.x.backstage_emails = @backstage_emails_before
|
|
49
|
+
<% end -%>
|
|
36
50
|
end
|
|
37
51
|
end
|
|
38
52
|
|
|
@@ -5,9 +5,11 @@ Description:
|
|
|
5
5
|
|
|
6
6
|
Authentication tries Rails credentials (backstage.username/password)
|
|
7
7
|
first, then falls back to environment variables.
|
|
8
|
-
With
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
With the app's own sign-in installed, the backstage is behind it instead,
|
|
9
|
+
and the Basic Auth is taken out of the files it wrote. With Clave, users
|
|
10
|
+
listed under backstage.emails in credentials get in; with Rails' sign-in
|
|
11
|
+
(maquina:registration), admins of an account whose allow_backstage is on
|
|
12
|
+
(a column it adds, off by default). Anyone else gets a 404.
|
|
11
13
|
|
|
12
14
|
Examples:
|
|
13
15
|
rails g maquina:mission_control_jobs --prefix /admin
|
|
@@ -35,6 +35,8 @@ module Maquina
|
|
|
35
35
|
|
|
36
36
|
# 3. Initializer
|
|
37
37
|
def create_initializer
|
|
38
|
+
return if backstage_signin_converted?(MISSION_CONTROL_INITIALIZER, "http_basic_auth_user")
|
|
39
|
+
|
|
38
40
|
template "config/initializers/mission_control.rb.tt",
|
|
39
41
|
"config/initializers/mission_control.rb"
|
|
40
42
|
end
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
Description:
|
|
2
2
|
Adds OAuth 2.1 sign-in for MCP clients (the MCP Inspector, Claude, ...) on
|
|
3
|
-
top of Clave's email
|
|
4
|
-
|
|
3
|
+
top of the app's own sign-in: Clave's email code (maquina:clave) or Rails'
|
|
4
|
+
password (maquina:registration), one of which it needs. Run maquina:mcp
|
|
5
|
+
afterwards for the /mcp server the tokens unlock. Tokens and codes go
|
|
6
|
+
through Mcp.enabled_for?(user) in app/models/mcp.rb, which the app owns
|
|
7
|
+
(with Clave it turns away a blocked user).
|
|
5
8
|
|
|
6
9
|
Clients identify themselves with a Client ID Metadata Document: the
|
|
7
10
|
client_id is an https URL, fetched by a guarded plain-Ruby fetcher that
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
require "rails/generators"
|
|
2
2
|
require "rails/generators/migration"
|
|
3
|
+
require_relative "../auth_flavor"
|
|
3
4
|
|
|
4
5
|
module Maquina
|
|
5
6
|
module Generators
|
|
6
7
|
class OauthGenerator < Rails::Generators::Base
|
|
7
8
|
include Rails::Generators::Migration
|
|
9
|
+
include AuthFlavor
|
|
8
10
|
|
|
9
11
|
source_root File.expand_path("templates", __dir__)
|
|
10
12
|
|
|
@@ -20,11 +22,12 @@ module Maquina
|
|
|
20
22
|
STATIC_FILE_PREFIXES = /(STATIC_FILE_PREFIXES = %w\[)([^\]]*)(\])/
|
|
21
23
|
OAUTH_EXEMPTED = /start_with\?\([^)]*"\/mcp", "\/oauth\/token"|STATIC_FILE_PREFIXES = %w\[[^\]]*\/mcp \/oauth\/token/
|
|
22
24
|
|
|
23
|
-
# The people and account the generated tests (and maquina:mcp's) sign in
|
|
25
|
+
# The people and account the generated tests (and maquina:mcp's) sign in
|
|
26
|
+
# as, with their role (auth_fixture_attributes adds what the sign-in needs).
|
|
24
27
|
USER_FIXTURES = {
|
|
25
|
-
"mcp_member" => {"email_address" => "mcp-member@example.com", "name" => "MCP Member", "
|
|
26
|
-
"mcp_admin" => {"email_address" => "mcp-admin@example.com", "name" => "MCP Admin", "
|
|
27
|
-
"mcp_other" => {"email_address" => "mcp-other@example.com", "name" => "MCP Other", "
|
|
28
|
+
"mcp_member" => [{"email_address" => "mcp-member@example.com", "name" => "MCP Member"}, "member"],
|
|
29
|
+
"mcp_admin" => [{"email_address" => "mcp-admin@example.com", "name" => "MCP Admin"}, "admin"],
|
|
30
|
+
"mcp_other" => [{"email_address" => "mcp-other@example.com", "name" => "MCP Other"}, "member"]
|
|
28
31
|
}.freeze
|
|
29
32
|
|
|
30
33
|
class_option :prefix, type: :string, default: "/settings",
|
|
@@ -42,12 +45,10 @@ module Maquina
|
|
|
42
45
|
(existing && existing >= current) ? existing.succ : current
|
|
43
46
|
end
|
|
44
47
|
|
|
45
|
-
# 1. The consent screen signs people in through
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
raise Thor::Error, "maquina:oauth signs people in through Clave. Run `rails g maquina:clave` first."
|
|
50
|
-
end
|
|
48
|
+
# 1. The consent screen signs people in through the app's own sign-in:
|
|
49
|
+
# Clave's email code or Rails' password (AuthFlavor).
|
|
50
|
+
def require_authentication
|
|
51
|
+
require_authentication!("oauth")
|
|
51
52
|
|
|
52
53
|
return if flag_name.nil? || flag_name.match?(FLAG_NAME)
|
|
53
54
|
|
|
@@ -123,16 +124,17 @@ module Maquina
|
|
|
123
124
|
copy_file "test/fixtures/access_tokens.yml", "test/fixtures/access_tokens.yml"
|
|
124
125
|
|
|
125
126
|
append_fixture "test/fixtures/accounts.yml", "mcp", {"name" => "MCP"}
|
|
126
|
-
USER_FIXTURES.each do |label, attributes|
|
|
127
|
-
append_fixture "test/fixtures/users.yml", label, attributes.merge("account" => "mcp")
|
|
127
|
+
USER_FIXTURES.each do |label, (attributes, role)|
|
|
128
|
+
append_fixture "test/fixtures/users.yml", label, attributes.merge(auth_fixture_attributes(role: role), "account" => "mcp")
|
|
128
129
|
end
|
|
130
|
+
@role_pending = rails_auth? && !user_role_enum?
|
|
129
131
|
end
|
|
130
132
|
|
|
131
133
|
# 10. Test helper and the generated tests
|
|
132
134
|
def create_tests
|
|
133
135
|
template "test/test_helpers/oauth_test_helper.rb.tt", "test/test_helpers/oauth_test_helper.rb"
|
|
134
136
|
copy_file "test/models/oauth_authorization_code_test.rb", "test/models/oauth_authorization_code_test.rb"
|
|
135
|
-
|
|
137
|
+
template "test/models/access_token_test.rb.tt", "test/models/access_token_test.rb"
|
|
136
138
|
copy_file "test/models/connected_application_test.rb", "test/models/connected_application_test.rb"
|
|
137
139
|
copy_file "test/integration/oauth_discovery_test.rb", "test/integration/oauth_discovery_test.rb"
|
|
138
140
|
template "test/integration/oauth_authorization_test.rb.tt", "test/integration/oauth_authorization_test.rb"
|
|
@@ -203,6 +205,11 @@ module Maquina
|
|
|
203
205
|
say " app/views/connected_applications/index.html.erb # @connected_applications"
|
|
204
206
|
say ""
|
|
205
207
|
end
|
|
208
|
+
if @role_pending
|
|
209
|
+
say "test/fixtures/users.yml: User has no role enum, so mcp_admin, mcp_member and", :yellow
|
|
210
|
+
say " mcp_other got no role. Give mcp_admin whatever makes a user an admin in this app."
|
|
211
|
+
say ""
|
|
212
|
+
end
|
|
206
213
|
if @user_pending
|
|
207
214
|
say "Add to app/models/user.rb, so deleting a person drops their grants:", :yellow
|
|
208
215
|
say user_associations
|
|
@@ -36,12 +36,12 @@ class AccessToken < ApplicationRecord
|
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
# The live token for a raw access token: not revoked, not expired, and its
|
|
39
|
-
# user
|
|
39
|
+
# user still let through Mcp.enabled_for? (app/models/mcp.rb). nil otherwise.
|
|
40
40
|
def self.authenticate(raw)
|
|
41
41
|
return if raw.blank?
|
|
42
42
|
|
|
43
43
|
token = unrevoked.where("expires_at > ?", Time.current).find_by(token_digest: digest(raw))
|
|
44
|
-
token
|
|
44
|
+
token if token && Mcp.enabled_for?(token.user)
|
|
45
45
|
end
|
|
46
46
|
|
|
47
47
|
# The unrevoked row a raw refresh token belongs to, or nil.
|
|
@@ -54,7 +54,7 @@ class AccessToken < ApplicationRecord
|
|
|
54
54
|
# expire, but they are bound to the client they were issued to and work
|
|
55
55
|
# once; anything else raises InvalidGrant.
|
|
56
56
|
def rotate!(client:)
|
|
57
|
-
raise InvalidGrant unless oauth_client == client &&
|
|
57
|
+
raise InvalidGrant unless oauth_client == client && Mcp.enabled_for?(user)
|
|
58
58
|
|
|
59
59
|
transaction do
|
|
60
60
|
raise InvalidGrant unless self.class.unrevoked.where(id: id).update_all(revoked_at: Time.current) == 1
|
|
@@ -13,8 +13,20 @@ module Mcp
|
|
|
13
13
|
#
|
|
14
14
|
# With a FLAG, it reads credentials (features: { <flag>: true }) and then
|
|
15
15
|
# the upcased ENV variable.
|
|
16
|
+
<% if clave? -%>
|
|
17
|
+
#
|
|
18
|
+
# A blocked user is turned away everywhere, tokens and codes included.
|
|
19
|
+
def self.enabled_for?(user)
|
|
20
|
+
return false if user&.blocked?
|
|
21
|
+
return true if FLAG.nil?
|
|
22
|
+
<% else -%>
|
|
23
|
+
#
|
|
24
|
+
# Tokens and codes are checked here too, so a check that turns a person away
|
|
25
|
+
# (a deactivated user, a suspended account) goes first:
|
|
26
|
+
# return false unless user&.active?
|
|
16
27
|
def self.enabled_for?(user)
|
|
17
28
|
return true if FLAG.nil?
|
|
29
|
+
<% end -%>
|
|
18
30
|
|
|
19
31
|
ActiveModel::Type::Boolean.new.cast(Rails.application.credentials.dig(:features, FLAG) || ENV[FLAG.to_s.upcase]) == true
|
|
20
32
|
end
|
|
@@ -37,7 +37,7 @@ class OauthAuthorizationCode < ApplicationRecord
|
|
|
37
37
|
end
|
|
38
38
|
|
|
39
39
|
def redeemable?(client:, redirect_uri:, verifier:)
|
|
40
|
-
used_at.nil? && expires_at.future? && oauth_client == client &&
|
|
40
|
+
used_at.nil? && expires_at.future? && oauth_client == client && Mcp.enabled_for?(user) &&
|
|
41
41
|
redirect_uri.to_s == self.redirect_uri && verifier_matches?(verifier)
|
|
42
42
|
end
|
|
43
43
|
|
|
@@ -32,12 +32,14 @@ class AccessTokenTest < ActiveSupport::TestCase
|
|
|
32
32
|
assert_nil AccessToken.authenticate(access)
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
+
<% if clave? -%>
|
|
35
36
|
test "a blocked user's token stops working" do
|
|
36
37
|
users(:mcp_member).block!(reason: "test")
|
|
37
38
|
|
|
38
39
|
assert_nil AccessToken.authenticate(MEMBER_READ_ACCESS_TOKEN)
|
|
39
40
|
end
|
|
40
41
|
|
|
42
|
+
<% end -%>
|
|
41
43
|
test "rotation revokes the row and keeps the scope" do
|
|
42
44
|
token = access_tokens(:mcp_member_write)
|
|
43
45
|
access, refresh = token.rotate!(client: @client)
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
Description:
|
|
2
|
-
Adds passkey (WebAuthn) sign-in
|
|
3
|
-
|
|
2
|
+
Adds passkey (WebAuthn) sign-in beside the app's own sign-in: Clave's
|
|
3
|
+
email code (maquina:clave) or Rails' password (maquina:registration), one
|
|
4
|
+
of which it needs. The email code or the password stays as the way in
|
|
5
|
+
without a passkey.
|
|
4
6
|
|
|
5
7
|
Adds the webauthn gem, which only verifies the ceremonies; the flow is
|
|
6
8
|
generated into the app. Installs the Passkey model and a webauthn_id on
|
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
require "rails/generators"
|
|
2
2
|
require "rails/generators/migration"
|
|
3
|
+
require_relative "../auth_flavor"
|
|
3
4
|
|
|
4
5
|
module Maquina
|
|
5
6
|
module Generators
|
|
6
7
|
class PasskeysGenerator < Rails::Generators::Base
|
|
7
8
|
include Rails::Generators::Migration
|
|
9
|
+
include AuthFlavor
|
|
8
10
|
|
|
9
11
|
source_root File.expand_path("templates", __dir__)
|
|
10
12
|
|
|
11
13
|
GEM_LINE = %(gem "webauthn", "~> 3.4")
|
|
12
14
|
USER_SESSIONS = /^ has_many :sessions, dependent: :destroy\n/
|
|
13
15
|
USER_PRIVATE = /^ private\n/
|
|
16
|
+
CLASS_END = /^end\s*\z/
|
|
14
17
|
SIGN_IN_FORM_END = /^ <% end %>\n/
|
|
15
18
|
EMAIL_AUTOCOMPLETE = %(autocomplete: "email")
|
|
16
19
|
TEST_HELP = /^require "rails\/test_help"\n/
|
|
@@ -28,13 +31,11 @@ module Maquina
|
|
|
28
31
|
(existing && existing >= current) ? existing.succ : current
|
|
29
32
|
end
|
|
30
33
|
|
|
31
|
-
# 1. Passkeys sign people in through
|
|
32
|
-
# stays as the way to get in
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
raise Thor::Error, "maquina:passkeys adds passkeys to Clave's email-code sign-in. Run `rails g maquina:clave` first."
|
|
34
|
+
# 1. Passkeys sign people in through the app's own sessions (Clave's or
|
|
35
|
+
# Rails'); the email code or the password stays as the way to get in
|
|
36
|
+
# without one.
|
|
37
|
+
def require_authentication
|
|
38
|
+
require_authentication!("passkeys")
|
|
38
39
|
end
|
|
39
40
|
|
|
40
41
|
# 2. The webauthn gem verifies the ceremonies; the flow stays in the app.
|
|
@@ -70,6 +71,10 @@ module Maquina
|
|
|
70
71
|
if content.match?(USER_SESSIONS) && content.scan(USER_PRIVATE).one?
|
|
71
72
|
inject_into_file path, user_association, after: USER_SESSIONS
|
|
72
73
|
inject_into_file path, user_webauthn_id, before: USER_PRIVATE
|
|
74
|
+
elsif content.match?(USER_SESSIONS) && !content.match?(USER_PRIVATE) && content.match?(CLASS_END)
|
|
75
|
+
# Rails' User has no private section: the method goes last.
|
|
76
|
+
inject_into_file path, user_association, after: USER_SESSIONS
|
|
77
|
+
inject_into_file path, "\n#{user_webauthn_id.chomp}", before: CLASS_END
|
|
73
78
|
else
|
|
74
79
|
@user_pending = true
|
|
75
80
|
end
|
|
@@ -80,7 +85,7 @@ module Maquina
|
|
|
80
85
|
copy_file "app/controllers/concerns/passkey_challenge.rb", "app/controllers/concerns/passkey_challenge.rb"
|
|
81
86
|
copy_file "app/controllers/concerns/passkey_reauthentication.rb", "app/controllers/concerns/passkey_reauthentication.rb"
|
|
82
87
|
copy_file "app/controllers/session/passkey_challenges_controller.rb", "app/controllers/session/passkey_challenges_controller.rb"
|
|
83
|
-
|
|
88
|
+
template "app/controllers/session/passkeys_controller.rb.tt", "app/controllers/session/passkeys_controller.rb"
|
|
84
89
|
copy_file "app/controllers/passkey_challenges_controller.rb", "app/controllers/passkey_challenges_controller.rb"
|
|
85
90
|
copy_file "app/controllers/passkeys_controller.rb", "app/controllers/passkeys_controller.rb"
|
|
86
91
|
end
|
|
@@ -122,14 +127,14 @@ module Maquina
|
|
|
122
127
|
|
|
123
128
|
# 9. Locale files
|
|
124
129
|
def create_locale_files
|
|
125
|
-
|
|
126
|
-
|
|
130
|
+
template "config/locales/passkeys.en.yml.tt", "config/locales/passkeys.en.yml"
|
|
131
|
+
template "config/locales/passkeys.es.yml.tt", "config/locales/passkeys.es.yml"
|
|
127
132
|
end
|
|
128
133
|
|
|
129
134
|
# 10. Test helper and the flow's integration test
|
|
130
135
|
def create_test_helper
|
|
131
136
|
copy_file "test/test_helpers/webauthn_test_helper.rb", "test/test_helpers/webauthn_test_helper.rb"
|
|
132
|
-
|
|
137
|
+
template "test/integration/passkeys_test.rb.tt", "test/integration/passkeys_test.rb"
|
|
133
138
|
|
|
134
139
|
path = File.join(destination_root, "test/test_helper.rb")
|
|
135
140
|
return unless File.exist?(path)
|
|
@@ -2,7 +2,7 @@ class Session::PasskeysController < ApplicationController
|
|
|
2
2
|
include PasskeyChallenge
|
|
3
3
|
|
|
4
4
|
allow_unauthenticated_access
|
|
5
|
-
rate_limit to: 10, within: 3.minutes, only: :create, with: -> { render json: {error: t("flash.
|
|
5
|
+
rate_limit to: 10, within: 3.minutes, only: :create, with: -> { render json: {error: t("flash.passkeys.rate_limited")}, status: :too_many_requests }
|
|
6
6
|
|
|
7
7
|
# POST /session/passkey
|
|
8
8
|
def create
|
|
@@ -22,7 +22,11 @@ class Session::PasskeysController < ApplicationController
|
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
user = passkey.user
|
|
25
|
+
<% if clave? -%>
|
|
25
26
|
if user.blocked? || credential.user_handle != user.webauthn_id
|
|
27
|
+
<% else -%>
|
|
28
|
+
if credential.user_handle != user.webauthn_id
|
|
29
|
+
<% end -%>
|
|
26
30
|
Rails.logger.info "[AUTH] Passkey sign-in refused for #{user.email_address} from #{request.remote_ip}"
|
|
27
31
|
return render_failure(t("flash.passkeys.failed"))
|
|
28
32
|
end
|
|
@@ -35,7 +39,7 @@ class Session::PasskeysController < ApplicationController
|
|
|
35
39
|
|
|
36
40
|
Rails.logger.info "[AUTH] Successful passkey login for #{user.email_address} from #{request.remote_ip}"
|
|
37
41
|
start_new_session_for(user)
|
|
38
|
-
flash[:notice] = t("flash.
|
|
42
|
+
flash[:notice] = t("flash.passkeys.signed_in")
|
|
39
43
|
render json: {redirect_url: after_authentication_url}
|
|
40
44
|
rescue WebAuthn::SignCountVerificationError
|
|
41
45
|
# A valid signature with a counter that went backwards: possibly a cloned
|
data/lib/generators/maquina/passkeys/templates/config/locales/{passkeys.en.yml → passkeys.en.yml.tt}
RENAMED
|
@@ -4,11 +4,18 @@ en:
|
|
|
4
4
|
created: "Passkey added. You can now sign in with it."
|
|
5
5
|
updated: "Passkey renamed."
|
|
6
6
|
destroyed: "Passkey removed."
|
|
7
|
+
<% if clave? -%>
|
|
7
8
|
failed: "We couldn't sign you in with that passkey. Try again or use your email."
|
|
8
9
|
unknown: "That passkey is no longer linked to an account. Sign in with your email instead."
|
|
10
|
+
<% else -%>
|
|
11
|
+
failed: "We couldn't sign you in with that passkey. Try again or use your password."
|
|
12
|
+
unknown: "That passkey is no longer linked to an account. Sign in with your password instead."
|
|
13
|
+
<% end -%>
|
|
9
14
|
challenge_expired: "The request expired. Please try again."
|
|
10
15
|
registration_failed: "We couldn't add that passkey. Please try again."
|
|
11
16
|
reauthenticate: "Sign in again to manage your passkeys."
|
|
17
|
+
signed_in: "You have signed in successfully."
|
|
18
|
+
rate_limited: "Too many attempts. Please wait a few minutes and try again."
|
|
12
19
|
|
|
13
20
|
sessions:
|
|
14
21
|
passkey:
|
|
@@ -19,7 +26,11 @@ en:
|
|
|
19
26
|
passkeys:
|
|
20
27
|
index:
|
|
21
28
|
title: "Passkeys"
|
|
29
|
+
<% if clave? -%>
|
|
22
30
|
description: "Sign in with Face ID, a fingerprint or your device PIN instead of an email code."
|
|
31
|
+
<% else -%>
|
|
32
|
+
description: "Sign in with Face ID, a fingerprint or your device PIN instead of your password."
|
|
33
|
+
<% end -%>
|
|
23
34
|
add: "Add a passkey"
|
|
24
35
|
empty: "You don't have any passkeys yet."
|
|
25
36
|
rename: "Save"
|
|
@@ -30,7 +41,11 @@ en:
|
|
|
30
41
|
last_used: "Last used %{time} ago"
|
|
31
42
|
never_used: "Not used yet"
|
|
32
43
|
add_another: "This passkey lives on one device. Add another so you can still sign in if you lose it."
|
|
44
|
+
<% if clave? -%>
|
|
33
45
|
fallback: "You can always sign in with a code sent to your email."
|
|
46
|
+
<% else -%>
|
|
47
|
+
fallback: "You can always sign in with your email and password."
|
|
48
|
+
<% end -%>
|
|
34
49
|
unsupported: "This browser doesn't support passkeys."
|
|
35
50
|
error: "Something went wrong with the passkey. Please try again."
|
|
36
51
|
duplicate: "This device already has a passkey for your account."
|
data/lib/generators/maquina/passkeys/templates/config/locales/{passkeys.es.yml → passkeys.es.yml.tt}
RENAMED
|
@@ -4,11 +4,18 @@ es:
|
|
|
4
4
|
created: "Llave de acceso agregada. Ya puedes iniciar sesión con ella."
|
|
5
5
|
updated: "Llave de acceso renombrada."
|
|
6
6
|
destroyed: "Llave de acceso eliminada."
|
|
7
|
+
<% if clave? -%>
|
|
7
8
|
failed: "No pudimos iniciar tu sesión con esa llave de acceso. Inténtalo de nuevo o usa tu correo."
|
|
8
9
|
unknown: "Esa llave de acceso ya no está vinculada a una cuenta. Inicia sesión con tu correo."
|
|
10
|
+
<% else -%>
|
|
11
|
+
failed: "No pudimos iniciar tu sesión con esa llave de acceso. Inténtalo de nuevo o usa tu contraseña."
|
|
12
|
+
unknown: "Esa llave de acceso ya no está vinculada a una cuenta. Inicia sesión con tu contraseña."
|
|
13
|
+
<% end -%>
|
|
9
14
|
challenge_expired: "La solicitud expiró. Inténtalo de nuevo."
|
|
10
15
|
registration_failed: "No pudimos agregar esa llave de acceso. Inténtalo de nuevo."
|
|
11
16
|
reauthenticate: "Inicia sesión de nuevo para administrar tus llaves de acceso."
|
|
17
|
+
signed_in: "Iniciaste sesión."
|
|
18
|
+
rate_limited: "Demasiados intentos. Espera unos minutos e inténtalo de nuevo."
|
|
12
19
|
|
|
13
20
|
sessions:
|
|
14
21
|
passkey:
|
|
@@ -19,7 +26,11 @@ es:
|
|
|
19
26
|
passkeys:
|
|
20
27
|
index:
|
|
21
28
|
title: "Llaves de acceso"
|
|
29
|
+
<% if clave? -%>
|
|
22
30
|
description: "Inicia sesión con Face ID, tu huella o el PIN de tu dispositivo en lugar de un código por correo."
|
|
31
|
+
<% else -%>
|
|
32
|
+
description: "Inicia sesión con Face ID, tu huella o el PIN de tu dispositivo en lugar de tu contraseña."
|
|
33
|
+
<% end -%>
|
|
23
34
|
add: "Agregar llave de acceso"
|
|
24
35
|
empty: "Todavía no tienes llaves de acceso."
|
|
25
36
|
rename: "Guardar"
|
|
@@ -30,7 +41,11 @@ es:
|
|
|
30
41
|
last_used: "Usada hace %{time}"
|
|
31
42
|
never_used: "Sin usar"
|
|
32
43
|
add_another: "Esta llave de acceso vive en un solo dispositivo. Agrega otra para no perder el acceso si lo pierdes."
|
|
44
|
+
<% if clave? -%>
|
|
33
45
|
fallback: "Siempre puedes iniciar sesión con un código enviado a tu correo."
|
|
46
|
+
<% else -%>
|
|
47
|
+
fallback: "Siempre puedes iniciar sesión con tu correo y contraseña."
|
|
48
|
+
<% end -%>
|
|
34
49
|
unsupported: "Este navegador no es compatible con llaves de acceso."
|
|
35
50
|
error: "Algo salió mal con la llave de acceso. Inténtalo de nuevo."
|
|
36
51
|
duplicate: "Este dispositivo ya tiene una llave de acceso para tu cuenta."
|
|
@@ -5,7 +5,11 @@ require_relative "../test_helpers/session_test_helper"
|
|
|
5
5
|
# WebAuthn::FakeClient as the browser and the authenticator.
|
|
6
6
|
class PasskeysTest < ActionDispatch::IntegrationTest
|
|
7
7
|
setup do
|
|
8
|
+
<% if clave? -%>
|
|
8
9
|
@user = User.create!(account: Account.create!(name: "Acme"), email_address: "passkey@example.com")
|
|
10
|
+
<% else -%>
|
|
11
|
+
@user = User.create!(account: Account.create!(name: "Acme"), email_address: "passkey@example.com", password: SecureRandom.base58(24))
|
|
12
|
+
<% end -%>
|
|
9
13
|
end
|
|
10
14
|
|
|
11
15
|
test "adds a passkey and signs in with it" do
|
|
@@ -62,6 +66,7 @@ class PasskeysTest < ActionDispatch::IntegrationTest
|
|
|
62
66
|
assert response.parsed_body["credential_id"].present?
|
|
63
67
|
end
|
|
64
68
|
|
|
69
|
+
<% if clave? -%>
|
|
65
70
|
test "a blocked user cannot sign in with a passkey" do
|
|
66
71
|
sign_in_as @user
|
|
67
72
|
register_passkey
|
|
@@ -73,6 +78,7 @@ class PasskeysTest < ActionDispatch::IntegrationTest
|
|
|
73
78
|
assert_response :unprocessable_entity
|
|
74
79
|
end
|
|
75
80
|
|
|
81
|
+
<% end -%>
|
|
76
82
|
test "adding a passkey needs a recent sign-in" do
|
|
77
83
|
sign_in_as @user
|
|
78
84
|
Current.session.update_column(:created_at, 2.hours.ago)
|
|
@@ -8,6 +8,11 @@ module Maquina
|
|
|
8
8
|
|
|
9
9
|
source_root File.expand_path("templates", __dir__)
|
|
10
10
|
|
|
11
|
+
USERS_FIXTURES = "test/fixtures/users.yml"
|
|
12
|
+
ACCOUNTS_FIXTURES = "test/fixtures/accounts.yml"
|
|
13
|
+
# A top-level fixture: its label line and the indented lines under it.
|
|
14
|
+
FIXTURE_ENTRY = /^[a-z_][a-z0-9_]*:\n(?:[ \t]+.*\n)*/
|
|
15
|
+
|
|
11
16
|
class_option :skip_views, type: :boolean, default: false,
|
|
12
17
|
desc: "Skip view templates"
|
|
13
18
|
class_option :quiet, type: :boolean, default: false,
|
|
@@ -105,6 +110,22 @@ module Maquina
|
|
|
105
110
|
"db/migrate/add_account_fields_to_users.rb"
|
|
106
111
|
end
|
|
107
112
|
|
|
113
|
+
# 10b. Fixtures. Rails' authentication generator writes the users one and
|
|
114
|
+
# two with no account, which add_account_fields_to_users makes NOT
|
|
115
|
+
# NULL, so every test that loads fixtures failed. Each user fixture
|
|
116
|
+
# without one joins the `one` account.
|
|
117
|
+
def update_fixtures
|
|
118
|
+
users = File.join(destination_root, USERS_FIXTURES)
|
|
119
|
+
return unless File.exist?(users)
|
|
120
|
+
|
|
121
|
+
content = File.read(users)
|
|
122
|
+
updated = content.gsub(FIXTURE_ENTRY) { |entry| entry.match?(/^ account:/) ? entry : "#{entry} account: one\n" }
|
|
123
|
+
return if updated == content
|
|
124
|
+
|
|
125
|
+
add_account_fixture
|
|
126
|
+
create_file USERS_FIXTURES, updated, force: true
|
|
127
|
+
end
|
|
128
|
+
|
|
108
129
|
# 11. Post-install message
|
|
109
130
|
def show_post_install
|
|
110
131
|
return if options[:quiet]
|
|
@@ -128,6 +149,17 @@ module Maquina
|
|
|
128
149
|
|
|
129
150
|
private
|
|
130
151
|
|
|
152
|
+
def add_account_fixture
|
|
153
|
+
entry = "one:\n name: One\n"
|
|
154
|
+
path = File.join(destination_root, ACCOUNTS_FIXTURES)
|
|
155
|
+
return create_file(ACCOUNTS_FIXTURES, entry) unless File.exist?(path)
|
|
156
|
+
|
|
157
|
+
content = File.read(path)
|
|
158
|
+
return if content.match?(/^one:/)
|
|
159
|
+
|
|
160
|
+
append_to_file ACCOUNTS_FIXTURES, (content.empty? || content.end_with?("\n\n")) ? entry : "\n#{entry}"
|
|
161
|
+
end
|
|
162
|
+
|
|
131
163
|
def migration_version
|
|
132
164
|
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
|
133
165
|
end
|
|
@@ -5,6 +5,10 @@
|
|
|
5
5
|
<%%= t(".title") %>
|
|
6
6
|
</h1>
|
|
7
7
|
|
|
8
|
+
<%%# The sign-up rate limit reports here. %>
|
|
9
|
+
<%%= tag.p(flash[:alert], class: "mb-6 rounded-lg border border-destructive/50 px-4 py-3 text-sm text-destructive", role: "alert") if flash[:alert] %>
|
|
10
|
+
<%%= tag.p(flash[:notice], class: "mb-6 rounded-lg border border-border bg-muted px-4 py-3 text-sm text-foreground", role: "status") if flash[:notice] %>
|
|
11
|
+
|
|
8
12
|
<%%= form_with url: registrations_path, method: :post, class: "space-y-6" do |form| %>
|
|
9
13
|
<div>
|
|
10
14
|
<%%= form.label :account_name, t(".account_name"), class: "block text-sm font-medium text-foreground mb-2", data: { component: "label" } %>
|
|
@@ -5,6 +5,11 @@
|
|
|
5
5
|
<%%= t(".title") %>
|
|
6
6
|
</h1>
|
|
7
7
|
|
|
8
|
+
<%%# Rails' SessionsController and PasswordsController report here (a wrong password, a reset sent). One line each,
|
|
9
|
+
with no end tag, so maquina:passkeys still finds the form's only one. %>
|
|
10
|
+
<%%= tag.p(flash[:alert], class: "mb-6 rounded-lg border border-destructive/50 px-4 py-3 text-sm text-destructive", role: "alert") if flash[:alert] %>
|
|
11
|
+
<%%= tag.p(flash[:notice], class: "mb-6 rounded-lg border border-border bg-muted px-4 py-3 text-sm text-foreground", role: "status") if flash[:notice] %>
|
|
12
|
+
|
|
8
13
|
<%%= form_with url: session_path, method: :post, class: "space-y-6" do |form| %>
|
|
9
14
|
<div>
|
|
10
15
|
<%%= form.label :email_address, t(".email"), class: "block text-sm font-medium text-foreground mb-2", data: { component: "label" } %>
|
|
@@ -9,9 +9,11 @@ Description:
|
|
|
9
9
|
navigation tab. The page is behind the backstage Basic Auth and answers
|
|
10
10
|
503 until credentials are set.
|
|
11
11
|
|
|
12
|
-
With
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
With the app's own sign-in installed, the backstage is behind it instead,
|
|
13
|
+
and the Basic Auth is taken out of the files it wrote. With Clave, users
|
|
14
|
+
listed under backstage.emails in credentials get in; with Rails' sign-in
|
|
15
|
+
(maquina:registration), admins of an account whose allow_backstage is on
|
|
16
|
+
(a column it adds, off by default). Anyone else gets a 404.
|
|
15
17
|
|
|
16
18
|
When maquina:mcp is installed, it also writes app/models/mcp/tools/security.rb:
|
|
17
19
|
recent_blocks and security_summary, for users on the backstage list. With
|