diff --git a/src/invidious/routes/api/v1/authentication.cr b/src/invidious/routes/api/v1/authentication.cr index 2701418e..b942ef2e 100644 --- a/src/invidious/routes/api/v1/authentication.cr +++ b/src/invidious/routes/api/v1/authentication.cr @@ -1,24 +1,14 @@ module Invidious::Routes::API::V1::Authentication def self.register(env) env.response.content_type = "application/json" - if !CONFIG.registration_enabled - return error_json(400, "Registration has been disabled by administrator") - else - # check if user is registering or responding to captcha + if CONFIG.registration_enabled begin creds = Credentials.from_json(env.request.body || "{}") rescue JSON::SerializableError creds = nil end - - # begin - # captcha_response = CaptchaResponse.from_json(env.request.body || "{}") - # rescue JSON::SerializableError - # captcha_response = nil - # end - + # get user info if creds - # user is registering username = creds.username.downcase password = creds.password username = "" if username.nil? @@ -43,45 +33,59 @@ module Invidious::Routes::API::V1::Authentication username = username.byte_slice(0, 254) password = password.byte_slice(0, 55) - + # send captcha if enabled if CONFIG.captcha_enabled - # if captcha is enabled, send captcha - captcha = Invidious::User::Captcha.generate_text(HMAC_KEY) - # puts captcha - return captcha - end - end - # if captcha_response - # # process captcha response - # answer = captcha_response.answer - # answer = answer.lstrip('0') - # answer = OpenSSL::HMAC.hexdigest(:sha256, HMAC_KEY, answer) - # begin - # validate_request(, answer, env.request, HMAC_KEY, locale) - # rescue ex - # return error_jsonror(400, ex) - # end - # end - # create user if we made it past credentials and captcha - sid = Base64.urlsafe_encode(Random::Secure.random_bytes(32)) - user, sid = create_user(sid, username, password) - Invidious::Database::Users.insert(user) - Invidious::Database::SessionIDs.insert(sid, username) - # send user info - if token = Invidious::Database::SessionIDs.select_one(sid: sid) - response = JSON.build do |json| - json.object do - json.field "session", token[:session] - json.field "issued", token[:issued].to_unix + begin + captcha_response = CaptchaResponse.from_json(env.request.body || "{}") + rescue JSON::SerializableError + captcha_response = nil + end + # check if user is responding to captcha + if captcha_response + # process captcha response + # answer = captcha_response.answer + # answer = answer.lstrip('0') + # answer = OpenSSL::HMAC.hexdigest(:sha256, HMAC_KEY, answer) + # begin + # validate_request(, answer, env.request, HMAC_KEY, locale) + # rescue ex + # return error_jsonror(400, ex) + # end + else + # send captcha + captcha = Invidious::User::Captcha.generate_text(HMAC_KEY) + # puts captcha + return captcha end end - return response + # create user + sid = Base64.urlsafe_encode(Random::Secure.random_bytes(32)) + user, sid = create_user(sid, username, password) + Invidious::Database::Users.insert(user) + Invidious::Database::SessionIDs.insert(sid, username) + # send user info + if token = Invidious::Database::SessionIDs.select_one(sid: sid) + response = JSON.build do |json| + json.object do + json.field "session", token[:session] + json.field "issued", token[:issued].to_unix + end + end + return response + else + return error_json(500, "Token not found") + end else - return error_json(500, "Token not found") + return error_json(400, "No credentials") end + else + return error_json(400, "Registration has been disabled by administrator") end end + def self.captcha(env) + end + def self.login(env) env.response.content_type = "application/json" # locale = env.get("preferences").as(Preferences).locale @@ -131,6 +135,8 @@ struct CaptchaResponse include JSON::Serializable include YAML::Serializable + property username : String + property password : String property answer : String # property tokens : Array() end @@ -139,6 +145,14 @@ struct Credentials include JSON::Serializable include YAML::Serializable + property username : String + property password : String +end + +struct Login + include JSON::Serializable + include YAML::Serializable + property username : String property password : String property token : String