Note: You are viewing the README for the development version of webauthn-ruby.
For the current release version see https://github.com/cedarcode/webauthn-ruby/blob/2-stable/README.md.
webauthn-ruby


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.
Table of Contents
Security
Please report security vulnerabilities to security@cedarcode.com.
More: SECURITY
Background
What is WebAuthn?
WebAuthn (Web Authentication) is a W3C standard for secure public-key authentication on the Web supported by all leading browsers and platforms.
Good Intros
In Depth
Prerequisites
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:
- Google Chrome for Android 70+ and Android's Fingerprint-based platform authenticator
- Microsoft Edge and Windows 10 platform authenticator
- Mozilla Firefox for Desktop and Yubico's Security Key roaming authenticator via USB
- Safari in iOS 13.3+ and YubiKey 5 NFC via NFC
For a complete list:
Install
Add this line to your application's Gemfile:
gem 'webauthn'
And then execute:
$ bundle
Or install it yourself as:
$ gem install webauthn
Usage
You can find a working example on how to use this gem in a pasword-less 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
.
Configuration
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
.
WebAuthn.configure do |config|
config.allowed_origins = ["https://auth.example.com"]
config.rp_name = "Example Inc."
end
Credential Registration
The ceremony where a user, a Relying Party, and the user’s client (containing at least one authenticator) work in concert to create a public key credential and associate it with the user’s Relying Party account. Note that this includes employing a test of user presence or user verification.
[source]
Initiation phase
if !user.webauthn_id
user.update!(webauthn_id: WebAuthn.generate_user_id)
end
options = WebAuthn::Credential.options_for_create(
user: { id: user.webauthn_id, name: user.name },
exclude: user.credentials.map { |c| c.webauthn_id }
)
session[:creation_challenge] = options.challenge
Verification phase
webauthn_credential = WebAuthn::Credential.from_create(params[:publicKeyCredential])
begin
webauthn_credential.verify(session[:creation_challenge])
user.credentials.create!(
webauthn_id: webauthn_credential.id,
public_key: webauthn_credential.public_key,
sign_count: webauthn_credential.sign_count
)
rescue WebAuthn::Error => e
end
Credential Authentication
The 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]
Initiation phase
options = WebAuthn::Credential.options_for_get(allow: user.credentials.map { |c| c.webauthn_id })
session[:authentication_challenge] = options.challenge
Verification phase
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.
webauthn_credential = WebAuthn::Credential.from_get(params[:publicKeyCredential])
stored_credential = user.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
)
stored_credential.update!(sign_count: webauthn_credential.sign_count)
rescue WebAuthn::SignCountVerificationError => e
rescue WebAuthn::Error => e
end
Extensions
The mechanism for generating public key credentials, as well as requesting and generating Authentication assertions, as defined in Web Authentication API, can be extended to suit particular use cases. Each case is addressed by defining a registration extension and/or an authentication extension.
When creating a public key credential or requesting an authentication assertion, a WebAuthn Relying Party can request the use of a set of extensions. These extensions will be invoked during the requested ceremony if they are supported by the WebAuthn Client and/or the WebAuthn Authenticator. The Relying Party sends the client extension input for each extension in the get() call (for authentication extensions) or create() call (for registration extensions) to the WebAuthn client. [source]
Extensions can be requested in the initiation phase in both Credential Registration and Authentication ceremonies by adding the extension parameter when generating the options for create/get:
creation_options = WebAuthn::Credential.options_for_create(
user: { id: user.webauthn_id, name: user.name },
exclude: user.credentials.map { |c| c.webauthn_id },
extensions: { appidExclude: domain.to_s }
)
options = WebAuthn::Credential.options_for_get(
allow: user.credentials.map { |c| c.webauthn_id },
extensions: { appid: domain.to_s }
)
Consequently, after these options
are sent to the WebAuthn client:
The WebAuthn client performs client extension processing for each extension that the client supports, and augments the client data as specified by each extension, by including the extension identifier and client extension output values.
For authenticator extensions, as part of the client extension processing, the client also creates the CBOR authenticator extension input value for each extension (often based on the corresponding client extension input value), and passes them to the authenticator in the create() call (for registration extensions) or the get() call (for authentication extensions).
The authenticator, in turn, performs additional processing for the extensions that it supports, and returns the CBOR authenticator extension output for each as specified by the extension. Part of the client extension processing for authenticator extensions is to use the authenticator extension output as an input to creating the client extension output. [source]
Finally, you can check the values returned for each extension by calling client_extension_outputs
and authenticator_extension_outputs
respectively.
For example, following the initialization phase for the Credential Authentication ceremony specified in the above example:
webauthn_credential = WebAuthn::Credential.from_get(credential_get_result_hash)
webauthn_credential.client_extension_outputs
webauthn_credential.authenticator_extension_outputs
A list of all currently defined extensions:
API
WebAuthn.generate_user_id
Generates a WebAuthn User Handle that follows the WebAuthn spec recommendations.
WebAuthn.generate_user_id
WebAuthn::Credential.options_for_create(options)
Helper method to build the necessary PublicKeyCredentialCreationOptions
to be used in the client-side code to call navigator.credentials.create({ "publicKey": publicKeyCredentialCreationOptions })
.
creation_options = WebAuthn::Credential.options_for_create(
user: { id: user.webauthn_id, name: user.name }
exclude: user.credentials.map { |c| c.webauthn_id }
)
session[:creation_challenge] = creation_options.challenge
WebAuthn::Credential.options_for_get([options])
Helper method to build the necessary PublicKeyCredentialRequestOptions
to be used in the client-side code to call navigator.credentials.get({ "publicKey": publicKeyCredentialRequestOptions })
.
request_options = WebAuthn::Credential.options_for_get(allow: user.credentials.map { |c| c.webauthn_id })
session[:authentication_challenge] = request_options.challenge
WebAuthn::Credential.from_create(credential_create_result)
credential_with_attestation = WebAuthn::Credential.from_create(params[:publicKeyCredential])
WebAuthn::Credential.from_get(credential_get_result)
credential_with_assertion = WebAuthn::Credential.from_get(params[:publicKeyCredential])
PublicKeyCredentialWithAttestation#verify(challenge)
Verifies the created WebAuthn credential is valid.
credential_with_attestation.verify(session[:creation_challenge])
PublicKeyCredentialWithAssertion#verify(challenge, public_key:, sign_count:)
Verifies the asserted WebAuthn credential is valid.
Mainly, that the client provided a valid cryptographic signature for the corresponding stored credential public
key, among other extra validations.
credential_with_assertion.verify(
session[:authentication_challenge],
public_key: stored_credential.public_key,
sign_count: stored_credential.sign_count
)
PublicKeyCredential#client_extension_outputs
credential = WebAuthn::Credential.from_create(params[:publicKeyCredential])
credential.client_extension_outputs
PublicKeyCredential#authenticator_extension_outputs
credential = WebAuthn::Credential.from_create(params[:publicKeyCredential])
credential.authenticator_extension_outputs
Attestation
Attestation Statement Formats
Attestation Statement Format | Supported? |
---|
packed (self attestation) | Yes |
packed (x5c attestation) | Yes |
tpm (x5c attestation) | Yes |
android-key | Yes |
android-safetynet | Yes |
apple | Yes |
fido-u2f | Yes |
none | Yes |
Attestation Types
You can define what trust policy to enforce by setting acceptable_attestation_types
config to a subset of ['None', 'Self', 'Basic', 'AttCA', 'Basic_or_AttCA']
and attestation_root_certificates_finders
to an object that responds to #find
and returns the corresponding root certificate for each registration. The #find
method will be called passing keyword arguments attestation_format
, aaguid
and attestation_certificate_key_id
.
Testing Your Integration
The Webauthn spec requires for data that is signed and authenticated. As a result, it can be difficult to create valid test authenticator data when testing your integration. webauthn-ruby exposes WebAuthn::FakeClient for you to use in your tests. Example usage can be found in webauthn-ruby/spec/webauthn/authenticator_assertion_response_spec.rb.
Contributing
See the contributing file!
Bug reports, feature suggestions, and pull requests are welcome on GitHub at https://github.com/cedarcode/webauthn-ruby.
License
The library is available as open source under the terms of the MIT License.