Skip to content

Commit

Permalink
merge to develpoment (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
nam20485 committed Feb 21, 2024
2 parents 90e3eac + 391fd37 commit acc9e3b
Show file tree
Hide file tree
Showing 13 changed files with 86 additions and 45 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ jobs:
with:
repository: ${{ github.repository }}
event-type: trigger_deploy_release_event
client-payload: |
client-payload: >
'{
"ref_name": "${{ github.ref_name }}",
"run_number": "${{ github.run_number }}",
Expand Down
38 changes: 20 additions & 18 deletions OdbDesignLib/App/BasicRequestAuthentication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,34 @@ using namespace Utils;

namespace Odb::Lib::App
{
BasicRequestAuthentication::BasicRequestAuthentication(bool disableAuthentication)
: RequestAuthenticationBase(disableAuthentication)
{
}

crow::response BasicRequestAuthentication::AuthenticateRequest(const crow::request& req)
{
// if running debug build AND in Local environment, bypass authentication
if (IsDebug() && IsLocal())
auto resp = RequestAuthenticationBase::AuthenticateRequest(req);
if (resp.code != crow::status::OK)
{
// 200 Authorized!
return crow::response(200, "Authorized");
}
const auto& authHeader = req.get_header_value(AUTHORIZATION_HEADER_NAME);
if (authHeader.empty()) return crow::response(401, "Unauthorized");

const auto& authHeader = req.get_header_value("Authorization");
if (authHeader.empty()) return crow::response(401, "Unauthorized");
auto authValue = authHeader.substr(6);
if (authValue.empty()) return crow::response(401, "Unauthorized");

auto authValue = authHeader.substr(6);
if (authValue.empty()) return crow::response(401, "Unauthorized");

auto authValueDecoded = crow::utility::base64decode(authValue, authValue.size());
if (authValueDecoded.empty()) return crow::response(401, "Unauthorized");
auto authValueDecoded = crow::utility::base64decode(authValue, authValue.size());
if (authValueDecoded.empty()) return crow::response(401, "Unauthorized");

auto seperatorPos = authValueDecoded.find(':');
if (seperatorPos == std::string::npos) return crow::response(401, "Unauthorized");
auto seperatorPos = authValueDecoded.find(':');
if (seperatorPos == std::string::npos) return crow::response(401, "Unauthorized");

auto username = authValueDecoded.substr(0, seperatorPos);
auto password = authValueDecoded.substr(seperatorPos + 1);
auto username = authValueDecoded.substr(0, seperatorPos);
auto password = authValueDecoded.substr(seperatorPos + 1);

//if (! VerifyCredentials(username, password)) return crow::response(403, "Invalid username or password");
auto resp = VerifyCredentials(username, password);
//if (! VerifyCredentials(username, password)) return crow::response(403, "Invalid username or password");
resp = VerifyCredentials(username, password);
}
return resp;
}

Expand Down
13 changes: 7 additions & 6 deletions OdbDesignLib/App/BasicRequestAuthentication.h
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
#pragma once

#include "IRequestAuthentication.h"
#include "RequestAuthenticationBase.h"
#include "../odbdesign_export.h"

namespace Odb::Lib::App
{
class ODBDESIGN_EXPORT BasicRequestAuthentication : public IRequestAuthentication
class ODBDESIGN_EXPORT BasicRequestAuthentication : public RequestAuthenticationBase
{
public:
//BasicRequestAuthentication()
//{
//}
BasicRequestAuthentication(bool disableAuthentication);

// Inherited via IRequestAuthentication
// Inherited via RequestAuthenticationBase
crow::response AuthenticateRequest(const crow::request& req) override;

private:

const inline static char AUTHORIZATION_HEADER_NAME[] = "Authorization";

crow::response VerifyCredentials(const std::string& username, const std::string& password);

const inline static char USERNAME_ENV_NAME[] = "ODBDESIGN_SERVER_REQUEST_USERNAME";
const inline static char PASSWORD_ENV_NAME[] = "ODBDESIGN_SERVER_REQUEST_PASSWORD";


};
}
6 changes: 3 additions & 3 deletions OdbDesignLib/App/IOdbServerApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "IOdbApp.h"
#include "../odbdesign_export.h"
#include "IRequestAuthentication.h"
#include "RequestAuthenticationBase.h"

namespace Odb::Lib::App
{
Expand All @@ -12,8 +12,8 @@ namespace Odb::Lib::App
virtual ~IOdbServerApp() {}

virtual CrowApp& crow_app() = 0;
virtual IRequestAuthentication& request_auth() = 0;
virtual void request_auth(std::unique_ptr<IRequestAuthentication> requestAuthentication) = 0;
virtual RequestAuthenticationBase& request_auth() = 0;
virtual void request_auth(std::unique_ptr<RequestAuthenticationBase> requestAuthentication) = 0;

protected:
IOdbServerApp() = default;
Expand Down
6 changes: 0 additions & 6 deletions OdbDesignLib/App/IRequestAuthentication.cpp

This file was deleted.

6 changes: 6 additions & 0 deletions OdbDesignLib/App/OdbDesignArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ namespace Odb::Lib::App
return boolArg("load-all", DEFAULT_LOAD_ALL);
}

bool OdbDesignArgs::disableAuthentication() const
{
return boolArg("disable-authentication", DEFAULT_DISABLE_AUTH);
}

std::string OdbDesignArgs::getUsageString() const
{
std::stringstream ss;
Expand All @@ -60,6 +65,7 @@ namespace Odb::Lib::App
ss << " --templates-dir <dir> Directory containing template files (default: " << DEFAULT_TEMPLATES_DIR << ")\n";
ss << " --load-design <design> Design to load on startup (default: " << DEFAULT_LOAD_DESIGN << ")\n";
ss << " --load-all Load all designs on startup (default: " << (DEFAULT_LOAD_ALL ? "true" : "false") << ")\n";
ss << " --disable-authentication Disable authentication (default: " << (DEFAULT_DISABLE_AUTH ? "true" : "false") << ")\n";
ss << " --help Print this help message\n";
return ss.str();
}
Expand Down
2 changes: 2 additions & 0 deletions OdbDesignLib/App/OdbDesignArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ namespace Odb::Lib::App
bool help() const;
std::string loadDesign() const;
bool loadAll() const;
bool disableAuthentication() const;

protected:
// Inherited via CommandLineArgs
Expand All @@ -32,6 +33,7 @@ namespace Odb::Lib::App
constexpr static const bool DEFAULT_HELP = false;
constexpr static const char* DEFAULT_LOAD_DESIGN = "";
constexpr static const bool DEFAULT_LOAD_ALL = false;
constexpr static const bool DEFAULT_DISABLE_AUTH = false;

};
}
4 changes: 2 additions & 2 deletions OdbDesignLib/App/OdbServerAppBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ namespace Odb::Lib::App
return m_crowApp;
}

IRequestAuthentication& OdbServerAppBase::request_auth()
RequestAuthenticationBase& OdbServerAppBase::request_auth()
{
return *m_pRequestAuthentication;
}

void OdbServerAppBase::request_auth(std::unique_ptr<IRequestAuthentication> pRequestAuthentication)
void OdbServerAppBase::request_auth(std::unique_ptr<RequestAuthenticationBase> pRequestAuthentication)
{
m_pRequestAuthentication = std::move(pRequestAuthentication);
}
Expand Down
8 changes: 4 additions & 4 deletions OdbDesignLib/App/OdbServerAppBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "OdbAppBase.h"
#include "RouteController.h"
#include "../odbdesign_export.h"
#include "IRequestAuthentication.h"
#include "RequestAuthenticationBase.h"
#include "BasicRequestAuthentication.h"

namespace Odb::Lib::App
Expand All @@ -16,8 +16,8 @@ namespace Odb::Lib::App

CrowApp& crow_app() override;

IRequestAuthentication& request_auth() override;
void request_auth(std::unique_ptr<IRequestAuthentication> pRequestAuthentication) override;
RequestAuthenticationBase& request_auth() override;
void request_auth(std::unique_ptr<RequestAuthenticationBase> pRequestAuthentication) override;

Utils::ExitCode Run() override;

Expand All @@ -35,7 +35,7 @@ namespace Odb::Lib::App
private:
CrowApp m_crowApp;
//crow::SimpleApp m_crowApp;
std::unique_ptr<IRequestAuthentication> m_pRequestAuthentication;
std::unique_ptr<RequestAuthenticationBase> m_pRequestAuthentication;

void register_routes();

Expand Down
31 changes: 31 additions & 0 deletions OdbDesignLib/App/RequestAuthenticationBase.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "RequestAuthenticationBase.h"
#include "macros.h"

using namespace Utils;

namespace Odb::Lib::App
{
RequestAuthenticationBase::RequestAuthenticationBase(bool disableAuthentication)
: m_disableAuthentication(disableAuthentication)
{
}

crow::response RequestAuthenticationBase::AuthenticateRequest(const crow::request& req)
{
// if running debug build AND in Local environment, bypass authentication
if (IsDebug() && IsLocal())
{
// 200 Authorized!
return crow::response(200, "Authorized");
}
else if (m_disableAuthentication)
{
// 200 Authorized!
return crow::response(200, "Authorized");
}
else
{
return crow::response(401, "Unauthorized");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@

namespace Odb::Lib::App
{
class ODBDESIGN_EXPORT IRequestAuthentication
class ODBDESIGN_EXPORT RequestAuthenticationBase
{
public:
virtual crow::response AuthenticateRequest(const crow::request& req) = 0;
virtual crow::response AuthenticateRequest(const crow::request& req);

protected:
RequestAuthenticationBase(bool disableAuthentication);

// pure virtual interface
IRequestAuthentication() = default;
RequestAuthenticationBase() = default;

bool m_disableAuthentication = false;

};
}
2 changes: 1 addition & 1 deletion OdbDesignLib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ add_library(OdbDesign SHARED
"ProtoBuf/via.pb.h" "ProtoBuf/via.pb.cc"
"ProtoBuf/package.pb.h" "ProtoBuf/package.pb.cc"
"FileModel/parse_error.h" "FileModel/parse_info.h" "FileModel/parse_info.cpp" "FileModel/parse_error.cpp" "FileModel/invalid_odb_error.h" "FileModel/invalid_odb_error.cpp" "ProtoBuf/common.pb.h" "ProtoBuf/common.pb.cc" "ProtoBuf/componentsfile.pb.h" "ProtoBuf/componentsfile.pb.cc" "FileModel/Design/PropertyRecord.h" "FileModel/Design/PropertyRecord.cpp" "FileModel/Design/FeaturesFile.h" "FileModel/Design/FeaturesFile.cpp" "FileModel/Design/ContourPolygon.h" "FileModel/Design/ContourPolygon.cpp" "FileModel/Design/SymbolName.h" "FileModel/Design/SymbolName.cpp" "FileModel/Design/SymbolsDirectory.h" "FileModel/Design/SymbolsDirectory.cpp" "FileModel/Design/StepHdrFile.h" "FileModel/Design/StepHdrFile.cpp" "FileModel/Design/AttributeLookupTable.h" "FileModel/Design/AttributeLookupTable.cpp"
"App/IRequestAuthentication.h" "App/IRequestAuthentication.cpp" "App/BasicRequestAuthentication.h" "App/BasicRequestAuthentication.cpp")
"App/RequestAuthenticationBase.h" "App/RequestAuthenticationBase.cpp" "App/BasicRequestAuthentication.h" "App/BasicRequestAuthentication.cpp")

# disable warning C4250: inheritance by dominance
target_compile_options(OdbDesign PUBLIC
Expand Down
3 changes: 2 additions & 1 deletion OdbDesignServer/OdbDesignServerApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ namespace Odb::App::Server
}

// add authentication
auto basicRequestAuth = std::make_unique<BasicRequestAuthentication>(BasicRequestAuthentication());
bool disableAuth = args().disableAuthentication();
auto basicRequestAuth = std::make_unique<BasicRequestAuthentication>(BasicRequestAuthentication(disableAuth));
request_auth(std::move(basicRequestAuth));

return true;
Expand Down

0 comments on commit acc9e3b

Please sign in to comment.