webauthn-rails 0.0.1 → 0.1.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/MIT-LICENSE +20 -0
- data/README.md +167 -13
- data/Rakefile +12 -4
- data/lib/generators/erb/webauthn_authentication/templates/app/views/passkeys/new.html.erb.tt +18 -0
- data/lib/generators/erb/webauthn_authentication/templates/app/views/second_factor_authentications/new.html.erb.tt +18 -0
- data/lib/generators/erb/webauthn_authentication/templates/app/views/second_factor_webauthn_credentials/new.html.erb.tt +18 -0
- data/lib/generators/erb/webauthn_authentication/webauthn_authentication_generator.rb +35 -0
- data/lib/generators/test_unit/webauthn_authentication/templates/test/controllers/passkeys_controller_test.rb +111 -0
- data/lib/generators/test_unit/webauthn_authentication/templates/test/controllers/webauthn_sessions_controller_test.rb +125 -0
- data/lib/generators/test_unit/webauthn_authentication/templates/test/system/manage_webauthn_credentials_test.rb +76 -0
- data/lib/generators/test_unit/webauthn_authentication/templates/test/test_helpers/virtual_authenticator_test_helper.rb +9 -0
- data/lib/generators/test_unit/webauthn_authentication/webauthn_authentication_generator.rb +23 -0
- data/lib/generators/webauthn_authentication/bundle_helper.rb +30 -0
- data/lib/generators/webauthn_authentication/templates/app/controllers/passkeys_controller.rb +61 -0
- data/lib/generators/webauthn_authentication/templates/app/controllers/second_factor_authentications_controller.rb +62 -0
- data/lib/generators/webauthn_authentication/templates/app/controllers/second_factor_webauthn_credentials_controller.rb +59 -0
- data/lib/generators/webauthn_authentication/templates/app/controllers/webauthn_sessions_controller.rb +50 -0
- data/lib/generators/webauthn_authentication/templates/app/javascript/controllers/webauthn_credentials_controller.js +64 -0
- data/lib/generators/webauthn_authentication/templates/app/models/webauthn_credential.rb +12 -0
- data/lib/generators/webauthn_authentication/templates/config/initializers/webauthn.rb +8 -0
- data/lib/generators/webauthn_authentication/webauthn_authentication_generator.rb +182 -0
- data/lib/tasks/webauthn/rails_tasks.rake +4 -0
- data/lib/webauthn/rails/version.rb +1 -3
- data/lib/webauthn/rails.rb +1 -5
- metadata +53 -18
- data/.rspec +0 -3
- data/CHANGELOG.md +0 -5
- data/LICENSE.txt +0 -21
- data/sig/webauthn/rails.rbs +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5bf8803deeac8d925eef701fa4b9a25b3ecea1288e5af2b07dad81e60b16574d
|
4
|
+
data.tar.gz: ad28b85f03d4d693ec12d94c6702e6a8a6a0fbdd9226303efa520e61bc6c1000
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7d10d95538f98bfd0375159c3bd7acba311992cf06062aecfae5fe1f1c4bdf57de6494fd6c5ade9e70a5d5e096be723a768d330ec3722fe1f659904f4842d834
|
7
|
+
data.tar.gz: c81deb6084edc784f72d1cda4189e651a4748a75af71d80ad15e942a4ca6bd497f5f3211930b3416bb0ea474577fe7481850226938a9b725d1933bcbec652031
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright Cedarcode
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,32 +1,186 @@
|
|
1
|
-
#
|
1
|
+
# WebAuthn Rails
|
2
2
|
|
3
|
-
|
3
|
+
[](https://badge.fury.io/rb/webauthn-rails)
|
4
4
|
|
5
|
-
|
5
|
+
**webauthn-rails** adds passkeys to your Rails app with almost no setup. It provides a generator that installs everything you need for a secure passwordless and two-factor authentication flow, built on top of the [Rails Authentication system](https://guides.rubyonrails.org/security.html). Webauthn Rails combines [Stimulus](https://stimulus.hotwired.dev/) for the frontend experience with the [WebAuthn Ruby gem](https://github.com/cedarcode/webauthn-ruby) on the server side – giving you a ready-to-use authentication system.
|
6
6
|
|
7
|
-
|
7
|
+
## Requirements
|
8
8
|
|
9
|
-
|
9
|
+
- **Ruby**: 3.2+
|
10
|
+
- **Rails**: 8.0+
|
11
|
+
- **Stimulus Rails**: This gem requires [stimulus-rails](https://github.com/hotwired/stimulus-rails) to be installed and configured in your application
|
10
12
|
|
11
|
-
|
13
|
+
### JavaScript Dependencies
|
12
14
|
|
13
|
-
|
15
|
+
The generator automatically handles JavaScript dependencies based on your setup:
|
14
16
|
|
15
|
-
|
17
|
+
- **Importmap**: Pins `@github/webauthn-json/browser-ponyfill` to your importmap
|
18
|
+
- **Node.js/Yarn/Bun**: Adds the package to your package manager
|
16
19
|
|
17
20
|
## Usage
|
18
21
|
|
19
|
-
|
22
|
+
Install the gem by running:
|
20
23
|
|
21
|
-
|
24
|
+
```bash
|
25
|
+
$ bundle add webauthn-rails --group development
|
26
|
+
```
|
22
27
|
|
23
|
-
|
28
|
+
Next, you need to run the generator:
|
24
29
|
|
25
|
-
|
30
|
+
```bash
|
31
|
+
$ bin/rails generate webauthn_authentication
|
32
|
+
```
|
33
|
+
|
34
|
+
If you haven't generated Rails authentication yet, you can pass the `--with-rails-authentication` flag in order to generate it alongside the webauthn authentication:
|
35
|
+
```bash
|
36
|
+
$ bin/rails generate webauthn_authentication --with-rails-authentication
|
37
|
+
```
|
38
|
+
|
39
|
+
This generator will:
|
40
|
+
|
41
|
+
- **Optionally** invoke the [Rails Authentication generator](https://github.com/rails/rails/blob/main/railties/lib/rails/generators/rails/authentication/authentication_generator.rb) if the `--with-rails-authentication` flag is passed.
|
42
|
+
- Modifies the `SessionsController` to support WebAuthn two-factor authentication.
|
43
|
+
- Create controllers for handling passwordless and two-factor authentication, as well as credential management.
|
44
|
+
- Update new session views to support passkey authentication.
|
45
|
+
- Add views for credential management and two-factor authentication.
|
46
|
+
- Update the `User` model to include association with credentials and webauthn-related logic.
|
47
|
+
- Generate database migrations for WebAuthn credentials.
|
48
|
+
- Add passkey authentication, two-factor authentication and credential management routes.
|
49
|
+
- Generate a Stimulus controller for WebAuthn interactions.
|
50
|
+
- Create the WebAuthn initializer.
|
51
|
+
|
52
|
+
### Post-Installation Configuration
|
53
|
+
|
54
|
+
After running the generator, you **must** configure the WebAuthn settings:
|
55
|
+
|
56
|
+
1. Edit `config/initializers/webauthn.rb` and set your allowed origins and Relying Party name:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
WebAuthn.configure do |config|
|
60
|
+
# This value needs to match `window.location.origin` evaluated by
|
61
|
+
# the User Agent during registration and authentication ceremonies.
|
62
|
+
config.allowed_origins = ["https://yourapp.com"]
|
63
|
+
|
64
|
+
# Relying Party name for display purposes
|
65
|
+
config.rp_name = "Your App Name"
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
2. Run the migrations:
|
70
|
+
|
71
|
+
```bash
|
72
|
+
$ bin/rails db:migrate
|
73
|
+
```
|
74
|
+
|
75
|
+
## How it Works
|
76
|
+
|
77
|
+
### User Sign-In
|
78
|
+
|
79
|
+
Users can sign in by visiting `/session/new`. The generated setup supports two ways to log in:
|
80
|
+
|
81
|
+
- Email and password – via the standard Rails Authentication flow. On top of that, if the user has enabled two-factor authentication, they will be prompted to verify with a WebAuthn credential.
|
82
|
+
- Passkey (WebAuthn) – by selecting a [passkey](https://www.w3.org/TR/webauthn-3/#discoverable-credential) linked to the user’s account.
|
83
|
+
|
84
|
+
The WebAuthn passkey sign-in flow works as follows:
|
85
|
+
1. User clicks "Sign in with Passkey", starting a WebAuthn authentication ceremony.
|
86
|
+
2. Browser shows available passkeys.
|
87
|
+
3. User selects a passkey and verifies with their [authenticator](https://www.w3.org/TR/webauthn-3/#webauthn-authenticator).
|
88
|
+
4. The server verifies the response and signs in the user.
|
89
|
+
|
90
|
+
The WebAuthn two-factor authentication flow works as follows:
|
91
|
+
1. User signs in with email and password.
|
92
|
+
2. If the user has 2FA enabled, they are asked to use a webauthn credential to complete sign-in.
|
93
|
+
3. User selects a credential and verifies with their authenticator.
|
94
|
+
4. The server verifies the response and completes sign-in.
|
95
|
+
|
96
|
+
### Adding Credentials
|
97
|
+
|
98
|
+
Signed-in users can add passkeys by visiting `/passkeys/new`, and second factor credentials by visiting `/second_factor_webauthn_credentials/new`.
|
99
|
+
|
100
|
+
|
101
|
+
### Models
|
102
|
+
|
103
|
+
#### User Model
|
104
|
+
|
105
|
+
The generator adds WebAuthn functionality to your User model:
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
class User < ApplicationRecord
|
109
|
+
has_many :webauthn_credentials, dependent: :destroy
|
110
|
+
with_options class_name: "WebauthnCredential" do
|
111
|
+
has_many :second_factor_webauthn_credentials, -> { second_factor }
|
112
|
+
has_many :passkeys, -> { passkey }
|
113
|
+
end
|
114
|
+
|
115
|
+
after_initialize do
|
116
|
+
self.webauthn_id ||= WebAuthn.generate_user_id
|
117
|
+
end
|
118
|
+
|
119
|
+
def second_factor_enabled?
|
120
|
+
webauthn_credentials.any?
|
121
|
+
end
|
122
|
+
end
|
123
|
+
```
|
124
|
+
|
125
|
+
#### WebauthnCredential Model
|
126
|
+
|
127
|
+
Stores the public keys and metadata for each registered authenticator:
|
128
|
+
|
129
|
+
```ruby
|
130
|
+
class WebauthnCredential < ApplicationRecord
|
131
|
+
belongs_to :user
|
132
|
+
|
133
|
+
validates :external_id, :public_key, :nickname, :sign_count, presence: true
|
134
|
+
validates :external_id, uniqueness: true
|
135
|
+
validates :sign_count,
|
136
|
+
numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 2**32 - 1 }
|
137
|
+
|
138
|
+
enum :authentication_factor, { first_factor: 0, second_factor: 1 }
|
139
|
+
|
140
|
+
scope :passkey, -> { first_factor }
|
141
|
+
end
|
142
|
+
```
|
143
|
+
|
144
|
+
## Customization
|
145
|
+
|
146
|
+
### Views
|
147
|
+
|
148
|
+
The generator creates view templates that you can customize:
|
149
|
+
|
150
|
+
- `app/views/passkeys/new.html.erb` - Add new passkey form.
|
151
|
+
- `app/views/second_factor_webauthn_credentials/new.html.erb` - Add new second factor credential form.
|
152
|
+
- `app/views/second_factor_authentications/new.html.erb` - Two-factor authentication form.
|
153
|
+
|
154
|
+
### Stimulus Controller
|
155
|
+
|
156
|
+
The generated Stimulus controller (`webauthn_credentials_controller.js`) handles the WebAuthn JavaScript API interactions. You can extend or customize it for your specific needs.
|
26
157
|
|
27
158
|
## Contributing
|
28
159
|
|
29
|
-
|
160
|
+
Issues and pull requests are welcome on GitHub at https://github.com/cedarcode/webauthn-rails.
|
161
|
+
|
162
|
+
### Development
|
163
|
+
|
164
|
+
After checking out the repo, run:
|
165
|
+
|
166
|
+
```bash
|
167
|
+
$ bundle install
|
168
|
+
```
|
169
|
+
|
170
|
+
To run the tests:
|
171
|
+
|
172
|
+
```bash
|
173
|
+
$ bundle exec rake test
|
174
|
+
$ bundle exec rake test_dummy
|
175
|
+
```
|
176
|
+
|
177
|
+
To run the linter:
|
178
|
+
|
179
|
+
```bash
|
180
|
+
$ bundle exec rubocop
|
181
|
+
```
|
182
|
+
|
183
|
+
Before submitting a PR, make sure both tests pass and there are no linting errors.
|
30
184
|
|
31
185
|
## License
|
32
186
|
|
data/Rakefile
CHANGED
@@ -1,8 +1,16 @@
|
|
1
|
-
|
1
|
+
require "bundler/setup"
|
2
|
+
require 'rake/testtask'
|
2
3
|
|
3
4
|
require "bundler/gem_tasks"
|
4
|
-
require "rspec/core/rake_task"
|
5
5
|
|
6
|
-
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << 'lib'
|
8
|
+
t.libs << 'test'
|
9
|
+
t.test_files = FileList['test/**/*_test.rb'].exclude('test/tmp/**/*_test.rb').exclude('test/dummy/**/*_test.rb')
|
10
|
+
end
|
7
11
|
|
8
|
-
|
12
|
+
Rake::TestTask.new(:test_dummy) do |t|
|
13
|
+
t.libs << 'test/dummy/lib'
|
14
|
+
t.libs << 'test/dummy/test'
|
15
|
+
t.test_files = FileList['test/dummy/**/*_test.rb']
|
16
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
<%%= tag.div(flash[:alert], style: "color:red") if flash[:alert] %>
|
2
|
+
<%%= tag.div(flash[:notice], style: "color:green") if flash[:notice] %>
|
3
|
+
|
4
|
+
<%%= form_with(
|
5
|
+
scope: :credential,
|
6
|
+
url: passkeys_path,
|
7
|
+
data: {
|
8
|
+
controller: "webauthn-credentials",
|
9
|
+
action: "webauthn-credentials#create:prevent",
|
10
|
+
"webauthn-credentials-options-url-value": create_options_passkeys_path,
|
11
|
+
}) do |form| %>
|
12
|
+
<div class="field">
|
13
|
+
<%%= form.label :nickname, 'Security Key nickname' %>
|
14
|
+
<%%= form.text_field :nickname, required: true %>
|
15
|
+
</div>
|
16
|
+
<%%= form.hidden_field :public_key_credential, data: { "webauthn-credentials-target": "credentialHiddenInput" } %>
|
17
|
+
<%%= form.submit "Add Security Key", disabled: true, data: { "webauthn-credentials-target": "submitButton" } %>
|
18
|
+
<%% end %>
|
@@ -0,0 +1,18 @@
|
|
1
|
+
<%%= tag.div(flash[:alert], style: "color:red") if flash[:alert] %>
|
2
|
+
<%%= tag.div(flash[:notice], style: "color:green") if flash[:notice] %>
|
3
|
+
|
4
|
+
<h3>Two-factor authentication</h3>
|
5
|
+
<p>Use a security key to sign in.</p>
|
6
|
+
|
7
|
+
<%%= form_with(
|
8
|
+
scope: :session,
|
9
|
+
url: second_factor_authentication_path,
|
10
|
+
method: :post,
|
11
|
+
data: {
|
12
|
+
controller: "webauthn-credentials",
|
13
|
+
action: "webauthn-credentials#get:prevent",
|
14
|
+
"webauthn-credentials-options-url-value": get_options_second_factor_authentication_path,
|
15
|
+
}) do |f| %>
|
16
|
+
<%%= f.hidden_field :public_key_credential, data: { "webauthn-credentials-target": "credentialHiddenInput" } %>
|
17
|
+
<%%= f.submit "Use Security Key", disabled: true, data: { "webauthn-credentials-target": "submitButton" } %>
|
18
|
+
<%% end %>
|
@@ -0,0 +1,18 @@
|
|
1
|
+
<%%= tag.div(flash[:alert], style: "color:red") if flash[:alert] %>
|
2
|
+
<%%= tag.div(flash[:notice], style: "color:green") if flash[:notice] %>
|
3
|
+
|
4
|
+
<%%= form_with(
|
5
|
+
scope: :credential,
|
6
|
+
url: second_factor_webauthn_credentials_path,
|
7
|
+
data: {
|
8
|
+
controller: "webauthn-credentials",
|
9
|
+
action: "webauthn-credentials#create:prevent",
|
10
|
+
"webauthn-credentials-options-url-value": create_options_second_factor_webauthn_credentials_path,
|
11
|
+
}) do |form| %>
|
12
|
+
<div class="field">
|
13
|
+
<%%= form.label :nickname, 'Security Key nickname' %>
|
14
|
+
<%%= form.text_field :nickname, required: true %>
|
15
|
+
</div>
|
16
|
+
<%%= form.hidden_field :public_key_credential, data: { "webauthn-credentials-target": "credentialHiddenInput" } %>
|
17
|
+
<%%= form.submit "Add Security Key", disabled: true, data: { "webauthn-credentials-target": "submitButton" } %>
|
18
|
+
<%% end %>
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require "rails/generators/erb"
|
2
|
+
|
3
|
+
module Erb
|
4
|
+
module Generators
|
5
|
+
class WebauthnAuthenticationGenerator < Rails::Generators::Base
|
6
|
+
hide!
|
7
|
+
source_root File.expand_path("../templates", __FILE__)
|
8
|
+
|
9
|
+
def create_files
|
10
|
+
template "app/views/passkeys/new.html.erb.tt"
|
11
|
+
template "app/views/second_factor_authentications/new.html.erb.tt"
|
12
|
+
template "app/views/second_factor_webauthn_credentials/new.html.erb.tt"
|
13
|
+
end
|
14
|
+
|
15
|
+
def inject_into_rails_session_view
|
16
|
+
append_to_file "app/views/sessions/new.html.erb" do
|
17
|
+
<<-ERB.strip_heredoc
|
18
|
+
<%= form_with(
|
19
|
+
scope: :session,
|
20
|
+
url: webauthn_session_path,
|
21
|
+
method: :post,
|
22
|
+
data: {
|
23
|
+
controller: "webauthn-credentials",
|
24
|
+
action: "webauthn-credentials#get:prevent",
|
25
|
+
"webauthn-credentials-options-url-value": get_options_webauthn_session_path,
|
26
|
+
}) do |f| %>
|
27
|
+
<%= f.hidden_field :public_key_credential, data: { "webauthn-credentials-target": "credentialHiddenInput" } %>
|
28
|
+
<%= f.submit "Sign In with Passkey", disabled: true, data: { "webauthn-credentials-target": "submitButton" } %>
|
29
|
+
<% end %>
|
30
|
+
ERB
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
require "webauthn/fake_client"
|
3
|
+
|
4
|
+
class PasskeysControllerTest < ActionDispatch::IntegrationTest
|
5
|
+
setup do
|
6
|
+
@user = users(:one)
|
7
|
+
@client = WebAuthn::FakeClient.new(WebAuthn.configuration.allowed_origins.first)
|
8
|
+
end
|
9
|
+
|
10
|
+
test "initiates Passkey creation when user is authenticated" do
|
11
|
+
sign_in_as @user
|
12
|
+
post create_options_passkeys_url
|
13
|
+
|
14
|
+
assert_response :success
|
15
|
+
body = JSON.parse(response.body)
|
16
|
+
assert body["challenge"].present?
|
17
|
+
assert body["authenticatorSelection"]["residentKey"] == "required"
|
18
|
+
assert body["authenticatorSelection"]["userVerification"] == "required"
|
19
|
+
|
20
|
+
assert_equal session[:current_registration][:challenge], body["challenge"]
|
21
|
+
end
|
22
|
+
|
23
|
+
test "requires authentication to initiate Passkey creation" do
|
24
|
+
post create_options_passkeys_url
|
25
|
+
|
26
|
+
assert_response :redirect
|
27
|
+
assert_redirected_to new_session_url
|
28
|
+
end
|
29
|
+
|
30
|
+
test "creates passkey when user is authenticated" do
|
31
|
+
sign_in_as @user
|
32
|
+
|
33
|
+
post create_options_passkeys_url
|
34
|
+
challenge = session[:current_registration][:challenge]
|
35
|
+
|
36
|
+
public_key_credential = @client.create(
|
37
|
+
challenge: challenge,
|
38
|
+
user_verified: true,
|
39
|
+
)
|
40
|
+
|
41
|
+
assert_difference("WebauthnCredential.count", 1) do
|
42
|
+
post passkeys_url, params: {
|
43
|
+
credential: {
|
44
|
+
nickname: "My Passkey",
|
45
|
+
public_key_credential: public_key_credential.to_json
|
46
|
+
}
|
47
|
+
}
|
48
|
+
end
|
49
|
+
|
50
|
+
assert_redirected_to root_path
|
51
|
+
assert_match (/Security Key registered successfully/), flash[:notice]
|
52
|
+
assert_nil session[:current_registration]
|
53
|
+
end
|
54
|
+
|
55
|
+
test "does not create passkey when there is a Webauthn error" do
|
56
|
+
sign_in_as @user
|
57
|
+
|
58
|
+
post create_options_passkeys_url
|
59
|
+
challenge = session[:current_registration][:challenge]
|
60
|
+
|
61
|
+
public_key_credential = @client.create(
|
62
|
+
challenge: challenge,
|
63
|
+
user_verified: false,
|
64
|
+
)
|
65
|
+
|
66
|
+
assert_no_difference("WebauthnCredential.count") do
|
67
|
+
post passkeys_url, params: {
|
68
|
+
credential: {
|
69
|
+
nickname: "My Passkey",
|
70
|
+
public_key_credential: public_key_credential.to_json
|
71
|
+
}
|
72
|
+
}
|
73
|
+
end
|
74
|
+
|
75
|
+
assert_redirected_to new_passkey_path
|
76
|
+
assert_match (/Verification failed/), flash[:alert]
|
77
|
+
assert_nil session[:current_registration]
|
78
|
+
end
|
79
|
+
|
80
|
+
test "requires authentication to create passkey" do
|
81
|
+
post passkeys_url, params: {
|
82
|
+
credential: {
|
83
|
+
nickname: "My Passkey",
|
84
|
+
public_key_credential: "{}"
|
85
|
+
}
|
86
|
+
}
|
87
|
+
|
88
|
+
assert_response :redirect
|
89
|
+
assert_redirected_to new_session_url
|
90
|
+
end
|
91
|
+
|
92
|
+
test "deletes passkey when user is authenticated" do
|
93
|
+
2.times do |i|
|
94
|
+
WebauthnCredential.create!(
|
95
|
+
nickname: "My Passkey #{i}",
|
96
|
+
user: @user,
|
97
|
+
external_id: "external-id-#{i}",
|
98
|
+
public_key: "public-key-#{i}",
|
99
|
+
sign_count: 0,
|
100
|
+
authentication_factor: 0
|
101
|
+
)
|
102
|
+
end
|
103
|
+
|
104
|
+
sign_in_as @user
|
105
|
+
|
106
|
+
assert_difference("WebauthnCredential.count", -1) do
|
107
|
+
delete passkey_url(@user.webauthn_credentials.first)
|
108
|
+
end
|
109
|
+
assert_redirected_to root_path
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,125 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
require "webauthn/fake_client"
|
3
|
+
|
4
|
+
class WebauthnSessionsControllerTest < ActionDispatch::IntegrationTest
|
5
|
+
setup do
|
6
|
+
@user = users(:one)
|
7
|
+
@client = WebAuthn::FakeClient.new(WebAuthn.configuration.allowed_origins.first)
|
8
|
+
|
9
|
+
creation_options = WebAuthn::Credential.options_for_create(
|
10
|
+
user: { id: @user.webauthn_id, name: @user.email_address }
|
11
|
+
)
|
12
|
+
create_options = @client.create(challenge: creation_options.challenge)
|
13
|
+
credential = WebAuthn::Credential.from_create(create_options)
|
14
|
+
|
15
|
+
WebauthnCredential.create!(
|
16
|
+
nickname: "My Passkey",
|
17
|
+
user: @user,
|
18
|
+
external_id: credential.id,
|
19
|
+
public_key: credential.public_key,
|
20
|
+
sign_count: 0,
|
21
|
+
authentication_factor: 0
|
22
|
+
)
|
23
|
+
end
|
24
|
+
|
25
|
+
test "should return get_options" do
|
26
|
+
post get_options_webauthn_session_url
|
27
|
+
|
28
|
+
assert_response :success
|
29
|
+
body = JSON.parse(response.body)
|
30
|
+
assert body["challenge"].present?
|
31
|
+
assert body["userVerification"] == "required"
|
32
|
+
|
33
|
+
assert_equal session[:current_authentication][:challenge], body["challenge"]
|
34
|
+
end
|
35
|
+
|
36
|
+
test "should create session with valid credential" do
|
37
|
+
post get_options_webauthn_session_url
|
38
|
+
challenge = session[:current_authentication][:challenge]
|
39
|
+
|
40
|
+
public_key_credential = @client.get(challenge: challenge, user_verified: true)
|
41
|
+
|
42
|
+
post webauthn_session_url, params: {
|
43
|
+
session: {
|
44
|
+
public_key_credential: public_key_credential.to_json
|
45
|
+
}
|
46
|
+
}
|
47
|
+
|
48
|
+
assert_redirected_to root_path
|
49
|
+
assert_nil session[:current_authentication]
|
50
|
+
end
|
51
|
+
|
52
|
+
test "should not create session when there is a Webauthn error" do
|
53
|
+
post get_options_webauthn_session_url
|
54
|
+
challenge = session[:current_authentication][:challenge]
|
55
|
+
|
56
|
+
public_key_credential = @client.get(challenge: challenge, user_verified: false)
|
57
|
+
|
58
|
+
post webauthn_session_url, params: {
|
59
|
+
session: {
|
60
|
+
public_key_credential: public_key_credential.to_json
|
61
|
+
}
|
62
|
+
}
|
63
|
+
|
64
|
+
assert_redirected_to new_session_path
|
65
|
+
assert_match (/Verification failed/), flash[:alert]
|
66
|
+
assert_nil session[:current_authentication]
|
67
|
+
end
|
68
|
+
|
69
|
+
test "should not create session with unrecognized credential" do
|
70
|
+
post get_options_webauthn_session_url
|
71
|
+
challenge = session[:current_authentication][:challenge]
|
72
|
+
|
73
|
+
public_key_credential = @client.get(challenge: challenge, user_verified: true)
|
74
|
+
public_key_credential["id"] = "invalid-id"
|
75
|
+
|
76
|
+
post webauthn_session_url, params: {
|
77
|
+
session: {
|
78
|
+
public_key_credential: public_key_credential.to_json
|
79
|
+
}
|
80
|
+
}
|
81
|
+
|
82
|
+
assert_redirected_to new_session_path
|
83
|
+
assert_equal "Credential not recognized", flash[:alert]
|
84
|
+
assert_nil session[:current_authentication]
|
85
|
+
end
|
86
|
+
|
87
|
+
test "should not create session with a second factor credential" do
|
88
|
+
client = WebAuthn::FakeClient.new(WebAuthn.configuration.allowed_origins.first)
|
89
|
+
|
90
|
+
creation_options = WebAuthn::Credential.options_for_create(
|
91
|
+
user: { id: @user.webauthn_id, name: @user.email_address }
|
92
|
+
)
|
93
|
+
create_options = client.create(challenge: creation_options.challenge)
|
94
|
+
credential = WebAuthn::Credential.from_create(create_options)
|
95
|
+
|
96
|
+
WebauthnCredential.create!(
|
97
|
+
nickname: "Second Factor Key",
|
98
|
+
user: @user,
|
99
|
+
external_id: credential.id,
|
100
|
+
public_key: credential.public_key,
|
101
|
+
sign_count: 0,
|
102
|
+
authentication_factor: 1
|
103
|
+
)
|
104
|
+
|
105
|
+
post get_options_webauthn_session_url
|
106
|
+
challenge = session[:current_authentication][:challenge]
|
107
|
+
|
108
|
+
public_key_credential = client.get(challenge: challenge, user_verified: true)
|
109
|
+
|
110
|
+
post webauthn_session_url, params: {
|
111
|
+
session: {
|
112
|
+
public_key_credential: public_key_credential.to_json
|
113
|
+
}
|
114
|
+
}
|
115
|
+
|
116
|
+
assert_redirected_to new_session_path
|
117
|
+
assert_equal "Credential not recognized", flash[:alert]
|
118
|
+
assert_nil session[:current_authentication]
|
119
|
+
end
|
120
|
+
|
121
|
+
test "should destroy session" do
|
122
|
+
delete webauthn_session_url
|
123
|
+
assert_redirected_to new_session_path
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require "application_system_test_case"
|
2
|
+
require_relative "../test_helpers/virtual_authenticator_test_helper"
|
3
|
+
|
4
|
+
class ManageWebauthnCredentialsTest < ApplicationSystemTestCase
|
5
|
+
include VirtualAuthenticatorTestHelper
|
6
|
+
|
7
|
+
def setup
|
8
|
+
user = User.create!(email_address: "alice@example.com", password: "S3cr3tP@ssw0rd!")
|
9
|
+
sign_in_as(user)
|
10
|
+
@authenticator = add_virtual_authenticator
|
11
|
+
end
|
12
|
+
|
13
|
+
def teardown
|
14
|
+
@authenticator.remove!
|
15
|
+
end
|
16
|
+
|
17
|
+
test "add credentials and sign in" do
|
18
|
+
visit root_path
|
19
|
+
|
20
|
+
click_on "Add Passkey"
|
21
|
+
|
22
|
+
fill_in("Security Key nickname", with: "Touch ID")
|
23
|
+
click_on "Add Security Key"
|
24
|
+
|
25
|
+
assert_current_path "/"
|
26
|
+
assert_selector "div", text: "Security Key registered successfully"
|
27
|
+
assert_selector "span", text: "Touch ID"
|
28
|
+
|
29
|
+
click_on "Sign out"
|
30
|
+
assert_selector("input[type=submit][value='Sign in']")
|
31
|
+
|
32
|
+
click_on "Sign In with Passkey"
|
33
|
+
|
34
|
+
assert_current_path "/"
|
35
|
+
assert_selector "h3", text: "Your Passkeys"
|
36
|
+
end
|
37
|
+
|
38
|
+
test "sign in with 2FA WebAuthn credential" do
|
39
|
+
visit root_path
|
40
|
+
|
41
|
+
click_on "Add Second Factor Key"
|
42
|
+
|
43
|
+
fill_in("Security Key nickname", with: "Touch ID")
|
44
|
+
click_on "Add Security Key"
|
45
|
+
|
46
|
+
assert_current_path "/"
|
47
|
+
assert_selector "div", text: "Security Key registered successfully"
|
48
|
+
assert_selector "span", text: "Touch ID"
|
49
|
+
|
50
|
+
click_on "Sign out"
|
51
|
+
assert_selector("input[type=submit][value='Sign in']")
|
52
|
+
|
53
|
+
fill_in "email_address", with: "alice@example.com"
|
54
|
+
fill_in "password", with: "S3cr3tP@ssw0rd!"
|
55
|
+
click_on "Sign in"
|
56
|
+
|
57
|
+
assert_selector "h3", text: "Two-factor authentication"
|
58
|
+
click_on "Use Security Key"
|
59
|
+
|
60
|
+
assert_current_path "/"
|
61
|
+
assert_selector "h3", text: "Your Passkeys"
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def sign_in_as(user)
|
67
|
+
visit new_session_path
|
68
|
+
|
69
|
+
fill_in "email_address", with: user.email_address
|
70
|
+
fill_in "password", with: user.password
|
71
|
+
|
72
|
+
click_on "Sign in"
|
73
|
+
|
74
|
+
assert_selector "h3", text: "Your Passkeys"
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module VirtualAuthenticatorTestHelper
|
2
|
+
def add_virtual_authenticator
|
3
|
+
options = ::Selenium::WebDriver::VirtualAuthenticatorOptions.new
|
4
|
+
options.user_verification = true
|
5
|
+
options.user_verified = true
|
6
|
+
options.resident_key = true
|
7
|
+
page.driver.browser.add_virtual_authenticator(options)
|
8
|
+
end
|
9
|
+
end
|