added user authentication and updated README.md
This commit is contained in:
@@ -11,44 +11,59 @@
|
||||
|
||||
#include "dto/LoginResultDto.hpp"
|
||||
#include "manager/TokenManager.h"
|
||||
#include "manager/UserManager.h"
|
||||
#include "model/Models.h"
|
||||
|
||||
namespace controller
|
||||
namespace controller {
|
||||
class LoginController : public oatpp::web::server::api::ApiController
|
||||
{
|
||||
class LoginController : public oatpp::web::server::api::ApiController
|
||||
{
|
||||
public:
|
||||
LoginController(std::string p, OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper))
|
||||
: oatpp::web::server::api::ApiController(objectMapper), exe_path(p)
|
||||
{ }
|
||||
LoginController(const model::BinaryPath& bConf, OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper))
|
||||
:oatpp::web::server::api::ApiController(objectMapper), m_bConf(bConf)
|
||||
{ }
|
||||
public:
|
||||
LoginController(std::string p, OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper))
|
||||
: oatpp::web::server::api::ApiController(objectMapper), exe_path(p)
|
||||
{ }
|
||||
LoginController(const model::BinaryPath& bConf, OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper))
|
||||
:oatpp::web::server::api::ApiController(objectMapper), m_bConf(bConf)
|
||||
{ }
|
||||
|
||||
|
||||
#include OATPP_CODEGEN_BEGIN(ApiController)
|
||||
#include OATPP_CODEGEN_BEGIN(ApiController)
|
||||
|
||||
ENDPOINT("POST", "/api/v1/login", data, BODY_DTO(dto::UserDto::ObjectWrapper, usr)) {
|
||||
OATPP_LOGI("icarus", "logging in");
|
||||
ENDPOINT("POST", "/api/v1/login", data, BODY_DTO(dto::UserDto::ObjectWrapper, usr)) {
|
||||
OATPP_LOGI("icarus", "logging in");
|
||||
|
||||
manager::TokenManager tok;
|
||||
auto token = tok.retrieveToken(m_bConf);
|
||||
manager::UserManager usrMgr(m_bConf);
|
||||
auto user = manager::UserManager::userDtoConv(usr);
|
||||
|
||||
auto logRes = dto::LoginResultDto::createShared();
|
||||
logRes->id = 0; // TODO: change this later on to something meaningful
|
||||
logRes->username = usr->username->c_str();
|
||||
logRes->token = token.accessToken.c_str();
|
||||
logRes->token_type = token.tokenType.c_str();
|
||||
logRes->expiration = token.expiration;
|
||||
logRes->message = "Successful";
|
||||
|
||||
return createDtoResponse(Status::CODE_200, logRes);
|
||||
if (!usrMgr.doesUserExist(user)) {
|
||||
std::cout << "user does not exist" << std::endl;
|
||||
return createResponse(Status::CODE_401, "invalid credentials");
|
||||
}
|
||||
|
||||
#include OATPP_CODEGEN_END(ApiController)
|
||||
private:
|
||||
std::string exe_path;
|
||||
model::BinaryPath m_bConf;
|
||||
};
|
||||
std::cout << "user exists" << std::endl;
|
||||
|
||||
if (!usrMgr.validatePassword(user)) {
|
||||
std::cout << "password is not valid" << std::endl;
|
||||
return createResponse(Status::CODE_401, "invalid credentials");
|
||||
}
|
||||
|
||||
manager::TokenManager tok;
|
||||
auto token = tok.retrieveToken(m_bConf);
|
||||
|
||||
auto logRes = dto::LoginResultDto::createShared();
|
||||
logRes->id = 0; // TODO: change this later on to something meaningful or remove it
|
||||
logRes->username = usr->username->c_str();
|
||||
logRes->token = token.accessToken.c_str();
|
||||
logRes->token_type = token.tokenType.c_str();
|
||||
logRes->expiration = token.expiration;
|
||||
logRes->message = "Successful";
|
||||
|
||||
return createDtoResponse(Status::CODE_200, logRes);
|
||||
}
|
||||
|
||||
#include OATPP_CODEGEN_END(ApiController)
|
||||
private:
|
||||
std::string exe_path;
|
||||
model::BinaryPath m_bConf;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -7,36 +7,51 @@
|
||||
|
||||
#include "database/BaseRepository.h"
|
||||
#include "model/Models.h"
|
||||
#include "type/SaltFilter.h"
|
||||
#include "type/UserFilter.h"
|
||||
|
||||
namespace database {
|
||||
class UserRepository : BaseRepository {
|
||||
public:
|
||||
UserRepository(const model::BinaryPath&);
|
||||
class UserRepository : BaseRepository {
|
||||
public:
|
||||
UserRepository(const model::BinaryPath&);
|
||||
|
||||
model::User retrieveUserRecord(model::User&, type::UserFilter);
|
||||
model::User retrieveUserRecord(model::User&, type::UserFilter);
|
||||
model::PassSec retrieverUserSaltRecord(model::PassSec&, type::SaltFilter);
|
||||
|
||||
void saveUserRecord(const model::User&);
|
||||
private:
|
||||
struct UserLengths;
|
||||
struct UserLengths
|
||||
{
|
||||
unsigned long firstnameLength;
|
||||
unsigned long lastnameLength;
|
||||
unsigned long phoneLength;
|
||||
unsigned long emailLength;
|
||||
unsigned long usernameLength;
|
||||
unsigned long passwordLength;
|
||||
};
|
||||
bool doesUserRecordExist(const model::User&, type::UserFilter);
|
||||
|
||||
std::shared_ptr<MYSQL_BIND> insertUserValues(const model::User&, std::shared_ptr<UserLengths>);
|
||||
std::shared_ptr<MYSQL_BIND> valueBind(model::User&, std::tuple<char*, char*, char*, char*, char*, char*>&);
|
||||
std::shared_ptr<UserLengths> fetchUserLengths(const model::User&);
|
||||
void saveUserRecord(const model::User&);
|
||||
void saveUserSalt(const model::PassSec&);
|
||||
private:
|
||||
struct UserLengths;
|
||||
struct SaltLengths;
|
||||
|
||||
std::tuple<char*, char*, char*, char*, char*, char*> fetchUV();
|
||||
|
||||
model::User parseRecord(MYSQL_STMT*);
|
||||
struct UserLengths
|
||||
{
|
||||
unsigned long firstnameLength;
|
||||
unsigned long lastnameLength;
|
||||
unsigned long phoneLength;
|
||||
unsigned long emailLength;
|
||||
unsigned long usernameLength;
|
||||
unsigned long passwordLength;
|
||||
};
|
||||
struct SaltLengths
|
||||
{
|
||||
unsigned long saltLength;
|
||||
};
|
||||
|
||||
std::shared_ptr<MYSQL_BIND> insertUserValues(const model::User&, std::shared_ptr<UserLengths>);
|
||||
std::shared_ptr<MYSQL_BIND> insertSaltValues(const model::PassSec&, std::shared_ptr<SaltLengths>);
|
||||
std::shared_ptr<MYSQL_BIND> valueBind(model::User&, std::tuple<char*, char*, char*, char*, char*, char*>&);
|
||||
std::shared_ptr<MYSQL_BIND> saltValueBind(model::PassSec&, char*);
|
||||
std::shared_ptr<UserLengths> fetchUserLengths(const model::User&);
|
||||
std::shared_ptr<SaltLengths> fetchSaltLengths(const model::PassSec&);
|
||||
|
||||
std::tuple<char*, char*, char*, char*, char*, char*> fetchUV();
|
||||
|
||||
model::User parseRecord(MYSQL_STMT*);
|
||||
model::PassSec parseSaltRecord(MYSQL_STMT*);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -6,20 +6,22 @@
|
||||
#include "dto/LoginResultDto.hpp"
|
||||
#include "model/Models.h"
|
||||
|
||||
namespace manager
|
||||
namespace manager {
|
||||
class UserManager
|
||||
{
|
||||
class UserManager
|
||||
{
|
||||
public:
|
||||
UserManager(const model::BinaryPath&);
|
||||
public:
|
||||
UserManager(const model::BinaryPath&);
|
||||
|
||||
model::User userDtoConv(dto::UserDto::ObjectWrapper&);
|
||||
bool doesUserExist(const model::User&);
|
||||
bool validatePassword(const model::User&);
|
||||
|
||||
void printUser(const model::User&);
|
||||
void registerUser(model::User&);
|
||||
private:
|
||||
model::BinaryPath m_bConf;
|
||||
};
|
||||
void printUser(const model::User&);
|
||||
void registerUser(model::User&);
|
||||
|
||||
static model::User userDtoConv(dto::UserDto::ObjectWrapper&);
|
||||
private:
|
||||
model::BinaryPath m_bConf;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
+135
-134
@@ -4,162 +4,163 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace model
|
||||
namespace model {
|
||||
struct Song
|
||||
{
|
||||
struct Song
|
||||
Song() = default;
|
||||
Song(const int id) : id(id) { }
|
||||
|
||||
int id;
|
||||
std::string title;
|
||||
std::string artist;
|
||||
std::string album;
|
||||
std::string genre;
|
||||
int year;
|
||||
int duration;
|
||||
int track;
|
||||
int disc;
|
||||
std::string songPath;
|
||||
std::vector<unsigned char> data;
|
||||
int coverArtId;
|
||||
int artistId;
|
||||
int albumId;
|
||||
int genreId;
|
||||
int yearId;
|
||||
};
|
||||
|
||||
struct Artist
|
||||
{
|
||||
Artist() = default;
|
||||
Artist(const Song& song)
|
||||
{
|
||||
Song() = default;
|
||||
Song(const int id) : id(id) { }
|
||||
id = song.artistId;
|
||||
artist = song.artist;
|
||||
}
|
||||
Artist(const int id) : id(id) { }
|
||||
|
||||
int id;
|
||||
std::string title;
|
||||
std::string artist;
|
||||
std::string album;
|
||||
std::string genre;
|
||||
int year;
|
||||
int duration;
|
||||
int track;
|
||||
int disc;
|
||||
std::string songPath;
|
||||
std::vector<unsigned char> data;
|
||||
int coverArtId;
|
||||
int artistId;
|
||||
int albumId;
|
||||
int genreId;
|
||||
int yearId;
|
||||
};
|
||||
int id;
|
||||
std::string artist;
|
||||
};
|
||||
|
||||
struct Artist
|
||||
struct Album
|
||||
{
|
||||
Album() = default;
|
||||
Album(const Song& song)
|
||||
{
|
||||
Artist() = default;
|
||||
Artist(const Song& song)
|
||||
{
|
||||
id = song.artistId;
|
||||
artist = song.artist;
|
||||
}
|
||||
Artist(const int id) : id(id) { }
|
||||
id = song.albumId;
|
||||
title = song.album;
|
||||
year = song.year;
|
||||
}
|
||||
Album(const int id) : id(id) { }
|
||||
|
||||
int id;
|
||||
std::string artist;
|
||||
};
|
||||
int id;
|
||||
std::string title;
|
||||
int year;
|
||||
std::vector<Song> songs;
|
||||
};
|
||||
|
||||
struct Album
|
||||
struct Genre
|
||||
{
|
||||
Genre() = default;
|
||||
Genre(const Song& song)
|
||||
{
|
||||
Album() = default;
|
||||
Album(const Song& song)
|
||||
{
|
||||
id = song.albumId;
|
||||
title = song.album;
|
||||
year = song.year;
|
||||
}
|
||||
Album(const int id) : id(id) { }
|
||||
id = song.genreId;
|
||||
category = song.genre;
|
||||
}
|
||||
Genre(const int id) : id(id) { }
|
||||
|
||||
int id;
|
||||
std::string title;
|
||||
int year;
|
||||
std::vector<Song> songs;
|
||||
};
|
||||
int id;
|
||||
std::string category;
|
||||
|
||||
struct Genre
|
||||
};
|
||||
|
||||
struct Year
|
||||
{
|
||||
Year() = default;
|
||||
Year(const Song& song)
|
||||
{
|
||||
Genre() = default;
|
||||
Genre(const Song& song)
|
||||
{
|
||||
id = song.genreId;
|
||||
category = song.genre;
|
||||
}
|
||||
Genre(const int id) : id(id) { }
|
||||
id = song.yearId;
|
||||
year = song.year;
|
||||
}
|
||||
Year(const int id) : id(id) { }
|
||||
|
||||
int id;
|
||||
std::string category;
|
||||
int id;
|
||||
int year;
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
struct Year
|
||||
struct Cover
|
||||
{
|
||||
Cover() = default;
|
||||
Cover(const Song& song)
|
||||
{
|
||||
Year() = default;
|
||||
Year(const Song& song)
|
||||
{
|
||||
id = song.yearId;
|
||||
year = song.year;
|
||||
}
|
||||
Year(const int id) : id(id) { }
|
||||
id = song.coverArtId;
|
||||
songTitle = song.title;
|
||||
}
|
||||
Cover(const int id) : id(id) { }
|
||||
|
||||
int id;
|
||||
int year;
|
||||
};
|
||||
int id;
|
||||
std::string songTitle;
|
||||
std::string imagePath;
|
||||
// Not being used but it should be
|
||||
std::vector<unsigned char> data;
|
||||
};
|
||||
|
||||
struct Cover
|
||||
{
|
||||
Cover() = default;
|
||||
Cover(const Song& song)
|
||||
{
|
||||
id = song.coverArtId;
|
||||
songTitle = song.title;
|
||||
}
|
||||
Cover(const int id) : id(id) { }
|
||||
struct LoginResult
|
||||
{
|
||||
int userId;
|
||||
std::string username;
|
||||
std::string accessToken;
|
||||
std::string tokenType;
|
||||
std::string message;
|
||||
int expiration;
|
||||
};
|
||||
|
||||
int id;
|
||||
std::string songTitle;
|
||||
std::string imagePath;
|
||||
// Not being used but it should be
|
||||
std::vector<unsigned char> data;
|
||||
};
|
||||
struct User
|
||||
{
|
||||
int id;
|
||||
std::string firstname;
|
||||
std::string lastname;
|
||||
std::string email;
|
||||
std::string phone;
|
||||
std::string username;
|
||||
std::string password;
|
||||
};
|
||||
|
||||
struct LoginResult
|
||||
{
|
||||
int userId;
|
||||
std::string username;
|
||||
std::string accessToken;
|
||||
std::string tokenType;
|
||||
std::string message;
|
||||
int expiration;
|
||||
};
|
||||
struct PassSec
|
||||
{
|
||||
int id;
|
||||
std::string hashPassword;
|
||||
std::string salt;
|
||||
int userId;
|
||||
};
|
||||
|
||||
struct User
|
||||
{
|
||||
int id;
|
||||
std::string firstname;
|
||||
std::string lastname;
|
||||
std::string email;
|
||||
std::string phone;
|
||||
std::string username;
|
||||
std::string password;
|
||||
};
|
||||
struct AuthCredentials
|
||||
{
|
||||
std::string domain;
|
||||
std::string apiIdentifier;
|
||||
std::string clientId;
|
||||
std::string clientSecret;
|
||||
std::string uri;
|
||||
std::string endpoint;
|
||||
};
|
||||
|
||||
struct PassSec
|
||||
{
|
||||
std::string hashPassword;
|
||||
std::string salt;
|
||||
};
|
||||
struct DatabaseConnection
|
||||
{
|
||||
std::string server;
|
||||
std::string username;
|
||||
std::string password;
|
||||
std::string database;
|
||||
};
|
||||
|
||||
struct AuthCredentials
|
||||
{
|
||||
std::string domain;
|
||||
std::string apiIdentifier;
|
||||
std::string clientId;
|
||||
std::string clientSecret;
|
||||
std::string uri;
|
||||
std::string endpoint;
|
||||
};
|
||||
struct BinaryPath
|
||||
{
|
||||
BinaryPath() = default;
|
||||
BinaryPath(const char *p) : path(std::move(p)) { }
|
||||
BinaryPath(std::string& p) : path(p) { }
|
||||
BinaryPath(const std::string& p) : path(p) { }
|
||||
|
||||
struct DatabaseConnection
|
||||
{
|
||||
std::string server;
|
||||
std::string username;
|
||||
std::string password;
|
||||
std::string database;
|
||||
};
|
||||
|
||||
struct BinaryPath
|
||||
{
|
||||
BinaryPath() = default;
|
||||
BinaryPath(const char *p) : path(std::move(p)) { }
|
||||
BinaryPath(std::string& p) : path(p) { }
|
||||
BinaryPath(const std::string& p) : path(p) { }
|
||||
|
||||
std::string path;
|
||||
};
|
||||
std::string path;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -5,7 +5,8 @@ namespace type {
|
||||
enum class SaltFilter
|
||||
{
|
||||
id = 0,
|
||||
salt
|
||||
salt,
|
||||
userId
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -6,22 +6,21 @@
|
||||
|
||||
#include "model/Models.h"
|
||||
|
||||
namespace utility
|
||||
namespace utility {
|
||||
class MetadataRetriever
|
||||
{
|
||||
class MetadataRetriever
|
||||
{
|
||||
public:
|
||||
model::Song retrieveMetadata(std::string&);
|
||||
public:
|
||||
model::Song retrieveMetadata(std::string&);
|
||||
|
||||
model::Cover updateCoverArt(const model::Song&, model::Cover&, const std::string&);
|
||||
model::Cover applyStockCoverArt(const model::Song&, model::Cover&, const std::string&);
|
||||
model::Cover applyCoverArt(const model::Song&, model::Cover&);
|
||||
model::Cover updateCoverArt(const model::Song&, model::Cover&, const std::string&);
|
||||
model::Cover applyStockCoverArt(const model::Song&, model::Cover&, const std::string&);
|
||||
model::Cover applyCoverArt(const model::Song&, model::Cover&);
|
||||
|
||||
bool songContainsCoverArt(const model::Song&);
|
||||
bool songContainsCoverArt(const model::Song&);
|
||||
|
||||
void updateMetadata(model::Song&, const model::Song&);
|
||||
private:
|
||||
};
|
||||
void updateMetadata(model::Song&, const model::Song&);
|
||||
private:
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -6,12 +6,14 @@
|
||||
#include "model/Models.h"
|
||||
|
||||
namespace utility {
|
||||
class PasswordEncryption {
|
||||
public:
|
||||
model::PassSec hashPassword(const model::User&);
|
||||
private:
|
||||
int saltSize();
|
||||
};
|
||||
class PasswordEncryption {
|
||||
public:
|
||||
model::PassSec hashPassword(const model::User&);
|
||||
|
||||
bool isPasswordValid(const model::User&, const model::PassSec&);
|
||||
private:
|
||||
int saltSize();
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user