Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/recaptcha.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ def self.verify_via_api_call_enterprise(response, options)

raw_reply = api_verification_enterprise(query_params, body, project_id, timeout: options[:timeout])
reply = Reply.new(raw_reply, enterprise: true)

reply.success?(options)
result = reply.success?(options)
options[:with_reply] == true ? [result, reply] : result
end

def self.verify_via_api_call_free(response, options)
Expand All @@ -90,8 +90,8 @@ def self.verify_via_api_call_free(response, options)

raw_reply = api_verification_free(verify_hash, timeout: options[:timeout], json: options[:json])
reply = Reply.new(raw_reply, enterprise: false)

reply.success?(options)
result = reply.success?(options)
options[:with_reply] == true ? [result, reply] : result
end

def self.http_client_for(uri:, timeout: nil)
Expand Down
16 changes: 8 additions & 8 deletions lib/recaptcha/adapters/controller_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ def verify_recaptcha(options = {})
Recaptcha.verify_via_api_call(recaptcha_response, options.merge(with_reply: true))

unless success
@_recaptcha_failure_reason = if @_recaptcha_reply.score &&
@_recaptcha_reply.score.to_f < options[:minimum_score].to_f
"Recaptcha score didn't exceed the minimum: #{@_recaptcha_reply.score} < #{options[:minimum_score]}."
elsif @_recaptcha_reply.error_codes?
"Recaptcha api call returned with error-codes: #{@_recaptcha_reply.error_codes}."
else
"Recaptcha failure after api call. Api reply: #{@_recaptcha_reply}."
end
@_recaptcha_failure_reason =
if @_recaptcha_reply.score && @_recaptcha_reply.score.to_f < options[:minimum_score].to_f
"Recaptcha score didn't exceed the minimum: #{@_recaptcha_reply.score} < #{options[:minimum_score]}."
elsif @_recaptcha_reply.error_codes.any?
"Recaptcha api call returned with error-codes: #{@_recaptcha_reply.error_codes}."
else
"Recaptcha failure after api call. Api reply: #{@_recaptcha_reply}."
end
end

success
Expand Down
58 changes: 29 additions & 29 deletions lib/recaptcha/reply.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,56 @@ def initialize(raw_reply, enterprise:)
@enterprise = enterprise
end

def token_properties
@raw_reply['tokenProperties'] if enterprise?
end

def success?(options = {})
result = success.to_s == 'true' &&
success.to_s == 'true' &&
hostname_valid?(options[:hostname]) &&
action_valid?(options[:action]) &&
score_above_threshold?(options[:minimum_score]) &&
score_below_threshold?(options[:maximum_score])
end

if options[:with_reply] == true
[result, self]
else
result
end
def token_properties
@raw_reply['tokenProperties'] if enterprise?
end

def success
return @raw_reply['success'] unless enterprise?

token_properties&.dig('valid')
if enterprise?
token_properties&.dig('valid')
else
@raw_reply['success']
end
end

def hostname
return @raw_reply['hostname'] unless enterprise?

token_properties&.dig('hostname')
if enterprise?
token_properties&.dig('hostname')
else
@raw_reply['hostname']
end
end

def action
return @raw_reply['action'] unless enterprise?

token_properties&.dig('action')
if enterprise?
token_properties&.dig('action')
else
@raw_reply['action']
end
end

def score
return @raw_reply['score'] unless enterprise?

@raw_reply.dig('riskAnalysis', 'score')
if enterprise?
@raw_reply.dig('riskAnalysis', 'score')
else
@raw_reply['score'] unless enterprise?
end
end

def error_codes
return [] if enterprise?

@raw_reply['error-codes'] || []
end

def error_codes?
!error_codes.empty?
if enterprise?
[]
else
@raw_reply['error-codes'] || []
end
end

def challenge_ts
Expand Down