switch to captcha v2 api

This commit is contained in:
Kyle Drake 2018-02-21 09:29:00 -08:00
parent 7b0c26c1c2
commit dae2466487
9 changed files with 29 additions and 45 deletions

View file

@ -8,7 +8,6 @@ gem 'bcrypt'
gem 'sinatra-flash', require: 'sinatra/flash'
gem 'sinatra-xsendfile', require: 'sinatra/xsendfile'
gem 'puma', require: nil
gem 'rack-recaptcha', require: 'rack/recaptcha'
gem 'rmagick', require: nil
gem 'sidekiq', '~> 4.1.2'
gem 'mail'

View file

@ -180,8 +180,6 @@ GEM
rack (>= 0.4)
rack-protection (2.0.0)
rack
rack-recaptcha (0.6.6)
json
rack-test (0.8.2)
rack (>= 1.0, < 3)
rack_session_access (0.1.1)
@ -331,7 +329,6 @@ DEPENDENCIES
pry-byebug
puma
rack-cache
rack-recaptcha
rack-test
rack_session_access
rake
@ -363,4 +360,4 @@ DEPENDENCIES
zipruby
BUNDLED WITH
1.16.0
1.16.1

2
app.rb
View file

@ -9,9 +9,7 @@ use Rack::Session::Cookie, key: 'neocities',
same_site: :lax,
secure: ENV['RACK_ENV'] == 'production'
use Rack::Recaptcha, public_key: $config['recaptcha_public_key'], private_key: $config['recaptcha_private_key']
use Rack::TempfileReaper
helpers Rack::Recaptcha::Helpers
helpers do
def site_change_file_display_class(filename)

View file

@ -40,7 +40,7 @@ post '/contact' do
no_footer: true
})
flash[:success] = 'Your contact has been sent.'
flash[:success] = 'Your contact message has been sent.'
redirect '/'
end
end

View file

@ -1,21 +1,3 @@
def new_recaptcha_valid?
return true if ENV['RACK_ENV'] == 'test' || ENV['TRAVIS']
return false unless params[:'g-recaptcha-response']
resp = Net::HTTP.get URI(
'https://www.google.com/recaptcha/api/siteverify?'+
Rack::Utils.build_query(
secret: $config['recaptcha_private_key'],
response: params[:'g-recaptcha-response']
)
)
if JSON.parse(resp)['success'] == true
true
else
false
end
end
CREATE_MATCH_REGEX = /^username$|^password$|^email$|^new_tags_string$|^is_education$/
def education_whitelisted?
@ -82,7 +64,7 @@ post '/create' do
if education_whitelisted?
@site.email_confirmed = true
else
if !new_recaptcha_valid?
if !recaptcha_valid?
flash[:error] = 'The captcha was not valid, please try again.'
return {result: 'error'}.to_json
end

View file

@ -122,3 +122,21 @@ end
def flash_display(opts={})
erb :'_flash', layout: false, locals: {opts: opts}
end
def recaptcha_valid?
return true if ENV['RACK_ENV'] == 'test' || ENV['TRAVIS']
return false unless params[:'g-recaptcha-response']
resp = Net::HTTP.get URI(
'https://www.google.com/recaptcha/api/siteverify?'+
Rack::Utils.build_query(
secret: $config['recaptcha_private_key'],
response: params[:'g-recaptcha-response']
)
)
if JSON.parse(resp)['success'] == true
true
else
false
end
end

View file

@ -1,8 +1,4 @@
<script>
var RecaptchaOptions = {
theme : 'clean'
};
</script>
<script src='https://www.google.com/recaptcha/api.js'></script>
<div class="header-Outro">
<div class="row content single-Col">
@ -227,8 +223,9 @@
<textarea name="body" id="your_comments" class="col-75" rows="10"><%= params[:body] %></textarea>
<label>Fill out the captcha so we know youre not a robot:</label>
<div class="recaptcha">
<%== recaptcha_tag :challenge, ssl: request.ssl? %>
<div id="captcha-input" class="g-recaptcha"
data-sitekey="<%= $config['recaptcha_public_key'] %>"
data-theme="light" data-placement="left" data-trigger="manual">
</div>
<input class="btn-Action" type="submit" value="Send">

View file

@ -1,8 +1,4 @@
<script>
var RecaptchaOptions = {
theme : 'clean'
};
</script>
<script src='https://www.google.com/recaptcha/api.js'></script>
<div class="header-Outro">
<div class="row content single-Col">
@ -51,8 +47,9 @@
<textarea name="body" id="your_comments" class="col-80" rows="10"><%= params[:body] %></textarea>
<label>Fill out the captcha so we know youre not a robot:</label>
<div class="recaptcha">
<%== recaptcha_tag :challenge, ssl: request.ssl? %>
<div id="captcha-input" class="g-recaptcha"
data-sitekey="<%= $config['recaptcha_public_key'] %>"
data-theme="light" data-placement="left" data-trigger="manual">
</div>
<input class="btn-Action" type="submit" value="Send">

View file

@ -38,10 +38,6 @@
<script type="text/javascript" src="/js/html5.min.js"></script>
<![endif]-->
<script>
var RecaptchaOptions = {theme: 'clean'}
</script>
<script src="/js/jquery-1.11.0.min.js"></script>
<script src="/js/highlight.pack.js"></script>