Class: Webauthn::RegisterService

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

Instance Attribute Summary

Attributes inherited from BaseService

#current_user, #params, #project

Instance Method Summary collapse

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?

Constructor Details

#initialize(user, params, challenge) ⇒ RegisterService

Returns a new instance of RegisterService.



5
6
7
8
9
# File 'app/services/webauthn/register_service.rb', line 5

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

Instance Method Details

#executeObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/services/webauthn/register_service.rb', line 11

def execute
  registration = WebauthnRegistration.new

  begin
    webauthn_credential = WebAuthn::Credential.from_create(Gitlab::Json.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
    )
  rescue JSON::ParserError
    registration.errors.add(:base, _('Your WebAuthn device did not send a valid JSON response.'))
  rescue WebAuthn::Error => e
    registration.errors.add(:base, e.message)
  end

  registration
end