Class: Authn::Passkey::AuthenticateService

Inherits:
BaseService
  • Object
show all
Includes:
WebauthnErrors, SafeFormatHelper
Defined in:
app/services/authn/passkey/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 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(device_response, challenge) ⇒ AuthenticateService

Returns a new instance of AuthenticateService.



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

def initialize(device_response, challenge)
  @device_response = device_response
  @challenge = challenge
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
# File 'app/services/authn/passkey/authenticate_service.rb', line 16

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

  passkey_credential = WebAuthn::Credential.from_get(parsed_device_response)
  encoded_raw_id = Base64.strict_encode64(passkey_credential.raw_id)
  @stored_passkey_credential = find_matching_credential_xid(encoded_raw_id)

  encoder = WebAuthn.configuration.encoder

  verify_passkey(@stored_passkey_credential, passkey_credential, @challenge, encoder)

  @stored_passkey_credential.update!(
    counter: passkey_credential.sign_count,
    last_used_at: Time.current
  )

  ServiceResponse.success(
    message: _('Passkey successfully authenticated.'),
    payload: find_matching_user_with_passkey(@stored_passkey_credential)
  )

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