-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathAuthenticationManager.cpp
More file actions
179 lines (149 loc) · 6.06 KB
/
AuthenticationManager.cpp
File metadata and controls
179 lines (149 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Local include
#include "AuthenticationManager.hpp"
// Common includes
#include "common/TraceException.hpp"
#include "common/DynaLog.hpp"
// Standard includes
#include <iostream>
namespace SDMS {
namespace Core {
AuthenticationManager::AuthenticationManager(
std::map<PublicKeyType, time_t> purge_intervals,
std::map<PublicKeyType, std::vector<std::unique_ptr<Condition>>>
&&purge_conditions,
const std::string &db_url, const std::string &db_user,
const std::string &db_pass)
: m_purge_interval(purge_intervals),
m_purge_conditions(std::move(purge_conditions)),
m_auth_mapper(m_purge_interval[PublicKeyType::TRANSIENT],
m_purge_interval[PublicKeyType::SESSION],
db_url, db_user, db_pass) {
for (const auto &purge_int : m_purge_interval) {
m_next_purge[purge_int.first] = time(0) + purge_int.second;
}
}
AuthenticationManager &
AuthenticationManager::operator=(AuthenticationManager &&other) {
// Only need to lock the mutex moving from
if (this != &other) {
std::lock_guard<std::mutex> lock(other.m_lock);
m_next_purge = other.m_next_purge;
m_purge_interval = other.m_purge_interval;
m_purge_conditions = std::move(other.m_purge_conditions);
m_auth_mapper = std::move(other.m_auth_mapper);
}
return *this;
}
void AuthenticationManager::purge() {
purge(PublicKeyType::TRANSIENT);
purge(PublicKeyType::SESSION);
}
void AuthenticationManager::purge(const PublicKeyType pub_key_type) {
std::lock_guard<std::mutex> lock(m_lock);
if (m_auth_mapper.size(pub_key_type)) {
const time_t now = time(0);
if (now >= m_next_purge[pub_key_type]) {
const std::vector<std::string> expired_keys =
m_auth_mapper.getExpiredKeys(pub_key_type, now);
for (const auto &pub_key : expired_keys) {
if (m_purge_conditions[pub_key_type].size()) {
for (std::unique_ptr<Condition> &condition :
m_purge_conditions[pub_key_type]) {
condition->enforce(m_auth_mapper, pub_key);
}
} else {
m_auth_mapper.removeKey(pub_key_type, pub_key);
}
}
m_next_purge[pub_key_type] = now + m_purge_interval[pub_key_type];
}
}
}
void AuthenticationManager::incrementKeyAccessCounter(
const std::string &public_key,
LogContext log_context) {
std::lock_guard<std::mutex> lock(m_lock);
if (m_auth_mapper.hasKey(PublicKeyType::TRANSIENT, public_key, log_context)) {
m_auth_mapper.incrementKeyAccessCounter(PublicKeyType::TRANSIENT,
public_key, log_context);
} else if (m_auth_mapper.hasKey(PublicKeyType::SESSION, public_key, log_context)) {
m_auth_mapper.incrementKeyAccessCounter(PublicKeyType::SESSION, public_key, log_context);
}
// Ignore persistent cases because counter does nothing for them
}
bool AuthenticationManager::hasKey(const std::string &public_key, LogContext log_context) const {
std::lock_guard<std::mutex> lock(m_lock);
if (m_auth_mapper.hasKey(PublicKeyType::TRANSIENT, public_key, log_context)) {
return true;
}
if (m_auth_mapper.hasKey(PublicKeyType::SESSION, public_key, log_context)) {
return true;
}
if (m_auth_mapper.hasKey(PublicKeyType::PERSISTENT, public_key, log_context)) {
return true;
}
return false;
}
std::string AuthenticationManager::getUID(const std::string &public_key, LogContext log_context) const {
std::lock_guard<std::mutex> lock(m_lock);
if (m_auth_mapper.hasKey(PublicKeyType::TRANSIENT, public_key, log_context)) {
return m_auth_mapper.getUID(PublicKeyType::TRANSIENT, public_key, log_context);
}
if (m_auth_mapper.hasKey(PublicKeyType::SESSION, public_key, log_context)) {
return m_auth_mapper.getUID(PublicKeyType::SESSION, public_key, log_context);
}
if (m_auth_mapper.hasKey(PublicKeyType::PERSISTENT, public_key, log_context)) {
return m_auth_mapper.getUID(PublicKeyType::PERSISTENT, public_key, log_context);
}
EXCEPT(1, "Unrecognized public_key during execution of getUID.");
}
void AuthenticationManager::addKey(const PublicKeyType &pub_key_type,
const std::string &public_key,
const std::string &uid) {
std::lock_guard<std::mutex> lock(m_lock);
m_auth_mapper.addKey(pub_key_type, public_key, uid);
}
bool AuthenticationManager::hasKey(const PublicKeyType &pub_key_type,
const std::string &public_key,
LogContext log_context) const {
std::lock_guard<std::mutex> lock(m_lock);
return m_auth_mapper.hasKey(pub_key_type, public_key, log_context);
}
void AuthenticationManager::migrateKey(const PublicKeyType &from_type,
const PublicKeyType &to_type,
const std::string &public_key,
const std::string &uid) {
std::lock_guard<std::mutex> lock(m_lock);
m_auth_mapper.migrateKey(from_type, to_type, public_key, uid);
}
void AuthenticationManager::clearTransientKeys() {
std::lock_guard<std::mutex> lock(m_lock);
m_auth_mapper.clearTransientKeys();
}
void AuthenticationManager::clearSessionKeys() {
std::lock_guard<std::mutex> lock(m_lock);
m_auth_mapper.clearSessionKeys();
}
void AuthenticationManager::clearAllNonPersistentKeys() {
std::lock_guard<std::mutex> lock(m_lock);
m_auth_mapper.clearAllNonPersistentKeys();
}
std::string AuthenticationManager::getUIDSafe(const std::string &public_key, LogContext log_context) const {
std::lock_guard<std::mutex> lock(m_lock);
// Try each key type in order
std::string uid = m_auth_mapper.getUIDSafe(PublicKeyType::TRANSIENT, public_key, log_context);
if (!uid.empty()) {
return uid;
}
uid = m_auth_mapper.getUIDSafe(PublicKeyType::SESSION, public_key, log_context);
if (!uid.empty()) {
return uid;
}
uid = m_auth_mapper.getUIDSafe(PublicKeyType::PERSISTENT, public_key, log_context);
if (!uid.empty()) {
return uid;
}
return ""; // Return empty string if not found anywhere
}
} // namespace Core
} // namespace SDMS