WebAuthn ruby server library ― Make your Ruby/Rails web server become a conformant WebAuthn Relying Party
WebAuthn ruby server library ― Make your Ruby/Rails web server become a conformant WebAuthn Relying Party
WebAuthn ruby server library
Makes your Ruby/Rails web server become a functional WebAuthn Relying Party.
Takes care of the server-side operations needed to register or authenticate a user's public key credential (also called a "passkey"), including the necessary cryptographic checks.
Please report security vulnerabilities to [email protected].
More: SECURITY
WebAuthn (Web Authentication) is a W3C standard for secure public-key authentication on the Web supported by all leading browsers and platforms. WebAuthn is the standard that underpins passkeys, the phishing-resistant replacement for passwords.
This ruby library will help your Ruby/Rails server act as a conforming Relying-Party, in WebAuthn terminology. But for the Registration and Authentication ceremonies to fully work, you will also need to add two more pieces to the puzzle, a conforming User Agent + Authenticator pair.
Known conformant pairs are, for example:
For a complete list:
Add this line to your application's Gemfile:
gem 'webauthn'And then execute:
$ bundleOr install it yourself as:
$ gem install webauthnYou can find a working example on how to use this gem in a passwordless login in a Rails app in webauthn-rails-demo-app. If you want to see an example on how to use this gem as a second factor authenticator in a Rails application instead, you can check it in webauthn-2fa-rails-demo.
If you are migrating an existing application from the legacy FIDO U2F JavaScript API to WebAuthn, also refer to
docs/u2f_migration.md.
If you have a multi-tenant application or just need to configure WebAuthn differently for separate parts of your application (e.g. if your users authenticate to different subdomains in the same application), we strongly recommend you look at this Advanced Configuration section instead of this.
For a Rails application this would go in config/initializers/webauthn.rb.
…ruby
if !user.webauthn_user_handle user.update!(webauthn_user_handle: WebAuthn.generate_user_handle) end
options = WebAuthn::Credential.options_for_create( user: { id: user.webauthn_user_handle, name: user.name }, exclude: user.webauthn_credentials.map { |c| c.webauthn_id }, authenticator_selection: { resident_key: "discouraged", # For a passwordless login or 2FA. Use "required" for a passkey-based (passwordless and usernameless) login. user_verification: "required" # For a passwordless or passkey-based (passwordless and usernameless) login. Use "discouraged" for 2FA. } )
session[:creation_challenge] = options.challenge
options back to the browser, so that they can be usednavigator.credentials.create({ "publicKey": options })options.as_json to get a ruby hash with a JSON representation if needed.render json: options will just work.PublicKeyCredential.parseCreationOptionsFromJSON(options) allows you to decode the options,credential.toJSON() to send the PublicKeyCredential object back to the server.
#### Verification phase
```ruby
# Assuming you're using the built-in `credential.toJSON()` to send the `PublicKeyCredential` object back
# in params[:publicKeyCredential]:
webauthn_credential = WebAuthn::Credential.from_create(params[:publicKeyCredential])
begin
# Enforce user verification (pairs with the "required" request above). Omit for 2FA.
webauthn_credential.verify(session[:creation_challenge], user_verification: true)
# Store Credential ID, Credential Public Key and Sign Count for future authentications
user.webauthn_credentials.create!(
webauthn_id: webauthn_credential.id,
public_key: webauthn_credential.public_key,
sign_count: webauthn_credential.sign_count
)
rescue WebAuthn::Error => e
# Handle error
endThe ceremony where a user, and the user’s client (containing at least one authenticator) work in concert to cryptographically prove to a Relying Party that the user controls the credential private key associated with a previously-registered public key credential (see Registration). Note that this includes a test of user presence or user verification. [source]
options = WebAuthn::Credential.options_for_get(
# Pass `allow` when the user is already known (passwordless/2FA/reauth).
# For a passkey-based (usernameless and passwordless) login, omit it and resolve the user from `user_handle` after `from_get`.
allow: user.webauthn_credentials.map { |c| c.webauthn_id },
user_verification: "required" # For a passwordless or passkey-based (passwordless and usernameless) login. Use "discouraged" for 2FA.
)
# Store the newly generated challenge somewhere so you can have it
# for the verification phase.
session[:authentication_challenge] = options.challenge
# Send `options` back to the browser, so that they can be used
# to call `navigator.credentials.get({ "publicKey": options })`
# You can call `options.as_json` to get a ruby hash with a JSON representation if needed.
# If inside a Rails controller, `render json: options` will just work.
# I.e. it will encode and convert the options to JSON automatically.
# For your frontend code, you might find the [built-in browser methods](https://developer.mozilla.org/en-US/docs/Web/API/PublicKeyCredential) useful.
# The built-in `PublicKeyCredential.parseRequestOptionsFromJSON(options)` allows you to decode the options,
# and the built-in `credential.toJSON()` to send the `PublicKeyCredential` object back to the server.You need to look up the stored credential for a user by matching the id attribute from the PublicKeyCredential
interface returned by the browser to the stored credential_id. The corresponding public_key and sign_count
attributes must be passed as keyword arguments to the verify method call.
# Assuming you're using the built-in `credential.toJSON()` to send the `PublicKeyCredential` object back
# in params[:publicKeyCredential]:
webauthn_credential = WebAuthn::Credential.from_get(params[:publicKeyCredential])
stored_credential = user.webauthn_credentials.find_by(webauthn_id: webauthn_credential.id)
begin
webauthn_credential.verify(
session[:authentication_challenge],
public_key: stored_credential.public_key,
sign_count: stored_credential.sign_count,
user_verification: true # For a passwordless or passkey-based (passwordless and usernameless) login. Omit for 2FA.
)
# Update the stored credential sign count with the value from `webauthn_credential.sign_count`
stored_credential.update!(sign_count: webauthn_credential.sign_count)
# Continue with successful sign in or 2FA verification...
rescue WebAuthn::SignCountVerificationError => e
# Cryptographic verification of the authenticator data succeeded, but the signature counter was less then or equal
# to the stored value. This can have several reasons and depending on your risk tolerance you can choose to fail or
# pass authentication. For more information see https://www.w3.org/TR/webauthn/#sign-counter
rescue WebAuthn::Error => e
# Handle error
end
…
ruby
# Credential Registration
creation_options = WebAuthn::Credential.options_for_create(
user: { id: user.webauthn_user_handle, name: user.name },
exclude: user.webauthn_credentials.map { |c| c.webauthn_id },
extensions: { appidExclude: domain.to_s },
authenticator_selection: {
resident_key: "discouraged", # For a passwordless login or 2FA. Use "required" for a passkey-based (passwordless and usernameless) login.
user_verification: "required" # For a passwordless or passkey-based (passwordless and usernameless) login. Use "discouraged" for 2FA.
}
)
# OR
# Credential Authentication
options = WebAuthn::Credential.options_for_get(
allow: user.webauthn_credentials.map { |c| c.webauthn_id },
extensions: { appid: domain.to_s }
)
…
ruby
webauthn_credential = WebAuthn::Credential.from_get(credential_get_result_hash)
webauthn_credential.client_extension_outputs #=> { "appid" => true }
webauthn_credential.authenticator_extension_outputs #=> nilA list of all currently defined extensions:
WebAuthn.generate_user_handleGenerates a WebAuthn User Handle that follows the WebAuthn spec recommendations.
WebAuthn.generate_user_handle # "lWoMZTGf_ml2RoY5qPwbwrkxrvTqWjGOxEoYBgxft3zG-LlrICvE-y8bxFi06zMyIOyNsJoWx4Fa2TOqoRmnxA"
WebAuthn.generate_user_idis also available as an alias.
WebAuthn::Credential.options_for_create(options)Helper method to build the necessary PublicKeyCredentialCreationOptions to be used in the client-side code to call `navigator.creden
No open issues yet, or sync has not completed.