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

add support to version 2 api (no captcha recaptcha) #114

Merged
merged 2 commits into from
Dec 9, 2014
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
20 changes: 16 additions & 4 deletions lib/recaptcha.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,23 @@
require 'recaptcha/verify'

module Recaptcha
RECAPTCHA_API_SERVER_URL = '//www.google.com/recaptcha/api'
RECAPTCHA_API_SECURE_SERVER_URL = 'https://www.google.com/recaptcha/api'
RECAPTCHA_VERIFY_URL = 'http://www.google.com/recaptcha/api/verify'
USE_SSL_BY_DEFAULT = false
CONFIG =
{
'v1' => {
'server_url' => '//www.google.com/recaptcha/api',
'secure_server_url' => 'https://www.google.com/recaptcha/api',
'verify_url' => 'http://www.google.com/recaptcha/api/verify'
},

'v2' => {
'server_url' => '//www.google.com/recaptcha/api.js',
'secure_server_url' => 'https://www.google.com/recaptcha/api.js',
'verify_url' => 'https://www.google.com/recaptcha/api/siteverify'
}
}

RECAPTCHA_API_VERSION = 'v2'
USE_SSL_BY_DEFAULT = false
HANDLE_TIMEOUTS_GRACEFULLY = true
SKIP_VERIFY_ENV = ['test', 'cucumber']

Expand Down
45 changes: 44 additions & 1 deletion lib/recaptcha/client_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ module ClientHelper
# Your public API can be specified in the +options+ hash or preferably
# using the Configuration.
def recaptcha_tags(options = {})
return v1_tags(options) if Recaptcha.configuration.v1?
return v2_tags(options) if Recaptcha.configuration.v2?
end # recaptcha_tags

def v1_tags(options)
# Default options
key = options[:public_key] ||= Recaptcha.configuration.public_key
raise RecaptchaError, "No public key specified." unless key
Expand Down Expand Up @@ -55,7 +60,45 @@ def recaptcha_tags(options = {})
end
end
return (html.respond_to?(:html_safe) && html.html_safe) || html
end # recaptcha_tags
end

def v2_tags(options)
key = options[:public_key] ||= Recaptcha.configuration.public_key
raise RecaptchaError, "No public key specified." unless key
error = options[:error] ||= ((defined? flash) ? flash[:recaptcha_error] : "")
uri = Recaptcha.configuration.api_server_url(options[:ssl])

v2_options = options.slice(:theme, :type, :callback).map {|k,v| %{data-#{k}="#{v}"} }.join(" ")

html = ""
html << %{<script src="#{uri}" async defer></script>\n}
html << %{<div class="g-recaptcha" data-sitekey="#{key}" #{v2_options}></div>\n}

unless options[:noscript] == false
html << %{<noscript>}
html << %{<div style="width: 302px; height: 352px;">}
html << %{ <div style="width: 302px; height: 352px; position: relative;">}
html << %{ <div style="width: 302px; height: 352px; position: absolute;">}
html << %{ <iframe src="https://www.google.com/recaptcha/api/fallback?k=your_site_key"}
html << %{ frameborder="0" scrolling="no"}
html << %{ style="width: 302px; height:352px; border-style: none;">}
html << %{ </iframe>}
html << %{ </div>}
html << %{ <div style="width: 250px; height: 80px; position: absolute; border-style: none; }
html << %{ bottom: 21px; left: 25px; margin: 0px; padding: 0px; right: 25px;">}
html << %{ <textarea id="g-recaptcha-response" name="g-recaptcha-response" }
html << %{ class="g-recaptcha-response" }
html << %{ style="width: 250px; height: 80px; border: 1px solid #c1c1c1; }
html << %{ margin: 0px; padding: 0px; resize: none;" value=""> }
html << %{ </textarea>}
html << %{ </div>}
html << %{ </div>}
html << %{ </div>}
html << %{</noscript>}
end

return (html.respond_to?(:html_safe) && html.html_safe) || html
end

private

Expand Down
30 changes: 23 additions & 7 deletions lib/recaptcha/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ module Recaptcha
# end
#
class Configuration
attr_accessor :nonssl_api_server_url,
:ssl_api_server_url,
:verify_url,
attr_accessor :api_version,
:skip_verify_env,
:private_key,
:public_key,
Expand All @@ -39,20 +37,38 @@ class Configuration
:use_ssl_by_default

def initialize #:nodoc:
@nonssl_api_server_url = RECAPTCHA_API_SERVER_URL
@ssl_api_server_url = RECAPTCHA_API_SECURE_SERVER_URL
@verify_url = RECAPTCHA_VERIFY_URL
@api_version = RECAPTCHA_API_VERSION
@skip_verify_env = SKIP_VERIFY_ENV
@handle_timeouts_gracefully = HANDLE_TIMEOUTS_GRACEFULLY
@use_ssl_by_default = USE_SSL_BY_DEFAULT

@private_key = ENV['RECAPTCHA_PRIVATE_KEY']
@public_key = ENV['RECAPTCHA_PUBLIC_KEY']
@public_key = ENV['RECAPTCHA_PUBLIC_KEY']
end

def api_server_url(ssl = nil) #:nodoc:
ssl = use_ssl_by_default if ssl.nil?
ssl ? ssl_api_server_url : nonssl_api_server_url
end

def nonssl_api_server_url
CONFIG[@api_version]['server_url']
end

def ssl_api_server_url
CONFIG[@api_version]['secure_server_url']
end

def verify_url
CONFIG[@api_version]['verify_url']
end

def v1?
@api_version == 'v1'
end

def v2?
@api_version == 'v2'
end
end
end
35 changes: 30 additions & 5 deletions lib/recaptcha/verify.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,41 @@ def verify_recaptcha(options = {})
http = Net::HTTP
end

Timeout::timeout(options[:timeout] || 3) do
recaptcha = http.post_form(URI.parse(Recaptcha.configuration.verify_url), {
if Recaptcha.configuration.v1?
verify_hash = {
"privatekey" => private_key,
"remoteip" => request.remote_ip,
"challenge" => params[:recaptcha_challenge_field],
"response" => params[:recaptcha_response_field]
})
}
Timeout::timeout(options[:timeout] || 3) do
recaptcha = http.post_form(URI.parse(Recaptcha.configuration.verify_url), verify_hash)
end
answer, error = recaptcha.body.split.map { |s| s.chomp }
end

if Recaptcha.configuration.v2?
verify_hash = {
"secret" => private_key,
"remoteip" => request.remote_ip,
"response" => params['g-recaptcha-response']
}

Timeout::timeout(options[:timeout] || 3) do
uri = URI.parse(Recaptcha.configuration.verify_url + '?' + verify_hash.to_query)
http_instance = http.new(uri.host, uri.port)
if uri.port==443
http_instance.use_ssl =
http_instance.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
request = Net::HTTP::Get.new(uri.request_uri)
recaptcha = http_instance.request(request)
end
answer, error = JSON.parse(recaptcha.body).values
end
answer, error = recaptcha.body.split.map { |s| s.chomp }
unless answer == 'true'

unless answer.to_s == 'true'
error = 'verification_failed' if error && Recaptcha.configuration.v2?
flash[:recaptcha_error] = if defined?(I18n)
I18n.translate("recaptcha.errors.#{error}", {:default => error})
else
Expand Down