pica/handler/login.cpp

80 lines
2.9 KiB
C++
Raw Normal View History

2023-12-30 22:42:11 +00:00
//SPDX-FileCopyrightText: 2023 Yury Gubich <blue@macaw.me>
//SPDX-License-Identifier: GPL-3.0-or-later
2023-12-22 23:25:20 +00:00
#include "login.h"
#include "server/server.h"
#include "database/exceptions.h"
2023-12-30 22:42:11 +00:00
Handler::Login::Login(std::weak_ptr<Server> server):
2023-12-22 23:25:20 +00:00
Handler("login", Request::Method::post),
server(server)
{}
void Handler::Login::handle(Request& request) {
std::map form = request.getForm();
std::map<std::string, std::string>::const_iterator itr = form.find("login");
if (itr == form.end())
return error(request, Result::noLogin, Response::Status::badRequest);
const std::string& login = itr->second;
if (login.empty())
return error(request, Result::emptyLogin, Response::Status::badRequest);
itr = form.find("password");
if (itr == form.end())
return error(request, Result::noPassword, Response::Status::badRequest);
const std::string& password = itr->second;
if (password.empty())
return error(request, Result::emptyPassword, Response::Status::badRequest);
2023-12-30 22:42:11 +00:00
std::shared_ptr<Server> srv = server.lock();
if (!srv)
return error(request, Result::unknownError, Response::Status::internalError);
2023-12-22 23:25:20 +00:00
bool success = false;
try {
2023-12-30 22:42:11 +00:00
success = srv->validatePassword(login, password);
2023-12-29 17:40:00 +00:00
} catch (const DB::NoLogin& e) {
2023-12-23 20:23:38 +00:00
std::cerr << "Exception on logging in:\n\t" << e.what() << std::endl;
2023-12-28 20:26:08 +00:00
return error(request, Result::wrongCredentials, Response::Status::badRequest);
2023-12-22 23:25:20 +00:00
} catch (const std::exception& e) {
2023-12-23 20:23:38 +00:00
std::cerr << "Exception on logging in:\n\t" << e.what() << std::endl;
2023-12-22 23:25:20 +00:00
return error(request, Result::unknownError, Response::Status::internalError);
} catch (...) {
2023-12-23 20:23:38 +00:00
std::cerr << "Unknown exception on ogging in" << std::endl;
2023-12-22 23:25:20 +00:00
return error(request, Result::unknownError, Response::Status::internalError);
}
if (!success)
2023-12-28 20:26:08 +00:00
return error(request, Result::wrongCredentials, Response::Status::badRequest);
2023-12-22 23:25:20 +00:00
nlohmann::json body = nlohmann::json::object();
body["result"] = Result::success;
2023-12-23 20:23:38 +00:00
try {
2023-12-30 22:42:11 +00:00
Session& session = srv->openSession(login);
2023-12-23 20:23:38 +00:00
body["accessToken"] = session.getAccessToken();
body["renewToken"] = session.getRenewToken();
} catch (const std::exception& e) {
std::cerr << "Exception on opening a session:\n\t" << e.what() << std::endl;
return error(request, Result::unknownError, Response::Status::internalError);
} catch (...) {
std::cerr << "Unknown exception on opening a session" << std::endl;
return error(request, Result::unknownError, Response::Status::internalError);
}
Response& res = request.createResponse();
2023-12-22 23:25:20 +00:00
res.setBody(body);
res.send();
}
void Handler::Login::error(Request& request, Result result, Response::Status code) {
Response& res = request.createResponse(code);
nlohmann::json body = nlohmann::json::object();
body["result"] = result;
res.setBody(body);
res.send();
}