Class: Biometric::Check

Inherits:
Object
  • Object
show all
Defined in:
lib/biometric/check.rb

Overview

Check class for handling biometric checks

Class Method Summary collapse

Class Method Details

.call(params, keyable_model) ⇒ Object



10
11
12
13
14
15
16
17
18
19
# File 'lib/biometric/check.rb', line 10

def self.call(params, keyable_model)
  device_id, signature, keyable = extract_params(params, keyable_model)
  return error_response('Device ID and Signature are required') unless device_id && signature
  return error_response('keyable not found') unless keyable

  biometric_key = find_biometric_key(keyable, device_id)
  return error_response('Biometric key not found for the specified device') unless biometric_key

  verify_signature(biometric_key, signature, keyable)
end

.error_response(message) ⇒ Object



47
48
49
# File 'lib/biometric/check.rb', line 47

def self.error_response(message)
  { success: false, error: message }
end

.extract_params(params, keyable_model) ⇒ Object



21
22
23
# File 'lib/biometric/check.rb', line 21

def self.extract_params(params, keyable_model)
  [params[:device_id], params[:signature], keyable_model.find(params[:keyable_id])]
end

.find_biometric_key(keyable, device_id) ⇒ Object



25
26
27
# File 'lib/biometric/check.rb', line 25

def self.find_biometric_key(keyable, device_id)
  keyable.biometric_keys.find_by(device_id: device_id)
end

.success_responseObject



43
44
45
# File 'lib/biometric/check.rb', line 43

def self.success_response
  { success: true }
end

.verify_signature(biometric_key, signature, keyable) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/biometric/check.rb', line 29

def self.verify_signature(biometric_key, signature, keyable)
  public_key = OpenSSL::PKey::RSA.new(Base64.decode64(biometric_key.public_key))
  payload = keyable.id.to_s + Biometric.configuration.secret_key

  signature_verified = verify_with_rescue(public_key, signature, payload)
  signature_verified ? success_response : error_response('not_found')
end

.verify_with_rescue(public_key, signature, payload) ⇒ Object



37
38
39
40
41
# File 'lib/biometric/check.rb', line 37

def self.verify_with_rescue(public_key, signature, payload)
  public_key.verify(OpenSSL::Digest.new('SHA256'), Base64.decode64(signature), payload)
rescue OpenSSL::PKey::RSAError
  error_response('Invalid public key format')
end