Class: Webauthn::AuthenticateService

Inherits:
BaseService show all
Includes:
Authn::WebauthnErrors, SafeFormatHelper
Defined in:
app/services/webauthn/authenticate_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 SafeFormatHelper

#safe_format, #tag_pair

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, device_response, challenge) ⇒ AuthenticateService

Returns a new instance of AuthenticateService.



8
9
10
11
12
# File 'app/services/webauthn/authenticate_service.rb', line 8

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

Instance Method Details

#executeObject



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
# File 'app/services/webauthn/authenticate_service.rb', line 14

def execute
  parsed_device_response = Gitlab::Json.safe_parse(@device_response)

  webauthn_credential = WebAuthn::Credential.from_get(parsed_device_response)
  encoded_raw_id = Base64.strict_encode64(webauthn_credential.raw_id)
  stored_webauthn_credential = stored_passkey_or_second_factor_webauthn_credential(encoded_raw_id)

  encoder = WebAuthn.configuration.encoder

  verify_webauthn(stored_webauthn_credential, webauthn_credential, @challenge, encoder)

  stored_webauthn_credential.update!(
    counter: webauthn_credential.sign_count,
    last_used_at: Time.current
  )

  ServiceResponse.success(
    message: _('WebAuthn device successfully authenticated.')
  )

rescue JSON::ParserError
  ServiceResponse.error(
    message: _('Your WebAuthn device did not send a valid JSON response.')
  )
rescue ActiveRecord::RecordNotFound
  ServiceResponse.error(
    message: webauthn_credential_not_found_error
  )
rescue WebAuthn::Error => err
  ServiceResponse.error(
    message: webauthn_human_readable_errors(err.class.name)
  )
end