Class: Webauthn::RegisterService

Inherits:
BaseService show all
Includes:
Authn::WebauthnErrors
Defined in:
app/services/webauthn/register_service.rb

Constant Summary

Constants inherited from BaseService

BaseService::UnauthorizedError

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods included from Authn::WebauthnErrors

#webauthn_error_messages, #webauthn_generic_error_messages, #webauthn_human_readable_errors

Methods included from BaseServiceUtility

#deny_visibility_level, #event_service, #log_error, #log_info, #notification_service, #system_hook_service, #todo_service, #visibility_level

Methods included from Gitlab::Allowable

#can?, #can_all?, #can_any?

Constructor Details

#initialize(user, params, challenge) ⇒ RegisterService

Returns a new instance of RegisterService.



7
8
9
10
11
# File 'app/services/webauthn/register_service.rb', line 7

def initialize(user, params, challenge)
  @user = user
  @params = params
  @challenge = challenge
end

Instance Method Details

#executeObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/services/webauthn/register_service.rb', line 13

def execute
  registration = WebauthnRegistration.new

  begin
    webauthn_credential = WebAuthn::Credential.from_create(Gitlab::Json.safe_parse(@params[:device_response]))
    webauthn_credential.verify(@challenge)

    registration.update!(
      credential_xid: Base64.strict_encode64(webauthn_credential.raw_id),
      public_key: webauthn_credential.public_key,
      counter: webauthn_credential.sign_count,
      name: @params[:name],
      user: @user,
      passkey_eligible: passkey?(webauthn_credential),
      last_used_at: Time.current
    )

    ServiceResponse.success(
      message: _('Your WebAuthn device was registered!'),
      payload: registration
    )
  rescue JSON::ParserError
    ServiceResponse.error(
      message: _('Your WebAuthn device did not send a valid JSON response.'),
      payload: registration
    )
  rescue ActiveRecord::RecordInvalid => err
    ServiceResponse.error(
      message: err.message
    )
  rescue WebAuthn::Error => err
    ServiceResponse.error(
      message: webauthn_human_readable_errors(err.class.name)
    )
  end
end