passkey_auth 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 +7 -0
- data/CHANGELOG.md +30 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +334 -0
- data/Rakefile +12 -0
- data/app/controllers/passkey_auth/application_controller.rb +13 -0
- data/app/controllers/passkey_auth/magic_links_controller.rb +120 -0
- data/app/controllers/passkey_auth/webauthn/authentications/challenges_controller.rb +40 -0
- data/app/controllers/passkey_auth/webauthn/authentications_controller.rb +62 -0
- data/app/controllers/passkey_auth/webauthn/credentials/challenges_controller.rb +53 -0
- data/app/controllers/passkey_auth/webauthn/credentials_controller.rb +58 -0
- data/app/javascript/passkey_auth/controllers/index.js +4 -0
- data/app/javascript/passkey_auth/controllers/passkey_authentication_controller.js +156 -0
- data/app/javascript/passkey_auth/controllers/webauthn_auth_controller.js +40 -0
- data/app/javascript/passkey_auth/controllers/webauthn_register_controller.js +70 -0
- data/app/javascript/passkey_auth/index.js +3 -0
- data/app/javascript/passkey_auth/lib/passkey.js +103 -0
- data/app/mailers/passkey_auth/application_mailer.rb +8 -0
- data/app/mailers/passkey_auth/magic_link_mailer.rb +17 -0
- data/app/models/passkey_auth/magic_link/short_code.rb +29 -0
- data/app/models/passkey_auth/magic_link.rb +92 -0
- data/app/models/passkey_auth/webauthn_credential.rb +14 -0
- data/app/views/passkey_auth/magic_link_mailer/login_link.html.erb +45 -0
- data/app/views/passkey_auth/magic_links/new.html.erb +19 -0
- data/app/views/passkey_auth/magic_links/verify_code.html.erb +21 -0
- data/config/locales/en.yml +53 -0
- data/config/routes.rb +28 -0
- data/lib/generators/passkey_auth/install/install_generator.rb +98 -0
- data/lib/generators/passkey_auth/install/templates/add_passwordless_to_users.rb.erb +8 -0
- data/lib/generators/passkey_auth/install/templates/create_passkey_auth_magic_links.rb.erb +21 -0
- data/lib/generators/passkey_auth/install/templates/create_passkey_auth_webauthn_credentials.rb.erb +16 -0
- data/lib/generators/passkey_auth/install/templates/initializer.rb +26 -0
- data/lib/passkey_auth/concerns/passwordless.rb +102 -0
- data/lib/passkey_auth/engine.rb +32 -0
- data/lib/passkey_auth/version.rb +5 -0
- data/lib/passkey_auth.rb +34 -0
- data/sig/passkey_auth.rbs +4 -0
- metadata +127 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: a60423909de7215a1cc93d2c5d3d9720f33dbe36a845de57df87fb99035eef08
|
|
4
|
+
data.tar.gz: '0985d40feba5052547a0a1855513aab775fb8c8770186409ea328cf32cd8d007'
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 06af92a77355abde48623fad8fd6044f7b031ab67d576cb7075de84a384dd0ecec2842f0714e56ad24b07ea3bc472b244c948d7d67e5159056d1c5ef7d117d3f
|
|
7
|
+
data.tar.gz: 2f1070b3717de924749f3660f4b6c04051f4e38cc984acd35568825670e3720900c4cce145befa988eb9c9b24d641c8cf947e7b2573591f53ce6df3796b0159e
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
## [Unreleased]
|
|
2
|
+
|
|
3
|
+
## [0.1.0] - 2025-11-30
|
|
4
|
+
|
|
5
|
+
### Initial Release
|
|
6
|
+
|
|
7
|
+
#### Features
|
|
8
|
+
- **Passwordless Authentication**: Magic links with email verification codes and WebAuthn passkeys
|
|
9
|
+
- **Session Management**: Secure session tracking with user agent and IP address
|
|
10
|
+
- **Rate Limiting**: Built-in rate limiting for magic link requests (configurable)
|
|
11
|
+
- **WebAuthn/Passkey Support**: Full passkey authentication with platform authenticators and security keys
|
|
12
|
+
- **Customizable Hooks**: Add custom behavior to authentication events (login, logout, magic link requested, passkey created)
|
|
13
|
+
- **I18n Support**: Internationalization with English translations included
|
|
14
|
+
- **JavaScript Integration**: Stimulus controllers for passkey authentication and registration
|
|
15
|
+
- **Rails Engine**: Mountable Rails engine with isolated namespace
|
|
16
|
+
- **Installation Generator**: One-command installation with migrations, initializer, and JavaScript setup
|
|
17
|
+
|
|
18
|
+
#### Components
|
|
19
|
+
- **Models**: Session, MagicLink, WebauthnCredential, and Authenticatable concern for User model
|
|
20
|
+
- **Controllers**: Sessions, MagicLinks, WebAuthn Credentials/Authentications with challenge endpoints
|
|
21
|
+
- **Views**: Minimal, customizable views for login, magic link creation, and code verification
|
|
22
|
+
- **Mailers**: Magic link email with both link and verification code
|
|
23
|
+
- **JavaScript**: Stimulus controllers for passkey authentication, registration, and auth flows
|
|
24
|
+
- **Configuration**: Flexible configuration with sensible defaults
|
|
25
|
+
|
|
26
|
+
#### Requirements
|
|
27
|
+
- Ruby >= 3.2
|
|
28
|
+
- Rails >= 7.0
|
|
29
|
+
- Hotwire (Turbo + Stimulus)
|
|
30
|
+
- @github/webauthn-json (automatically configured via importmap)
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
|
10
|
+
identity and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
|
26
|
+
community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
|
31
|
+
any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
|
35
|
+
without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official email address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
[INSERT CONTACT METHOD].
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
|
86
|
+
actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
|
93
|
+
ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
|
113
|
+
community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.1, available at
|
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by
|
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
123
|
+
|
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
|
127
|
+
|
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Jeremy Baker
|
|
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.
|
data/README.md
ADDED
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
# PasskeyAuth
|
|
2
|
+
|
|
3
|
+
A Rails engine that adds passwordless authentication methods to your Rails app using WebAuthn passkeys and magic links with email verification codes. Works seamlessly alongside Rails' built-in authentication system.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Complements Rails Authentication**: Works with `rails generate authentication` - doesn't replace it
|
|
8
|
+
- **Passwordless Options**: Give users alternatives to password authentication
|
|
9
|
+
- **WebAuthn/Passkey Support**: Modern biometric and hardware-key authentication
|
|
10
|
+
- **Magic Links**: Email-based authentication with short verification codes
|
|
11
|
+
- **Rate Limiting**: Built-in protection against abuse
|
|
12
|
+
- **Customizable Hooks**: Add your own behavior to authentication events
|
|
13
|
+
- **I18n Support**: Fully internationalized with English translations included
|
|
14
|
+
- **Rails 8+ Compatible**: Designed for modern Rails applications
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
Add this line to your application's Gemfile:
|
|
19
|
+
|
|
20
|
+
```ruby
|
|
21
|
+
gem 'passkey_auth'
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
And then execute:
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
bundle install
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Or install it yourself as:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
gem install passkey_auth
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Setup
|
|
37
|
+
|
|
38
|
+
### 1. Install Rails Authentication (if not already done)
|
|
39
|
+
|
|
40
|
+
PasskeyAuth requires Rails' authentication system. If you haven't already:
|
|
41
|
+
|
|
42
|
+
```bash
|
|
43
|
+
rails generate authentication
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
This creates the base authentication system with sessions, User model, and the Authentication concern.
|
|
47
|
+
|
|
48
|
+
**Important:** Ensure your Rails app has a `root` route defined in `config/routes.rb`. PasskeyAuth redirects users to the root URL after authentication.
|
|
49
|
+
|
|
50
|
+
### 2. Install PasskeyAuth
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
rails generate passkey_auth:install
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
This will:
|
|
57
|
+
- Create migrations for magic links and WebAuthn credentials
|
|
58
|
+
- Add passwordless fields to your users table
|
|
59
|
+
- Add an initializer at `config/initializers/passkey_auth.rb`
|
|
60
|
+
- Mount the engine routes
|
|
61
|
+
- Copy JavaScript controllers
|
|
62
|
+
|
|
63
|
+
### 3. Add to your User model
|
|
64
|
+
|
|
65
|
+
```ruby
|
|
66
|
+
class User < ApplicationRecord
|
|
67
|
+
has_secure_password # From Rails authentication
|
|
68
|
+
include PasskeyAuth::Concerns::Passwordless # Add passwordless methods
|
|
69
|
+
|
|
70
|
+
# Optional: If you prefer using :email instead of :email_address
|
|
71
|
+
alias_attribute :email, :email_address
|
|
72
|
+
end
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
**Note:** Rails 8's authentication uses `email_address` as the attribute name. PasskeyAuth defaults to this, but you can configure it in the initializer if your app uses a different attribute name.
|
|
76
|
+
|
|
77
|
+
### 4. Register Stimulus Controllers
|
|
78
|
+
|
|
79
|
+
Add the PasskeyAuth Stimulus controllers to your `app/javascript/controllers/application.js`:
|
|
80
|
+
|
|
81
|
+
```javascript
|
|
82
|
+
import { application } from "./application"
|
|
83
|
+
import { PasskeyAuthenticationController, WebauthnRegisterController, WebauthnAuthController }
|
|
84
|
+
from "passkey_auth/controllers"
|
|
85
|
+
|
|
86
|
+
// Register PasskeyAuth controllers
|
|
87
|
+
application.register("passkey-authentication", PasskeyAuthenticationController)
|
|
88
|
+
application.register("webauthn-register", WebauthnRegisterController)
|
|
89
|
+
application.register("webauthn-auth", WebauthnAuthController)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
**Note:** The installer automatically adds the required JavaScript dependencies to your importmap. If you're using a different bundler (esbuild, webpack, etc.), install the WebAuthn dependency:
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
npm install @github/webauthn-json
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### 5. Run migrations
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
rails db:migrate
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
## Usage
|
|
105
|
+
|
|
106
|
+
### How It Works
|
|
107
|
+
|
|
108
|
+
PasskeyAuth provides **alternative authentication methods** that work alongside password-based auth:
|
|
109
|
+
|
|
110
|
+
- **Password auth** (Rails): `SessionsController` handles traditional email/password login
|
|
111
|
+
- **Magic links** (PasskeyAuth): Users can request email-based login codes
|
|
112
|
+
- **Passkeys** (PasskeyAuth): Users can register biometric/hardware keys for login
|
|
113
|
+
|
|
114
|
+
All three methods use the **same session management** from Rails' Authentication concern. You simply give users more options for how they authenticate.
|
|
115
|
+
|
|
116
|
+
### Session Management
|
|
117
|
+
|
|
118
|
+
PasskeyAuth uses Rails' session management methods from the Authentication concern:
|
|
119
|
+
|
|
120
|
+
- `current_user` - Access the authenticated user
|
|
121
|
+
- `start_new_session_for(user)` - Log a user in
|
|
122
|
+
- `terminate_session` - Log the user out
|
|
123
|
+
- `allow_unauthenticated_access` - Allow public access to actions
|
|
124
|
+
|
|
125
|
+
These are provided by Rails' `Authentication` concern in your `ApplicationController`.
|
|
126
|
+
|
|
127
|
+
### Magic Links
|
|
128
|
+
|
|
129
|
+
Offer magic link authentication as an alternative to passwords:
|
|
130
|
+
|
|
131
|
+
```erb
|
|
132
|
+
<!-- In your login page -->
|
|
133
|
+
<%= link_to "Sign in with email code", passkey_auth.new_magic_link_path %>
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Users can then:
|
|
137
|
+
1. Enter their email address
|
|
138
|
+
2. Receive an email with a magic link and short code
|
|
139
|
+
3. Click the link or enter the code to authenticate
|
|
140
|
+
|
|
141
|
+
The magic link flow:
|
|
142
|
+
- `GET /auth/magic_links/new` - Request form
|
|
143
|
+
- `POST /auth/magic_links` - Send magic link email
|
|
144
|
+
- `GET /auth/magic_links/verify_code` - Code entry form
|
|
145
|
+
- `POST /auth/magic_links/verify_with_code` - Verify code
|
|
146
|
+
- `GET /auth/magic_links/:token` - Verify token from email link
|
|
147
|
+
|
|
148
|
+
### Passkeys (WebAuthn)
|
|
149
|
+
|
|
150
|
+
Configure the WebAuthn origin in the initializer:
|
|
151
|
+
|
|
152
|
+
```ruby
|
|
153
|
+
PasskeyAuth.setup do |config|
|
|
154
|
+
config.webauthn_origin = ENV.fetch("WEBAUTHN_ORIGIN", "http://localhost:3000")
|
|
155
|
+
config.webauthn_rp_name = "Your App Name"
|
|
156
|
+
end
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
#### Adding Passkey Sign-In Button
|
|
160
|
+
|
|
161
|
+
Add the passkey authentication UI to your sign-in page:
|
|
162
|
+
|
|
163
|
+
```erb
|
|
164
|
+
<div data-controller="passkey-authentication"
|
|
165
|
+
data-passkey-authentication-challenge-path-value="<%= passkey_auth.webauthn_credentials_challenge_path(format: :json) %>"
|
|
166
|
+
data-passkey-authentication-authentication-path-value="<%= passkey_auth.webauthn_authentications_path(format: :json) %>">
|
|
167
|
+
|
|
168
|
+
<button type="button"
|
|
169
|
+
data-passkey-authentication-target="signinButton"
|
|
170
|
+
data-action="click->passkey-authentication#signIn">
|
|
171
|
+
Sign in with Passkey
|
|
172
|
+
</button>
|
|
173
|
+
</div>
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
#### Registering a New Passkey
|
|
177
|
+
|
|
178
|
+
Allow users to register new passkeys in their profile:
|
|
179
|
+
|
|
180
|
+
```erb
|
|
181
|
+
<%= form_with url: passkey_auth.webauthn_credentials_path,
|
|
182
|
+
data: {
|
|
183
|
+
controller: "webauthn-register",
|
|
184
|
+
webauthn_register_callback_url_value: passkey_auth.webauthn_credentials_path,
|
|
185
|
+
action: "submit->webauthn-register#submit"
|
|
186
|
+
} do |form| %>
|
|
187
|
+
<%= form.text_field :nickname, placeholder: "Name this passkey", data: { webauthn_register_target: "nickname" } %>
|
|
188
|
+
<%= form.submit "Add Passkey" %>
|
|
189
|
+
<% end %>
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## Configuration
|
|
193
|
+
|
|
194
|
+
Edit `config/initializers/passkey_auth.rb`:
|
|
195
|
+
|
|
196
|
+
```ruby
|
|
197
|
+
PasskeyAuth.setup do |config|
|
|
198
|
+
# User model (default: "User")
|
|
199
|
+
config.user_model_name = "User"
|
|
200
|
+
|
|
201
|
+
# Email attribute on User model (default: :email_address)
|
|
202
|
+
# Rails 8 uses :email_address, but you can change this if your app uses :email
|
|
203
|
+
# Or use alias_attribute in your User model instead
|
|
204
|
+
config.user_email_attribute = :email_address
|
|
205
|
+
|
|
206
|
+
# Magic link expiration (default: 10.minutes)
|
|
207
|
+
config.magic_link_expiration = 10.minutes
|
|
208
|
+
|
|
209
|
+
# Rate limit for magic links (default: 1.minute)
|
|
210
|
+
config.magic_link_rate_limit = 1.minute
|
|
211
|
+
|
|
212
|
+
# WebAuthn settings
|
|
213
|
+
config.webauthn_origin = ENV.fetch("WEBAUTHN_ORIGIN")
|
|
214
|
+
config.webauthn_rp_name = "Your App"
|
|
215
|
+
end
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### Email Attribute Configuration
|
|
219
|
+
|
|
220
|
+
Rails 8's authentication uses `email_address` as the default attribute name. PasskeyAuth respects this default, but you have two options if you prefer using `email`:
|
|
221
|
+
|
|
222
|
+
**Option 1: Use alias_attribute in your User model (recommended)**
|
|
223
|
+
```ruby
|
|
224
|
+
class User < ApplicationRecord
|
|
225
|
+
has_secure_password
|
|
226
|
+
include PasskeyAuth::Concerns::Passwordless
|
|
227
|
+
|
|
228
|
+
alias_attribute :email, :email_address
|
|
229
|
+
end
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
**Option 2: Configure PasskeyAuth to use :email**
|
|
233
|
+
```ruby
|
|
234
|
+
PasskeyAuth.setup do |config|
|
|
235
|
+
config.user_email_attribute = :email
|
|
236
|
+
end
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### Hooks
|
|
240
|
+
|
|
241
|
+
Add custom behavior to authentication events:
|
|
242
|
+
|
|
243
|
+
```ruby
|
|
244
|
+
PasskeyAuth.setup do |config|
|
|
245
|
+
config.on_magic_link_requested = ->(user, magic_link) {
|
|
246
|
+
# Track magic link requests
|
|
247
|
+
Analytics.track(user_id: user.id, event: 'magic_link_requested')
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
config.on_passkey_created = ->(user, credential) {
|
|
251
|
+
# Track passkey creation
|
|
252
|
+
Analytics.track(user_id: user.id, event: 'passkey_created')
|
|
253
|
+
}
|
|
254
|
+
end
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
## Customization
|
|
258
|
+
|
|
259
|
+
### Views
|
|
260
|
+
|
|
261
|
+
Override any view by creating a file at the corresponding path in your app:
|
|
262
|
+
|
|
263
|
+
```
|
|
264
|
+
app/views/passkey_auth/magic_links/new.html.erb
|
|
265
|
+
app/views/passkey_auth/magic_links/verify_code.html.erb
|
|
266
|
+
app/views/passkey_auth/webauthn/credentials/index.html.erb
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
### Mailers
|
|
270
|
+
|
|
271
|
+
Override the magic link email template:
|
|
272
|
+
|
|
273
|
+
```
|
|
274
|
+
app/views/passkey_auth/magic_link_mailer/login_link.html.erb
|
|
275
|
+
```
|
|
276
|
+
|
|
277
|
+
### Routes
|
|
278
|
+
|
|
279
|
+
The engine is mounted at `/auth` by default. Change this in `config/routes.rb`:
|
|
280
|
+
|
|
281
|
+
```ruby
|
|
282
|
+
mount PasskeyAuth::Engine => "/authentication"
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
## Security Considerations
|
|
286
|
+
|
|
287
|
+
- Magic links expire after 10 minutes by default
|
|
288
|
+
- Magic links can only be used once
|
|
289
|
+
- Rate limiting prevents abuse (1 request per minute by default)
|
|
290
|
+
- Sessions are managed by Rails' Authentication concern
|
|
291
|
+
- WebAuthn credentials use public key cryptography
|
|
292
|
+
- All sensitive operations use database transactions
|
|
293
|
+
|
|
294
|
+
## Architecture
|
|
295
|
+
|
|
296
|
+
PasskeyAuth is designed to complement, not replace, Rails authentication:
|
|
297
|
+
|
|
298
|
+
**What Rails Provides:**
|
|
299
|
+
- `Authentication` concern for session management
|
|
300
|
+
- `SessionsController` for password-based login
|
|
301
|
+
- `current_user`, `start_new_session_for`, etc.
|
|
302
|
+
- User model with `has_secure_password`
|
|
303
|
+
|
|
304
|
+
**What PasskeyAuth Adds:**
|
|
305
|
+
- `Passwordless` concern with magic link and passkey methods
|
|
306
|
+
- Controllers for magic link and WebAuthn flows
|
|
307
|
+
- Models for storing magic links and WebAuthn credentials
|
|
308
|
+
- JavaScript for browser WebAuthn integration
|
|
309
|
+
|
|
310
|
+
Users can authenticate with **any of these methods**, and all use the same session system.
|
|
311
|
+
|
|
312
|
+
## Requirements
|
|
313
|
+
|
|
314
|
+
- Ruby >= 3.2
|
|
315
|
+
- Rails >= 8.0 (with `rails generate authentication` installed)
|
|
316
|
+
- PostgreSQL, MySQL, or SQLite
|
|
317
|
+
|
|
318
|
+
## Development
|
|
319
|
+
|
|
320
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
321
|
+
|
|
322
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
323
|
+
|
|
324
|
+
## Contributing
|
|
325
|
+
|
|
326
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/passkey_auth. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/passkey_auth/blob/main/CODE_OF_CONDUCT.md).
|
|
327
|
+
|
|
328
|
+
## License
|
|
329
|
+
|
|
330
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
331
|
+
|
|
332
|
+
## Code of Conduct
|
|
333
|
+
|
|
334
|
+
Everyone interacting in the PasskeyAuth project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/passkey_auth/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PasskeyAuth
|
|
4
|
+
class ApplicationController < ::ApplicationController
|
|
5
|
+
private
|
|
6
|
+
|
|
7
|
+
# Override after_authentication_url to work in engine context
|
|
8
|
+
# The main app's Authentication concern calls root_url which doesn't exist here
|
|
9
|
+
def after_authentication_url
|
|
10
|
+
session.delete(:return_to_after_authenticating) || main_app.root_url
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module PasskeyAuth
|
|
4
|
+
class MagicLinksController < PasskeyAuth::ApplicationController
|
|
5
|
+
# Expects Rails Authentication concern to provide:
|
|
6
|
+
# - allow_unauthenticated_access
|
|
7
|
+
# - start_new_session_for(user)
|
|
8
|
+
# - after_authentication_url
|
|
9
|
+
# - new_session_path (for error redirects)
|
|
10
|
+
|
|
11
|
+
allow_unauthenticated_access
|
|
12
|
+
|
|
13
|
+
def new
|
|
14
|
+
@email = params[:email]
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def create
|
|
18
|
+
email = params[:email]&.downcase&.strip
|
|
19
|
+
user = PasskeyAuth.user_class.find_by(email: email)
|
|
20
|
+
|
|
21
|
+
if user
|
|
22
|
+
result = user.send_magic_link!
|
|
23
|
+
else
|
|
24
|
+
result = { success: false, error: "user_not_found" }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
if result[:success]
|
|
28
|
+
session[:passkey_auth_magic_link_email] = email
|
|
29
|
+
redirect_to verify_code_magic_links_path
|
|
30
|
+
else
|
|
31
|
+
handle_error(result[:error])
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def show
|
|
36
|
+
link = MagicLink.consume_by_token(params[:token], ip: request.ip, user_agent: request.user_agent)
|
|
37
|
+
|
|
38
|
+
if link
|
|
39
|
+
user = link.user
|
|
40
|
+
|
|
41
|
+
# Clear any previous session values
|
|
42
|
+
if current_user && current_user.id != user.id
|
|
43
|
+
reset_session
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Log the user in
|
|
47
|
+
start_new_session_for(user)
|
|
48
|
+
|
|
49
|
+
# If user has no passkeys, suggest setting one up after login
|
|
50
|
+
if user.webauthn_credentials.none?
|
|
51
|
+
session[:suggest_passkey_setup] = true
|
|
52
|
+
redirect_to after_authentication_url, notice: I18n.t("passkey_auth.magic_links.success_with_passkey_prompt")
|
|
53
|
+
else
|
|
54
|
+
redirect_to after_authentication_url, notice: I18n.t("passkey_auth.magic_links.success")
|
|
55
|
+
end
|
|
56
|
+
else
|
|
57
|
+
redirect_to new_session_path, alert: error_message(link)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def verify_code
|
|
62
|
+
@email = session[:passkey_auth_magic_link_email]
|
|
63
|
+
redirect_to new_magic_link_path if @email.blank?
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def verify_with_code
|
|
67
|
+
email = params[:email]&.downcase&.strip
|
|
68
|
+
short_code = MagicLink::ShortCode.clean(params[:code])
|
|
69
|
+
|
|
70
|
+
link = MagicLink.consume_by_email_and_code(email, short_code, ip: request.ip, user_agent: request.user_agent)
|
|
71
|
+
|
|
72
|
+
if link
|
|
73
|
+
user = link.user
|
|
74
|
+
|
|
75
|
+
# Clear the session email
|
|
76
|
+
session.delete(:passkey_auth_magic_link_email)
|
|
77
|
+
|
|
78
|
+
# Log the user in
|
|
79
|
+
start_new_session_for(user)
|
|
80
|
+
|
|
81
|
+
# If user has no passkeys, suggest setting one up after login
|
|
82
|
+
if user.webauthn_credentials.none?
|
|
83
|
+
session[:suggest_passkey_setup] = true
|
|
84
|
+
redirect_to after_authentication_url, notice: I18n.t("passkey_auth.magic_links.success_with_passkey_prompt")
|
|
85
|
+
else
|
|
86
|
+
redirect_to after_authentication_url, notice: I18n.t("passkey_auth.magic_links.success")
|
|
87
|
+
end
|
|
88
|
+
else
|
|
89
|
+
flash.now[:alert] = error_message(link)
|
|
90
|
+
@email = email
|
|
91
|
+
render :verify_code, status: :unprocessable_content
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
private
|
|
96
|
+
|
|
97
|
+
def handle_error(error)
|
|
98
|
+
case error
|
|
99
|
+
when "rate_limited"
|
|
100
|
+
redirect_to new_session_path, alert: I18n.t("passkey_auth.magic_links.errors.rate_limited")
|
|
101
|
+
when "user_not_found"
|
|
102
|
+
redirect_to new_session_path, alert: I18n.t("passkey_auth.magic_links.errors.user_not_found")
|
|
103
|
+
else
|
|
104
|
+
redirect_to new_session_path, alert: I18n.t("passkey_auth.magic_links.errors.send_failed")
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def error_message(link)
|
|
109
|
+
if link.nil?
|
|
110
|
+
I18n.t("passkey_auth.magic_links.errors.invalid_code")
|
|
111
|
+
elsif link.expired?
|
|
112
|
+
I18n.t("passkey_auth.magic_links.errors.expired")
|
|
113
|
+
elsif link.used?
|
|
114
|
+
I18n.t("passkey_auth.magic_links.errors.already_used")
|
|
115
|
+
else
|
|
116
|
+
I18n.t("passkey_auth.magic_links.errors.generic")
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|