Define an endpoint to retrieve a LibreCaptcha captcha
Some checks failed
Check usage of free licenses / check-licenses (pull_request) Failing after 48s
Add copyright notice / copyright_notice (pull_request) Successful in 57s
Run unit tests / unit_tests (pull_request) Failing after 3m28s

This commit is contained in:
Manuel Bustillo 2024-12-01 19:42:25 +01:00
parent 241668b84d
commit b237239a1f
8 changed files with 67 additions and 0 deletions

View File

@ -21,6 +21,7 @@ gem 'rack-cors'
gem 'react-rails'
gem 'rubytree'
gem 'acts_as_tenant'
gem 'httparty'
group :development, :test do
gem 'annotaterb'

View File

@ -126,6 +126,10 @@ GEM
raabro (~> 1.4)
globalid (1.2.1)
activesupport (>= 6.1)
httparty (0.22.0)
csv
mini_mime (>= 1.0.0)
multi_xml (>= 0.5.2)
i18n (1.14.6)
concurrent-ruby (~> 1.0)
importmap-rails (2.0.3)
@ -188,6 +192,8 @@ GEM
money (6.19.0)
i18n (>= 0.6.4, <= 2)
msgpack (1.7.2)
multi_xml (0.7.1)
bigdecimal (~> 3.1)
net-imap (0.5.1)
date
net-protocol
@ -398,6 +404,7 @@ DEPENDENCIES
devise (~> 4.9)
factory_bot_rails
faker
httparty
importmap-rails
jbuilder
jsonapi-rails

View File

@ -0,0 +1,11 @@
class CaptchaController < ApplicationController
skip_before_action :authenticate_user!
def create
id = LibreCaptcha.new.get_id
render json: {
id:,
media_url: media_captcha_index_url(id:)
}, status: :created
end
end

View File

@ -0,0 +1,12 @@
class LibreCaptcha
def get_id
HTTParty.post("http://libre-captcha:8888/v2/captcha",
body: {
input_type: "text",
level: :hard,
media: 'image/png',
size: '350x100'
}.to_json
).then { |raw| JSON.parse(raw)['id'] }
end
end

View File

@ -23,6 +23,9 @@ Rails.application.routes.draw do
resources :tables_arrangements, only: %i[index show]
end
resources :captcha, only: :create do
get 'v2/media', to: 'captcha#media', on: :collection, as: :media
end
mount Rswag::Ui::Engine => '/api-docs'
mount Rswag::Api::Engine => '/api-docs'

View File

@ -36,6 +36,12 @@ services:
- backend
volumes:
- ../wedding-planner-frontend/:/app
libre-captcha:
image: librecaptcha/lc-core:latest
volumes:
- "./tmp/libre-captcha-data:/lc-core/data"
ports:
- "8888:8888"
nginx:
image: nginx:latest
ports:

View File

@ -12,6 +12,11 @@ server {
proxy_set_header Host $http_host;
}
location /captcha/ {
proxy_pass http://libre-captcha:8888/;
proxy_set_header Host $http_host;
}
location / {
proxy_pass http://frontend:3000;
proxy_set_header Host $http_host;

View File

@ -0,0 +1,22 @@
require 'swagger_helper'
RSpec.describe 'captcha', type: :request do
path '/captcha' do
post('create a CAPTCHA challenge') do
tags 'CAPTCHA'
consumes 'application/json'
produces 'application/json'
response(201, 'created') do
schema type: :object,
required: %i[id],
properties: {
id: { type: :string, format: :uuid }
media_url: { type: :string, format: :uri }
}
xit
end
end
end
end