Class: Stytch::WebAuthn

Inherits:
Object
  • Object
show all
Includes:
RequestHelper
Defined in:
lib/stytch/webauthn.rb

Instance Method Summary collapse

Methods included from RequestHelper

#delete_request, #get_request, #post_request, #put_request, #request_with_query_params

Constructor Details

#initialize(connection) ⇒ WebAuthn

Returns a new instance of WebAuthn.



15
16
17
# File 'lib/stytch/webauthn.rb', line 15

def initialize(connection)
  @connection = connection
end

Instance Method Details

#authenticate(public_key_credential:, session_token: nil, session_duration_minutes: nil, session_jwt: nil, session_custom_claims: nil) ⇒ Object

Complete the authentication of a Passkey or WebAuthn registration by passing the response from the [navigator.credentials.get()](www.w3.org/TR/webauthn-2/#sctn-getAssertion) request to the authenticate endpoint.

If the [webauthn-json](github.com/github/webauthn-json) library’s ‘get()` method was used, the response can be passed directly to the [authenticate endpoint](stytch.com/docs/api/webauthn-authenticate). If not some fields from the [navigator.credentials.get()](www.w3.org/TR/webauthn-2/#sctn-getAssertion) response will need to be converted from array buffers to strings and marshalled into JSON.

Parameters:

public_key_credential

The response of the [navigator.credentials.create()](www.w3.org/TR/webauthn-2/#sctn-createCredential). The type of this field is String.

session_token

The ‘session_token` associated with a User’s existing Session. The type of this field is nilable String.

session_duration_minutes

Set the session lifetime to be this many minutes from now. This will start a new session if one doesn’t already exist, returning both an opaque ‘session_token` and `session_jwt` for this session. Remember that the `session_jwt` will have a fixed lifetime of five minutes regardless of the underlying session duration, and will need to be refreshed over time.

This value must be a minimum of 5 and a maximum of 527040 minutes (366 days).

If a ‘session_token` or `session_jwt` is provided then a successful authentication will continue to extend the session this many minutes.

If the ‘session_duration_minutes` parameter is not specified, a Stytch session will not be created. The type of this field is nilable Integer.

session_jwt

The ‘session_jwt` associated with a User’s existing Session. The type of this field is nilable String.

session_custom_claims

Add a custom claims map to the Session being authenticated. Claims are only created if a Session is initialized by providing a value in ‘session_duration_minutes`. Claims will be included on the Session object and in the JWT. To update a key in an existing Session, supply a new value. To delete a key, supply a null value.

Custom claims made with reserved claims (“iss”, “sub”, “aud”, “exp”, “nbf”, “iat”, “jti”) will be ignored. Total custom claims size cannot exceed four kilobytes. The type of this field is nilable object.

Returns:

An object with the following fields:

request_id

Globally unique UUID that is returned with every API call. This value is important to log for debugging purposes; we may ask for this value to help identify a specific API call when helping you debug an issue. The type of this field is String.

user_id

The unique ID of the affected User. The type of this field is String.

webauthn_registration_id

The unique ID for the Passkey or WebAuthn registration. The type of this field is String.

session_token

A secret token for a given Stytch Session. The type of this field is String.

session_jwt

The JSON Web Token (JWT) for a given Stytch Session. The type of this field is String.

user

The ‘user` object affected by this API call. See the [Get user endpoint](stytch.com/docs/api/get-user) for complete response field details. The type of this field is User (object).

status_code

The HTTP status code of the response. Stytch follows standard HTTP response status code patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX are server errors. The type of this field is Integer.

session

If you initiate a Session, by including ‘session_duration_minutes` in your authenticate call, you’ll receive a full Session object in the response.

See [GET sessions](stytch.com/docs/api/session-get) for complete response fields.

The type of this field is nilable Session (object).



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/stytch/webauthn.rb', line 271

def authenticate(
  public_key_credential:,
  session_token: nil,
  session_duration_minutes: nil,
  session_jwt: nil,
  session_custom_claims: nil
)
  headers = {}
  request = {
    public_key_credential: public_key_credential
  }
  request[:session_token] = session_token unless session_token.nil?
  request[:session_duration_minutes] = session_duration_minutes unless session_duration_minutes.nil?
  request[:session_jwt] = session_jwt unless session_jwt.nil?
  request[:session_custom_claims] = session_custom_claims unless session_custom_claims.nil?

  post_request('/v1/webauthn/authenticate', request, headers)
end

#authenticate_start(domain:, user_id: nil, return_passkey_credential_options: nil) ⇒ Object

Initiate the authentication of a Passkey or WebAuthn registration.

To optimize for Passkeys, set the ‘return_passkey_credential_options` field to `true`.

After calling this endpoint, the browser will need to call [navigator.credentials.get()](www.w3.org/TR/webauthn-2/#sctn-getAssertion) with the data from ‘public_key_credential_request_options` passed to the [navigator.credentials.get()](www.w3.org/TR/webauthn-2/#sctn-getAssertion) request via the public key argument. We recommend using the `get()` wrapper provided by the webauthn-json library.

If you are not using the [webauthn-json](github.com/github/webauthn-json) library, ‘the public_key_credential_request_options` will need to be converted to a suitable public key by unmarshalling the JSON and converting some the fields to array buffers.

Parameters:

domain

The domain for Passkeys or WebAuthn. Defaults to ‘window.location.hostname`. The type of this field is String.

user_id

The ‘user_id` of an active user the Passkey or WebAuthn registration should be tied to. The type of this field is nilable String.

return_passkey_credential_options

If true, the ‘public_key_credential_creation_options` returned will be optimized for Passkeys with `userVerification` set to `“preferred”`.

The type of this field is nilable Boolean.

Returns:

An object with the following fields:

request_id

Globally unique UUID that is returned with every API call. This value is important to log for debugging purposes; we may ask for this value to help identify a specific API call when helping you debug an issue. The type of this field is String.

user_id

The unique ID of the affected User. The type of this field is String.

public_key_credential_request_options

Options used for Passkey or WebAuthn authentication. The type of this field is String.

status_code

The HTTP status code of the response. Stytch follows standard HTTP response status code patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX are server errors. The type of this field is Integer.



196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/stytch/webauthn.rb', line 196

def authenticate_start(
  domain:,
  user_id: nil,
  return_passkey_credential_options: nil
)
  headers = {}
  request = {
    domain: domain
  }
  request[:user_id] = user_id unless user_id.nil?
  request[:return_passkey_credential_options] = return_passkey_credential_options unless return_passkey_credential_options.nil?

  post_request('/v1/webauthn/authenticate/start', request, headers)
end

#register(user_id:, public_key_credential:, session_token: nil, session_duration_minutes: nil, session_jwt: nil, session_custom_claims: nil) ⇒ Object

Complete the creation of a WebAuthn registration by passing the response from the [navigator.credentials.create()](www.w3.org/TR/webauthn-2/#sctn-createCredential) request to this endpoint as the ‘public_key_credential` parameter.

If the [webauthn-json](github.com/github/webauthn-json) library’s ‘create()` method was used, the response can be passed directly to the [register endpoint](stytch.com/docs/api/webauthn-register). If not, some fields (the client data and the attestation object) from the [navigator.credentials.create()](www.w3.org/TR/webauthn-2/#sctn-createCredential) response will need to be converted from array buffers to strings and marshalled into JSON.

Parameters:

user_id

The ‘user_id` of an active user the Passkey or WebAuthn registration should be tied to. The type of this field is String.

public_key_credential

The response of the [navigator.credentials.create()](www.w3.org/TR/webauthn-2/#sctn-createCredential). The type of this field is String.

session_token

The ‘session_token` associated with a User’s existing Session. The type of this field is nilable String.

session_duration_minutes

Set the session lifetime to be this many minutes from now. This will start a new session if one doesn’t already exist, returning both an opaque ‘session_token` and `session_jwt` for this session. Remember that the `session_jwt` will have a fixed lifetime of five minutes regardless of the underlying session duration, and will need to be refreshed over time.

This value must be a minimum of 5 and a maximum of 527040 minutes (366 days).

If a ‘session_token` or `session_jwt` is provided then a successful authentication will continue to extend the session this many minutes.

If the ‘session_duration_minutes` parameter is not specified, a Stytch session will not be created. The type of this field is nilable Integer.

session_jwt

The ‘session_jwt` associated with a User’s existing Session. The type of this field is nilable String.

session_custom_claims

Add a custom claims map to the Session being authenticated. Claims are only created if a Session is initialized by providing a value in ‘session_duration_minutes`. Claims will be included on the Session object and in the JWT. To update a key in an existing Session, supply a new value. To delete a key, supply a null value.

Custom claims made with reserved claims (“iss”, “sub”, “aud”, “exp”, “nbf”, “iat”, “jti”) will be ignored. Total custom claims size cannot exceed four kilobytes. The type of this field is nilable object.

Returns:

An object with the following fields:

request_id

Globally unique UUID that is returned with every API call. This value is important to log for debugging purposes; we may ask for this value to help identify a specific API call when helping you debug an issue. The type of this field is String.

user_id

The unique ID of the affected User. The type of this field is String.

webauthn_registration_id

The unique ID for the Passkey or WebAuthn registration. The type of this field is String.

session_token

A secret token for a given Stytch Session. The type of this field is String.

session_jwt

The JSON Web Token (JWT) for a given Stytch Session. The type of this field is String.

user

(no documentation yet) The type of this field is User (object).

status_code

The HTTP status code of the response. Stytch follows standard HTTP response status code patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX are server errors. The type of this field is Integer.

session

If you initiate a Session, by including ‘session_duration_minutes` in your authenticate call, you’ll receive a full Session object in the response.

See [GET sessions](stytch.com/docs/api/session-get) for complete response fields.

The type of this field is nilable Session (object).



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/stytch/webauthn.rb', line 141

def register(
  user_id:,
  public_key_credential:,
  session_token: nil,
  session_duration_minutes: nil,
  session_jwt: nil,
  session_custom_claims: nil
)
  headers = {}
  request = {
    user_id: user_id,
    public_key_credential: public_key_credential
  }
  request[:session_token] = session_token unless session_token.nil?
  request[:session_duration_minutes] = session_duration_minutes unless session_duration_minutes.nil?
  request[:session_jwt] = session_jwt unless session_jwt.nil?
  request[:session_custom_claims] = session_custom_claims unless session_custom_claims.nil?

  post_request('/v1/webauthn/register', request, headers)
end

#register_start(user_id:, domain:, user_agent: nil, authenticator_type: nil, return_passkey_credential_options: nil) ⇒ Object

Initiate the process of creating a new Passkey or WebAuthn registration.

To optimize for Passkeys, set the ‘return_passkey_credential_options` field to `true`.

After calling this endpoint, the browser will need to call [navigator.credentials.create()](www.w3.org/TR/webauthn-2/#sctn-createCredential) with the data from [public_key_credential_creation_options](w3c.github.io/webauthn/#dictionary-makecredentialoptions) passed to the [navigator.credentials.create()](www.w3.org/TR/webauthn-2/#sctn-createCredential) request via the public key argument. We recommend using the ‘create()` wrapper provided by the webauthn-json library.

If you are not using the [webauthn-json](github.com/github/webauthn-json) library, the ‘public_key_credential_creation_options` will need to be converted to a suitable public key by unmarshalling the JSON, base64 decoding the user ID field, and converting user ID and the challenge fields into an array buffer.

Parameters:

user_id

The ‘user_id` of an active user the Passkey or WebAuthn registration should be tied to. The type of this field is String.

domain

The domain for Passkeys or WebAuthn. Defaults to ‘window.location.hostname`. The type of this field is String.

user_agent

The user agent of the User. The type of this field is nilable String.

authenticator_type

The requested authenticator type of the Passkey or WebAuthn device. The two valid values are platform and cross-platform. If no value passed, we assume both values are allowed. The type of this field is nilable String.

return_passkey_credential_options

If true, the ‘public_key_credential_creation_options` returned will be optimized for Passkeys with `residentKey` set to `“required”` and `userVerification` set to `“preferred”`.

The type of this field is nilable Boolean.

Returns:

An object with the following fields:

request_id

Globally unique UUID that is returned with every API call. This value is important to log for debugging purposes; we may ask for this value to help identify a specific API call when helping you debug an issue. The type of this field is String.

user_id

The unique ID of the affected User. The type of this field is String.

public_key_credential_creation_options

Options used for Passkey or WebAuthn registration. The type of this field is String.

status_code

The HTTP status code of the response. Stytch follows standard HTTP response status code patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX are server errors. The type of this field is Integer.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/stytch/webauthn.rb', line 59

def register_start(
  user_id:,
  domain:,
  user_agent: nil,
  authenticator_type: nil,
  return_passkey_credential_options: nil
)
  headers = {}
  request = {
    user_id: user_id,
    domain: domain
  }
  request[:user_agent] = user_agent unless user_agent.nil?
  request[:authenticator_type] = authenticator_type unless authenticator_type.nil?
  request[:return_passkey_credential_options] = return_passkey_credential_options unless return_passkey_credential_options.nil?

  post_request('/v1/webauthn/register/start', request, headers)
end

#update(webauthn_registration_id:, name:) ⇒ Object

Updates a Passkey or WebAuthn registration.

Parameters:

webauthn_registration_id

Globally unique UUID that identifies a Passkey or WebAuthn registration in the Stytch API. The ‘webauthn_registration_id` is used when you need to operate on a specific User’s WebAuthn registration. The type of this field is String.

name

The ‘name` of the WebAuthn registration or Passkey. The type of this field is String.

Returns:

An object with the following fields:

request_id

Globally unique UUID that is returned with every API call. This value is important to log for debugging purposes; we may ask for this value to help identify a specific API call when helping you debug an issue. The type of this field is String.

status_code

The HTTP status code of the response. Stytch follows standard HTTP response status code patterns, e.g. 2XX values equate to success, 3XX values are redirects, 4XX are client errors, and 5XX are server errors. The type of this field is Integer.

webauthn_registration

A Passkey or WebAuthn registration. The type of this field is nilable WebAuthnRegistration (object).



311
312
313
314
315
316
317
318
319
320
321
# File 'lib/stytch/webauthn.rb', line 311

def update(
  webauthn_registration_id:,
  name:
)
  headers = {}
  request = {
    name: name
  }

  put_request("/v1/webauthn/#{webauthn_registration_id}", request, headers)
end