devise-webauthn 0.2.1 → 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 +4 -4
- data/.github/dependabot.yml +6 -0
- data/.github/workflows/ruby.yml +6 -2
- data/.rubocop.yml +1 -0
- data/Appraisals +18 -4
- data/CHANGELOG.md +50 -8
- data/Gemfile +2 -2
- data/Gemfile.lock +3 -5
- data/README.md +158 -5
- data/app/assets/javascript/devise/webauthn.js +179 -0
- data/app/controllers/devise/passkeys_controller.rb +6 -1
- data/app/controllers/devise/second_factor_webauthn_credentials_controller.rb +31 -4
- data/config/locales/en.yml +6 -0
- data/devise-webauthn.gemspec +1 -1
- data/gemfiles/devise_5_0.gemfile +26 -0
- data/gemfiles/rails_7_1.gemfile +3 -2
- data/gemfiles/rails_7_2.gemfile +3 -2
- data/gemfiles/rails_8_0.gemfile +3 -2
- data/gemfiles/rails_8_1.gemfile +3 -2
- data/gemfiles/rails_edge.gemfile +2 -1
- data/lib/devise/strategies/database_authenticatable.rb +1 -0
- data/lib/devise/strategies/webauthn_two_factor_authenticatable.rb +11 -5
- data/lib/devise/webauthn/engine.rb +6 -0
- data/lib/devise/webauthn/helpers/credentials_helper.rb +21 -36
- data/lib/devise/webauthn/routes.rb +1 -1
- data/lib/devise/webauthn/test/authenticator_helpers.rb +42 -0
- data/lib/devise/webauthn/version.rb +1 -1
- data/lib/generators/devise/webauthn/controllers_generator.rb +5 -1
- data/lib/generators/devise/webauthn/install/install_generator.rb +2 -2
- data/lib/generators/devise/webauthn/javascript/javascript_configuration_generator.rb +64 -0
- data/lib/generators/devise/webauthn/templates/controllers/second_factor_webauthn_credentials_controller.rb.tt +48 -0
- data/lib/generators/devise/webauthn/templates/controllers/two_factor_authentications_controller.rb.tt +13 -0
- data/lib/generators/devise/webauthn/views_generator.rb +2 -0
- data/lib/generators/devise/webauthn/webauthn_credential_model/templates/webauthn_credential_migration.rb.erb +17 -0
- data/lib/generators/devise/webauthn/webauthn_credential_model/webauthn_credential_model_generator.rb +15 -9
- metadata +10 -5
- data/lib/generators/devise/webauthn/stimulus/stimulus_generator.rb +0 -24
- data/lib/generators/devise/webauthn/stimulus/templates/webauthn_credentials_controller.js +0 -31
|
@@ -14,19 +14,34 @@ module Devise
|
|
|
14
14
|
else
|
|
15
15
|
set_flash_message! :alert, :webauthn_credential_verification_failed, scope: :"devise.failure"
|
|
16
16
|
end
|
|
17
|
-
redirect_to
|
|
17
|
+
redirect_to after_create_path
|
|
18
18
|
rescue WebAuthn::Error
|
|
19
19
|
set_flash_message! :alert, :webauthn_credential_verification_failed, scope: :"devise.failure"
|
|
20
|
-
redirect_to
|
|
20
|
+
redirect_to after_create_path
|
|
21
21
|
ensure
|
|
22
22
|
session.delete(:webauthn_challenge)
|
|
23
23
|
end
|
|
24
24
|
|
|
25
|
-
def
|
|
26
|
-
resource.second_factor_webauthn_credentials.
|
|
25
|
+
def update
|
|
26
|
+
if resource.second_factor_webauthn_credentials.find(params[:id]).update(authentication_factor: 0)
|
|
27
|
+
set_flash_message! :notice, :security_key_promoted
|
|
28
|
+
else
|
|
29
|
+
set_flash_message! :alert, :security_key_promotion_failed, scope: :"devise.failure"
|
|
30
|
+
end
|
|
31
|
+
|
|
27
32
|
redirect_to after_update_path
|
|
28
33
|
end
|
|
29
34
|
|
|
35
|
+
def destroy
|
|
36
|
+
if resource.second_factor_webauthn_credentials.destroy(params[:id])
|
|
37
|
+
set_flash_message! :notice, :security_key_deleted
|
|
38
|
+
else
|
|
39
|
+
set_flash_message! :alert, :security_key_deletion_failed, scope: :"devise.failure"
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
redirect_to after_destroy_path
|
|
43
|
+
end
|
|
44
|
+
|
|
30
45
|
private
|
|
31
46
|
|
|
32
47
|
def authenticate_scope!
|
|
@@ -47,9 +62,21 @@ module Devise
|
|
|
47
62
|
)
|
|
48
63
|
end
|
|
49
64
|
|
|
65
|
+
# The default url to be used after creating a second factor key. You can overwrite
|
|
66
|
+
# this method in your own SecondFactorWebauthnCredentialsController.
|
|
67
|
+
def after_create_path
|
|
68
|
+
new_second_factor_webauthn_credential_path(resource_name)
|
|
69
|
+
end
|
|
70
|
+
|
|
50
71
|
# The default url to be used after creating a second factor key. You can overwrite
|
|
51
72
|
# this method in your own SecondFactorWebauthnCredentialsController.
|
|
52
73
|
def after_update_path
|
|
74
|
+
request.referer || new_second_factor_webauthn_credential_path(resource_name)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# The default url to be used after deleting a second factor key. You can overwrite
|
|
78
|
+
# this method in your own SecondFactorWebauthnCredentialsController.
|
|
79
|
+
def after_destroy_path
|
|
53
80
|
new_second_factor_webauthn_credential_path(resource_name)
|
|
54
81
|
end
|
|
55
82
|
end
|
data/config/locales/en.yml
CHANGED
|
@@ -2,11 +2,17 @@ en:
|
|
|
2
2
|
devise:
|
|
3
3
|
passkeys:
|
|
4
4
|
passkey_created: "Passkey created successfully."
|
|
5
|
+
passkey_deleted: "Passkey deleted successfully."
|
|
5
6
|
second_factor_webauthn_credentials:
|
|
6
7
|
security_key_created: "Security Key created successfully."
|
|
8
|
+
security_key_deleted: "Security Key deleted successfully."
|
|
9
|
+
security_key_promoted: "Security Key promoted to passkey successfully."
|
|
7
10
|
failure:
|
|
8
11
|
passkey_not_found: "Your passkey doesn't exist or is not valid."
|
|
9
12
|
passkey_verification_failed: "Passkey verification failed."
|
|
13
|
+
passkey_deletion_failed: "Passkey deletion failed."
|
|
14
|
+
security_key_deletion_failed: "Security Key deletion failed."
|
|
15
|
+
security_key_promotion_failed: "Security Key promotion to passkey failed."
|
|
10
16
|
sign_in_not_initiated: "Sign in was not initiated."
|
|
11
17
|
two_factor_required: "Two-factor authentication is required to sign in."
|
|
12
18
|
webauthn_credential_not_found: "Your WebAuthn credential doesn't exist or is not valid."
|
data/devise-webauthn.gemspec
CHANGED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# This file was generated by Appraisal
|
|
2
|
+
|
|
3
|
+
source "https://rubygems.org"
|
|
4
|
+
|
|
5
|
+
gem "appraisal", "~> 2.5", require: false
|
|
6
|
+
gem "capybara", "~> 3.39"
|
|
7
|
+
gem "combustion", "~> 1.3"
|
|
8
|
+
gem "devise", "~> 5.0.0.rc"
|
|
9
|
+
gem "importmap-rails", "~> 2.0"
|
|
10
|
+
gem "propshaft", "~> 1.2"
|
|
11
|
+
gem "pry-byebug", "~> 3.10"
|
|
12
|
+
gem "puma", "~> 6.6"
|
|
13
|
+
gem "rails", ">= 7.1"
|
|
14
|
+
gem "rspec-rails", ">= 7.1"
|
|
15
|
+
gem "rubocop", "~> 1.79"
|
|
16
|
+
gem "rubocop-rails", "~> 2.32"
|
|
17
|
+
gem "rubocop-rspec", "~> 3.6"
|
|
18
|
+
gem "selenium-webdriver"
|
|
19
|
+
gem "sqlite3", ">= 1.6", "!= 1.7.0", "!= 1.7.1", "!= 1.7.2", "!= 1.7.3"
|
|
20
|
+
gem "stimulus-rails", "~> 1.3"
|
|
21
|
+
|
|
22
|
+
install_if -> { RUBY_VERSION < "3.0" } do
|
|
23
|
+
gem "rack", "~> 2.2"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
gemspec path: "../"
|
data/gemfiles/rails_7_1.gemfile
CHANGED
|
@@ -2,14 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
source "https://rubygems.org"
|
|
4
4
|
|
|
5
|
-
gem "appraisal", "~> 2.5"
|
|
5
|
+
gem "appraisal", "~> 2.5", require: false
|
|
6
6
|
gem "capybara", "~> 3.39"
|
|
7
7
|
gem "combustion", "~> 1.3"
|
|
8
|
+
gem "devise", "~> 4.9"
|
|
8
9
|
gem "importmap-rails", "~> 2.0"
|
|
9
10
|
gem "propshaft", "~> 1.2"
|
|
10
11
|
gem "pry-byebug", "~> 3.10"
|
|
11
12
|
gem "puma", "~> 6.6"
|
|
12
|
-
gem "rails", "~> 7.1"
|
|
13
|
+
gem "rails", "~> 7.1.x"
|
|
13
14
|
gem "rspec-rails", "~> 7.1"
|
|
14
15
|
gem "rubocop", "~> 1.79"
|
|
15
16
|
gem "rubocop-rails", "~> 2.32"
|
data/gemfiles/rails_7_2.gemfile
CHANGED
|
@@ -2,14 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
source "https://rubygems.org"
|
|
4
4
|
|
|
5
|
-
gem "appraisal", "~> 2.5"
|
|
5
|
+
gem "appraisal", "~> 2.5", require: false
|
|
6
6
|
gem "capybara", "~> 3.40"
|
|
7
7
|
gem "combustion", "~> 1.3"
|
|
8
|
+
gem "devise", "~> 4.9"
|
|
8
9
|
gem "importmap-rails", "~> 2.2"
|
|
9
10
|
gem "propshaft", "~> 1.2"
|
|
10
11
|
gem "pry-byebug", "~> 3.11"
|
|
11
12
|
gem "puma", "~> 6.6"
|
|
12
|
-
gem "rails", "~> 7.2"
|
|
13
|
+
gem "rails", "~> 7.2.x"
|
|
13
14
|
gem "rspec-rails", "~> 8.0"
|
|
14
15
|
gem "rubocop", "~> 1.79"
|
|
15
16
|
gem "rubocop-rails", "~> 2.32"
|
data/gemfiles/rails_8_0.gemfile
CHANGED
|
@@ -2,14 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
source "https://rubygems.org"
|
|
4
4
|
|
|
5
|
-
gem "appraisal", "~> 2.5"
|
|
5
|
+
gem "appraisal", "~> 2.5", require: false
|
|
6
6
|
gem "capybara", "~> 3.40"
|
|
7
7
|
gem "combustion", "~> 1.3"
|
|
8
|
+
gem "devise", "~> 4.9"
|
|
8
9
|
gem "importmap-rails", "~> 2.2"
|
|
9
10
|
gem "propshaft", "~> 1.2"
|
|
10
11
|
gem "pry-byebug", "~> 3.11"
|
|
11
12
|
gem "puma", "~> 6.6"
|
|
12
|
-
gem "rails", "~> 8.0"
|
|
13
|
+
gem "rails", "~> 8.0.x"
|
|
13
14
|
gem "rspec-rails", "~> 8.0"
|
|
14
15
|
gem "rubocop", "~> 1.79"
|
|
15
16
|
gem "rubocop-rails", "~> 2.32"
|
data/gemfiles/rails_8_1.gemfile
CHANGED
|
@@ -2,14 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
source "https://rubygems.org"
|
|
4
4
|
|
|
5
|
-
gem "appraisal", "~> 2.5"
|
|
5
|
+
gem "appraisal", "~> 2.5", require: false
|
|
6
6
|
gem "capybara", "~> 3.40"
|
|
7
7
|
gem "combustion", "~> 1.3"
|
|
8
|
+
gem "devise", "~> 4.9"
|
|
8
9
|
gem "importmap-rails", "~> 2.2"
|
|
9
10
|
gem "propshaft", "~> 1.2"
|
|
10
11
|
gem "pry-byebug", "~> 3.11"
|
|
11
12
|
gem "puma", "~> 6.6"
|
|
12
|
-
gem "rails", "~> 8.1"
|
|
13
|
+
gem "rails", "~> 8.1.x"
|
|
13
14
|
gem "rspec-rails", "~> 8.0"
|
|
14
15
|
gem "rubocop", "~> 1.79"
|
|
15
16
|
gem "rubocop-rails", "~> 2.32"
|
data/gemfiles/rails_edge.gemfile
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
|
|
3
3
|
source "https://rubygems.org"
|
|
4
4
|
|
|
5
|
-
gem "appraisal", "~> 2.5"
|
|
5
|
+
gem "appraisal", "~> 2.5", require: false
|
|
6
6
|
gem "capybara", "~> 3.40"
|
|
7
7
|
gem "combustion", "~> 1.3"
|
|
8
|
+
gem "devise", "~> 4.9"
|
|
8
9
|
gem "importmap-rails", "~> 2.2"
|
|
9
10
|
gem "propshaft", "~> 1.2"
|
|
10
11
|
gem "pry-byebug", "~> 3.11"
|
|
@@ -13,6 +13,7 @@ module Devise
|
|
|
13
13
|
if validate(resource){ hashed = true; resource.valid_password?(password) }
|
|
14
14
|
if second_factor_enabled?(resource)
|
|
15
15
|
session[:current_authentication_resource_id] = resource.id
|
|
16
|
+
session[:current_authentication_remember_me] = remember_me?
|
|
16
17
|
request.flash[:notice] = two_factor_required_message
|
|
17
18
|
request.commit_flash
|
|
18
19
|
redirect!(two_factor_authentication_path, {}, message: two_factor_required_message)
|
|
@@ -4,26 +4,32 @@ module Devise
|
|
|
4
4
|
module Strategies
|
|
5
5
|
class WebauthnTwoFactorAuthenticatable < Devise::Strategies::Base
|
|
6
6
|
def valid?
|
|
7
|
-
credential_param.present? &&
|
|
7
|
+
credential_param.present? &&
|
|
8
|
+
session[:current_authentication_resource_id].present? &&
|
|
9
|
+
session[:two_factor_authentication_challenge].present?
|
|
8
10
|
end
|
|
9
11
|
|
|
12
|
+
# rubocop:disable Metrics/AbcSize
|
|
10
13
|
def authenticate!
|
|
11
14
|
credential_from_params = WebAuthn::Credential.from_get(JSON.parse(credential_param))
|
|
12
|
-
|
|
15
|
+
resource = resource_class.find_by(id: session[:current_authentication_resource_id])
|
|
16
|
+
stored_credential = resource&.webauthn_credentials&.find_by(external_id: credential_from_params.id)
|
|
13
17
|
|
|
14
18
|
return fail!(:webauthn_credential_not_found) if stored_credential.blank?
|
|
15
19
|
|
|
16
20
|
verify_credential(credential_from_params, stored_credential)
|
|
17
21
|
|
|
18
|
-
resource =
|
|
22
|
+
resource.remember_me = session[:current_authentication_remember_me] if resource.respond_to?(:remember_me=)
|
|
19
23
|
success!(resource)
|
|
20
24
|
|
|
21
25
|
session.delete(:current_authentication_resource_id)
|
|
26
|
+
session.delete(:current_authentication_remember_me)
|
|
22
27
|
rescue WebAuthn::Error
|
|
23
28
|
fail!(:webauthn_credential_verification_failed)
|
|
24
29
|
ensure
|
|
25
30
|
session.delete(:two_factor_authentication_challenge)
|
|
26
31
|
end
|
|
32
|
+
# rubocop:enable Metrics/AbcSize
|
|
27
33
|
|
|
28
34
|
private
|
|
29
35
|
|
|
@@ -41,8 +47,8 @@ module Devise
|
|
|
41
47
|
stored_credential.update!(sign_count: credential_from_params.sign_count)
|
|
42
48
|
end
|
|
43
49
|
|
|
44
|
-
def
|
|
45
|
-
mapping.to
|
|
50
|
+
def resource_class
|
|
51
|
+
mapping.to
|
|
46
52
|
end
|
|
47
53
|
end
|
|
48
54
|
end
|
|
@@ -34,6 +34,12 @@ module Devise
|
|
|
34
34
|
initializer "devise.webauthn.url_helpers" do
|
|
35
35
|
Devise.include_helpers(Devise::Webauthn)
|
|
36
36
|
end
|
|
37
|
+
|
|
38
|
+
initializer "devise.webauthn.assets" do
|
|
39
|
+
if ::Rails.application.config.respond_to?(:assets)
|
|
40
|
+
::Rails.application.config.assets.precompile += %w[devise/webauthn.js]
|
|
41
|
+
end
|
|
42
|
+
end
|
|
37
43
|
end
|
|
38
44
|
end
|
|
39
45
|
end
|
|
@@ -8,16 +8,12 @@ module Devise
|
|
|
8
8
|
form_with(
|
|
9
9
|
url: passkeys_path(resource),
|
|
10
10
|
method: :post,
|
|
11
|
-
class: form_classes
|
|
12
|
-
data: {
|
|
13
|
-
action: "webauthn-credentials#create:prevent",
|
|
14
|
-
controller: "webauthn-credentials",
|
|
15
|
-
webauthn_credentials_options_param: create_passkey_options(resource)
|
|
16
|
-
}
|
|
11
|
+
class: form_classes
|
|
17
12
|
) do |f|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
13
|
+
tag.webauthn_create(data: { options_json: create_passkey_options(resource) }) do
|
|
14
|
+
concat f.hidden_field(:public_key_credential, data: { webauthn_target: "response" })
|
|
15
|
+
concat capture(f, &block)
|
|
16
|
+
end
|
|
21
17
|
end
|
|
22
18
|
end
|
|
23
19
|
|
|
@@ -25,16 +21,13 @@ module Devise
|
|
|
25
21
|
form_with(
|
|
26
22
|
url: session_path,
|
|
27
23
|
method: :post,
|
|
28
|
-
data: {
|
|
29
|
-
action: "webauthn-credentials#get:prevent",
|
|
30
|
-
controller: "webauthn-credentials",
|
|
31
|
-
webauthn_credentials_options_param: passkey_authentication_options
|
|
32
|
-
},
|
|
33
24
|
class: form_classes
|
|
34
25
|
) do |f|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
26
|
+
tag.webauthn_get(data: { options_json: passkey_authentication_options }) do
|
|
27
|
+
concat f.hidden_field(:public_key_credential, data: { webauthn_target: "response" })
|
|
28
|
+
|
|
29
|
+
concat f.button(text, type: "submit", class: button_classes, &block)
|
|
30
|
+
end
|
|
38
31
|
end
|
|
39
32
|
end
|
|
40
33
|
|
|
@@ -42,16 +35,12 @@ module Devise
|
|
|
42
35
|
form_with(
|
|
43
36
|
url: second_factor_webauthn_credentials_path(resource),
|
|
44
37
|
method: :post,
|
|
45
|
-
class: form_classes
|
|
46
|
-
data: {
|
|
47
|
-
action: "webauthn-credentials#create:prevent",
|
|
48
|
-
controller: "webauthn-credentials",
|
|
49
|
-
webauthn_credentials_options_param: create_security_key_options(resource)
|
|
50
|
-
}
|
|
38
|
+
class: form_classes
|
|
51
39
|
) do |f|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
40
|
+
tag.webauthn_create(data: { options_json: create_security_key_options(resource) }) do
|
|
41
|
+
concat f.hidden_field(:public_key_credential, data: { webauthn_target: "response" })
|
|
42
|
+
concat capture(f, &block)
|
|
43
|
+
end
|
|
55
44
|
end
|
|
56
45
|
end
|
|
57
46
|
|
|
@@ -59,21 +48,15 @@ module Devise
|
|
|
59
48
|
form_with(
|
|
60
49
|
url: two_factor_authentication_path(resource),
|
|
61
50
|
method: :post,
|
|
62
|
-
data: {
|
|
63
|
-
action: "webauthn-credentials#get:prevent",
|
|
64
|
-
controller: "webauthn-credentials",
|
|
65
|
-
webauthn_credentials_options_param: security_key_authentication_options(resource)
|
|
66
|
-
},
|
|
67
51
|
class: form_classes
|
|
68
52
|
) do |f|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
53
|
+
tag.webauthn_get(data: { options_json: security_key_authentication_options(resource) }) do
|
|
54
|
+
concat f.hidden_field(:public_key_credential, data: { webauthn_target: "response" })
|
|
55
|
+
concat f.button(text, type: "submit", class: button_classes, &block)
|
|
56
|
+
end
|
|
72
57
|
end
|
|
73
58
|
end
|
|
74
59
|
|
|
75
|
-
private
|
|
76
|
-
|
|
77
60
|
def create_passkey_options(resource)
|
|
78
61
|
@create_passkey_options ||= begin
|
|
79
62
|
options = WebAuthn::Credential.options_for_create(
|
|
@@ -143,6 +126,8 @@ module Devise
|
|
|
143
126
|
end
|
|
144
127
|
end
|
|
145
128
|
|
|
129
|
+
private
|
|
130
|
+
|
|
146
131
|
def resource_human_palatable_identifier
|
|
147
132
|
authentication_keys = resource.class.authentication_keys
|
|
148
133
|
authentication_keys = authentication_keys.keys if authentication_keys.is_a?(Hash)
|
|
@@ -15,7 +15,7 @@ module ActionDispatch
|
|
|
15
15
|
controller: controllers[:two_factor_authentications]
|
|
16
16
|
|
|
17
17
|
resources :second_factor_webauthn_credentials,
|
|
18
|
-
only: %i[new create destroy],
|
|
18
|
+
only: %i[new create update destroy],
|
|
19
19
|
controller: controllers[:second_factor_webauthn_credentials]
|
|
20
20
|
end
|
|
21
21
|
end
|
|
@@ -11,6 +11,48 @@ module Devise
|
|
|
11
11
|
options.resident_key = true
|
|
12
12
|
page.driver.browser.add_virtual_authenticator(options)
|
|
13
13
|
end
|
|
14
|
+
|
|
15
|
+
def add_passkey_to_authenticator(authenticator, resource, name: "My Passkey")
|
|
16
|
+
add_credential_to_authenticator(authenticator, resource, passkey: true, name: name)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def add_security_key_to_authenticator(authenticator, resource, name: "My Security Key")
|
|
20
|
+
add_credential_to_authenticator(authenticator, resource, passkey: false, name: name)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# rubocop:disable Metrics/AbcSize
|
|
24
|
+
# rubocop:disable Metrics/MethodLength
|
|
25
|
+
def add_credential_to_authenticator(authenticator, resource, passkey:, name: "My Credential")
|
|
26
|
+
credential_id = SecureRandom.random_bytes(16)
|
|
27
|
+
encoded_credential_id = Base64.urlsafe_encode64(credential_id)
|
|
28
|
+
key = OpenSSL::PKey.generate_key("ED25519")
|
|
29
|
+
encoded_private_key = Base64.urlsafe_encode64(key.private_to_der)
|
|
30
|
+
|
|
31
|
+
cose_public_key = COSE::Key::OKP.from_pkey(OpenSSL::PKey.read(key.public_to_der))
|
|
32
|
+
cose_public_key.alg = -8
|
|
33
|
+
encoded_cose_public_key = Base64.urlsafe_encode64(cose_public_key.serialize)
|
|
34
|
+
|
|
35
|
+
credential_json = {
|
|
36
|
+
"credentialId" => encoded_credential_id,
|
|
37
|
+
"isResidentCredential" => passkey,
|
|
38
|
+
"rpId" => "localhost",
|
|
39
|
+
"privateKey" => encoded_private_key,
|
|
40
|
+
"signCount" => 0
|
|
41
|
+
}
|
|
42
|
+
credential_json["userHandle"] = resource.webauthn_id if passkey
|
|
43
|
+
|
|
44
|
+
authenticator.add_credential(credential_json)
|
|
45
|
+
|
|
46
|
+
resource.webauthn_credentials.create!(
|
|
47
|
+
name: name,
|
|
48
|
+
external_id: Base64.urlsafe_encode64(credential_id, padding: false),
|
|
49
|
+
public_key: encoded_cose_public_key,
|
|
50
|
+
sign_count: 0,
|
|
51
|
+
authentication_factor: passkey ? :first_factor : :second_factor
|
|
52
|
+
)
|
|
53
|
+
end
|
|
54
|
+
# rubocop:enable Metrics/MethodLength
|
|
55
|
+
# rubocop:enable Metrics/AbcSize
|
|
14
56
|
end
|
|
15
57
|
end
|
|
16
58
|
end
|
|
@@ -5,7 +5,11 @@ require "rails/generators"
|
|
|
5
5
|
module Devise
|
|
6
6
|
module Webauthn
|
|
7
7
|
class ControllersGenerator < Rails::Generators::Base
|
|
8
|
-
CONTROLLERS = %w[
|
|
8
|
+
CONTROLLERS = %w[
|
|
9
|
+
passkeys
|
|
10
|
+
second_factor_webauthn_credentials
|
|
11
|
+
two_factor_authentications
|
|
12
|
+
].freeze
|
|
9
13
|
|
|
10
14
|
desc "Create inherited Devise::Webauthn controllers in your app/controllers folder."
|
|
11
15
|
|
|
@@ -26,8 +26,8 @@ module Devise
|
|
|
26
26
|
invoke "devise:webauthn:webauthn_id", [], resource_name: options[:resource_name]
|
|
27
27
|
end
|
|
28
28
|
|
|
29
|
-
def
|
|
30
|
-
invoke "devise:webauthn:
|
|
29
|
+
def generate_javascript_configuration
|
|
30
|
+
invoke "devise:webauthn:javascript"
|
|
31
31
|
end
|
|
32
32
|
|
|
33
33
|
def final_message
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "rails/generators"
|
|
4
|
+
|
|
5
|
+
module Devise
|
|
6
|
+
module Webauthn
|
|
7
|
+
class JavascriptConfigurationGenerator < Rails::Generators::Base
|
|
8
|
+
hide!
|
|
9
|
+
namespace "devise:webauthn:javascript"
|
|
10
|
+
|
|
11
|
+
desc "Configure JavaScript loading for devise-webauthn"
|
|
12
|
+
|
|
13
|
+
def configure_javascript
|
|
14
|
+
if importmap?
|
|
15
|
+
setup_importmap
|
|
16
|
+
elsif using_node?
|
|
17
|
+
setup_node
|
|
18
|
+
else
|
|
19
|
+
say "Could not detect JavaScript setup. Please manually configure `devise/webauthn.js` loading.", :red
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
private
|
|
24
|
+
|
|
25
|
+
def importmap?
|
|
26
|
+
File.exist?(File.join(destination_root, "config/importmap.rb"))
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def using_node?
|
|
30
|
+
File.exist?(File.join(destination_root, "package.json"))
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def setup_importmap
|
|
34
|
+
say "Detected importmap-rails setup", :green
|
|
35
|
+
|
|
36
|
+
append_to_file "config/importmap.rb", %(pin "devise/webauthn", to: "devise/webauthn.js"\n)
|
|
37
|
+
say "Added pin to config/importmap.rb", :green
|
|
38
|
+
|
|
39
|
+
if File.exist?(File.join(destination_root, "app/javascript/application.js"))
|
|
40
|
+
append_to_file "app/javascript/application.js", %(import "devise/webauthn"\n)
|
|
41
|
+
say "Added import to app/javascript/application.js", :green
|
|
42
|
+
else
|
|
43
|
+
say "Could not find app/javascript/application.js!", :red
|
|
44
|
+
say " Please add `import \"devise/webauthn\"` to your application.js file manually."
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def setup_node
|
|
49
|
+
say "Detected JavaScript bundler setup (Bun/Node)", :green
|
|
50
|
+
|
|
51
|
+
if File.exist?(File.join(destination_root, "app/views/layouts/application.html.erb"))
|
|
52
|
+
inject_into_file "app/views/layouts/application.html.erb",
|
|
53
|
+
%(\n <%= javascript_include_tag "devise/webauthn" %>),
|
|
54
|
+
before: "</head>"
|
|
55
|
+
say "Added javascript_include_tag to app/views/layouts/application.html.erb", :green
|
|
56
|
+
else
|
|
57
|
+
say "Could not find app/views/layouts/application.html.erb.", :red
|
|
58
|
+
say " Please add `<%= javascript_include_tag \"devise/webauthn\" %>` " \
|
|
59
|
+
"within the <head> tag in your custom layout."
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class <%= @scope_prefix %>SecondFactorWebauthnCredentialsController < Devise::SecondFactorWebauthnCredentialsController
|
|
4
|
+
# GET /resource/second_factor_webauthn_credentials/new
|
|
5
|
+
# def new
|
|
6
|
+
# super
|
|
7
|
+
# end
|
|
8
|
+
|
|
9
|
+
# POST /second_factor_webauthn_credentials/passkeys
|
|
10
|
+
# def create
|
|
11
|
+
# super
|
|
12
|
+
# end
|
|
13
|
+
|
|
14
|
+
# PUT /resource/second_factor_webauthn_credentials/:id
|
|
15
|
+
# def update
|
|
16
|
+
# super
|
|
17
|
+
# end
|
|
18
|
+
|
|
19
|
+
# DELETE /resource/second_factor_webauthn_credentials/:id
|
|
20
|
+
# def destroy
|
|
21
|
+
# super
|
|
22
|
+
# end
|
|
23
|
+
|
|
24
|
+
# private
|
|
25
|
+
|
|
26
|
+
# Verifies the security key coming in the params and saves it to the database.
|
|
27
|
+
# def verify_and_save_security_key(security_key_from_params)
|
|
28
|
+
# super
|
|
29
|
+
# end
|
|
30
|
+
|
|
31
|
+
# The default url to be used after creating a second factor key. You can overwrite
|
|
32
|
+
# this method in your own SecondFactorWebauthnCredentialsController.
|
|
33
|
+
# def after_create_path
|
|
34
|
+
# super
|
|
35
|
+
# end
|
|
36
|
+
|
|
37
|
+
# The default url to be used after creating a second factor key. You can overwrite
|
|
38
|
+
# this method in your own SecondFactorWebauthnCredentialsController.
|
|
39
|
+
# def after_update_path
|
|
40
|
+
# super
|
|
41
|
+
# end
|
|
42
|
+
|
|
43
|
+
# The default url to be used after deleting a second factor key. You can overwrite
|
|
44
|
+
# this method in your own SecondFactorWebauthnCredentialsController.
|
|
45
|
+
# def after_destroy_path
|
|
46
|
+
# super
|
|
47
|
+
# end
|
|
48
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class <%= @scope_prefix %>TwoFactorAuthenticationsController < Devise::TwoFactorAuthenticationsController
|
|
4
|
+
# GET /resource/two_factor_authentication/new
|
|
5
|
+
# def new
|
|
6
|
+
# super
|
|
7
|
+
# end
|
|
8
|
+
|
|
9
|
+
# POST /resource/two_factor_authentication
|
|
10
|
+
# def create
|
|
11
|
+
# super
|
|
12
|
+
# end
|
|
13
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class CreateWebauthnCredentials < ActiveRecord::Migration[<%= Rails.version.to_f %>]
|
|
4
|
+
def change
|
|
5
|
+
create_table :webauthn_credentials do |t|
|
|
6
|
+
t.string :external_id, null: false
|
|
7
|
+
t.string :name, null: false
|
|
8
|
+
t.text :public_key, null: false
|
|
9
|
+
t.integer :sign_count, limit: 8, null: false
|
|
10
|
+
t.references :<%= user_model_name %>, null: false, foreign_key: true
|
|
11
|
+
t.integer :authentication_factor, limit: 1, null: false
|
|
12
|
+
|
|
13
|
+
t.timestamps
|
|
14
|
+
end
|
|
15
|
+
add_index :webauthn_credentials, :external_id, unique: true
|
|
16
|
+
end
|
|
17
|
+
end
|