# frozen_string_literal: true # !!! # WARNING: This file is autogenerated # Only modify code within MANUAL() sections # or your changes may be overwritten later! # !!! require_relative 'request_helper' module Stytch class WebAuthn include Stytch::RequestHelper def initialize(connection) @connection = connection end # Initiate the process of creating a new WebAuthn registration. After calling this endpoint, the browser will need to call [navigator.credentials.create()](https://www.w3.org/TR/webauthn-2/#sctn-createCredential) with the data from [public_key_credential_creation_options](https://w3c.github.io/webauthn/#dictionary-makecredentialoptions) passed to the [navigator.credentials.create()](https://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](https://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 WebAuthn registration should be tied to. # The type of this field is +String+. # domain:: # The domain for 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 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. This includes making `residentKey` required, `userVerification` preferred, and ignoring the `authenticator_type` passed. # 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 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+. def register_start( user_id:, domain:, user_agent: nil, authenticator_type: nil, return_passkey_credential_options: nil ) 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) end # Complete the creation of a WebAuthn registration by passing the response from the [navigator.credentials.create()](https://www.w3.org/TR/webauthn-2/#sctn-createCredential) request to this endpoint as the `public_key_credential` parameter. # # If the [webauthn-json](https://github.com/github/webauthn-json) library's `create()` method was used, the response can be passed directly to the [register endpoint](https://stytch.com/docs/api/webauthn-register). If not, some fields (the client data and the attestation object) from the [navigator.credentials.create()](https://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 WebAuthn registration should be tied to. # The type of this field is +String+. # public_key_credential:: # The response of the [navigator.credentials.create()](https://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 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](https://stytch.com/docs/api/session-get) for complete response fields. # # The type of this field is nilable +Session+ (+object+). def register( user_id:, public_key_credential:, session_token: nil, session_duration_minutes: nil, session_jwt: nil, session_custom_claims: nil ) 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) end # Initiate the authentication of a WebAuthn registration. After calling this endpoint, the browser will need to call [navigator.credentials.get()](https://www.w3.org/TR/webauthn-2/#sctn-getAssertion) with the data from `public_key_credential_request_options` passed to the [navigator.credentials.get()](https://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](https://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 WebAuthn. Defaults to `window.location.hostname`. # The type of this field is +String+. # user_id:: # The `user_id` of an active user the 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. This includes making `userVerification` 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 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+. def authenticate_start( domain:, user_id: nil, return_passkey_credential_options: nil ) 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) end # Complete the authentication of a WebAuthn registration by passing the response from the [navigator.credentials.get()](https://www.w3.org/TR/webauthn-2/#sctn-getAssertion) request to the authenticate endpoint. # # If the [webauthn-json](https://github.com/github/webauthn-json) library's `get()` method was used, the response can be passed directly to the [authenticate endpoint](https://stytch.com/docs/api/webauthn-authenticate). If not some fields from the [navigator.credentials.get()](https://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()](https://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 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](https://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](https://stytch.com/docs/api/session-get) for complete response fields. # # The type of this field is nilable +Session+ (+object+). def authenticate( public_key_credential:, session_token: nil, session_duration_minutes: nil, session_jwt: nil, session_custom_claims: nil ) 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) end # Updates a WebAuthn registration. # # == Parameters: # webauthn_registration_id:: # Globally unique UUID that identifies a WebAuthn registration in the Stytch API. The `webautn_registration_id` is used when you need to operate on a specific User's WebAuthn registartion. # The type of this field is +String+. # name:: # The `name` of the WebAuthn registration. # 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 WebAuthn registration. # The type of this field is nilable +WebAuthnRegistration+ (+object+). def update( webauthn_registration_id:, name: ) request = { name: name } put_request("/v1/webauthn/#{webauthn_registration_id}", request) end end end