-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rb
89 lines (74 loc) · 2.3 KB
/
server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require 'bundler/setup'
require 'altcha'
require 'sinatra'
require 'json'
require 'base64'
require 'dotenv'
# Load environment variables
Dotenv.load
ALTCHA_HMAC_KEY = ENV['ALTCHA_HMAC_KEY'] || 'default-hmac-key'
class Server < Sinatra::Base
set :port, ENV['PORT'] || 3000
before do
content_type :json
headers 'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => ['GET', 'POST', 'OPTIONS'],
'Access-Control-Allow-Headers' => '*'
end
options '*' do
response.headers['Access-Control-Allow-Origin'] = '*'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST, OPTIONS'
response.headers['Access-Control-Allow-Headers'] = '*'
200
end
get '/' do
content_type 'text/plain'
<<~TEXT
ALTCHA server demo endpoints:
GET /altcha - use this endpoint as challengeurl for the widget
POST /submit - use this endpoint as the form action
POST /submit_spam_filter - use this endpoint for form submissions with spam filtering
TEXT
end
# Fetch challenge
get '/altcha' do
options = Altcha::ChallengeOptions.new(
algorithm: 'SHA-256',
hmac_key: ALTCHA_HMAC_KEY,
max_number: 50_000
)
challenge = Altcha.create_challenge(options)
challenge.to_json
end
# Handle solution submissions
post '/submit' do
payload = params['altcha']
if payload.nil?
halt 400, { error: 'Altcha payload missing' }.to_json
end
verified = Altcha.verify_solution(payload, ALTCHA_HMAC_KEY)
if verified
{ success: true, data: params }.to_json
else
halt 400, { error: 'Invalid Altcha payload' }.to_json
end
end
# Handle submissions with spam filter
post '/submit_spam_filter' do
payload = params['altcha']
if payload.nil?
halt 400, { error: 'Altcha payload missing' }.to_json
end
verified, verification_data = Altcha.verify_server_signature(payload, ALTCHA_HMAC_KEY)
fields_verified = Altcha.verify_fields_hash(params, verification_data.fields, verification_data.fields_hash, 'SHA-256')
if verified && fields_verified
{ success: true, form_data: params, verification_data: verification_data }.to_json
else
halt 400, { error: 'Invalid Altcha payload' }.to_json
end
end
end
# Run the application
if __FILE__ == $0
Server.run!
end