Skip to content

Commit c668a4d

Browse files
authoredAug 6, 2024··
Merge pull request #4 from kobaltz/remove-action-auth-namespace
Remove ActionAuth Namespace - the user, session and webauthn_credential tables have had their ActionAuth namespace removed - tests updated to reflect the new tables - updated the models to use the table_name - updated README
2 parents 4bd78f6 + 12ce803 commit c668a4d

26 files changed

+108
-87
lines changed
 

‎Gemfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
action_auth (0.3.0)
4+
action_auth (1.0.0)
55
bcrypt (~> 3.1.0)
66
rails (~> 7.1)
77

‎README.md

+38-19
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,43 @@ user experience akin to that offered by the well-regarded Devise gem.
2222
7. [License](#license)
2323
8. [Credits](#credits)
2424

25+
## Breaking Changes
26+
27+
With the release of v1.0.0, there are some breaking changes that have been introduced. The
28+
biggest change is that the `ActionAuth::User` model now uses the table name of `users` instead
29+
of `action_auth_users`. This was done to make it easier to integrate with your application
30+
without having to worry about the table name. If you have an existing application that is
31+
using ActionAuth, you will need to rename the table to `users` with a migration like
32+
33+
```ruby
34+
rename_table :action_auth_users, :users
35+
```
36+
37+
Coming from `v0.3.0` to `v1.0.0`, you will need to create a migration to rename the table and foreign keys.
38+
39+
```ruby
40+
class UpgradeActionAuth < ActiveRecord::Migration[7.1]
41+
def change
42+
rename_table :action_auth_users, :users
43+
44+
rename_table :action_auth_sessions, :sessions
45+
rename_column :sessions, :action_auth_user_id, :user_id
46+
47+
rename_table :action_auth_webauthn_credentials, :webauthn_credentials
48+
rename_column :webauthn_credentials, :action_auth_user_id, :user_id
49+
end
50+
end
51+
```
52+
53+
You will then need to undo the migrations where the foreign keys were added in cases where `foreign_key: true` was
54+
changed to `foreign_key: { to_table: 'action_auth_users' }`. You can do this for each table with a migration like:
55+
56+
```ruby
57+
add_foreign_key :user_settings, :users, column: :user_id unless foreign_key_exists?(:user_settings, :users)
58+
add_foreign_key :profiles, :users, column: :user_id unless foreign_key_exists?(:profiles, :users)
59+
add_foreign_key :nfcs, :users, column: :user_id unless foreign_key_exists?(:nfcs, :users)
60+
```
61+
2562
## Installation
2663
Add this line to your application's Gemfile:
2764

@@ -242,30 +279,12 @@ end
242279

243280
#### Generating an association
244281

245-
There's one little gotcha when generating the associations. We are using `user:belongs_to` instead of
246-
`action_auth_user:belongs_to`. However, when the foreign key is generated, it will look for the users table
247-
instead of the action_auth_users table. To get around this, we'll need to modify the migration.
282+
We are using `user:belongs_to` instead of `action_auth_user:belongs_to`.
248283

249284
```bash
250285
bin/rails g scaffold posts user:belongs_to title
251286
```
252287

253-
We can update the `foreign_key` from `true` to `{ to_table: :action_auth_users }` to get around this.
254-
255-
```ruby
256-
# db/migrate/XXXXXXXXXXX_create_posts.rb
257-
class CreatePosts < ActiveRecord::Migration[7.1]
258-
def change
259-
create_table :posts do |t|
260-
t.belongs_to :user, null: false, foreign_key: { to_table: :action_auth_users }
261-
t.string :title
262-
263-
t.timestamps
264-
end
265-
end
266-
end
267-
```
268-
269288
And the post model doesn't need anything special to ActionAuth.
270289

271290
```ruby

‎app/controllers/action_auth/registrations_controller.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def create
1212
send_email_verification
1313
redirect_to sign_in_path, notice: "Welcome! You have signed up successfully. Please check your email to verify your account."
1414
else
15-
session_record = @user.action_auth_sessions.create!
15+
session_record = @user.sessions.create!
1616
cookies.signed.permanent[:session_token] = { value: session_record.id, httponly: true }
1717

1818
redirect_to sign_in_path, notice: "Welcome! You have signed up successfully"

‎app/controllers/action_auth/sessions_controller.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class SessionsController < ApplicationController
55

66
def index
77
@action_auth_wide = true
8-
@sessions = Current.user.action_auth_sessions.order(created_at: :desc)
8+
@sessions = Current.user.sessions.order(created_at: :desc)
99
end
1010

1111
def new
@@ -18,7 +18,7 @@ def create
1818
redirect_to new_webauthn_credential_authentications_path
1919
else
2020
return if check_if_email_is_verified(user)
21-
@session = user.action_auth_sessions.create
21+
@session = user.sessions.create
2222
cookies.signed.permanent[:session_token] = { value: @session.id, httponly: true }
2323
redirect_to main_app.root_path, notice: "Signed in successfully"
2424
end
@@ -28,7 +28,7 @@ def create
2828
end
2929

3030
def destroy
31-
session = Current.user.action_auth_sessions.find(params[:id])
31+
session = Current.user.sessions.find(params[:id])
3232
session.destroy
3333
redirect_to main_app.root_path, notice: "That session has been logged out"
3434
end

‎app/controllers/action_auth/webauthn_credential_authentications_controller.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ class ActionAuth::WebauthnCredentialAuthenticationsController < ApplicationContr
44
layout "action_auth/application"
55

66
def new
7-
get_options = WebAuthn::Credential.options_for_get(allow: user.action_auth_webauthn_credentials.pluck(:external_id))
7+
get_options = WebAuthn::Credential.options_for_get(allow: user.webauthn_credentials.pluck(:external_id))
88
session[:current_challenge] = get_options.challenge
99
@options = get_options
1010
end
1111

1212
def create
1313
webauthn_credential = WebAuthn::Credential.from_get(params)
1414

15-
credential = user.action_auth_webauthn_credentials.find_by(external_id: webauthn_credential.id)
15+
credential = user.webauthn_credentials.find_by(external_id: webauthn_credential.id)
1616

1717
begin
1818
webauthn_credential.verify(
@@ -23,7 +23,7 @@ def create
2323

2424
credential.update!(sign_count: webauthn_credential.sign_count)
2525
session.delete(:webauthn_user_id)
26-
session = user.action_auth_sessions.create
26+
session = user.sessions.create
2727
cookies.signed.permanent[:session_token] = { value: session.id, httponly: true }
2828
render json: { status: "ok" }, status: :ok
2929
rescue WebAuthn::Error => e

‎app/controllers/action_auth/webauthn_credentials_controller.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def options
1515
id: current_user.webauthn_id,
1616
name: current_user.email
1717
},
18-
exclude: current_user.action_auth_webauthn_credentials.pluck(:external_id)
18+
exclude: current_user.webauthn_credentials.pluck(:external_id)
1919
)
2020

2121
session[:current_challenge] = create_options.challenge
@@ -34,7 +34,7 @@ def create
3434
begin
3535
webauthn_credential.verify(session[:current_challenge])
3636

37-
credential = current_user.action_auth_webauthn_credentials.build(
37+
credential = current_user.webauthn_credentials.build(
3838
external_id: webauthn_credential.id,
3939
nickname: params[:credential_nickname],
4040
public_key: webauthn_credential.public_key,
@@ -53,7 +53,7 @@ def create
5353
end
5454

5555
def destroy
56-
current_user.action_auth_webauthn_credentials.destroy(params[:id])
56+
current_user.webauthn_credentials.destroy(params[:id])
5757

5858
redirect_to sessions_path
5959
end

‎app/models/action_auth/current.rb

+1-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ class Current < ActiveSupport::CurrentAttributes
33
attribute :session
44
attribute :user_agent, :ip_address
55

6-
delegate :action_auth_user, to: :session, allow_nil: true
7-
8-
def user
9-
action_auth_user
10-
end
6+
delegate :user, to: :session, allow_nil: true
117
end
128
end

‎app/models/action_auth/session.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module ActionAuth
22
class Session < ApplicationRecord
3-
belongs_to :action_auth_user, class_name: "ActionAuth::User", foreign_key: "action_auth_user_id"
3+
self.table_name = "sessions"
4+
5+
belongs_to :user, class_name: "ActionAuth::User", foreign_key: "user_id"
46

57
before_create do
68
self.user_agent = Current.user_agent

‎app/models/action_auth/user.rb

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
module ActionAuth
22
class User < ApplicationRecord
3+
self.table_name = "users"
4+
35
has_secure_password
46

5-
has_many :action_auth_sessions, dependent: :destroy,
6-
class_name: "ActionAuth::Session", foreign_key: "action_auth_user_id"
7+
has_many :sessions, dependent: :destroy,
8+
class_name: "ActionAuth::Session", foreign_key: "user_id"
79

810
if ActionAuth.configuration.webauthn_enabled?
9-
has_many :action_auth_webauthn_credentials, dependent: :destroy,
10-
class_name: "ActionAuth::WebauthnCredential", foreign_key: "action_auth_user_id"
11+
has_many :webauthn_credentials, dependent: :destroy,
12+
class_name: "ActionAuth::WebauthnCredential", foreign_key: "user_id"
1113
end
1214

1315
generates_token_for :email_verification, expires_in: 2.days do
@@ -28,12 +30,12 @@ class User < ApplicationRecord
2830
end
2931

3032
after_update if: :password_digest_previously_changed? do
31-
action_auth_sessions.where.not(id: Current.session).delete_all
33+
sessions.where.not(id: Current.session).delete_all
3234
end
3335

3436
def second_factor_enabled?
3537
return false unless ActionAuth.configuration.webauthn_enabled?
36-
action_auth_webauthn_credentials.any?
38+
webauthn_credentials.any?
3739
end
3840
end
3941
end

‎app/models/action_auth/webauthn_credential.rb

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module ActionAuth
22
class WebauthnCredential < ApplicationRecord
3+
self.table_name = "webauthn_credentials"
4+
35
validates :external_id, :public_key, :nickname, :sign_count, presence: true
46
validates :external_id, uniqueness: true
57
validates :sign_count,

‎app/views/action_auth/sessions/index.html.erb

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
</tr>
4242
</thead>
4343
<tbody>
44-
<% current_user.action_auth_webauthn_credentials.each do |credential| %>
44+
<% current_user.webauthn_credentials.each do |credential| %>
4545
<%= content_tag :tr, id: dom_id(credential) do %>
4646
<td><%= credential.nickname %></td>
4747
<td nowrap><%= credential.created_at.strftime('%B %d, %Y') %></td>
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
class CreateActionAuthUsers < ActiveRecord::Migration[7.1]
22
def change
3-
create_table :action_auth_users do |t|
3+
create_table :users do |t|
44
t.string :email
55
t.string :password_digest
66
t.boolean :verified
77

88
t.timestamps
99
end
10-
add_index :action_auth_users, :email, unique: true
10+
add_index :users, :email, unique: true
1111
end
1212
end

‎db/migrate/20231107170349_create_action_auth_sessions.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class CreateActionAuthSessions < ActiveRecord::Migration[7.1]
22
def change
3-
create_table :action_auth_sessions do |t|
4-
t.references :action_auth_user, null: false, foreign_key: true
3+
create_table :sessions do |t|
4+
t.references :user, null: false, foreign_key: true
55
t.string :user_agent
66
t.string :ip_address
77

‎db/migrate/20240111125859_add_webauthn_credentials.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class AddWebauthnCredentials < ActiveRecord::Migration[7.1]
22
def change
3-
create_table :action_auth_webauthn_credentials do |t|
3+
create_table :webauthn_credentials do |t|
44
t.string :external_id, null: false
55
t.string :public_key, null: false
66
t.string :nickname, null: false
77
t.bigint :sign_count, null: false, default: 0
88

99
t.index :external_id, unique: true
1010

11-
t.references :action_auth_user, foreign_key: true
11+
t.references :user, foreign_key: true
1212

1313
t.timestamps
1414
end
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class AddWebauthnIdToUsers < ActiveRecord::Migration[7.1]
22
def change
3-
add_column :action_auth_users, :webauthn_id, :string
3+
add_column :users, :webauthn_id, :string
44
end
55
end

‎lib/action_auth/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module ActionAuth
2-
VERSION = "0.3.0"
2+
VERSION = "1.0.0"
33
end

‎test/controllers/action_auth/sessions_controller_test.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class SessionsControllerTest < ActionDispatch::IntegrationTest
3434
test "should sign out" do
3535
sign_in_as(@user)
3636

37-
delete session_url(@user.action_auth_sessions.last)
37+
delete session_url(@user.sessions.last)
3838
assert_response :redirect
3939
end
4040
end

‎test/controllers/action_auth/webauthn_credentials_controller_test.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ class WebauthnCredentialsControllerTest < ActionDispatch::IntegrationTest
7979

8080
public_key_credential = fake_client.get(challenge: authentication_challenge)
8181
post(action_auth.webauthn_credential_authentications_path, params: public_key_credential)
82-
delete action_auth.webauthn_credential_path(user.action_auth_webauthn_credentials.first.id)
82+
delete action_auth.webauthn_credential_path(user.webauthn_credentials.first.id)
8383

8484
assert_redirected_to action_auth.sessions_path
85-
assert_empty user.reload.action_auth_webauthn_credentials
85+
assert_empty user.reload.webauthn_credentials
8686
end
8787

8888
private

‎test/dummy/app/controllers/posts_controller.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def show
1212

1313
# GET /posts/new
1414
def new
15-
@post = Post.new
15+
@post = Current.user.posts.new
1616
end
1717

1818
# GET /posts/1/edit
@@ -21,7 +21,7 @@ def edit
2121

2222
# POST /posts
2323
def create
24-
@post = Post.new(post_params)
24+
@post = Current.user.posts.new(post_params)
2525

2626
if @post.save
2727
redirect_to @post, notice: "Post was successfully created."
@@ -53,6 +53,6 @@ def set_post
5353

5454
# Only allow a list of trusted parameters through.
5555
def post_params
56-
params.require(:post).permit(:user_id, :title)
56+
params.require(:post).permit(:title)
5757
end
5858
end

‎test/dummy/db/migrate/20240114051355_create_posts.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class CreatePosts < ActiveRecord::Migration[7.1]
22
def change
33
create_table :posts do |t|
4-
t.belongs_to :user, null: false, foreign_key: { to_table: :action_auth_users }
4+
t.belongs_to :user, null: false, foreign_key: true
55
t.string :title
66

77
t.timestamps

0 commit comments

Comments
 (0)
Please sign in to comment.