Class: CaptchaManager

Inherits:
Object
  • Object
show all
Defined in:
lib/trustcaptcha/captcha_manager.rb

Defined Under Namespace

Classes: SecretKeyInvalidException, TokenDecryptionFailedException, VerificationNotFoundException, VerificationTokenInvalidException

Class Method Summary collapse

Class Method Details

.decrypt_access_token(secret_key, verification_token) ⇒ Object



35
36
37
38
39
# File 'lib/trustcaptcha/captcha_manager.rb', line 35

def self.decrypt_access_token(secret_key, verification_token)
  AesEncryption.decrypt_to_string(secret_key, verification_token.encrypted_access_token)
rescue StandardError => e
  raise TokenDecryptionFailedException, "Failed to decrypt access token: #{e.message}"
end

.fetch_verification_result(verification_token, access_token) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/trustcaptcha/captcha_manager.rb', line 41

def self.fetch_verification_result(verification_token, access_token)
  url = URI("#{verification_token.api_endpoint}/verifications/#{verification_token.verification_id}/assessments?accessToken=#{access_token}")
  response = Net::HTTP.get_response(url)
  raise VerificationNotFoundException, "Verification not found" if response.code == '404'
  raise "Failed to retrieve verification result: HTTP #{response.code}" unless response.is_a?(Net::HTTPSuccess)

  VerificationResult.from_json(response.body)
rescue StandardError => e
  raise "Failed to retrieve verification result: #{e.message}"
end

.get_secret_key(base64_secret_key) ⇒ Object



23
24
25
26
27
# File 'lib/trustcaptcha/captcha_manager.rb', line 23

def self.get_secret_key(base64_secret_key)
  AesEncryption.to_aes_secret_key(base64_secret_key)
rescue StandardError => e
  raise SecretKeyInvalidException, "Invalid secret key: #{e.message}"
end

.get_verification_result(base64_secret_key, base64_verification_token) ⇒ Object



14
15
16
17
18
19
# File 'lib/trustcaptcha/captcha_manager.rb', line 14

def self.get_verification_result(base64_secret_key, base64_verification_token)
  verification_token = get_verification_token(base64_verification_token)
  secret_key = get_secret_key(base64_secret_key)
  decrypted_access_token = decrypt_access_token(secret_key, verification_token)
  fetch_verification_result(verification_token, decrypted_access_token)
end

.get_verification_token(verification_token) ⇒ Object



29
30
31
32
33
# File 'lib/trustcaptcha/captcha_manager.rb', line 29

def self.get_verification_token(verification_token)
  VerificationToken.from_base64(verification_token)
rescue StandardError => e
  raise VerificationTokenInvalidException, "Invalid verification token: #{e.message}"
end