webauthn-rails 0.0.1 → 0.1.1

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.
Files changed (32) hide show
  1. checksums.yaml +4 -4
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +167 -13
  4. data/Rakefile +12 -4
  5. data/lib/generators/erb/webauthn_authentication/templates/app/views/passkeys/new.html.erb.tt +18 -0
  6. data/lib/generators/erb/webauthn_authentication/templates/app/views/second_factor_authentications/new.html.erb.tt +18 -0
  7. data/lib/generators/erb/webauthn_authentication/templates/app/views/second_factor_webauthn_credentials/new.html.erb.tt +18 -0
  8. data/lib/generators/erb/webauthn_authentication/webauthn_authentication_generator.rb +35 -0
  9. data/lib/generators/test_unit/webauthn_authentication/templates/test/controllers/passkeys_controller_test.rb +103 -0
  10. data/lib/generators/test_unit/webauthn_authentication/templates/test/controllers/second_factor_authentications_controller_test.rb +131 -0
  11. data/lib/generators/test_unit/webauthn_authentication/templates/test/controllers/second_factor_webauthn_credentials_controller_test.rb +103 -0
  12. data/lib/generators/test_unit/webauthn_authentication/templates/test/controllers/webauthn_sessions_controller_test.rb +123 -0
  13. data/lib/generators/test_unit/webauthn_authentication/templates/test/system/manage_webauthn_credentials_test.rb +76 -0
  14. data/lib/generators/test_unit/webauthn_authentication/templates/test/test_helpers/virtual_authenticator_test_helper.rb +9 -0
  15. data/lib/generators/test_unit/webauthn_authentication/webauthn_authentication_generator.rb +25 -0
  16. data/lib/generators/webauthn_authentication/bundle_helper.rb +30 -0
  17. data/lib/generators/webauthn_authentication/templates/app/controllers/passkeys_controller.rb +61 -0
  18. data/lib/generators/webauthn_authentication/templates/app/controllers/second_factor_authentications_controller.rb +62 -0
  19. data/lib/generators/webauthn_authentication/templates/app/controllers/second_factor_webauthn_credentials_controller.rb +59 -0
  20. data/lib/generators/webauthn_authentication/templates/app/controllers/webauthn_sessions_controller.rb +50 -0
  21. data/lib/generators/webauthn_authentication/templates/app/javascript/controllers/webauthn_credentials_controller.js +64 -0
  22. data/lib/generators/webauthn_authentication/templates/app/models/webauthn_credential.rb +12 -0
  23. data/lib/generators/webauthn_authentication/templates/config/initializers/webauthn.rb +8 -0
  24. data/lib/generators/webauthn_authentication/webauthn_authentication_generator.rb +184 -0
  25. data/lib/tasks/webauthn/rails_tasks.rake +4 -0
  26. data/lib/webauthn/rails/version.rb +1 -3
  27. data/lib/webauthn/rails.rb +1 -5
  28. metadata +55 -18
  29. data/.rspec +0 -3
  30. data/CHANGELOG.md +0 -5
  31. data/LICENSE.txt +0 -21
  32. data/sig/webauthn/rails.rbs +0 -6
@@ -0,0 +1,184 @@
1
+ require "rails/generators/base"
2
+ require "rails/generators/active_record/migration"
3
+ if Rails.version >= "8.1"
4
+ require "rails/generators/bundle_helper"
5
+ else
6
+ require "generators/webauthn_authentication/bundle_helper"
7
+ end
8
+
9
+ class WebauthnAuthenticationGenerator < ::Rails::Generators::Base
10
+ include ActiveRecord::Generators::Migration
11
+ if Rails.version >= "8.1"
12
+ include Rails::Generators::BundleHelper
13
+ else
14
+ include BundleHelper
15
+ end
16
+
17
+ source_root File.expand_path("../templates", __FILE__)
18
+
19
+ desc "Injects webauthn files to your application."
20
+
21
+ class_option :api, type: :boolean,
22
+ desc: "Generate API-only files, with no view templates"
23
+
24
+ class_option :with_rails_authentication, type: :boolean,
25
+ desc: "Run the Ruby on Rails authentication generator"
26
+
27
+ def invoke_rails_authentication
28
+ invoke "authentication" if options.with_rails_authentication?
29
+ end
30
+
31
+ def modify_sessions_controller
32
+ if File.exist?(File.join(destination_root, "app/controllers/sessions_controller.rb"))
33
+ gsub_file "app/controllers/sessions_controller.rb",
34
+ /^ def create.*?^ end/m,
35
+ <<~RUBY.strip_heredoc.indent(2)
36
+ def create
37
+ if user = User.authenticate_by(params.permit(:email_address, :password))
38
+ if user.second_factor_enabled?
39
+ session[:current_authentication] = { user_id: user.id }
40
+ redirect_to new_second_factor_authentication_path
41
+ else
42
+ start_new_session_for user
43
+ redirect_to after_authentication_url
44
+ end
45
+ else
46
+ redirect_to new_session_path, alert: "Try another email address or password."
47
+ end
48
+ end
49
+ RUBY
50
+ else
51
+ raise Thor::Error, "Could not find app/controllers/sessions_controller.rb. Please make sure the Rails Authentication generator was executed, or pass the --with-rails-authentication option."
52
+ end
53
+ end
54
+
55
+ def inject_to_authentication_concern
56
+ inject_into_file "app/controllers/concerns/authentication.rb",
57
+ after: /def terminate_session.*?end\n/m do
58
+ <<-RUBY.strip_heredoc.indent(4)
59
+
60
+ def require_no_authentication
61
+ redirect_to root_path if find_session_by_cookie
62
+ end
63
+ RUBY
64
+ end
65
+ end
66
+
67
+ def copy_controllers_and_concerns
68
+ template "app/controllers/passkeys_controller.rb"
69
+ template "app/controllers/webauthn_sessions_controller.rb"
70
+ template "app/controllers/second_factor_authentications_controller.rb"
71
+ template "app/controllers/second_factor_webauthn_credentials_controller.rb"
72
+ end
73
+
74
+ hook_for :template_engine do |template_engine|
75
+ invoke template_engine unless options.api?
76
+ end
77
+
78
+ def copy_stimulus_controllers
79
+ if using_importmap? || using_bun? || has_package_json?
80
+ template "app/javascript/controllers/webauthn_credentials_controller.js"
81
+
82
+ if using_bun? || has_package_json?
83
+ run "bin/rails stimulus:manifest:update"
84
+ end
85
+ else
86
+ puts "You must either be running with node (package.json) or importmap-rails (config/importmap.rb) to use this gem."
87
+ end
88
+ end
89
+
90
+ def inject_js_packages
91
+ if using_importmap?
92
+ say %(Appending: pin "@github/webauthn-json/browser-ponyfill", to: "https://ga.jspm.io/npm:@github/webauthn-json@2.1.1/dist/esm/webauthn-json.browser-ponyfill.js")
93
+ append_to_file "config/importmap.rb", %(pin "@github/webauthn-json/browser-ponyfill", to: "https://ga.jspm.io/npm:@github/webauthn-json@2.1.1/dist/esm/webauthn-json.browser-ponyfill.js"\n)
94
+ elsif using_bun?
95
+ say "Adding webauthn-json to your package manager"
96
+ run "bun add @github/webauthn-json/browser-ponyfill"
97
+ elsif has_package_json?
98
+ say "Adding webauthn-json to your package manager"
99
+ run "yarn add @github/webauthn-json/browser-ponyfill"
100
+ else
101
+ puts "You must either be running with node (package.json) or importmap-rails (config/importmap.rb) to use this gem."
102
+ end
103
+ end
104
+
105
+ def inject_webauthn_dependency
106
+ unless File.read(File.expand_path("Gemfile", destination_root)).include?('gem "webauthn"')
107
+ bundle_command("add webauthn", {}, quiet: true)
108
+ end
109
+ end
110
+
111
+ def copy_initializer_file
112
+ template "config/initializers/webauthn.rb"
113
+ end
114
+
115
+ def inject_webauthn_content
116
+ generate "migration", "AddWebauthnToUsers", "webauthn_id:string"
117
+ inject_webauthn_content_to_user_model
118
+
119
+ inject_into_file "config/routes.rb", after: "Rails.application.routes.draw do\n" do
120
+ <<-RUBY.strip_heredoc.indent(2)
121
+ resource :webauthn_session, only: [ :create, :destroy ] do
122
+ post :get_options, on: :collection
123
+ end
124
+
125
+ resources :passkeys, only: [ :new, :create, :destroy ] do
126
+ post :create_options, on: :collection
127
+ end
128
+
129
+ resources :second_factor_webauthn_credentials, only: [ :new, :create, :destroy ] do
130
+ post :create_options, on: :collection
131
+ end
132
+
133
+ resource :second_factor_authentication, only: [ :new, :create ] do
134
+ post :get_options, on: :collection
135
+ end
136
+ RUBY
137
+ end
138
+
139
+ template "app/models/webauthn_credential.rb"
140
+ generate "migration", "CreateWebauthnCredentials", "user:references! external_id:string:uniq public_key:string nickname:string sign_count:integer{8} authentication_factor:integer{1}!"
141
+ end
142
+
143
+ hook_for :test_framework
144
+
145
+ def final_message
146
+ say ""
147
+ say "Almost done! Now edit `config/initializers/webauthn.rb` and set the `allowed_origins` and `rp_name` for your app.", :yellow
148
+ end
149
+
150
+ private
151
+
152
+ def using_bun?
153
+ File.exist?(File.join(destination_root, "bun.config.js"))
154
+ end
155
+
156
+ def using_importmap?
157
+ File.exist?(File.join(destination_root, "config/importmap.rb"))
158
+ end
159
+
160
+ def has_package_json?
161
+ File.exist?(File.join(destination_root, "package.json"))
162
+ end
163
+
164
+ def inject_webauthn_content_to_user_model
165
+ inject_into_file "app/models/user.rb", after: "normalizes :email_address, with: ->(e) { e.strip.downcase }\n" do
166
+ <<-RUBY.strip_heredoc.indent(2)
167
+
168
+ has_many :webauthn_credentials, dependent: :destroy
169
+ with_options class_name: "WebauthnCredential" do
170
+ has_many :second_factor_webauthn_credentials, -> { second_factor }
171
+ has_many :passkeys, -> { passkey }
172
+ end
173
+
174
+ after_initialize do
175
+ self.webauthn_id ||= WebAuthn.generate_user_id
176
+ end
177
+
178
+ def second_factor_enabled?
179
+ webauthn_credentials.any?
180
+ end
181
+ RUBY
182
+ end
183
+ end
184
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :webauthn_rails do
3
+ # # Task goes here
4
+ # end
@@ -1,7 +1,5 @@
1
- # frozen_string_literal: true
2
-
3
1
  module Webauthn
4
2
  module Rails
5
- VERSION = "0.0.1"
3
+ VERSION = "0.1.1"
6
4
  end
7
5
  end
@@ -1,10 +1,6 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "rails/version"
1
+ require "webauthn/rails/version"
4
2
 
5
3
  module Webauthn
6
4
  module Rails
7
- class Error < StandardError; end
8
- # Your code goes here...
9
5
  end
10
6
  end
metadata CHANGED
@@ -1,35 +1,72 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webauthn-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
- - Santiago Rodriguez
8
- autorequire:
9
- bindir: exe
7
+ - Cedarcode
8
+ autorequire:
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-02 00:00:00.000000000 Z
12
- dependencies: []
13
- description: Authentication for Rails using WebAuthn
11
+ date: 2025-10-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: railties
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '8'
27
+ description: |-
28
+ Provides a set of generators that will extend your Rails 8+ application's authentication
29
+ to enable Passkey usage as first or second factor.
14
30
  email:
15
- - santiago.rodriguez@cedarcode.com
31
+ - webauthn@cedarcode.com
16
32
  executables: []
17
33
  extensions: []
18
34
  extra_rdoc_files: []
19
35
  files:
20
- - ".rspec"
21
- - CHANGELOG.md
22
- - LICENSE.txt
36
+ - MIT-LICENSE
23
37
  - README.md
24
38
  - Rakefile
39
+ - lib/generators/erb/webauthn_authentication/templates/app/views/passkeys/new.html.erb.tt
40
+ - lib/generators/erb/webauthn_authentication/templates/app/views/second_factor_authentications/new.html.erb.tt
41
+ - lib/generators/erb/webauthn_authentication/templates/app/views/second_factor_webauthn_credentials/new.html.erb.tt
42
+ - lib/generators/erb/webauthn_authentication/webauthn_authentication_generator.rb
43
+ - lib/generators/test_unit/webauthn_authentication/templates/test/controllers/passkeys_controller_test.rb
44
+ - lib/generators/test_unit/webauthn_authentication/templates/test/controllers/second_factor_authentications_controller_test.rb
45
+ - lib/generators/test_unit/webauthn_authentication/templates/test/controllers/second_factor_webauthn_credentials_controller_test.rb
46
+ - lib/generators/test_unit/webauthn_authentication/templates/test/controllers/webauthn_sessions_controller_test.rb
47
+ - lib/generators/test_unit/webauthn_authentication/templates/test/system/manage_webauthn_credentials_test.rb
48
+ - lib/generators/test_unit/webauthn_authentication/templates/test/test_helpers/virtual_authenticator_test_helper.rb
49
+ - lib/generators/test_unit/webauthn_authentication/webauthn_authentication_generator.rb
50
+ - lib/generators/webauthn_authentication/bundle_helper.rb
51
+ - lib/generators/webauthn_authentication/templates/app/controllers/passkeys_controller.rb
52
+ - lib/generators/webauthn_authentication/templates/app/controllers/second_factor_authentications_controller.rb
53
+ - lib/generators/webauthn_authentication/templates/app/controllers/second_factor_webauthn_credentials_controller.rb
54
+ - lib/generators/webauthn_authentication/templates/app/controllers/webauthn_sessions_controller.rb
55
+ - lib/generators/webauthn_authentication/templates/app/javascript/controllers/webauthn_credentials_controller.js
56
+ - lib/generators/webauthn_authentication/templates/app/models/webauthn_credential.rb
57
+ - lib/generators/webauthn_authentication/templates/config/initializers/webauthn.rb
58
+ - lib/generators/webauthn_authentication/webauthn_authentication_generator.rb
59
+ - lib/tasks/webauthn/rails_tasks.rake
25
60
  - lib/webauthn/rails.rb
26
61
  - lib/webauthn/rails/version.rb
27
- - sig/webauthn/rails.rbs
28
62
  homepage: https://github.com/cedarcode/webauthn-rails
29
63
  licenses:
30
64
  - MIT
31
- metadata: {}
32
- post_install_message:
65
+ metadata:
66
+ homepage_uri: https://github.com/cedarcode/webauthn-rails
67
+ source_code_uri: https://github.com/cedarcode/webauthn-rails
68
+ changelog_uri: https://github.com/cedarcode/webauthn-rails/blob/main/CHANGELOG.md
69
+ post_install_message:
33
70
  rdoc_options: []
34
71
  require_paths:
35
72
  - lib
@@ -37,15 +74,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
37
74
  requirements:
38
75
  - - ">="
39
76
  - !ruby/object:Gem::Version
40
- version: '0'
77
+ version: '3.2'
41
78
  required_rubygems_version: !ruby/object:Gem::Requirement
42
79
  requirements:
43
80
  - - ">="
44
81
  - !ruby/object:Gem::Version
45
82
  version: '0'
46
83
  requirements: []
47
- rubygems_version: 3.5.11
48
- signing_key:
84
+ rubygems_version: 3.0.3.1
85
+ signing_key:
49
86
  specification_version: 4
50
- summary: Authentication for Rails using WebAuthn
87
+ summary: Authentication for Rails using Passkeys
51
88
  test_files: []
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
data/CHANGELOG.md DELETED
@@ -1,5 +0,0 @@
1
- ## [Unreleased]
2
-
3
- ## [0.1.0] - 2024-08-02
4
-
5
- - Initial release
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2024 Santiago Rodriguez
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
@@ -1,6 +0,0 @@
1
- module Webauthn
2
- module Rails
3
- VERSION: String
4
- # See the writing guide of rbs: https://github.com/ruby/rbs#guides
5
- end
6
- end