Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support logging api call replies #402

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## Next
* Gracefully handle invalid params
* support logging api call replies

## 5.8.0
* Add support for the enterprise API
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,8 @@ Recaptcha.configure do |config|
# config.enterprise = true
# config.enterprise_api_key = 'AIzvFyE3TU-g4K_Kozr9F1smEzZSGBVOfLKyupA'
# config.enterprise_project_id = 'my-project'

# config.logger = Rails.logger # log the api call replies
end
```

Expand Down
30 changes: 15 additions & 15 deletions lib/recaptcha.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,22 @@ def self.invalid_response?(resp)
end

def self.verify_via_api_call(response, options)
if Recaptcha.configuration.enterprise
verify_via_api_call_enterprise(response, options)
success, reply = if Recaptcha.configuration.enterprise
verify_via_api_call_enterprise(response, options.merge(with_reply: true))
else
verify_via_api_call_free(response, options)
verify_via_api_call_free(response, options.merge(with_reply: true))
end

Recaptcha.configuration.logger&.info(reply.merge(event: 'recaptcha-response'))

if options[:with_reply]
[success, reply]
else
success
end
end

def self.verify_via_api_call_enterprise(response, options)
private_class_method def self.verify_via_api_call_enterprise(response, options)
site_key = options.fetch(:site_key) { configuration.site_key! }
api_key = options.fetch(:enterprise_api_key) { configuration.enterprise_api_key! }
project_id = options.fetch(:enterprise_project_id) { configuration.enterprise_project_id! }
Expand All @@ -85,14 +93,10 @@ def self.verify_via_api_call_enterprise(response, options)
action_valid?(token_properties['action'], options[:action]) &&
score_above_threshold?(reply['score'], options[:minimum_score])

if options[:with_reply] == true
return success, reply
else
return success
end
[success, reply]
end

def self.verify_via_api_call_free(response, options)
private_class_method def self.verify_via_api_call_free(response, options)
secret_key = options.fetch(:secret_key) { configuration.secret_key! }
verify_hash = { 'secret' => secret_key, 'response' => response }
verify_hash['remoteip'] = options[:remote_ip] if options.key?(:remote_ip)
Expand All @@ -103,11 +107,7 @@ def self.verify_via_api_call_free(response, options)
action_valid?(reply['action'], options[:action]) &&
score_above_threshold?(reply['score'], options[:minimum_score])

if options[:with_reply] == true
return success, reply
else
return success
end
[success, reply]
end

def self.hostname_valid?(hostname, validation)
Expand Down
2 changes: 1 addition & 1 deletion lib/recaptcha/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Configuration
}.freeze

attr_accessor :default_env, :skip_verify_env, :proxy, :secret_key, :site_key, :handle_timeouts_gracefully, :hostname
attr_accessor :enterprise, :enterprise_api_key, :enterprise_project_id
attr_accessor :enterprise, :enterprise_api_key, :enterprise_project_id, :logger
attr_writer :api_server_url, :verify_url

def initialize #:nodoc:
Expand Down
10 changes: 10 additions & 0 deletions test/verify_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@
assert_equal "reCAPTCHA verification failed, please try again.", @controller.flash[:recaptcha_error]
end

it "can log the response" do
logger = mock('STDOUT')

Recaptcha.with_configuration(logger: logger) do
expect_http_post.to_return(body: '{"success":true}')
logger.expects(:info).with("success" => true, :event => "recaptcha-response")
@controller.verify_recaptcha
end
end

describe ':hostname' do
let(:hostname) { 'fake.hostname.com' }

Expand Down