Left some TODO's. Next thing is to check if the token is valid
This commit is contained in:
+6
-1
@@ -26,6 +26,7 @@ set(HEADERS
|
||||
include/metadata_retriever.h
|
||||
include/models.h
|
||||
include/token_manager.h
|
||||
include/types/scopes.h
|
||||
)
|
||||
|
||||
set (TAGLIB
|
||||
@@ -57,6 +58,10 @@ set(TAGLIB_HEADERS
|
||||
"${CMAKE_SOURCE_DIR}/3rdparty/taglib/taglib/xm"
|
||||
)
|
||||
|
||||
set(JWT_CPP_INCLUDE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/jwt-cpp/include
|
||||
)
|
||||
|
||||
set (ORM_DIR
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/ormpp
|
||||
)
|
||||
@@ -75,5 +80,5 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/authcredentials.json ${CMAKE_BINARY_D
|
||||
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/database.json ${CMAKE_BINARY_DIR}/bin/database.json COPYONLY)
|
||||
|
||||
add_executable(icarus ${SOURCES} ${HEADERS})
|
||||
target_include_directories(icarus PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/jwt-cpp/include)
|
||||
target_include_directories(icarus PUBLIC ${JWT_CPP_INCLUDE})
|
||||
target_link_libraries(icarus "-lstdc++fs" tag oatpp mysqlclient ${CONAN_LIBS} ${CPR_LIBRARIES})
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
#ifndef TOKEN_MANAGER_H_
|
||||
#define TOKEN_MANAGER_H_
|
||||
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
#include "models.h"
|
||||
#include "types/scopes.h"
|
||||
|
||||
class token_manager
|
||||
{
|
||||
@@ -12,6 +14,8 @@ public:
|
||||
|
||||
loginResult retrieve_token();
|
||||
loginResult retrieve_token(std::string_view);
|
||||
|
||||
bool is_token_valid(std::string&, Scope);
|
||||
private:
|
||||
auth_credentials parse_auth_credentials(std::string_view);
|
||||
};
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
#ifndef SCOPES_H_
|
||||
#define SCOPES_H_
|
||||
|
||||
enum class Scope
|
||||
{
|
||||
upload = 0,
|
||||
download
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -29,15 +29,16 @@ public:
|
||||
{
|
||||
OATPP_LOGI("icarus", "logging in");
|
||||
|
||||
std::cout << "user: " << usr->username->c_str() << std::endl;
|
||||
|
||||
token_manager tok;
|
||||
auto token = tok.retrieve_token(exe_path);
|
||||
|
||||
auto logRes = loginResultDto::createShared();
|
||||
logRes->access_token = token.access_token.c_str();
|
||||
logRes->id = 0; // TODO: change this later on to something meaningful
|
||||
logRes->username = usr->username->c_str();
|
||||
logRes->token = token.access_token.c_str();
|
||||
logRes->token_type = token.token_type.c_str();
|
||||
logRes->expiration = token.expiration;
|
||||
logRes->message = "Successful";
|
||||
|
||||
return createDtoResponse(Status::CODE_200, logRes);
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
|
||||
#include "managers/song_manager.h"
|
||||
#include "models.h"
|
||||
#include "token_manager.h"
|
||||
#include "types/scopes.h"
|
||||
|
||||
class songController : public oatpp::web::server::api::ApiController
|
||||
{
|
||||
@@ -30,6 +32,20 @@ public:
|
||||
ENDPOINT("POST", "/api/v1/song/data", songUpload,
|
||||
REQUEST(std::shared_ptr<IncomingRequest>, request))
|
||||
{
|
||||
auto authHeader = request->getHeader("Authorization");
|
||||
|
||||
OATPP_ASSERT_HTTP(authHeader, Status::CODE_403, "Nope");
|
||||
|
||||
auto auth = authHeader->std_str();
|
||||
|
||||
std::cout << "auth " << auth << std::endl;
|
||||
|
||||
token_manager tok;
|
||||
if (!tok.is_token_valid(auth, Scope::upload)) {
|
||||
// TODO: prevent user from moving forward
|
||||
// token did not have the specified scope (permission)
|
||||
}
|
||||
|
||||
auto mp = std::make_shared<oatpp::web::mime::multipart::Multipart>(request->getHeaders());
|
||||
|
||||
oatpp::web::mime::multipart::Reader mp_reader(mp.get());
|
||||
@@ -43,35 +59,16 @@ public:
|
||||
OATPP_ASSERT_HTTP(file, Status::CODE_400, "file is null");
|
||||
|
||||
auto stream = file->getInputStream();
|
||||
//char *buff = new char[file->getKnownSize()];
|
||||
auto buff = std::unique_ptr<char>(new char[file->getKnownSize()]);
|
||||
//std::vector<unsigned char> buff;
|
||||
//buff.reserve(file->getKnownSize());
|
||||
//std::string buff;
|
||||
//std::vector<unsigned char> buff;
|
||||
auto buffSize = stream->read(buff.get(), file->getKnownSize()-1);
|
||||
//auto buffSize = stream->read(&buff[0], file->getKnownSize()-1);
|
||||
std::cout << "buff size " << buffSize << std::endl;
|
||||
/**
|
||||
std::cout << "buff " << buff << std::endl;
|
||||
//std::cout << stream.get() << std::endl;
|
||||
*/
|
||||
auto buffSize = stream->read(buff.get(), file->getKnownSize());
|
||||
|
||||
std::vector<unsigned char> data(buff.get(), buff.get() + buffSize);
|
||||
std::cout << "size of data " << data.size() << std::endl;
|
||||
|
||||
Song sng;
|
||||
sng.data = std::move(data);
|
||||
//sng.data = std::move(buff);
|
||||
//std::cout << "data buff " << sng.data << std::endl;
|
||||
std::cout << "data size " << sng.data.size() << std::endl;
|
||||
|
||||
song_manager s_mgr(exe_path);
|
||||
s_mgr.saveSong(sng);
|
||||
/* */
|
||||
|
||||
//std::string fileStr = file->getInMemoryData()->c_str();
|
||||
//std::cout << fileStr << std::endl;
|
||||
//std::cout << "known size " << file->getKnownSize() << std::endl;
|
||||
//std::cout << "size " << fileStr.size() << std::endl;
|
||||
|
||||
return createResponse(Status::CODE_200, "OK");
|
||||
}
|
||||
|
||||
@@ -10,9 +10,12 @@ class loginResultDto : public oatpp::data::mapping::type::Object
|
||||
{
|
||||
DTO_INIT(loginResultDto, Object)
|
||||
|
||||
DTO_FIELD(String, access_token);
|
||||
DTO_FIELD(Int32, id);
|
||||
DTO_FIELD(String, username);
|
||||
DTO_FIELD(String, token);
|
||||
DTO_FIELD(String, token_type);
|
||||
DTO_FIELD(Int32, expiration);
|
||||
DTO_FIELD(String, message);
|
||||
};
|
||||
|
||||
class userDto : public oatpp::data::mapping::type::Object
|
||||
|
||||
@@ -55,6 +55,18 @@ loginResult token_manager::retrieve_token(std::string_view path)
|
||||
return lr;
|
||||
}
|
||||
|
||||
bool token_manager::is_token_valid(std::string& auth, Scope scope)
|
||||
{
|
||||
switch (scope) {
|
||||
case Scope::upload:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
auth_credentials token_manager::parse_auth_credentials(std::string_view path)
|
||||
{
|
||||
auto exe_path = fs::canonical(path).parent_path().string();
|
||||
|
||||
Reference in New Issue
Block a user