Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
W

webauthn-ruby

> 安全
Open source

WebAuthn ruby server library ― Make your Ruby/Rails web server become a conformant WebAuthn Relying Party

769 stars0 likes1 views
WebsiteGitHub

About

WebAuthn ruby server library ― Make your Ruby/Rails web server become a conformant WebAuthn Relying Party

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
  • Background
  • Prerequisites
  • Install
  • Usage
  • API
  • Attestation Statement Formats
  • Testing Your Integration
  • Contributing
  • License

Security

Please report security vulnerabilities to [email protected].

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. WebAuthn is the standard that underpins passkeys, the phishing-resistant replacement for passwords.

Good Intros

  • Guide to Web Authentication by Duo
  • What is WebAuthn? by Yubico

In Depth

  • WebAuthn W3C Recommendation (i.e. "The Standard")
  • Web Authentication API in MDN
  • How to use WebAuthn in native Android or macOS/iOS/iPadOS apps.
  • Security Benefits for WebAuthn Servers (a.k.a Relying Parties)

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:

  • User Agents (Clients): Can I Use: Web Authentication API
  • Authenticators: FIDO certified products (search for Type=Authenticator and Specification=FIDO2)

Install

Add this line to your application's Gemfile:

ruby
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 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.

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.

…

ruby

Generate and store the WebAuthn User Handle the first time the user registers a credential

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. } )

Store the newly generated challenge somewhere so you can have it

for the verification phase.

session[:creation_challenge] = options.challenge

Send options back to the browser, so that they can be used

to call navigator.credentials.create({ "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 useful.

The built-in PublicKeyCredential.parseCreationOptionsFromJSON(options) allows you to decode the options,

and the built-in 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
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

ruby
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.

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.

ruby
# 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 #=> nil

A list of all currently defined extensions:

  • Last published version
  • Next version (in draft)

API

WebAuthn.generate_user_handle

Generates a WebAuthn User Handle that follows the WebAuthn spec recommendations.

ruby
WebAuthn.generate_user_handle # "lWoMZTGf_ml2RoY5qPwbwrkxrvTqWjGOxEoYBgxft3zG-LlrICvE-y8bxFi06zMyIOyNsJoWx4Fa2TOqoRmnxA"

WebAuthn.generate_user_id is 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

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby2fa2fa-securityauthenticationfido2

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category安全
PricingOpen source

> Related tools

O
OWASP ZAP
开源 Web 应用安全扫描器
O
owasp-wstg-tracker
Simple web app to track OWASP WSTG security testing progress
H
homebridge-mi-gateway-security
XiaoMi Gateway Security plugin for HomeBridge.