some changes
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
#include "dto/conversion/DtoConversions.h"
|
||||
|
||||
namespace dto { namespace conversion {
|
||||
dto::LoginResultDto::ObjectWrapper DtoConversions::toLoginResultDto(const model::User& user,
|
||||
const model::Token& token) {
|
||||
auto logRes = dto::LoginResultDto::createShared();
|
||||
logRes->username = user.username.c_str();
|
||||
logRes->token = token.accessToken.c_str();
|
||||
logRes->token_type = token.tokenType.c_str();
|
||||
logRes->expiration = token.expiration;
|
||||
|
||||
return logRes;
|
||||
}
|
||||
|
||||
|
||||
dto::RegisterResultDto::ObjectWrapper DtoConversions::toRegisterResultDto(
|
||||
const model::RegisterResult& regRes) {
|
||||
auto result = dto::RegisterResultDto::createShared();
|
||||
result->message = regRes.message.c_str();
|
||||
result->registered = regRes.registered;
|
||||
result->username = regRes.username.c_str();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
model::Song DtoConversions::toSong(dto::SongDto::ObjectWrapper& songDto) {
|
||||
model::Song song;
|
||||
song.id = (songDto->id.getPtr() == nullptr) ? 0 : songDto->id->getValue();
|
||||
song.title = (songDto->title == nullptr) ? "" : songDto->title->c_str();
|
||||
song.album = (songDto->album == nullptr) ? "" : songDto->album->c_str();
|
||||
song.artist = (songDto->artist == nullptr) ? "" : songDto->artist->c_str();
|
||||
song.genre = (songDto->genre == nullptr) ? "" : songDto->genre->c_str();
|
||||
song.year = (songDto->year.getPtr() == nullptr) ? 0 : songDto->year->getValue();
|
||||
song.track = (songDto->track.getPtr() == nullptr) ? 0 : songDto->track->getValue();
|
||||
song.disc = (songDto->disc.getPtr() == nullptr) ? 0 : songDto->disc->getValue();
|
||||
|
||||
return song;
|
||||
}
|
||||
|
||||
|
||||
model::User DtoConversions::toUser(dto::UserDto::ObjectWrapper& userDto) {
|
||||
model::User user;
|
||||
user.id = (userDto->userId.getPtr() == nullptr) ? 0 : userDto->userId->getValue();
|
||||
user.firstname = (userDto->firstname == nullptr) ? "" : userDto->firstname->c_str();
|
||||
user.lastname = (userDto->lastname == nullptr) ? "" : userDto->lastname->c_str();
|
||||
user.phone = (userDto->phone == nullptr) ? "" : userDto->phone->c_str();
|
||||
user.email = (userDto->email == nullptr) ? "" : userDto->email->c_str();
|
||||
user.username = (userDto->username == nullptr) ? "" : userDto->username->c_str();
|
||||
user.password = (userDto->password == nullptr) ? "" : userDto->password->c_str();
|
||||
|
||||
return user;
|
||||
}
|
||||
}}
|
||||
@@ -308,8 +308,8 @@ void manager::SongManager::saveMisc(model::Song& song)
|
||||
auto musicRootPath = pathConfigContent["root_music_path"].get<std::string>();
|
||||
|
||||
auto cov = covMgr.saveCover(song);
|
||||
const auto songPath = createSongPath(song);
|
||||
|
||||
auto songPath = createSongPath(song);
|
||||
if (fs::exists(songPath)) {
|
||||
std::cout << "deleting old song with the same metadata" << std::endl;
|
||||
fs::remove(songPath);
|
||||
|
||||
@@ -19,28 +19,21 @@ TokenManager::TokenManager()
|
||||
}
|
||||
|
||||
|
||||
model::LoginResult TokenManager::retrieveToken(const model::BinaryPath& bConf)
|
||||
model::Token TokenManager::retrieveToken(const model::BinaryPath& bConf)
|
||||
{
|
||||
auto cred = parseAuthCredentials(bConf);
|
||||
|
||||
auto reqObj = createTokenBody(cred);
|
||||
|
||||
std::string uri{cred.uri};
|
||||
uri.append("/");
|
||||
uri.append(cred.endpoint);
|
||||
|
||||
auto r = cpr::Post(cpr::Url{uri},
|
||||
cpr::Body{reqObj.dump()},
|
||||
cpr::Header{{"Content-Type", "application/json"},
|
||||
{"Connection", "keep-alive"}});
|
||||
|
||||
|
||||
auto r = sendRequest(uri, reqObj);
|
||||
auto postRes = nlohmann::json::parse(r.text);
|
||||
|
||||
model::LoginResult lr;
|
||||
lr.accessToken = postRes["access_token"].get<std::string>();
|
||||
lr.tokenType = postRes["token_type"].get<std::string>();
|
||||
lr.expiration = postRes["expires_in"].get<int>();
|
||||
model::Token lr(std::move(postRes["access_token"].get<std::string>()),
|
||||
std::move(postRes["token_type"].get<std::string>()),
|
||||
postRes["expires_in"].get<int>());
|
||||
|
||||
return lr;
|
||||
}
|
||||
@@ -129,6 +122,7 @@ nlohmann::json TokenManager::createTokenBody(const model::AuthCredentials& auth)
|
||||
}
|
||||
|
||||
|
||||
[[depreacted("use the other function with the same name")]]
|
||||
model::AuthCredentials TokenManager::parseAuthCredentials(std::string_view path)
|
||||
{
|
||||
auto exe_path = DirectoryManager::configPath(path);
|
||||
@@ -165,7 +159,7 @@ std::vector<std::string> TokenManager::extractScopes(const jwt::decoded_jwt&& de
|
||||
{
|
||||
std::vector<std::string> scopes;
|
||||
|
||||
for (auto d : decoded.get_payload_claims()) {
|
||||
for (auto& d : decoded.get_payload_claims()) {
|
||||
if (d.first.compare("scope") == 0) {
|
||||
std::cout << "found scope" << std::endl;
|
||||
std::string allScopes(d.second.to_json().get<std::string>());
|
||||
@@ -188,7 +182,7 @@ std::pair<bool, std::vector<std::string>> TokenManager::fetchAuthHeader(const st
|
||||
|
||||
bool foundBearer = false;
|
||||
if (std::any_of(authHeader.begin(), authHeader.end(),
|
||||
[](std::string word) {
|
||||
[&](std::string_view word) {
|
||||
return (word.compare("Bearer") == 0);
|
||||
})) {
|
||||
std::cout << "Bearer found" << std::endl;
|
||||
@@ -199,11 +193,11 @@ std::pair<bool, std::vector<std::string>> TokenManager::fetchAuthHeader(const st
|
||||
}
|
||||
|
||||
|
||||
bool TokenManager::tokenSupportsScope(const std::vector<std::string> scopes,
|
||||
bool TokenManager::tokenSupportsScope(const std::vector<std::string>& scopes,
|
||||
const std::string&& scope)
|
||||
{
|
||||
return std::any_of(scopes.begin(), scopes.end(),
|
||||
[&](std::string foundScope) {
|
||||
[&](std::string_view foundScope) {
|
||||
return (foundScope.compare(scope) == 0);
|
||||
});
|
||||
}
|
||||
|
||||
+38
-49
@@ -9,45 +9,10 @@
|
||||
namespace manager {
|
||||
UserManager::UserManager(const model::BinaryPath& bConf) : m_bConf(bConf) { }
|
||||
|
||||
bool UserManager::doesUserExist(const model::User& user)
|
||||
{
|
||||
database::UserRepository userRepo(m_bConf);
|
||||
|
||||
return userRepo.doesUserRecordExist(user, type::UserFilter::username);
|
||||
}
|
||||
|
||||
bool UserManager::validatePassword(const model::User& user)
|
||||
{
|
||||
database::UserRepository userRepo(m_bConf);
|
||||
model::User usr;
|
||||
usr.username = user.username;
|
||||
usr = userRepo.retrieveUserRecord(usr, type::UserFilter::username);
|
||||
|
||||
model::PassSec userSec;
|
||||
userSec.userId = usr.id;
|
||||
userSec = userRepo.retrieverUserSaltRecord(userSec, type::SaltFilter::userId);
|
||||
userSec.hashPassword = usr.password;
|
||||
|
||||
utility::PasswordEncryption passEnc;
|
||||
|
||||
return passEnc.isPasswordValid(user, userSec);
|
||||
}
|
||||
|
||||
|
||||
void UserManager::printUser(const model::User& user)
|
||||
{
|
||||
std::cout << "\nuser info" << std::endl;
|
||||
std::cout << "id: " << user.id << std::endl;
|
||||
std::cout << "firstname: " << user.firstname << std::endl;
|
||||
std::cout << "lastname: " << user.lastname << std::endl;
|
||||
std::cout << "phone: " << user.phone << std::endl;
|
||||
std::cout << "email: " << user.email << std::endl;
|
||||
std::cout << "username: " << user.username << std::endl;
|
||||
std::cout << "password: " << user.password<< std::endl;
|
||||
}
|
||||
|
||||
void UserManager::registerUser(model::User& user)
|
||||
{
|
||||
model::RegisterResult UserManager::registerUser(model::User& user) {
|
||||
model::RegisterResult result;
|
||||
result.username = user.username;
|
||||
printUser(user);
|
||||
|
||||
utility::PasswordEncryption passEnc;
|
||||
@@ -66,22 +31,46 @@ void UserManager::registerUser(model::User& user)
|
||||
hashed.userId = usr.id;
|
||||
|
||||
usrRepo.saveUserSalt(hashed);
|
||||
result.registered = true;
|
||||
result.message = "successfully registered";
|
||||
printUser(usr);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
model::User UserManager::userDtoConv(dto::UserDto::ObjectWrapper& userDto)
|
||||
{
|
||||
model::User user;
|
||||
bool UserManager::doesUserExist(const model::User& user) {
|
||||
database::UserRepository userRepo(m_bConf);
|
||||
|
||||
user.id = (userDto->userId.getPtr() == nullptr) ? 0 : userDto->userId->getValue();
|
||||
user.firstname = (userDto->firstname == nullptr) ? "" : userDto->firstname->c_str();
|
||||
user.lastname = (userDto->lastname == nullptr) ? "" : userDto->lastname->c_str();
|
||||
user.phone = (userDto->phone == nullptr) ? "" : userDto->phone->c_str();
|
||||
user.email = (userDto->email == nullptr) ? "" : userDto->email->c_str();
|
||||
user.username = (userDto->username == nullptr) ? "" : userDto->username->c_str();
|
||||
user.password = (userDto->password == nullptr) ? "" : userDto->password->c_str();
|
||||
return userRepo.doesUserRecordExist(user, type::UserFilter::username);
|
||||
}
|
||||
|
||||
return user;
|
||||
bool UserManager::validatePassword(const model::User& user) {
|
||||
database::UserRepository userRepo(m_bConf);
|
||||
model::User usr;
|
||||
usr.username = user.username;
|
||||
usr = userRepo.retrieveUserRecord(usr, type::UserFilter::username);
|
||||
|
||||
model::PassSec userSec;
|
||||
userSec.userId = usr.id;
|
||||
userSec = userRepo.retrieverUserSaltRecord(userSec, type::SaltFilter::userId);
|
||||
userSec.hashPassword = usr.password;
|
||||
|
||||
utility::PasswordEncryption passEnc;
|
||||
|
||||
return passEnc.isPasswordValid(user, userSec);
|
||||
}
|
||||
|
||||
|
||||
void UserManager::printUser(const model::User& user) {
|
||||
std::cout << "\nuser info" << std::endl;
|
||||
std::cout << "id: " << user.id << std::endl;
|
||||
std::cout << "firstname: " << user.firstname << std::endl;
|
||||
std::cout << "lastname: " << user.lastname << std::endl;
|
||||
std::cout << "phone: " << user.phone << std::endl;
|
||||
std::cout << "email: " << user.email << std::endl;
|
||||
std::cout << "username: " << user.username << std::endl;
|
||||
std::cout << "password: " << user.password<< std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user