-
-
Notifications
You must be signed in to change notification settings - Fork 40
Restoring Rodauth Defaults
The rodauth-rails gem changes some of the default Rodauth settings for easier setup:
By default, on PostgreSQL, MySQL, and Microsoft SQL Server Rodauth uses database functions to access password hashes, with the user running the application unable to get direct access to password hashes. This reduces the risk of an attacker being able to access password hashes and use them to attack other sites.
While this is useful additional security, it is also more complex to set up and to reason about, as it requires having two different database users and making sure the correct migration is run for the correct user.
To keep with Rails' "convention over configuration" doctrine, rodauth-rails disables the use of database functions, though you can always turn it back on.
use_database_authentication_functions? true
To create the database functions, pass the Sequel database object into the Rodauth method for creating database functions:
# db/migrate/*_create_rodauth_database_functions.rb
require "rodauth/migrations"
class CreateRodauthDatabaseFunctions < ActiveRecord::Migration
def up
Rodauth.create_database_authentication_functions(db)
end
def down
Rodauth.drop_database_authentication_functions(db)
end
private
def db
RodauthMain.allocate.db
end
end
The recommended Rodauth migration stores possible account status values in a separate table, and creates a foreign key on the accounts table, which ensures only a valid status value will be persisted. Unfortunately, this doesn't work when the database is restored from the schema file, in which case the account statuses table will be empty. This happens in tests by default, but it's also not unusual to do it in development.
To address this, rodauth-rails uses a status
column without a separate table.
If you're worried about invalid status values creeping in, you may use enums
instead. Alternatively, you can always go back to the setup recommended by
Rodauth.
# in the migration:
create_table :account_statuses do |t|
t.string :name, null: false, unique: true
end
execute "INSERT INTO account_statuses (id, name) VALUES (1, 'Unverified'), (2, 'Verified'), (3, 'Closed')"
create_table :accounts do |t|
# ...
t.references :status, foreign_key: { to_table: :account_statuses }, null: false, default: 1
# ...
end
class RodauthMain < Rodauth::Rails::Auth
configure do
# ...
- account_status_column :status
# ...
end
end
To simplify changes to the database schema, rodauth-rails configures Rodauth to set deadline values for various features in Ruby, instead of relying on the database to set default column values.
You can easily change this back:
set_deadline_values? false