Class: Authn::Passkey::RegisterService

Inherits:
BaseService show all
Includes:
WebauthnErrors
Defined in:
app/services/authn/passkey/register_service.rb

Constant Summary

Constants inherited from BaseService

BaseService::UnauthorizedError

Instance Attribute Summary collapse

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

Methods included from 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.



10
11
12
13
14
# File 'app/services/authn/passkey/register_service.rb', line 10

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

Instance Attribute Details

#userObject (readonly)

Returns the value of attribute user.



8
9
10
# File 'app/services/authn/passkey/register_service.rb', line 8

def user
  @user
end

Instance Method Details

#executeObject



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
49
50
51
52
53
54
55
# File 'app/services/authn/passkey/register_service.rb', line 16

def execute
  registration = WebauthnRegistration.new

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

    @passkey_credential = passkey_credential

    registration.update!(
      credential_xid: Base64.strict_encode64(@passkey_credential.raw_id),
      public_key: @passkey_credential.public_key,
      counter: @passkey_credential.sign_count,
      name: @params[:name],
      user: @user,
      authentication_mode: :passwordless,
      passkey_eligible: true,
      last_used_at: Time.current
    )

    notify_on_success(user, registration.name)

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