Skip to content

Commit 58e0dfa

Browse files
committed
Improved
1 parent 595035f commit 58e0dfa

File tree

6 files changed

+132
-1
lines changed

6 files changed

+132
-1
lines changed

app/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ add_subdirectory(external)
66
add_subdirectory(common)
77
add_subdirectory(interfaces)
88
add_subdirectory(parsers)
9-
add_subdirectory(configs)
9+
add_subdirectory(configs)
10+
add_subdirectory(auth)

app/auth/CMakeLists.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
project(Auth)
2+
3+
4+
include_directories(${PROJECT_SOURCE_DIR}/include)
5+
6+
set(HEADERS
7+
${PROJECT_SOURCE_DIR}/include/auth/AuthManager.h
8+
)
9+
10+
set(SOURCES
11+
${PROJECT_SOURCE_DIR}/src/AuthManager.cpp
12+
)
13+
14+
add_library(${PROJECT_NAME} STATIC ${SOURCES} ${HEADERS})
15+
16+
target_link_libraries(${PROJECT_NAME} PRIVATE
17+
Common
18+
Interfaces
19+
)
20+
21+
22+
target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/include)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// Created by sajith on 6/28/22.
3+
//
4+
5+
#ifndef DESIGN_PATTERNS_AUTHMANAGER_H
6+
#define DESIGN_PATTERNS_AUTHMANAGER_H
7+
8+
#include "auth/IAuthentication.h"
9+
#include "auth/IAuthorization.h"
10+
11+
#include <functional>
12+
#include <set>
13+
14+
#include "types/User.h"
15+
16+
namespace auth
17+
{
18+
class AuthManger : public IAuthorization, public IAuthentication
19+
{
20+
public:
21+
22+
AuthManger();
23+
24+
bool authenticate(const types::Login &, const types::Password &) const override;
25+
26+
bool isAuthorized(const types::Login &, std::uint32_t) const override;
27+
28+
private:
29+
30+
std::set<types::User, std::less<>> users;
31+
};
32+
}
33+
34+
#endif //DESIGN_PATTERNS_AUTHMANAGER_H

app/auth/src/AuthManager.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// Created by sajith on 6/28/22.
3+
//
4+
5+
#include "auth/AuthManager.h"
6+
7+
8+
namespace
9+
{
10+
const types::Login dummyLogin{"John Doe"};
11+
const types::Password dummyPassword{"@12345"};
12+
const std::set<std::uint32_t> dummyTaxpayerIds{1, 2, 3, 4, 5, 5, 6, 8, 9, 10};
13+
}
14+
15+
namespace auth
16+
{
17+
AuthManger::AuthManger() : users{types::User{dummyLogin, dummyPassword, dummyTaxpayerIds}}
18+
{}
19+
20+
bool AuthManger::authenticate(const types::Login &login, const types::Password &password) const
21+
{
22+
const auto itr = users.find(login);
23+
return itr != users.end() && itr->password.value == password.value;
24+
}
25+
26+
bool AuthManger::isAuthorized(const types::Login &login, std::uint32_t taxpayer_id) const
27+
{
28+
const auto itr = users.find(login);
29+
return itr != users.end() && itr->taxpayers.count(taxpayer_id) > 0;
30+
}
31+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Created by sajith on 6/28/22.
3+
//
4+
5+
#ifndef DESIGN_PATTERNS_IAUTHENTICATION_H
6+
#define DESIGN_PATTERNS_IAUTHENTICATION_H
7+
8+
#include "types/User.h"
9+
10+
namespace auth
11+
{
12+
class IAuthentication
13+
{
14+
public:
15+
16+
virtual ~IAuthentication() = default;
17+
18+
virtual bool authenticate(const types::Login &, const types::Password &) const = 0;
19+
};
20+
}
21+
22+
#endif //DESIGN_PATTERNS_IAUTHENTICATION_H
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//
2+
// Created by sajith on 6/28/22.
3+
//
4+
5+
#ifndef DESIGN_PATTERNS_IAUTHORIZATION_H
6+
#define DESIGN_PATTERNS_IAUTHORIZATION_H
7+
8+
#include "types/User.h"
9+
10+
namespace auth
11+
{
12+
class IAuthorization
13+
{
14+
public:
15+
16+
virtual ~IAuthorization() = default;
17+
18+
virtual bool isAuthorized(const types::Login &, std::uint32_t) const = 0;
19+
};
20+
}
21+
#endif //DESIGN_PATTERNS_IAUTHORIZATION_H

0 commit comments

Comments
 (0)