Header files of the AuthService entity in the Secure Login example

authservice.h

#pragma once

#include <string>

class AuthService

{

public:

AuthService() = default;

bool Authentication(

const std::string &userName, const std::string &password) const;

};

diffiehellman.h

#pragma once

#include <openssl/bn.h>

#include <memory>

struct DhData

{

uint64_t openKey {0};

uint64_t secretKey {0};

uint64_t g {0};

uint64_t p {0};

uint64_t secret {0};

};

struct ISecretData

{

virtual ~ISecretData() = default;

virtual void Init() = 0;

virtual bool IsInit() = 0;

virtual void Dispose() = 0;

virtual const DhData &GetDhData() const = 0;

virtual uint64_t GetSecret(uint64_t oppositeOpenKey) const = 0;

};

using ISecretDataPtr = std::shared_ptr<ISecretData>;

class DiffieHellman : public ISecretData

{

public:

~DiffieHellman() override;

// ISecretData impl.

void Init() override;

bool IsInit() override;

void Dispose() override;

const DhData &GetDhData() const override

{

return m_dhData;

}

uint64_t GetSecret(uint64_t oppositeKey) const override;

private:

struct BignumDhData

{

BIGNUM *openKey {nullptr};

BIGNUM *secretKey {nullptr};

BIGNUM *g {nullptr};

BIGNUM *p {nullptr};

BIGNUM *secret {nullptr};

BIGNUM *oppositeKey {nullptr};

BN_CTX *ctx {nullptr};

BignumDhData()

{

openKey = BN_new();

secretKey = BN_new();

g = BN_new();

p = BN_new();

secret = BN_new();

oppositeKey = BN_new();

ctx = BN_CTX_new();

}

~BignumDhData()

{

BN_CTX_free(ctx);

ctx = nullptr;

BN_clear_free(openKey);

BN_clear_free(secretKey);

BN_clear_free(g);

BN_clear_free(p);

BN_clear_free(secret);

BN_clear_free(oppositeKey);

}

};

private:

DhData m_dhData {};

std::unique_ptr<BignumDhData> m_bignumDh;

};

using DiffieHellmanPtr = std::shared_ptr<DiffieHellman>;

general.h

#pragma once

namespace app {

constexpr const char *AppTag = "[AuthService] ";

}

htmlhelper.h

#pragma once

#include <fstream>

#include <string>

namespace html_helper {

bool ReplaceValueByMask(

std::string &htmlLine, const std::string &mask, const std::string &value);

bool ReplaceValueByMask(

std::string &htmlLine, const std::string &mask, long value);

void ReadHtmlLine(std::ifstream &fm, std::string &htmlLine);

}; // namespace html_helper

login_form_handler.h

#pragma once

#include "auth_service/AuthService.edl.h"

#include "diffiehellman.h"

#include <fstream>

#include <map>

#include <string>

#include <variant>

class LoginFormHandler : auth_service_ILoginForm_ops

{

public:

static auth_service_ILoginForm *CreateImpl(ISecretDataPtr secret);

LoginFormHandler() = delete;

~LoginFormHandler() = default;

bool OpenLoginStream();

bool ReadLine(std::string &htmlLine);

void Close();

private:

LoginFormHandler(ISecretDataPtr secret);

void PutSecret(std::string &htmlLine);

private:

ISecretDataPtr m_secret;

std::ifstream m_htmlFile;

std::map<std::string, std::variant<long, std::string>> m_htmlMaskData;

};

login_result_handler.h

#pragma once

#include "auth_service/AuthService.edl.h"

#include "diffiehellman.h"

#include <fstream>

#include <string>

class LoginResultHandler : auth_service_ILoginResultForm_ops

{

public:

~LoginResultHandler() = default;

bool OpenStream(

const std::string &userName,

const std::string &password,

const std::string &cryptoB);

bool ReadLine(std::string &htmlStream);

void Close();

static auth_service_ILoginResultForm *CreateImpl(ISecretDataPtr secret);

private:

LoginResultHandler(ISecretDataPtr secret);

static bool HexStrToNum(const std::string &hexValueString, char &value);

static std::string DecryptHexString(const std::string &input, long secret);

private:

ISecretDataPtr m_secret;

bool m_isAuthentified = false;

std::ifstream m_htmlFile;

};

server.h

#pragma once

#include "auth_service/AuthService.edl.h"

#include "diffiehellman.h"

class Server

{

public:

Server();

~Server() = default;

int Run(ISecretDataPtr secret);

private:

auth_service_AuthService_entity_req m_req {};

auth_service_AuthService_entity_res m_res {};

nk_arena m_reqArena;

nk_arena m_resArena;

char m_reqBuffer[auth_service_AuthService_entity_req_arena_size];

char m_resBuffer[auth_service_AuthService_entity_res_arena_size];

};

Page top