Validate the Captcha challenge for account signup
This commit is contained in:
parent
be9ca9e6b0
commit
5f01741943
@ -30,6 +30,18 @@ class ApplicationController < ActionController::Base
|
||||
|
||||
private
|
||||
|
||||
def validate_captcha!
|
||||
Rails.logger.info("Captcha params: #{captcha_params}")
|
||||
|
||||
return if LibreCaptcha.new.valid?(id: captcha_params[:id], answer: captcha_params[:answer])
|
||||
|
||||
render json: { error: 'Incorrect CAPTCHA solution' }, status: :unprocessable_entity
|
||||
end
|
||||
|
||||
def captcha_params
|
||||
params.expect(captcha: [:id, :answer])
|
||||
end
|
||||
|
||||
def default_url_options(options = {})
|
||||
options.merge(path_params: { slug: ActsAsTenant.current_tenant&.slug })
|
||||
end
|
||||
|
@ -4,6 +4,8 @@ class Users::RegistrationsController < Devise::RegistrationsController
|
||||
clear_respond_to
|
||||
respond_to :json
|
||||
|
||||
before_action :validate_captcha!, only: :create
|
||||
|
||||
def create
|
||||
wedding = Wedding.create(wedding_params)
|
||||
unless wedding.persisted?
|
||||
|
@ -11,4 +11,10 @@ class LibreCaptcha
|
||||
}.to_json
|
||||
).then { |raw| JSON.parse(raw)['id'] }
|
||||
end
|
||||
|
||||
def valid?(id:, answer:)
|
||||
HTTParty.post("http://libre-captcha:8888/v2/answer",
|
||||
body: { id:, answer: }.to_json
|
||||
).then { |raw| JSON.parse(raw)['result'] == 'True' }
|
||||
end
|
||||
end
|
@ -14,8 +14,8 @@ RSpec.describe 'captcha', type: :request do
|
||||
schema type: :object,
|
||||
required: %i[id],
|
||||
properties: {
|
||||
id: { type: :string, format: :uuid }
|
||||
media_url: { type: :string, format: :uri }
|
||||
id: { type: :string, format: :uuid },
|
||||
media_url: { type: :string, format: :uri },
|
||||
}
|
||||
xit
|
||||
end
|
||||
|
@ -18,5 +18,16 @@ module Swagger
|
||||
example: :default,
|
||||
description: 'Wedding slug'
|
||||
}
|
||||
|
||||
CAPTCHA = {
|
||||
captcha: {
|
||||
type: :object,
|
||||
required: %i[id answer],
|
||||
properties: {
|
||||
id: { type: :string, format: :uuid },
|
||||
answer: { type: :string }
|
||||
}
|
||||
}
|
||||
}
|
||||
end
|
||||
end
|
@ -30,7 +30,8 @@ RSpec.describe 'users/registrations', type: :request do
|
||||
properties: {
|
||||
date: { type: :string, format: :date},
|
||||
}
|
||||
}
|
||||
},
|
||||
**Swagger::Schema::CAPTCHA
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user