forked from klaudgrave/discourse-google-recaptcha
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f2b62f
commit a446efd
Showing
10 changed files
with
328 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
require 'active_support/concern' | ||
|
||
module RecaptchaVerifiable | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
before_action :recaptcha, only: [:create] | ||
end | ||
|
||
def recaptcha | ||
reroute_failed_recaptcha && return unless RecaptchaVerifier.verify(params["recaptcha-response"], request.ip) | ||
end | ||
|
||
def reroute_failed_recaptcha | ||
@person = Person.new | ||
flash.now[:error] = "Please verify you are not a robot." | ||
render action: "new" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class RecaptchaController < ApplicationController | ||
include RecaptchaVerifiable | ||
|
||
skip_before_action :verify_authenticity_token | ||
|
||
def index | ||
#render json: {name: "donut", description: "delicious!"} | ||
ver = RecaptchaVerifier.verify(params['recaptcha_response'].chomp, request.ip) | ||
testv = 'test' | ||
render json: {verify: ver, test: testv} | ||
|
||
end | ||
|
||
#RecaptchaVerifier.verify(recaptcha_response, ip_address) | ||
|
||
#skip_before_action :check_xhr | ||
# | ||
#def index | ||
# Rails.logger.info 'Method called' | ||
#end | ||
# | ||
#def verify | ||
# Rails.logger.info 'Called RecaptchaController#verify' | ||
#end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/*export default Ember.Controller.extend({ | ||
actions: { | ||
grVerify(){ | ||
$.ajax({ | ||
type: 'POST', | ||
contentType: 'application/json; charset=utf-8', | ||
url: "/grverify", | ||
data: JSON.stringify({recaptcha_response: g-recaptcha_response}), | ||
dataType: 'json', | ||
success: function (res) { | ||
console.log(res); | ||
}, | ||
error: function () { | ||
console.log('failure'); | ||
} | ||
}); | ||
}, | ||
} | ||
});*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/*import { ajax } from 'discourse/lib/ajax'; | ||
/*export default Ember.Route.extend({ | ||
model() { | ||
//console.log(ajax('/grverify/response.json')); | ||
Discourse.ajax('/grverify', { | ||
type: 'POST', | ||
data: {recaptcha_response: g-recaptcha_response} | ||
}).then(function(res) { | ||
if (res.verify) { | ||
console.log('dada'); | ||
} | ||
console.log(res); | ||
}); | ||
//return ajax('/grverify/response.json'); | ||
} | ||
});*/ | ||
/* | ||
App.grVerifyService = Ember.Service.extend({ | ||
function grVerify() { | ||
Discourse.ajax('/grverify', { | ||
type: 'POST', | ||
data: {recaptcha_response: g-recaptcha-response} | ||
}).then(function(res) { | ||
if (res.verify) { | ||
console.log('dada'); | ||
} | ||
console.log(res); | ||
}); | ||
} | ||
}); | ||
*/ | ||
/* | ||
function formGRSubmit() { | ||
$.ajax({ | ||
type: 'POST', | ||
contentType: 'application/json; charset=utf-8', | ||
url: "/grverify", | ||
data: JSON.stringify({recaptcha_response: g-recaptcha_response}), | ||
dataType: 'json', | ||
success: function (res) { | ||
console.log(res); | ||
}, | ||
error: function () { | ||
console.log('failure'); | ||
} | ||
}); | ||
}*/ |
128 changes: 128 additions & 0 deletions
128
assets/javascripts/discourse/templates/123123modal/create-account.hbs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<script src="https://www.google.com/recaptcha/api.js" async defer></script> | ||
|
||
<script type="text/x-handlebars" data-template-name="modal/create-account"> | ||
{{#create-account email=accountEmail disabled=submitDisabled action=(action "createAccount")}} | ||
{{#unless complete}} | ||
{{plugin-outlet name="create-account-before-modal-body"}} | ||
{{#d-modal-body title="create_account.title" class=(concat (if hasAtLeastOneLoginButton "has-alt-auth") " " (if userFields "has-user-fields"))}} | ||
{{#unless hasAuthOptions}} | ||
{{login-buttons externalLogin=(action "externalLogin")}} | ||
{{/unless}} | ||
{{#if showCreateForm}} | ||
<div class="login-form"> | ||
<form> | ||
<table> | ||
<tr class="input create-account-email"> | ||
<td class="label"><label for='new-account-email'>{{i18n 'user.email.title'}}</label></td> | ||
<td> | ||
{{input type="email" value=accountEmail id="new-account-email" disabled=emailValidated name="email" autofocus="autofocus"}} | ||
</td> | ||
</tr> | ||
<tr class="instructions create-account-email"> | ||
<td></td> | ||
{{input-tip validation=emailValidation id="account-email-validation"}} | ||
<td><label>{{i18n 'user.email.instructions'}}</label></td> | ||
</tr> | ||
{{#if usernameRequired}} | ||
<tr class="input"> | ||
<td class="label"><label for='new-account-username'>{{i18n 'user.username.title'}}</label></td> | ||
<td> | ||
{{input value=accountUsername id="new-account-username" name="username" maxlength=maxUsernameLength autocomplete="discourse"}} | ||
</td> | ||
</tr> | ||
<tr class="instructions"> | ||
<td></td> | ||
{{input-tip validation=usernameValidation id="username-validation"}} | ||
<td><label>{{i18n 'user.username.instructions'}}</label></td> | ||
</tr> | ||
{{/if}} | ||
{{#if fullnameRequired}} | ||
<tr class="input"> | ||
<td class="label"> | ||
<label for='new-account-name'>{{i18n 'user.name.title'}}</label> | ||
</td> | ||
<td> | ||
{{text-field value=accountName id="new-account-name"}} | ||
</td> | ||
</tr> | ||
<tr class="instructions"> | ||
<td></td> | ||
{{input-tip validation=nameValidation}} | ||
<td><label>{{nameInstructions}}</label></td> | ||
</tr> | ||
{{/if}} | ||
<div class="g-recaptcha" data-sitekey="6Lcg3LkUAAAAAKueM2PKktw-HVr5Zxjy1i22K9La"></div> | ||
{{plugin-outlet name="create-account-before-password" | ||
noTags=true | ||
args=(hash accountName=accountName | ||
accountUsername=accountUsername | ||
accountPassword=accountPassword | ||
userFields=userFields)}} | ||
{{#if passwordRequired}} | ||
<tr class="input"> | ||
<td class="label"><label for='new-account-password'>{{i18n 'user.password.title'}}</label></td> | ||
<td> | ||
{{password-field value=accountPassword type="password" id="new-account-password" capsLockOn=capsLockOn}} | ||
</td> | ||
</tr> | ||
<tr class="instructions"> | ||
<td></td> | ||
{{input-tip validation=passwordValidation}} | ||
<td> | ||
<label>{{passwordInstructions}}</label> | ||
<div class="caps-lock-warning {{unless capsLockOn 'hidden'}}"> | ||
{{d-icon "exclamation-triangle"}} {{i18n 'login.caps_lock_warning'}}</div> | ||
</td> | ||
</tr> | ||
{{/if}} | ||
{{plugin-outlet name="create-account-after-password" | ||
noTags=true | ||
args=(hash accountName=accountName | ||
accountUsername=accountUsername | ||
accountPassword=accountPassword | ||
userFields=userFields)}} | ||
</table> | ||
{{#if userFields}} | ||
<div class='user-fields'> | ||
{{#each userFields as |f|}} | ||
{{user-field field=f.field value=f.value}} | ||
{{/each}} | ||
</div> | ||
{{/if}} | ||
</form> | ||
</div> | ||
{{/if}} | ||
{{/d-modal-body}} | ||
{{#if showCreateForm}} | ||
<div class="modal-footer"> | ||
<button class='btn btn-large btn-primary' id="btn-sign-up" disabled={{submitDisabled}} {{action "create"}}>{{i18n 'create_account.title'}}</button> | ||
{{#conditional-loading-spinner condition=formSubmitted size="small"}} | ||
{{#d-button class="btn btn-large" id="login-link" action=(route-action "showLogin")}} | ||
{{i18n "log_in"}} | ||
{{/d-button}} | ||
{{/conditional-loading-spinner}} | ||
<div class='disclaimer'>{{{disclaimerHtml}}}</div> | ||
</div> | ||
{{/if}} | ||
{{/unless}} | ||
{{/create-account}} | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
en: | ||
site_settings: | ||
recaptcha_enabled: 'Enable Recaptcha' | ||
recaptcha_public_key: 'Recaptcha Public Key' | ||
recaptcha_secret_key: 'Recaptcha Secret Key' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
plugins: | ||
recaptcha_enabled: | ||
default: false | ||
recaptcha_public_key: | ||
default: "" | ||
recaptcha_secret_key: | ||
default: "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class GoogleRecaptcha | ||
BASE_URL = "https://www.google.com/".freeze | ||
VERIFY_URL = "recaptcha/api/siteverify".freeze | ||
|
||
def initialize | ||
@client = Faraday.new(BASE_URL) | ||
end | ||
|
||
def verify_recaptcha(params) | ||
response = perform_verify_request(params) | ||
return success?(response) | ||
end | ||
|
||
def success?(response) | ||
JSON.parse(response.body)["success"] | ||
end | ||
|
||
private | ||
|
||
attr_reader :client | ||
|
||
def perform_verify_request(params) | ||
client.post(VERIFY_URL) do |req| | ||
req.params = params.merge({secret: SiteSetting.recaptcha_secret_key}) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# name: recaptcha | ||
# about: add Google Recaptcha to Sign Up form | ||
# version: 0.1 | ||
# authors: klaudgrave | ||
# url: https://github.com/klaudgrave | ||
|
||
#gem 'recaptcha', '5.1.0' | ||
|
||
enabled_site_setting :recaptcha_enabled | ||
load File.expand_path('../lib/google_recaptcha.rb', __FILE__) | ||
load File.expand_path('../services/recaptcha_verifier.rb', __FILE__) | ||
load File.expand_path('../app/controllers/concerns/recaptcha_verifiable.rb', __FILE__) | ||
|
||
after_initialize do | ||
load File.expand_path('../app/controllers/recaptcha_controller.rb', __FILE__) | ||
|
||
|
||
Discourse::Application.routes.append do | ||
#put '/grverify' => 'recaptcha#verify' | ||
#mount ::RecaptchaVerifiable::recaptcha, at: "/grverify" | ||
match '/grverify' => 'recaptcha#index', :via => :post | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class RecaptchaVerifier | ||
#def self.verify(response, remote_ip, recaptcha_client=GoogleRecaptcha) | ||
#new(response, remote_ip, recaptcha_client).verify | ||
#return false unless recaptcha_response | ||
#recaptcha_client.verify_recaptcha(response: recaptcha_response, remoteip: remote_ip) | ||
#end | ||
|
||
def initialiaze(response, remote_ip, recaptcha_client) | ||
@recaptcha_response = response | ||
@remote_ip = remote_ip | ||
@recaptcha_client = GoogleRecaptcha.new | ||
end | ||
|
||
def self.verify(response, remoteip) | ||
#return false unless response | ||
recaptcha_client = GoogleRecaptcha.new | ||
recaptcha_client.verify_recaptcha(response: response, remoteip: remoteip) | ||
#rescue | ||
#false | ||
end | ||
|
||
private | ||
|
||
attr_reader :recaptcha_client, :recaptcha_response, :remote_ip | ||
end |