-
-
Notifications
You must be signed in to change notification settings - Fork 0
Fix ephemeral webauthn_id for existing users
#125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
24b34f1
eca0d34
bd370e2
9ba7eac
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class AddWebauthnIdTo<%= user_table_name.camelize %> < ActiveRecord::Migration[<%= Rails.version.to_f %>] | ||
| def up | ||
| add_column :<%= user_table_name %>, :webauthn_id, :string | ||
| add_index :<%= user_table_name %>, :webauthn_id, unique: true | ||
|
|
||
| # WARNING: The code below backfills webauthn_id for all existing records | ||
| # one row at a time. For larger tables, consider removing it and running | ||
| # the backfill separately (e.g., in a background job or maintenance task). | ||
| # | ||
| # Worth noting: PostgreSQL and MySQL support single-query backfills: | ||
| # | ||
| # PostgreSQL: | ||
| # UPDATE <%= user_table_name %> SET webauthn_id = encode(gen_random_bytes(64), 'base64') WHERE webauthn_id IS NULL | ||
| # | ||
| # MySQL: | ||
| # UPDATE <%= user_table_name %> SET webauthn_id = TO_BASE64(RANDOM_BYTES(64)) WHERE webauthn_id IS NULL | ||
| # | ||
|
Comment on lines
+8
to
+19
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @RenzoMinelli I'm hesitant over mentioning in the code the option of setting the default in the database – as the value that we are trying to set is not static, I think it will trigger a full rewrite of the database which would lock the table on most database engines 😕
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah looking into this it would be basically the same as what's presented here, needs to update each row individually. Okay sounds good |
||
| execute("SELECT id FROM <%= user_table_name %> WHERE webauthn_id IS NULL").each do |row| | ||
| webauthn_id = WebAuthn.generate_user_id | ||
| execute(ActiveRecord::Base.sanitize_sql_array( | ||
| ["UPDATE <%= user_table_name %> SET webauthn_id = ? WHERE id = ?", webauthn_id, row["id"]] | ||
| )) | ||
| end | ||
| # Note: if your application creates records using methods that skip | ||
| # callbacks (e.g., insert_all), consider adding a database default | ||
| # to ensure webauthn_id is always set. For example, in PostgreSQL: | ||
| # | ||
| # change_column_default :<%= user_table_name %>, :webauthn_id, from: nil, to: -> { "encode(gen_random_bytes(64), 'base64')" } | ||
| # | ||
| # For the same reason, you may want to add a NOT NULL constraint in a | ||
| # separate migration after the backfill is complete: | ||
| # | ||
| # change_column_null :<%= user_table_name %>, :webauthn_id, false | ||
| end | ||
|
|
||
| def down | ||
| remove_column :<%= user_table_name %>, :webauthn_id | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we have the generated migration on the internal app?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have the migrations in our internal app 😕