devise-webauthn 0.5.1 → 0.6.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/workflows/ruby.yml +4 -0
- data/.gitignore +6 -6
- data/.rspec +1 -0
- data/.rspec-api +4 -0
- data/.rubocop.yml +1 -0
- data/CHANGELOG.md +34 -0
- data/Gemfile.lock +1 -1
- data/README.md +154 -1
- data/Rakefile +6 -1
- data/app/controllers/devise/passkey_authentication_options_controller.rb +4 -3
- data/app/controllers/devise/passkey_registration_options_controller.rb +6 -11
- data/app/controllers/devise/passkeys_controller.rb +30 -15
- data/app/controllers/devise/second_factor_webauthn_credentials_controller.rb +45 -18
- data/app/controllers/devise/security_key_authentication_options_controller.rb +9 -4
- data/app/controllers/devise/security_key_registration_options_controller.rb +8 -13
- data/app/controllers/devise/two_factor_authentications_controller.rb +7 -3
- data/lib/devise/models/webauthn_credential_authenticatable.rb +9 -0
- data/lib/devise/strategies/database_authenticatable.rb +29 -5
- data/lib/devise/strategies/passkey_authenticatable.rb +15 -11
- data/lib/devise/strategies/webauthn_two_factor_authenticatable.rb +20 -17
- data/lib/devise/webauthn/challenge_store_access.rb +15 -0
- data/lib/devise/webauthn/challenge_stores/cache.rb +63 -0
- data/lib/devise/webauthn/challenge_stores/session.rb +32 -0
- data/lib/devise/webauthn/credential_properties.rb +25 -0
- data/lib/devise/webauthn/engine.rb +6 -1
- data/lib/devise/webauthn/pending_two_factor_sign_in.rb +42 -0
- data/lib/devise/webauthn/public_key_credential_param.rb +26 -0
- data/lib/devise/webauthn/test/authenticator_helpers.rb +9 -6
- data/lib/devise/webauthn/two_factor_token.rb +31 -0
- data/lib/devise/webauthn/version.rb +1 -1
- data/lib/devise/webauthn.rb +9 -0
- data/lib/generators/devise/webauthn/install/templates/webauthn.rb +3 -0
- data/lib/generators/devise/webauthn/templates/controllers/passkey_authentication_options_controller.rb.tt +2 -2
- data/lib/generators/devise/webauthn/templates/controllers/passkey_registration_options_controller.rb.tt +2 -2
- data/lib/generators/devise/webauthn/templates/controllers/security_key_authentication_options_controller.rb.tt +2 -2
- data/lib/generators/devise/webauthn/templates/controllers/security_key_registration_options_controller.rb.tt +2 -2
- data/lib/generators/devise/webauthn/webauthn_credential_model/templates/webauthn_credential_migration.rb.erb +2 -0
- metadata +9 -1
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
module Devise
|
|
4
4
|
class SecurityKeyRegistrationOptionsController < DeviseController
|
|
5
|
-
|
|
5
|
+
include Devise::Webauthn::ChallengeStoreAccess
|
|
6
|
+
|
|
7
|
+
skip_forgery_protection if respond_to?(:skip_forgery_protection)
|
|
6
8
|
|
|
7
9
|
before_action :authenticate_scope!
|
|
8
10
|
before_action :ensure_webauthn_id!
|
|
@@ -12,17 +14,17 @@ module Devise
|
|
|
12
14
|
WebAuthn::Credential.options_for_create(
|
|
13
15
|
user: {
|
|
14
16
|
id: resource.webauthn_id,
|
|
15
|
-
name:
|
|
17
|
+
name: resource.webauthn_user_name
|
|
16
18
|
},
|
|
17
19
|
exclude: resource.webauthn_credentials.pluck(:external_id),
|
|
18
20
|
authenticator_selection: {
|
|
19
21
|
resident_key: "discouraged",
|
|
20
|
-
user_verification: "
|
|
21
|
-
}
|
|
22
|
+
user_verification: "preferred"
|
|
23
|
+
},
|
|
24
|
+
extensions: { credProps: true }
|
|
22
25
|
)
|
|
23
26
|
|
|
24
|
-
|
|
25
|
-
session[:webauthn_challenge] = create_security_key_options.challenge
|
|
27
|
+
challenge_store.write(:security_key_registration, create_security_key_options.challenge)
|
|
26
28
|
|
|
27
29
|
render json: create_security_key_options
|
|
28
30
|
end
|
|
@@ -37,12 +39,5 @@ module Devise
|
|
|
37
39
|
def ensure_webauthn_id!
|
|
38
40
|
resource.ensure_webauthn_id!
|
|
39
41
|
end
|
|
40
|
-
|
|
41
|
-
def resource_human_palatable_identifier
|
|
42
|
-
authentication_keys = resource.class.authentication_keys
|
|
43
|
-
authentication_keys = authentication_keys.keys if authentication_keys.is_a?(Hash)
|
|
44
|
-
|
|
45
|
-
authentication_keys.filter_map { |authentication_key| resource.public_send(authentication_key) }.first
|
|
46
|
-
end
|
|
47
42
|
end
|
|
48
43
|
end
|
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
module Devise
|
|
4
4
|
class TwoFactorAuthenticationsController < DeviseController
|
|
5
|
+
include Devise::Webauthn::PendingTwoFactorSignIn
|
|
6
|
+
|
|
5
7
|
prepend_before_action :set_resource, only: :new
|
|
6
8
|
prepend_before_action :ensure_sign_in_initiated
|
|
7
9
|
prepend_before_action :require_no_authentication
|
|
@@ -9,7 +11,7 @@ module Devise
|
|
|
9
11
|
def new; end
|
|
10
12
|
|
|
11
13
|
def create
|
|
12
|
-
self.resource = warden.authenticate!(auth_options)
|
|
14
|
+
self.resource = warden.authenticate!(:webauthn_two_factor_authenticatable, auth_options)
|
|
13
15
|
set_flash_message! :notice, :signed_in, scope: :"devise.sessions"
|
|
14
16
|
sign_in(resource_name, resource)
|
|
15
17
|
yield resource if block_given?
|
|
@@ -25,14 +27,16 @@ module Devise
|
|
|
25
27
|
private
|
|
26
28
|
|
|
27
29
|
def ensure_sign_in_initiated
|
|
28
|
-
return if
|
|
30
|
+
return if two_factor_sign_in_attempt_for(resource_name).present?
|
|
31
|
+
|
|
32
|
+
throw :warden, scope: resource_name, message: :sign_in_not_initiated unless is_navigational_format?
|
|
29
33
|
|
|
30
34
|
set_flash_message! :alert, :sign_in_not_initiated, scope: :"devise.failure"
|
|
31
35
|
redirect_to new_session_path(resource_name)
|
|
32
36
|
end
|
|
33
37
|
|
|
34
38
|
def set_resource
|
|
35
|
-
@resource = resource_class.find(
|
|
39
|
+
@resource = resource_class.find(two_factor_sign_in_attempt_for(resource_name)["id"])
|
|
36
40
|
end
|
|
37
41
|
end
|
|
38
42
|
end
|
|
@@ -21,6 +21,15 @@ module Devise
|
|
|
21
21
|
|
|
22
22
|
webauthn_id
|
|
23
23
|
end
|
|
24
|
+
|
|
25
|
+
# WebAuthn `user.name` and `displayName`, which the browser or OS shows for the credential. Defaults to
|
|
26
|
+
# the first authentication key with a non-nil value. Override it when that key is nil on a saved record.
|
|
27
|
+
def webauthn_user_name
|
|
28
|
+
authentication_keys = self.class.authentication_keys
|
|
29
|
+
authentication_keys = authentication_keys.keys if authentication_keys.is_a?(Hash)
|
|
30
|
+
|
|
31
|
+
authentication_keys.filter_map { |authentication_key| public_send(authentication_key) }.first
|
|
32
|
+
end
|
|
24
33
|
end
|
|
25
34
|
end
|
|
26
35
|
end
|
|
@@ -6,17 +6,15 @@ module Devise
|
|
|
6
6
|
module Strategies
|
|
7
7
|
# Default strategy for signing in a user, based on their email and password in the database.
|
|
8
8
|
class DatabaseAuthenticatable < Authenticatable
|
|
9
|
+
include Devise::Webauthn::PendingTwoFactorSignIn
|
|
10
|
+
|
|
9
11
|
def authenticate!
|
|
10
12
|
resource = password.present? && mapping.to.find_for_database_authentication(authentication_hash)
|
|
11
13
|
hashed = false
|
|
12
14
|
|
|
13
15
|
if validate(resource){ hashed = true; resource.valid_password?(password) }
|
|
14
16
|
if second_factor_enabled?(resource)
|
|
15
|
-
|
|
16
|
-
session[:current_authentication_remember_me] = remember_me?
|
|
17
|
-
request.flash[:notice] = two_factor_required_message
|
|
18
|
-
request.commit_flash
|
|
19
|
-
redirect!(two_factor_authentication_path, {}, message: two_factor_required_message)
|
|
17
|
+
require_second_factor(resource)
|
|
20
18
|
else
|
|
21
19
|
remember_me(resource)
|
|
22
20
|
resource.after_database_authentication
|
|
@@ -39,6 +37,32 @@ module Devise
|
|
|
39
37
|
Rails.application.routes.url_helpers.send(:"new_#{scope}_two_factor_authentication_path")
|
|
40
38
|
end
|
|
41
39
|
|
|
40
|
+
def require_second_factor(resource)
|
|
41
|
+
token = start_two_factor_sign_in_attempt_for(resource, scope: scope, remember_me: remember_me?)
|
|
42
|
+
|
|
43
|
+
if navigational_request?
|
|
44
|
+
redirect_to_two_factor_authentication
|
|
45
|
+
else
|
|
46
|
+
custom!(two_factor_required_response(token))
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def redirect_to_two_factor_authentication
|
|
51
|
+
request.flash[:notice] = two_factor_required_message
|
|
52
|
+
request.commit_flash
|
|
53
|
+
redirect!(two_factor_authentication_path, {}, message: two_factor_required_message)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def navigational_request?
|
|
57
|
+
Devise.navigational_formats.include?(request.format.try(:ref))
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def two_factor_required_response(token)
|
|
61
|
+
body = { error: two_factor_required_message }
|
|
62
|
+
body[:two_factor_token] = token unless two_factor_token_stored_in_session?(scope)
|
|
63
|
+
Rack::Response.new(body.to_json, 401, "content-type" => "application/json").finish
|
|
64
|
+
end
|
|
65
|
+
|
|
42
66
|
def two_factor_required_message
|
|
43
67
|
I18n.t(:"#{scope}.two_factor_required", resource_name: scope, scope: "devise.failure", default: :two_factor_required)
|
|
44
68
|
end
|
|
@@ -3,12 +3,22 @@
|
|
|
3
3
|
module Devise
|
|
4
4
|
module Strategies
|
|
5
5
|
class PasskeyAuthenticatable < Devise::Strategies::Base
|
|
6
|
+
include Devise::Webauthn::ChallengeStoreAccess
|
|
7
|
+
include Devise::Webauthn::PublicKeyCredentialParam
|
|
8
|
+
|
|
6
9
|
def valid?
|
|
7
|
-
|
|
10
|
+
env["devise.allow_params_authentication"] &&
|
|
11
|
+
public_key_credential_param.present? &&
|
|
12
|
+
challenge_store.pending?(:passkey_authentication)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def store?
|
|
16
|
+
super && mapping.to.skip_session_storage.exclude?(:params_auth)
|
|
8
17
|
end
|
|
9
18
|
|
|
10
19
|
def authenticate! # rubocop:disable Metrics/AbcSize
|
|
11
|
-
|
|
20
|
+
expected_challenge = challenge_store.consume(:passkey_authentication)
|
|
21
|
+
passkey_from_params = WebAuthn::Credential.from_get(public_key_credential_param)
|
|
12
22
|
|
|
13
23
|
return fail!(:passkey_not_found) if passkey_from_params.user_handle.nil?
|
|
14
24
|
|
|
@@ -17,25 +27,19 @@ module Devise
|
|
|
17
27
|
|
|
18
28
|
return fail!(:passkey_not_found) if stored_passkey.blank?
|
|
19
29
|
|
|
20
|
-
verify_passkeys(passkey_from_params, stored_passkey)
|
|
30
|
+
verify_passkeys(passkey_from_params, stored_passkey, expected_challenge)
|
|
21
31
|
|
|
22
32
|
remember_me(resource)
|
|
23
33
|
success!(resource)
|
|
24
34
|
rescue WebAuthn::Error
|
|
25
35
|
fail!(:passkey_verification_failed)
|
|
26
|
-
ensure
|
|
27
|
-
session.delete(:authentication_challenge)
|
|
28
36
|
end
|
|
29
37
|
|
|
30
38
|
private
|
|
31
39
|
|
|
32
|
-
def
|
|
33
|
-
params[:public_key_credential]
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
def verify_passkeys(passkey_from_params, stored_passkey)
|
|
40
|
+
def verify_passkeys(passkey_from_params, stored_passkey, expected_challenge)
|
|
37
41
|
passkey_from_params.verify(
|
|
38
|
-
|
|
42
|
+
expected_challenge,
|
|
39
43
|
public_key: stored_passkey.public_key,
|
|
40
44
|
sign_count: stored_passkey.sign_count,
|
|
41
45
|
user_verification: true
|
|
@@ -3,16 +3,26 @@
|
|
|
3
3
|
module Devise
|
|
4
4
|
module Strategies
|
|
5
5
|
class WebauthnTwoFactorAuthenticatable < Devise::Strategies::Base
|
|
6
|
+
include Devise::Webauthn::ChallengeStoreAccess
|
|
7
|
+
include Devise::Webauthn::PublicKeyCredentialParam
|
|
8
|
+
include Devise::Webauthn::PendingTwoFactorSignIn
|
|
9
|
+
|
|
6
10
|
def valid?
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
11
|
+
public_key_credential_param.present? &&
|
|
12
|
+
two_factor_sign_in_attempt_for(scope).present? &&
|
|
13
|
+
challenge_store.pending?(:two_factor_authentication)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def store?
|
|
17
|
+
super && mapping.to.skip_session_storage.exclude?(:params_auth)
|
|
10
18
|
end
|
|
11
19
|
|
|
12
20
|
# rubocop:disable Metrics/AbcSize
|
|
13
21
|
def authenticate!
|
|
14
|
-
|
|
15
|
-
|
|
22
|
+
expected_challenge = challenge_store.consume(:two_factor_authentication)
|
|
23
|
+
credential_from_params = WebAuthn::Credential.from_get(public_key_credential_param)
|
|
24
|
+
sign_in_attempt = two_factor_sign_in_attempt_for(scope)
|
|
25
|
+
resource = resource_class.find_by(id: sign_in_attempt["id"])
|
|
16
26
|
stored_credential = resource&.webauthn_credentials&.find_by(external_id: credential_from_params.id)
|
|
17
27
|
|
|
18
28
|
return fail!(:webauthn_credential_not_found) if stored_credential.blank?
|
|
@@ -20,29 +30,22 @@ module Devise
|
|
|
20
30
|
return fail!(:webauthn_credential_verification_failed)
|
|
21
31
|
end
|
|
22
32
|
|
|
23
|
-
verify_credential(credential_from_params, stored_credential)
|
|
33
|
+
verify_credential(credential_from_params, stored_credential, expected_challenge)
|
|
24
34
|
|
|
25
|
-
resource.remember_me =
|
|
35
|
+
resource.remember_me = sign_in_attempt["remember_me"] if resource.respond_to?(:remember_me=)
|
|
26
36
|
success!(resource)
|
|
27
37
|
|
|
28
|
-
|
|
29
|
-
session.delete(:current_authentication_remember_me)
|
|
38
|
+
clear_two_factor_sign_in_attempt(scope)
|
|
30
39
|
rescue WebAuthn::Error
|
|
31
40
|
fail!(:webauthn_credential_verification_failed)
|
|
32
|
-
ensure
|
|
33
|
-
session.delete(:two_factor_authentication_challenge)
|
|
34
41
|
end
|
|
35
42
|
# rubocop:enable Metrics/AbcSize
|
|
36
43
|
|
|
37
44
|
private
|
|
38
45
|
|
|
39
|
-
def
|
|
40
|
-
params[:public_key_credential]
|
|
41
|
-
end
|
|
42
|
-
|
|
43
|
-
def verify_credential(credential_from_params, stored_credential)
|
|
46
|
+
def verify_credential(credential_from_params, stored_credential, expected_challenge)
|
|
44
47
|
credential_from_params.verify(
|
|
45
|
-
|
|
48
|
+
expected_challenge,
|
|
46
49
|
public_key: stored_credential.public_key,
|
|
47
50
|
sign_count: stored_credential.sign_count
|
|
48
51
|
)
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Devise
|
|
4
|
+
module Webauthn
|
|
5
|
+
module ChallengeStoreAccess
|
|
6
|
+
private
|
|
7
|
+
|
|
8
|
+
def challenge_store
|
|
9
|
+
store = Devise::Webauthn.challenge_store
|
|
10
|
+
store = Devise::Webauthn::ChallengeStores.const_get(store.to_s.camelize) if store.is_a?(Symbol)
|
|
11
|
+
store.new(request)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Devise
|
|
4
|
+
module Webauthn
|
|
5
|
+
module ChallengeStores
|
|
6
|
+
class Cache
|
|
7
|
+
cattr_accessor :cache
|
|
8
|
+
cattr_accessor :expires_in, default: 5.minutes
|
|
9
|
+
|
|
10
|
+
def initialize(request)
|
|
11
|
+
@request = request
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def write(purpose, challenge)
|
|
15
|
+
store.write(key(purpose, challenge), true, expires_in: expires_in)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def pending?(purpose)
|
|
19
|
+
challenge.present? && store.exist?(key(purpose, challenge))
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# `exist?` honours expiry, which MemoryStore#delete does not. `delete` decides which concurrent request wins.
|
|
23
|
+
def consume(purpose)
|
|
24
|
+
return if challenge.blank?
|
|
25
|
+
return unless store.exist?(key(purpose, challenge))
|
|
26
|
+
return unless store.delete(key(purpose, challenge))
|
|
27
|
+
|
|
28
|
+
challenge
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
private
|
|
32
|
+
|
|
33
|
+
def store
|
|
34
|
+
cache || Rails.cache
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def key(purpose, challenge)
|
|
38
|
+
"devise_webauthn:challenge:#{purpose}:#{Digest::SHA256.hexdigest(challenge)}"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def challenge
|
|
42
|
+
return @challenge if defined?(@challenge)
|
|
43
|
+
|
|
44
|
+
@challenge = begin
|
|
45
|
+
client_data_json = credential.dig("response", "clientDataJSON") if credential.is_a?(Hash)
|
|
46
|
+
client_data = WebAuthn::ClientData.new(encoder.decode(client_data_json)) if client_data_json.is_a?(String)
|
|
47
|
+
encoder.encode(client_data.challenge) if client_data
|
|
48
|
+
rescue JSON::ParserError, ArgumentError, TypeError, EncodingError, NoMethodError
|
|
49
|
+
nil
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def credential
|
|
54
|
+
@credential ||= PublicKeyCredentialParam.parse(@request.params[:public_key_credential])
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def encoder
|
|
58
|
+
WebAuthn.configuration.encoder
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Devise
|
|
4
|
+
module Webauthn
|
|
5
|
+
module ChallengeStores
|
|
6
|
+
class Session
|
|
7
|
+
KEYS = {
|
|
8
|
+
passkey_authentication: :authentication_challenge,
|
|
9
|
+
two_factor_authentication: :two_factor_authentication_challenge,
|
|
10
|
+
passkey_registration: :passkey_registration_challenge,
|
|
11
|
+
security_key_registration: :security_key_registration_challenge
|
|
12
|
+
}.freeze
|
|
13
|
+
|
|
14
|
+
def initialize(request)
|
|
15
|
+
@session = request.session
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def write(purpose, challenge)
|
|
19
|
+
@session[KEYS.fetch(purpose)] = challenge
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def pending?(purpose)
|
|
23
|
+
@session[KEYS.fetch(purpose)].present?
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def consume(purpose)
|
|
27
|
+
@session.delete(KEYS.fetch(purpose))
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Devise
|
|
4
|
+
module Webauthn
|
|
5
|
+
module CredentialProperties
|
|
6
|
+
private
|
|
7
|
+
|
|
8
|
+
def credential_record_attributes(credential, discoverable: reported_discoverable?(credential))
|
|
9
|
+
{
|
|
10
|
+
external_id: credential.id,
|
|
11
|
+
public_key: credential.public_key,
|
|
12
|
+
sign_count: credential.sign_count,
|
|
13
|
+
discoverable: discoverable,
|
|
14
|
+
uv_initialized: credential.response.authenticator_data.user_verified?
|
|
15
|
+
}
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# The browser reports rk, so it is not signed. It is false when the browser does not report it.
|
|
19
|
+
def reported_discoverable?(credential)
|
|
20
|
+
outputs = credential.client_extension_outputs
|
|
21
|
+
outputs.is_a?(Hash) && outputs["credProps"].is_a?(Hash) && outputs["credProps"]["rk"] == true
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -19,7 +19,8 @@ module Devise
|
|
|
19
19
|
:webauthn_two_factor_authenticatable,
|
|
20
20
|
{
|
|
21
21
|
model: "devise/models/webauthn_two_factor_authenticatable",
|
|
22
|
-
strategy
|
|
22
|
+
# Not a default strategy, so only `TwoFactorAuthenticationsController#create` runs it, by name.
|
|
23
|
+
strategy: false,
|
|
23
24
|
route: { two_factor_authentication: [] }
|
|
24
25
|
}
|
|
25
26
|
)
|
|
@@ -35,6 +36,10 @@ module Devise
|
|
|
35
36
|
Devise.include_helpers(Devise::Webauthn)
|
|
36
37
|
end
|
|
37
38
|
|
|
39
|
+
initializer "devise.webauthn.filter_parameters" do |app|
|
|
40
|
+
app.config.filter_parameters += [:two_factor_token]
|
|
41
|
+
end
|
|
42
|
+
|
|
38
43
|
initializer "devise.webauthn.assets" do
|
|
39
44
|
if ::Rails.application.config.respond_to?(:assets)
|
|
40
45
|
::Rails.application.config.assets.precompile += %w[devise/webauthn.js]
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Devise
|
|
4
|
+
module Webauthn
|
|
5
|
+
module PendingTwoFactorSignIn
|
|
6
|
+
private
|
|
7
|
+
|
|
8
|
+
def start_two_factor_sign_in_attempt_for(resource, scope:, remember_me:)
|
|
9
|
+
token = TwoFactorToken.generate(resource, scope: scope, remember_me: remember_me)
|
|
10
|
+
request.session[:two_factor_token] = token if two_factor_token_stored_in_session?(scope)
|
|
11
|
+
token
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def two_factor_sign_in_attempt_for(scope)
|
|
15
|
+
@two_factor_sign_in_attempts ||= {}
|
|
16
|
+
return @two_factor_sign_in_attempts[scope] if @two_factor_sign_in_attempts.key?(scope)
|
|
17
|
+
|
|
18
|
+
@two_factor_sign_in_attempts[scope] = TwoFactorToken.read(received_two_factor_token(scope), scope: scope)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def clear_two_factor_sign_in_attempt(scope)
|
|
22
|
+
@two_factor_sign_in_attempts = nil
|
|
23
|
+
request.session.delete(:two_factor_token) if two_factor_token_stored_in_session?(scope)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# Without session storage, clients send the `two_factor_token` in the request body.
|
|
27
|
+
# The query string is ignored because servers and proxies log URLs.
|
|
28
|
+
def received_two_factor_token(scope)
|
|
29
|
+
if two_factor_token_stored_in_session?(scope)
|
|
30
|
+
request.session[:two_factor_token]
|
|
31
|
+
else
|
|
32
|
+
request.request_parameters["two_factor_token"]
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# `devise.skip_storage` is Devise's per-request switch for what `skip_session_storage` does for every request.
|
|
37
|
+
def two_factor_token_stored_in_session?(scope)
|
|
38
|
+
!request.env["devise.skip_storage"] && Devise.mappings[scope].to.skip_session_storage.exclude?(:params_auth)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Devise
|
|
4
|
+
module Webauthn
|
|
5
|
+
module PublicKeyCredentialParam
|
|
6
|
+
# Browser forms send the credential as a JSON string; API clients send it as a JSON object.
|
|
7
|
+
def self.parse(value)
|
|
8
|
+
value = JSON.parse(value) if value.is_a?(String)
|
|
9
|
+
# Controller params are ActionController::Parameters, not a Hash. Strong parameters are not
|
|
10
|
+
# needed: WebAuthn only verifies the credential, and nothing assigns it to a model.
|
|
11
|
+
value = value.to_unsafe_h if value.respond_to?(:to_unsafe_h)
|
|
12
|
+
value if value.is_a?(Hash) && value["response"].is_a?(Hash)
|
|
13
|
+
rescue JSON::ParserError
|
|
14
|
+
nil
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def public_key_credential_param
|
|
20
|
+
return @public_key_credential_param if defined?(@public_key_credential_param)
|
|
21
|
+
|
|
22
|
+
@public_key_credential_param = PublicKeyCredentialParam.parse(params[:public_key_credential])
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -16,13 +16,14 @@ module Devise
|
|
|
16
16
|
add_credential_to_authenticator(authenticator, resource, passkey: true, name: name)
|
|
17
17
|
end
|
|
18
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)
|
|
19
|
+
def add_security_key_to_authenticator(authenticator, resource, name: "My Security Key", promotable: false)
|
|
20
|
+
add_credential_to_authenticator(authenticator, resource, passkey: false, name: name, discoverable: promotable)
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
# rubocop:disable Metrics/AbcSize
|
|
24
24
|
# rubocop:disable Metrics/MethodLength
|
|
25
|
-
def add_credential_to_authenticator(authenticator, resource, passkey:, name: "My Credential"
|
|
25
|
+
def add_credential_to_authenticator(authenticator, resource, passkey:, name: "My Credential",
|
|
26
|
+
discoverable: passkey)
|
|
26
27
|
credential_id = SecureRandom.random_bytes(16)
|
|
27
28
|
encoded_credential_id = Base64.urlsafe_encode64(credential_id)
|
|
28
29
|
key = OpenSSL::PKey.generate_key("ED25519")
|
|
@@ -34,12 +35,12 @@ module Devise
|
|
|
34
35
|
|
|
35
36
|
credential_json = {
|
|
36
37
|
"credentialId" => encoded_credential_id,
|
|
37
|
-
"isResidentCredential" =>
|
|
38
|
+
"isResidentCredential" => discoverable,
|
|
38
39
|
"rpId" => "localhost",
|
|
39
40
|
"privateKey" => encoded_private_key,
|
|
40
41
|
"signCount" => 0
|
|
41
42
|
}
|
|
42
|
-
credential_json["userHandle"] = resource.ensure_webauthn_id! if
|
|
43
|
+
credential_json["userHandle"] = resource.ensure_webauthn_id! if discoverable
|
|
43
44
|
|
|
44
45
|
authenticator.add_credential(credential_json)
|
|
45
46
|
|
|
@@ -48,7 +49,9 @@ module Devise
|
|
|
48
49
|
external_id: Base64.urlsafe_encode64(credential_id, padding: false),
|
|
49
50
|
public_key: encoded_cose_public_key,
|
|
50
51
|
sign_count: 0,
|
|
51
|
-
authentication_factor: passkey ? :first_factor : :second_factor
|
|
52
|
+
authentication_factor: passkey ? :first_factor : :second_factor,
|
|
53
|
+
discoverable: discoverable,
|
|
54
|
+
uv_initialized: discoverable
|
|
52
55
|
)
|
|
53
56
|
end
|
|
54
57
|
# rubocop:enable Metrics/MethodLength
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Devise
|
|
4
|
+
module Webauthn
|
|
5
|
+
class TwoFactorToken
|
|
6
|
+
PURPOSE = :devise_webauthn_two_factor
|
|
7
|
+
|
|
8
|
+
cattr_accessor :expires_in, default: 5.minutes
|
|
9
|
+
|
|
10
|
+
def self.generate(resource, scope:, remember_me:)
|
|
11
|
+
payload = { "id" => resource.id, "scope" => scope.to_s, "remember_me" => remember_me }
|
|
12
|
+
encryptor.encrypt_and_sign(payload, purpose: PURPOSE, expires_in: expires_in)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.read(token, scope:)
|
|
16
|
+
return unless token.is_a?(String)
|
|
17
|
+
|
|
18
|
+
payload = encryptor.decrypt_and_verify(token, purpose: PURPOSE)
|
|
19
|
+
payload if payload.is_a?(Hash) && payload["scope"] == scope.to_s
|
|
20
|
+
rescue ActiveSupport::MessageEncryptor::InvalidMessage
|
|
21
|
+
nil
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# A fixed serializer, so a change to the app's message serializer does not make issued tokens raise.
|
|
25
|
+
def self.encryptor
|
|
26
|
+
key = Rails.application.key_generator.generate_key(PURPOSE.to_s, ActiveSupport::MessageEncryptor.key_len)
|
|
27
|
+
ActiveSupport::MessageEncryptor.new(key, serializer: JSON)
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
data/lib/devise/webauthn.rb
CHANGED
|
@@ -4,6 +4,13 @@ require "devise"
|
|
|
4
4
|
require "webauthn"
|
|
5
5
|
|
|
6
6
|
require_relative "webauthn/version"
|
|
7
|
+
require_relative "webauthn/challenge_stores/session"
|
|
8
|
+
require_relative "webauthn/challenge_stores/cache"
|
|
9
|
+
require_relative "webauthn/challenge_store_access"
|
|
10
|
+
require_relative "webauthn/two_factor_token"
|
|
11
|
+
require_relative "webauthn/pending_two_factor_sign_in"
|
|
12
|
+
require_relative "webauthn/public_key_credential_param"
|
|
13
|
+
require_relative "webauthn/credential_properties"
|
|
7
14
|
require_relative "webauthn/engine"
|
|
8
15
|
require_relative "webauthn/helpers/credentials_helper"
|
|
9
16
|
require_relative "webauthn/routes"
|
|
@@ -11,6 +18,8 @@ require_relative "webauthn/url_helpers"
|
|
|
11
18
|
|
|
12
19
|
module Devise
|
|
13
20
|
module Webauthn
|
|
21
|
+
mattr_accessor :challenge_store, default: :session
|
|
22
|
+
|
|
14
23
|
module Test
|
|
15
24
|
autoload :AuthenticatorHelpers, "devise/webauthn/test/authenticator_helpers"
|
|
16
25
|
end
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
class <%= @scope_prefix %>PasskeyAuthenticationOptionsController < Devise::PasskeyAuthenticationOptionsController
|
|
4
|
-
#
|
|
5
|
-
# def
|
|
4
|
+
# POST /resource/passkey_authentication_options
|
|
5
|
+
# def create
|
|
6
6
|
# super
|
|
7
7
|
# end
|
|
8
8
|
end
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
class <%= @scope_prefix %>PasskeyRegistrationOptionsController < Devise::PasskeyRegistrationOptionsController
|
|
4
|
-
#
|
|
5
|
-
# def
|
|
4
|
+
# POST /resource/passkey_registration_options
|
|
5
|
+
# def create
|
|
6
6
|
# super
|
|
7
7
|
# end
|
|
8
8
|
end
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
class <%= @scope_prefix %>SecurityKeyAuthenticationOptionsController < Devise::SecurityKeyAuthenticationOptionsController
|
|
4
|
-
#
|
|
5
|
-
# def
|
|
4
|
+
# POST /resource/security_key_authentication_options
|
|
5
|
+
# def create
|
|
6
6
|
# super
|
|
7
7
|
# end
|
|
8
8
|
end
|