Class: Webauthn::AuthenticateService

Inherits:
BaseService show all
Defined in:
app/services/webauthn/authenticate_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, device_response, challenge) ⇒ AuthenticateService

Returns a new instance of AuthenticateService.



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

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

def execute
  parsed_device_response = Gitlab::Json.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 = @user.webauthn_registrations.find_by_credential_xid(encoded_raw_id)

  encoder = WebAuthn.configuration.encoder

  if stored_webauthn_credential &&
      validate_webauthn_credential(webauthn_credential) &&
      verify_webauthn_credential(webauthn_credential, stored_webauthn_credential, @challenge, encoder)

    stored_webauthn_credential.update!(counter: webauthn_credential.sign_count)
    return true
  end

  false
rescue JSON::ParserError, WebAuthn::SignCountVerificationError, WebAuthn::Error
  false
end