webauthn-slim 3.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. checksums.yaml +7 -0
  2. data/.github/FUNDING.yml +1 -0
  3. data/.github/actions/install-openssl/action.yml +35 -0
  4. data/.github/actions/install-ruby/action.yml +76 -0
  5. data/.github/dependabot.yml +6 -0
  6. data/.github/workflows/build.yml +49 -0
  7. data/.github/workflows/git.yml +21 -0
  8. data/.github/workflows/release.yml +28 -0
  9. data/.gitignore +16 -0
  10. data/.rspec +2 -0
  11. data/.rubocop.yml +306 -0
  12. data/CHANGELOG.md +462 -0
  13. data/CONTRIBUTING.md +39 -0
  14. data/Gemfile +8 -0
  15. data/LICENSE.txt +21 -0
  16. data/README.md +479 -0
  17. data/Rakefile +10 -0
  18. data/SECURITY.md +21 -0
  19. data/bin/console +15 -0
  20. data/bin/setup +8 -0
  21. data/docs/advanced_configuration.md +173 -0
  22. data/docs/u2f_migration.md +90 -0
  23. data/lib/webauthn/attestation_object.rb +47 -0
  24. data/lib/webauthn/attestation_statement/apple.rb +65 -0
  25. data/lib/webauthn/attestation_statement/base.rb +175 -0
  26. data/lib/webauthn/attestation_statement/fido_u2f/public_key.rb +44 -0
  27. data/lib/webauthn/attestation_statement/fido_u2f.rb +71 -0
  28. data/lib/webauthn/attestation_statement/none.rb +23 -0
  29. data/lib/webauthn/attestation_statement/packed.rb +72 -0
  30. data/lib/webauthn/attestation_statement.rb +35 -0
  31. data/lib/webauthn/authenticator_assertion_response.rb +87 -0
  32. data/lib/webauthn/authenticator_attestation_response.rb +84 -0
  33. data/lib/webauthn/authenticator_data/attested_credential_data.rb +94 -0
  34. data/lib/webauthn/authenticator_data.rb +130 -0
  35. data/lib/webauthn/authenticator_response.rb +137 -0
  36. data/lib/webauthn/cbor.rb +189 -0
  37. data/lib/webauthn/client_data.rb +67 -0
  38. data/lib/webauthn/configuration.rb +68 -0
  39. data/lib/webauthn/cose.rb +237 -0
  40. data/lib/webauthn/credential.rb +26 -0
  41. data/lib/webauthn/credential_creation_options.rb +97 -0
  42. data/lib/webauthn/credential_entity.rb +11 -0
  43. data/lib/webauthn/credential_options.rb +17 -0
  44. data/lib/webauthn/credential_request_options.rb +44 -0
  45. data/lib/webauthn/credential_rp_entity.rb +8 -0
  46. data/lib/webauthn/credential_user_entity.rb +16 -0
  47. data/lib/webauthn/encoder.rb +18 -0
  48. data/lib/webauthn/encoders.rb +62 -0
  49. data/lib/webauthn/error.rb +5 -0
  50. data/lib/webauthn/fake_authenticator/attestation_object.rb +90 -0
  51. data/lib/webauthn/fake_authenticator/authenticator_data.rb +156 -0
  52. data/lib/webauthn/fake_authenticator.rb +134 -0
  53. data/lib/webauthn/fake_client.rb +179 -0
  54. data/lib/webauthn/json_serializer.rb +45 -0
  55. data/lib/webauthn/public_key.rb +65 -0
  56. data/lib/webauthn/public_key_credential/creation_options.rb +93 -0
  57. data/lib/webauthn/public_key_credential/entity.rb +21 -0
  58. data/lib/webauthn/public_key_credential/options.rb +51 -0
  59. data/lib/webauthn/public_key_credential/request_options.rb +46 -0
  60. data/lib/webauthn/public_key_credential/rp_entity.rb +23 -0
  61. data/lib/webauthn/public_key_credential/user_entity.rb +24 -0
  62. data/lib/webauthn/public_key_credential.rb +94 -0
  63. data/lib/webauthn/public_key_credential_with_assertion.rb +49 -0
  64. data/lib/webauthn/public_key_credential_with_attestation.rb +30 -0
  65. data/lib/webauthn/relying_party.rb +150 -0
  66. data/lib/webauthn/u2f_migrator.rb +76 -0
  67. data/lib/webauthn/version.rb +5 -0
  68. data/lib/webauthn.rb +19 -0
  69. data/webauthn.gemspec +43 -0
  70. metadata +218 -0
data/README.md ADDED
@@ -0,0 +1,479 @@
1
+ # webauthn-ruby
2
+
3
+ ![banner](assets/webauthn-ruby.png)
4
+
5
+ [![Gem](https://img.shields.io/gem/v/webauthn.svg?style=flat-square)](https://rubygems.org/gems/webauthn)
6
+ [![Build](https://github.com/cedarcode/webauthn-ruby/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/cedarcode/webauthn-ruby/actions/workflows/build.yml)
7
+ [![Conventional Commits](https://img.shields.io/badge/Conventional%20Commits-1.0.0-informational.svg?style=flat-square)](https://conventionalcommits.org)
8
+ [![Join the chat at https://gitter.im/cedarcode/webauthn-ruby](https://badges.gitter.im/cedarcode/webauthn-ruby.svg)](https://gitter.im/cedarcode/webauthn-ruby?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
9
+
10
+ > WebAuthn ruby server library
11
+
12
+ Makes your Ruby/Rails web server become a functional [WebAuthn Relying Party](https://www.w3.org/TR/webauthn/#webauthn-relying-party).
13
+
14
+ Takes care of the [server-side operations](https://www.w3.org/TR/webauthn/#rp-operations) needed to
15
+ [register](https://www.w3.org/TR/webauthn/#registration) or [authenticate](https://www.w3.org/TR/webauthn/#authentication)
16
+ a user's [public key credential](https://www.w3.org/TR/webauthn/#public-key-credential) (also called a "passkey"), including the necessary cryptographic checks.
17
+
18
+ ## Table of Contents
19
+
20
+ - [Security](#security)
21
+ - [Background](#background)
22
+ - [Prerequisites](#prerequisites)
23
+ - [Install](#install)
24
+ - [Usage](#usage)
25
+ - [API](#api)
26
+ - [Attestation Statement Formats](#attestation-statement-formats)
27
+ - [Testing Your Integration](#testing-your-integration)
28
+ - [Contributing](#contributing)
29
+ - [License](#license)
30
+
31
+ ## Security
32
+
33
+ Please report security vulnerabilities to security@cedarcode.com.
34
+
35
+ _More_: [SECURITY](SECURITY.md)
36
+
37
+ ## Background
38
+
39
+ ### What is WebAuthn?
40
+
41
+ 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.
42
+
43
+ #### Good Intros
44
+
45
+ - [Guide to Web Authentication](https://webauthn.guide) by Duo
46
+ - [What is WebAuthn?](https://www.yubico.com/webauthn/) by Yubico
47
+
48
+ #### In Depth
49
+
50
+ - WebAuthn [W3C Recommendation](https://www.w3.org/TR/webauthn/) (i.e. "The Standard")
51
+ - [Web Authentication API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Authentication_API) in MDN
52
+ - How to use WebAuthn in native [Android](https://developers.google.com/identity/fido/android/native-apps) or [macOS/iOS/iPadOS](https://developer.apple.com/documentation/authenticationservices/public-private_key_authentication) apps.
53
+ - [Security Benefits for WebAuthn Servers (a.k.a Relying Parties)](https://www.w3.org/TR/webauthn/#sctn-rp-benefits)
54
+
55
+ ## Prerequisites
56
+
57
+ This ruby library will help your Ruby/Rails server act as a conforming [_Relying-Party_](https://www.w3.org/TR/webauthn/#relying-party), in WebAuthn terminology. But for the [_Registration_](https://www.w3.org/TR/webauthn/#registration) and [_Authentication_](https://www.w3.org/TR/webauthn/#authentication) ceremonies to fully work, you will also need to add two more pieces to the puzzle, a conforming [User Agent](https://www.w3.org/TR/webauthn/#conforming-user-agents) + [Authenticator](https://www.w3.org/TR/webauthn/#conforming-authenticators) pair.
58
+
59
+ Known conformant pairs are, for example:
60
+
61
+ - Google Chrome for Android 70+ and Android's Fingerprint-based platform authenticator
62
+ - Microsoft Edge and Windows 10 platform authenticator
63
+ - Mozilla Firefox for Desktop and Yubico's Security Key roaming authenticator via USB
64
+ - Safari in iOS 13.3+ and YubiKey 5 NFC via NFC
65
+
66
+ For a complete list:
67
+
68
+ - User Agents (Clients): [Can I Use: Web Authentication API](https://caniuse.com/#search=webauthn)
69
+ - Authenticators: [FIDO certified products](https://fidoalliance.org/certification/fido-certified-products) (search for Type=Authenticator and Specification=FIDO2)
70
+
71
+ ## Install
72
+
73
+ Add this line to your application's Gemfile:
74
+
75
+ ```ruby
76
+ gem 'webauthn'
77
+ ```
78
+
79
+ And then execute:
80
+
81
+ $ bundle
82
+
83
+ Or install it yourself as:
84
+
85
+ $ gem install webauthn
86
+
87
+ ## Usage
88
+
89
+ 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](https://github.com/cedarcode/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](https://github.com/cedarcode/webauthn-2fa-rails-demo).
90
+
91
+ If you are migrating an existing application from the legacy FIDO U2F JavaScript API to WebAuthn, also refer to
92
+ [`docs/u2f_migration.md`](docs/u2f_migration.md).
93
+
94
+ ### Configuration
95
+
96
+ 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](docs/advanced_configuration.md) section instead of this.
97
+
98
+ For a Rails application this would go in `config/initializers/webauthn.rb`.
99
+
100
+ ```ruby
101
+ WebAuthn.configure do |config|
102
+ # This value needs to match `window.location.origin` evaluated by
103
+ # the User Agent during registration and authentication ceremonies.
104
+ # Multiple origins can be used when needed. Using more than one will imply you MUST configure rp_id explicitely. If you need your credentials to be bound to a single origin but you have more than one tenant, please see [our Advanced Configuration section](https://github.com/cedarcode/webauthn-ruby/blob/master/docs/advanced_configuration.md) instead of adding multiple origins.
105
+ config.allowed_origins = ["https://auth.example.com"]
106
+
107
+ # When operating within iframes or embedded contexts, you may need to restrict
108
+ # which top-level origins are permitted to host WebAuthn ceremonies.
109
+ #
110
+ # crossOrigin / topOrigin verification is DISABLED by default:
111
+ # config.verify_cross_origin = false
112
+ #
113
+ # When `verify_cross_origin` is false, any `crossOrigin` / `topOrigin` values reported by the browser
114
+ # are ignored. As a result, credentials created or used within a cross-origin iframe will be treated
115
+ # as valid.
116
+ #
117
+ # When `verify_cross_origin` is true, you can either:
118
+ #
119
+ # (A) Allow only specific top-level origins to embed your ceremony
120
+ # (each entry must match the browser-reported `topOrigin` during registration/authentication):
121
+ #
122
+ # config.allowed_top_origins = ["https://app.example.com"]
123
+ #
124
+ # (B) Forbid ANY cross-origin iframe usage altogether
125
+ # (this rejects creation/authentication whenever `crossOrigin` is true):
126
+ #
127
+ # config.allowed_top_origins = []
128
+ #
129
+ # Note: if `verify_cross_origin` is not enabled, any values set in `allowed_top_origins`
130
+ # will be ignored.
131
+
132
+ # Relying Party name for display purposes
133
+ config.rp_name = "Example Inc."
134
+
135
+ # Optionally configure a client timeout hint, in milliseconds.
136
+ # This hint specifies how long the browser should wait for any
137
+ # interaction with the user.
138
+ # This hint may be overridden by the browser.
139
+ # https://www.w3.org/TR/webauthn/#dom-publickeycredentialcreationoptions-timeout
140
+ # config.credential_options_timeout = 120_000
141
+
142
+ # You can optionally specify a different Relying Party ID
143
+ # (https://www.w3.org/TR/webauthn/#relying-party-identifier)
144
+ # if it differs from the default one.
145
+ #
146
+ # In this case the default would be "auth.example.com", but you can set it to
147
+ # the suffix "example.com"
148
+ #
149
+ # config.rp_id = "example.com"
150
+
151
+ # Configure preferred binary-to-text encoding scheme. This should match the encoding scheme
152
+ # used in your client-side (user agent) code before sending the credential to the server.
153
+ # Supported values: `:base64url` (default), `:base64` or `false` to disable all encoding.
154
+ #
155
+ # config.encoding = :base64url
156
+
157
+ # Possible values: "ES256", "ES384", "ES512", "PS256", "PS384", "PS512", "RS256", "RS384", "RS512", "RS1"
158
+ # Default: ["ES256", "PS256", "RS256"]
159
+ #
160
+ # config.algorithms << "ES384"
161
+ end
162
+ ```
163
+
164
+ ### Credential Registration
165
+
166
+ > 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.
167
+ > [[source](https://www.w3.org/TR/webauthn-2/#registration-ceremony)]
168
+
169
+ #### Initiation phase
170
+
171
+ ```ruby
172
+ # Generate and store the WebAuthn User Handle the first time the user registers a credential
173
+ if !user.webauthn_user_handle
174
+ user.update!(webauthn_user_handle: WebAuthn.generate_user_handle)
175
+ end
176
+
177
+ options = WebAuthn::Credential.options_for_create(
178
+ user: { id: user.webauthn_user_handle, name: user.name },
179
+ exclude: user.webauthn_credentials.map { |c| c.webauthn_id },
180
+ authenticator_selection: {
181
+ resident_key: "discouraged", # For a passwordless login or 2FA. Use "required" for a passkey-based (passwordless and usernameless) login.
182
+ user_verification: "required" # For a passwordless or passkey-based (passwordless and usernameless) login. Use "discouraged" for 2FA.
183
+ }
184
+ )
185
+
186
+ # Store the newly generated challenge somewhere so you can have it
187
+ # for the verification phase.
188
+ session[:creation_challenge] = options.challenge
189
+
190
+ # Send `options` back to the browser, so that they can be used
191
+ # to call `navigator.credentials.create({ "publicKey": options })`
192
+ #
193
+ # You can call `options.as_json` to get a ruby hash with a JSON representation if needed.
194
+
195
+ # If inside a Rails controller, `render json: options` will just work.
196
+ # I.e. it will encode and convert the options to JSON automatically.
197
+
198
+ # For your frontend code, you might find the [built-in browser methods](https://developer.mozilla.org/en-US/docs/Web/API/PublicKeyCredential) useful.
199
+ # The built-in `PublicKeyCredential.parseCreationOptionsFromJSON(options)` allows you to decode the options,
200
+ # and the built-in `credential.toJSON()` to send the `PublicKeyCredential` object back to the server.
201
+ ```
202
+
203
+ #### Verification phase
204
+
205
+ ```ruby
206
+ # Assuming you're using the built-in `credential.toJSON()` to send the `PublicKeyCredential` object back
207
+ # in params[:publicKeyCredential]:
208
+ webauthn_credential = WebAuthn::Credential.from_create(params[:publicKeyCredential])
209
+
210
+ begin
211
+ # Enforce user verification (pairs with the "required" request above). Omit for 2FA.
212
+ webauthn_credential.verify(session[:creation_challenge], user_verification: true)
213
+
214
+ # Store Credential ID, Credential Public Key and Sign Count for future authentications
215
+ user.webauthn_credentials.create!(
216
+ webauthn_id: webauthn_credential.id,
217
+ public_key: webauthn_credential.public_key,
218
+ sign_count: webauthn_credential.sign_count
219
+ )
220
+ rescue WebAuthn::Error => e
221
+ # Handle error
222
+ end
223
+ ```
224
+
225
+ ### Credential Authentication
226
+
227
+ > 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](https://www.w3.org/TR/webauthn-2/#authentication-ceremony)]
228
+
229
+ #### Initiation phase
230
+
231
+ ```ruby
232
+ options = WebAuthn::Credential.options_for_get(
233
+ # Pass `allow` when the user is already known (passwordless/2FA/reauth).
234
+ # For a passkey-based (usernameless and passwordless) login, omit it and resolve the user from `user_handle` after `from_get`.
235
+ allow: user.webauthn_credentials.map { |c| c.webauthn_id },
236
+ user_verification: "required" # For a passwordless or passkey-based (passwordless and usernameless) login. Use "discouraged" for 2FA.
237
+ )
238
+
239
+ # Store the newly generated challenge somewhere so you can have it
240
+ # for the verification phase.
241
+ session[:authentication_challenge] = options.challenge
242
+
243
+ # Send `options` back to the browser, so that they can be used
244
+ # to call `navigator.credentials.get({ "publicKey": options })`
245
+
246
+ # You can call `options.as_json` to get a ruby hash with a JSON representation if needed.
247
+
248
+ # If inside a Rails controller, `render json: options` will just work.
249
+ # I.e. it will encode and convert the options to JSON automatically.
250
+
251
+ # For your frontend code, you might find the [built-in browser methods](https://developer.mozilla.org/en-US/docs/Web/API/PublicKeyCredential) useful.
252
+ # The built-in `PublicKeyCredential.parseRequestOptionsFromJSON(options)` allows you to decode the options,
253
+ # and the built-in `credential.toJSON()` to send the `PublicKeyCredential` object back to the server.
254
+ ```
255
+
256
+ #### Verification phase
257
+
258
+ You need to look up the stored credential for a user by matching the `id` attribute from the PublicKeyCredential
259
+ interface returned by the browser to the stored `credential_id`. The corresponding `public_key` and `sign_count`
260
+ attributes must be passed as keyword arguments to the `verify` method call.
261
+
262
+ ```ruby
263
+ # Assuming you're using the built-in `credential.toJSON()` to send the `PublicKeyCredential` object back
264
+ # in params[:publicKeyCredential]:
265
+ webauthn_credential = WebAuthn::Credential.from_get(params[:publicKeyCredential])
266
+
267
+ stored_credential = user.webauthn_credentials.find_by(webauthn_id: webauthn_credential.id)
268
+
269
+ begin
270
+ webauthn_credential.verify(
271
+ session[:authentication_challenge],
272
+ public_key: stored_credential.public_key,
273
+ sign_count: stored_credential.sign_count,
274
+ user_verification: true # For a passwordless or passkey-based (passwordless and usernameless) login. Omit for 2FA.
275
+ )
276
+
277
+ # Update the stored credential sign count with the value from `webauthn_credential.sign_count`
278
+ stored_credential.update!(sign_count: webauthn_credential.sign_count)
279
+
280
+ # Continue with successful sign in or 2FA verification...
281
+
282
+ rescue WebAuthn::SignCountVerificationError => e
283
+ # Cryptographic verification of the authenticator data succeeded, but the signature counter was less then or equal
284
+ # to the stored value. This can have several reasons and depending on your risk tolerance you can choose to fail or
285
+ # pass authentication. For more information see https://www.w3.org/TR/webauthn/#sign-counter
286
+ rescue WebAuthn::Error => e
287
+ # Handle error
288
+ end
289
+ ```
290
+
291
+ ### Extensions
292
+
293
+ > 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.
294
+
295
+ > 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](https://www.w3.org/TR/webauthn-2/#sctn-extensions)]
296
+
297
+ 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:
298
+
299
+ ```ruby
300
+ # Credential Registration
301
+ creation_options = WebAuthn::Credential.options_for_create(
302
+ user: { id: user.webauthn_user_handle, name: user.name },
303
+ exclude: user.webauthn_credentials.map { |c| c.webauthn_id },
304
+ extensions: { appidExclude: domain.to_s },
305
+ authenticator_selection: {
306
+ resident_key: "discouraged", # For a passwordless login or 2FA. Use "required" for a passkey-based (passwordless and usernameless) login.
307
+ user_verification: "required" # For a passwordless or passkey-based (passwordless and usernameless) login. Use "discouraged" for 2FA.
308
+ }
309
+ )
310
+
311
+ # OR
312
+
313
+ # Credential Authentication
314
+ options = WebAuthn::Credential.options_for_get(
315
+ allow: user.webauthn_credentials.map { |c| c.webauthn_id },
316
+ extensions: { appid: domain.to_s }
317
+ )
318
+ ```
319
+
320
+ Consequently, after these `options` are sent to the WebAuthn client:
321
+
322
+ > 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.
323
+
324
+ > 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).
325
+
326
+ > 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](https://www.w3.org/TR/webauthn-2/#sctn-extensions)]
327
+
328
+ Finally, you can check the values returned for each extension by calling `client_extension_outputs` and `authenticator_extension_outputs` respectively.
329
+ For example, following the initialization phase for the Credential Authentication ceremony specified in the above example:
330
+
331
+ ```ruby
332
+ webauthn_credential = WebAuthn::Credential.from_get(credential_get_result_hash)
333
+
334
+ webauthn_credential.client_extension_outputs #=> { "appid" => true }
335
+ webauthn_credential.authenticator_extension_outputs #=> nil
336
+ ```
337
+
338
+ A list of all currently defined extensions:
339
+
340
+ - [Last published version](https://www.w3.org/TR/webauthn-2/#sctn-defined-extensions)
341
+ - [Next version (in draft)](https://w3c.github.io/webauthn/#sctn-defined-extensions)
342
+
343
+ ## API
344
+
345
+ #### `WebAuthn.generate_user_handle`
346
+
347
+ Generates a [WebAuthn User Handle](https://www.w3.org/TR/webauthn-2/#user-handle) that follows the WebAuthn spec recommendations.
348
+
349
+ ```ruby
350
+ WebAuthn.generate_user_handle # "lWoMZTGf_ml2RoY5qPwbwrkxrvTqWjGOxEoYBgxft3zG-LlrICvE-y8bxFi06zMyIOyNsJoWx4Fa2TOqoRmnxA"
351
+ ```
352
+
353
+ > `WebAuthn.generate_user_id` is also available as an alias.
354
+
355
+ #### `WebAuthn::Credential.options_for_create(options)`
356
+
357
+ Helper method to build the necessary [PublicKeyCredentialCreationOptions](https://www.w3.org/TR/webauthn-2/#dictdef-publickeycredentialcreationoptions)
358
+ to be used in the client-side code to call `navigator.credentials.create({ "publicKey": publicKeyCredentialCreationOptions })`.
359
+
360
+ ```ruby
361
+ creation_options = WebAuthn::Credential.options_for_create(
362
+ user: { id: user.webauthn_user_handle, name: user.name },
363
+ exclude: user.webauthn_credentials.map { |c| c.webauthn_id }
364
+ )
365
+
366
+ # Store the newly generated challenge somewhere so you can have it
367
+ # for the verification phase.
368
+ session[:creation_challenge] = creation_options.challenge
369
+
370
+ # Send `creation_options` back to the browser, so that they can be used
371
+ # to call `navigator.credentials.create({ "publicKey": creationOptions })`
372
+ #
373
+ # You can call `creation_options.as_json` to get a ruby hash with a JSON representation if needed.
374
+
375
+ # If inside a Rails controller, `render json: creation_options` will just work.
376
+ # I.e. it will encode and convert the options to JSON automatically.
377
+ ```
378
+
379
+ #### `WebAuthn::Credential.options_for_get([options])`
380
+
381
+ Helper method to build the necessary [PublicKeyCredentialRequestOptions](https://www.w3.org/TR/webauthn-2/#dictdef-publickeycredentialrequestoptions)
382
+ to be used in the client-side code to call `navigator.credentials.get({ "publicKey": publicKeyCredentialRequestOptions })`.
383
+
384
+ ```ruby
385
+ request_options = WebAuthn::Credential.options_for_get(allow: user.webauthn_credentials.map { |c| c.webauthn_id })
386
+
387
+ # Store the newly generated challenge somewhere so you can have it
388
+ # for the verification phase.
389
+ session[:authentication_challenge] = request_options.challenge
390
+
391
+ # Send `request_options` back to the browser, so that they can be used
392
+ # to call `navigator.credentials.get({ "publicKey": requestOptions })`
393
+
394
+ # You can call `request_options.as_json` to get a ruby hash with a JSON representation if needed.
395
+
396
+ # If inside a Rails controller, `render json: request_options` will just work.
397
+ # I.e. it will encode and convert the options to JSON automatically.
398
+ ```
399
+
400
+ #### `WebAuthn::Credential.from_create(credential_create_result)`
401
+
402
+ ```ruby
403
+ credential_with_attestation = WebAuthn::Credential.from_create(params[:publicKeyCredential])
404
+ ```
405
+
406
+ #### `WebAuthn::Credential.from_get(credential_get_result)`
407
+
408
+ ```ruby
409
+ credential_with_assertion = WebAuthn::Credential.from_get(params[:publicKeyCredential])
410
+ ```
411
+
412
+ #### `PublicKeyCredentialWithAttestation#verify(challenge)`
413
+
414
+ Verifies the created WebAuthn credential is [valid](https://www.w3.org/TR/webauthn-2/#sctn-registering-a-new-credential).
415
+
416
+ ```ruby
417
+ credential_with_attestation.verify(session[:creation_challenge])
418
+ ```
419
+
420
+ #### `PublicKeyCredentialWithAssertion#verify(challenge, public_key:, sign_count:)`
421
+
422
+ Verifies the asserted WebAuthn credential is [valid](https://www.w3.org/TR/webauthn-2/#sctn-verifying-assertion).
423
+
424
+ Mainly, that the client provided a valid cryptographic signature for the corresponding stored credential public
425
+ key, among other extra validations.
426
+
427
+ ```ruby
428
+ credential_with_assertion.verify(
429
+ session[:authentication_challenge],
430
+ public_key: stored_credential.public_key,
431
+ sign_count: stored_credential.sign_count
432
+ )
433
+ ```
434
+
435
+ #### `PublicKeyCredential#client_extension_outputs`
436
+
437
+ ```ruby
438
+ credential = WebAuthn::Credential.from_create(params[:publicKeyCredential])
439
+
440
+ credential.client_extension_outputs
441
+ ```
442
+
443
+ #### `PublicKeyCredential#authenticator_extension_outputs`
444
+
445
+ ```ruby
446
+ credential = WebAuthn::Credential.from_create(params[:publicKeyCredential])
447
+
448
+ credential.authenticator_extension_outputs
449
+ ```
450
+
451
+ ## Attestation
452
+
453
+ ### Attestation Statement Formats
454
+
455
+ | Attestation Statement Format | Supported? |
456
+ | -------- | :--------: |
457
+ | packed (self attestation) | Yes |
458
+ | packed (x5c attestation) | Yes |
459
+ | apple | Yes |
460
+ | fido-u2f | Yes |
461
+ | none | Yes |
462
+
463
+ ### Attestation Types
464
+
465
+ 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`.
466
+
467
+ ## Testing Your Integration
468
+
469
+ 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](https://github.com/cedarcode/webauthn-ruby/blob/master/lib/webauthn/fake_client.rb) for you to use in your tests. Example usage can be found in [webauthn-ruby/spec/webauthn/authenticator_assertion_response_spec.rb](https://github.com/cedarcode/webauthn-ruby/blob/master/spec/webauthn/authenticator_assertion_response_spec.rb).
470
+
471
+ ## Contributing
472
+
473
+ See [the contributing file](CONTRIBUTING.md)!
474
+
475
+ Bug reports, feature suggestions, and pull requests are welcome on GitHub at https://github.com/cedarcode/webauthn-ruby.
476
+
477
+ ## License
478
+
479
+ The library is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+ require "rubocop/rake_task"
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ RuboCop::RakeTask.new
9
+
10
+ task default: [:rubocop, :spec]
data/SECURITY.md ADDED
@@ -0,0 +1,21 @@
1
+ # Security Policy
2
+
3
+ ## Supported Versions
4
+
5
+ | Version | Supported |
6
+ | ------- | ------------------ |
7
+ | 2.5.z | :white_check_mark: |
8
+ | 2.4.z | :white_check_mark: |
9
+ | 2.3.z | :white_check_mark: |
10
+ | 2.2.z | :x: |
11
+ | 2.1.z | :x: |
12
+ | 2.0.z | :x: |
13
+ | 1.18.z | :white_check_mark: |
14
+ | < 1.18 | :x: |
15
+
16
+ ## Reporting a Vulnerability
17
+
18
+ If you have discovered a security bug, please send an email to security@cedarcode.com
19
+ instead of posting to the GitHub issue tracker.
20
+
21
+ Thank you!
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "webauthn"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here