Update authentication.cr

This commit is contained in:
Ryan G 2023-06-12 23:47:09 -07:00
parent ae058da25c
commit 6014ee9cc1

View File

@ -35,16 +35,45 @@ module Invidious::Routes::API::V1::Authentication
password = password.byte_slice(0, 55) password = password.byte_slice(0, 55)
# send captcha if enabled # send captcha if enabled
if CONFIG.captcha_enabled if CONFIG.captcha_enabled
# send captcha captcha_response = nil
captcha = Invidious::User::Captcha.generate_text(HMAC_KEY, ":captcha") begin
# puts captcha captcha_response = CaptchaResponse.from_json(env.request.body || "{}")
response = JSON.build do |json| rescue
json.object do end
json.field "question", captcha["question"] if captcha_response
json.field "tokens", captcha["tokens"] answer = captcha_response.answer
end tokens = captcha_response.tokens
answer = Digest::MD5.hexdigest(answer.downcase.strip)
if tokens.empty?
return error_json(500, "Erroneous CAPTCHA")
end
found_valid_captcha = false
error_exception = Exception.new
tokens.each do |tok|
begin
validate_request(tok, answer, env.request, HMAC_KEY, locale)
found_valid_captcha = true
rescue ex
error_exception = ex
end
end
if !found_valid_captcha
return error_json(500, error_exception)
end
else
# send captcha
captcha = Invidious::User::Captcha.generate_text(HMAC_KEY, ":captcha")
# puts captcha
response = JSON.build do |json|
json.object do
json.field "question", captcha["question"]
json.field "tokens", captcha["tokens"]
end
end
return response
end end
return response
end end
# create user # create user
sid = Base64.urlsafe_encode(Random::Secure.random_bytes(32)) sid = Base64.urlsafe_encode(Random::Secure.random_bytes(32))