-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcredentials_helper.rb
More file actions
140 lines (120 loc) · 4.64 KB
/
credentials_helper.rb
File metadata and controls
140 lines (120 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# frozen_string_literal: true
# rubocop:disable Metrics/ModuleLength
module Devise
module Webauthn
module CredentialsHelper
def passkey_creation_form_for(resource, form_classes: nil, &block)
form_with(
url: passkeys_path(resource),
method: :post,
class: form_classes
) do |f|
tag.webauthn_create(data: { options_json: create_passkey_options(resource) }) do
concat f.hidden_field(:public_key_credential, class: "js-webauthn-response")
concat capture(f, &block)
end
end
end
def login_with_passkey_button(text = nil, session_path:, button_classes: nil, form_classes: nil, &block)
form_with(
url: session_path,
method: :post,
class: form_classes
) do |f|
tag.webauthn_get(data: { options_json: passkey_authentication_options }) do
concat f.hidden_field(:public_key_credential, class: "js-webauthn-response")
concat f.button(text, type: "submit", class: button_classes, &block)
end
end
end
def security_key_creation_form_for(resource, form_classes: nil, &block)
form_with(
url: second_factor_webauthn_credentials_path(resource),
method: :post,
class: form_classes
) do |f|
tag.webauthn_create(data: { options_json: create_security_key_options(resource) }) do
concat f.hidden_field(:public_key_credential, class: "js-webauthn-response")
concat capture(f, &block)
end
end
end
def login_with_security_key_button(text = nil, resource:, button_classes: nil, form_classes: nil, &block)
form_with(
url: two_factor_authentication_path(resource),
method: :post,
class: form_classes
) do |f|
tag.webauthn_get(data: { options_json: security_key_authentication_options(resource) }) do
concat f.hidden_field(:public_key_credential, class: "js-webauthn-response")
concat f.button(text, type: "submit", class: button_classes, &block)
end
end
end
private
def create_passkey_options(resource)
@create_passkey_options ||= begin
options = WebAuthn::Credential.options_for_create(
user: {
id: resource.webauthn_id,
name: resource_human_palatable_identifier
},
exclude: resource.passkeys.pluck(:external_id),
authenticator_selection: {
resident_key: "required",
user_verification: "required"
}
)
# Store challenge in session for later verification
session[:webauthn_challenge] = options.challenge
options
end
end
def passkey_authentication_options
@passkey_authentication_options ||= begin
options = WebAuthn::Credential.options_for_get(
user_verification: "required"
)
# Store challenge in session for later verification
session[:authentication_challenge] = options.challenge
options
end
end
def create_security_key_options(resource)
@create_security_key_options ||= begin
options = WebAuthn::Credential.options_for_create(
user: {
id: resource.webauthn_id,
name: resource_human_palatable_identifier
},
exclude: resource.webauthn_credentials.pluck(:external_id),
authenticator_selection: {
resident_key: "discouraged",
user_verification: "discouraged"
}
)
# Store challenge in session for later verification
session[:webauthn_challenge] = options.challenge
options
end
end
def security_key_authentication_options(resource)
@security_key_authentication_options ||= begin
options = WebAuthn::Credential.options_for_get(
allow: resource.webauthn_credentials.pluck(:external_id),
user_verification: "discouraged"
)
# Store challenge in session for later verification
session[:two_factor_authentication_challenge] = options.challenge
options
end
end
def resource_human_palatable_identifier
authentication_keys = resource.class.authentication_keys
authentication_keys = authentication_keys.keys if authentication_keys.is_a?(Hash)
authentication_keys.filter_map { |authentication_key| resource.public_send(authentication_key) }.first
end
end
end
end
# rubocop:enable Metrics/ModuleLength