some changes

This commit is contained in:
kdeng00
2019-10-13 11:29:40 -04:00
parent 32ed611112
commit 024134b801
14 changed files with 204 additions and 132 deletions
+8 -16
View File
@@ -10,13 +10,13 @@
#include "oatpp/web/server/api/ApiController.hpp"
#include "dto/LoginResultDto.hpp"
#include "dto/conversion/DtoConversions.h"
#include "manager/TokenManager.h"
#include "manager/UserManager.h"
#include "model/Models.h"
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)
@@ -32,29 +32,21 @@ public:
OATPP_LOGI("icarus", "logging in");
manager::UserManager usrMgr(m_bConf);
auto user = manager::UserManager::userDtoConv(usr);
auto user = dto::conversion::DtoConversions::toUser(usr);
if (!usrMgr.doesUserExist(user)) {
if (!usrMgr.doesUserExist(user) || !usrMgr.validatePassword(user)) {
auto logRes = dto::LoginResultDto::createShared();
logRes->message = "invalid credentials";
std::cout << "user does not exist" << std::endl;
return createResponse(Status::CODE_401, "invalid credentials");
return createDtoResponse(Status::CODE_401, logRes);
}
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;
auto logRes = dto::conversion::DtoConversions::toLoginResultDto(user, token);
logRes->message = "Successful";
return createDtoResponse(Status::CODE_200, logRes);
+10 -9
View File
@@ -9,27 +9,28 @@
#include "oatpp/web/server/api/ApiController.hpp"
#include "dto/LoginResultDto.hpp"
#include "dto/conversion/DtoConversions.h"
#include "manager/UserManager.h"
#include "model/Models.h"
namespace controller
{
class RegisterController : public oatpp::web::server::api::ApiController
{
namespace controller {
class RegisterController : public oatpp::web::server::api::ApiController {
public:
RegisterController(const model::BinaryPath& bConf,
OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper)) :
OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper)) :
oatpp::web::server::api::ApiController(objectMapper), m_bConf(bConf) { }
#include OATPP_CODEGEN_BEGIN(ApiController)
ENDPOINT("POST", "api/v1/register", registerUser, BODY_DTO(dto::UserDto::ObjectWrapper, usr)) {
ENDPOINT("POST", "api/v1/register", registerUser,
BODY_DTO(dto::UserDto::ObjectWrapper, usr)) {
manager::UserManager usrMgr(m_bConf);
auto user = usrMgr.userDtoConv(usr);
auto user = dto::conversion::DtoConversions::toUser(usr);
usrMgr.registerUser(user);
auto res = usrMgr.registerUser(user);
auto registerResult = dto::conversion::DtoConversions::toRegisterResultDto(res);
return createResponse(Status::CODE_200, "Reg");
return createDtoResponse(Status::CODE_200, registerResult);
}
#include OATPP_CODEGEN_END(ApiController)
private:
+1 -2
View File
@@ -30,8 +30,7 @@
namespace fs = std::filesystem;
namespace controller {
class SongController : public oatpp::web::server::api::ApiController
{
class SongController : public oatpp::web::server::api::ApiController {
public:
SongController(std::string p, OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper))
: oatpp::web::server::api::ApiController(objectMapper), m_exe_path(p)
+14 -6
View File
@@ -4,12 +4,12 @@
#include "oatpp/core/data/mapping/type/Object.hpp"
#include "oatpp/core/macro/codegen.hpp"
namespace dto
{
#include "model/Models.h"
namespace dto {
#include OATPP_CODEGEN_BEGIN(DTO)
class LoginResultDto : public oatpp::data::mapping::type::Object
{
class LoginResultDto : public oatpp::data::mapping::type::Object {
DTO_INIT(LoginResultDto, Object)
DTO_FIELD(Int32, id);
@@ -20,8 +20,15 @@ namespace dto
DTO_FIELD(String, message);
};
class UserDto : public oatpp::data::mapping::type::Object
{
class RegisterResultDto : public oatpp::data::mapping::type::Object {
DTO_INIT(RegisterResultDto, Object)
DTO_FIELD(String, username);
DTO_FIELD(Boolean, registered);
DTO_FIELD(String, message);
};
class UserDto : public oatpp::data::mapping::type::Object {
DTO_INIT(UserDto, Object)
DTO_FIELD(Int32, userId);
@@ -34,6 +41,7 @@ namespace dto
};
#include OATPP_CODEGEN_END(DTO)
}
#endif
+4
View File
@@ -4,6 +4,8 @@
#include "oatpp/core/data/mapping/type/Object.hpp"
#include "oatpp/core/macro/codegen.hpp"
#include "model/Models.h"
namespace dto
{
#include OATPP_CODEGEN_BEGIN(DTO)
@@ -24,6 +26,8 @@ namespace dto
};
#include OATPP_CODEGEN_END(DTO)
}
#endif
+22
View File
@@ -0,0 +1,22 @@
#ifndef DTOCONVERSIONS_H_
#define DTOCONVERSIONS_H_
#include "dto/LoginResultDto.hpp"
#include "dto/SongDto.hpp"
#include "model/Models.h"
namespace dto { namespace conversion {
class DtoConversions {
public:
static dto::LoginResultDto::ObjectWrapper toLoginResultDto(const model::User&, const model::Token&);
static dto::RegisterResultDto::ObjectWrapper toRegisterResultDto(
const model::RegisterResult&);
static model::Song toSong(dto::SongDto::ObjectWrapper&);
static model::User toUser(dto::UserDto::ObjectWrapper&);
};
}}
#endif
+3 -4
View File
@@ -14,12 +14,11 @@
#include "type/Scopes.h"
namespace manager {
class TokenManager
{
class TokenManager {
public:
TokenManager();
model::LoginResult retrieveToken(const model::BinaryPath&);
model::Token retrieveToken(const model::BinaryPath&);
bool isTokenValid(std::string&, type::Scope);
bool testAuth(const model::BinaryPath&);
@@ -34,7 +33,7 @@ private:
std::vector<std::string> extractScopes(const jwt::decoded_jwt&&);
std::pair<bool, std::vector<std::string>> fetchAuthHeader(const std::string&);
bool tokenSupportsScope(const std::vector<std::string>, const std::string&&);
bool tokenSupportsScope(const std::vector<std::string>&, const std::string&&);
};
}
+3 -5
View File
@@ -7,18 +7,16 @@
#include "model/Models.h"
namespace manager {
class UserManager
{
class UserManager {
public:
UserManager(const model::BinaryPath&);
model::RegisterResult registerUser(model::User&);
bool doesUserExist(const model::User&);
bool validatePassword(const model::User&);
void printUser(const model::User&);
void registerUser(model::User&);
static model::User userDtoConv(dto::UserDto::ObjectWrapper&);
private:
model::BinaryPath m_bConf;
};
+34 -24
View File
@@ -5,8 +5,7 @@
#include <vector>
namespace model {
struct Song
{
struct Song {
Song() = default;
Song(const int id) : id(id) { }
@@ -28,8 +27,7 @@ struct Song
int yearId;
};
struct Artist
{
struct Artist {
Artist() = default;
Artist(const Song& song)
{
@@ -42,8 +40,7 @@ struct Artist
std::string artist;
};
struct Album
{
struct Album {
Album() = default;
Album(const Song& song)
{
@@ -59,8 +56,7 @@ struct Album
std::vector<Song> songs;
};
struct Genre
{
struct Genre {
Genre() = default;
Genre(const Song& song)
{
@@ -74,8 +70,7 @@ struct Genre
};
struct Year
{
struct Year {
Year() = default;
Year(const Song& song)
{
@@ -88,8 +83,7 @@ struct Year
int year;
};
struct Cover
{
struct Cover {
Cover() = default;
Cover(const Song& song)
{
@@ -105,8 +99,22 @@ struct Cover
std::vector<unsigned char> data;
};
struct LoginResult
{
class Token {
public:
Token() = default;
Token(const std::string& accessToken) :
accessToken(accessToken) { }
Token(const std::string& accessToken, const std::string& tokenType,
const int expiration) :
accessToken(accessToken), tokenType(tokenType),
expiration(expiration) { }
std::string accessToken;
int expiration;
std::string tokenType;
};
struct LoginResult {
int userId;
std::string username;
std::string accessToken;
@@ -115,8 +123,14 @@ struct LoginResult
int expiration;
};
struct User
{
class RegisterResult {
public:
std::string username;
bool registered;
std::string message;
};
struct User {
int id;
std::string firstname;
std::string lastname;
@@ -126,16 +140,14 @@ struct User
std::string password;
};
struct PassSec
{
struct PassSec {
int id;
std::string hashPassword;
std::string salt;
int userId;
};
struct AuthCredentials
{
struct AuthCredentials {
std::string domain;
std::string apiIdentifier;
std::string clientId;
@@ -144,16 +156,14 @@ struct AuthCredentials
std::string endpoint;
};
struct DatabaseConnection
{
struct DatabaseConnection {
std::string server;
std::string username;
std::string password;
std::string database;
};
struct BinaryPath
{
struct BinaryPath {
BinaryPath() = default;
BinaryPath(const char *p) : path(std::move(p)) { }
BinaryPath(std::string& p) : path(p) { }