Skip to content

Commit 09f4015

Browse files
feat: use pwd for token instead of username
2 parents 2215664 + 5fe3fb1 commit 09f4015

File tree

1 file changed

+23
-41
lines changed

1 file changed

+23
-41
lines changed

auth-plugin/plugin_libcurl.cpp

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -136,26 +136,16 @@ void flashmq_plugin_poll_event_received(void *thread_data, int fd, uint32_t even
136136

137137

138138

139-
enum class Allowed_Access {
140-
playbook,
141-
bettingClient,
142-
sportsClient,
143-
pump,
144-
};
145-
146-
std::map<Allowed_Access, std::string> allowed_user_access_map = {
147-
{Allowed_Access::playbook, "playbook"},
148-
{Allowed_Access::sportsClient, "sports-client"},
149-
{Allowed_Access::bettingClient, "anonymous-betting-client"},
150-
{Allowed_Access::pump, "pump"},
151-
};
152-
153-
std::string allowed_user_access_check(Allowed_Access username) {
154-
auto it = allowed_user_access_map.find(username);
155-
if (it != allowed_user_access_map.end()) {
156-
return it->second;
157-
}
158-
return "Unknown"; // Handle invalid enum values
139+
140+
bool allow_user_access(const std::string &username) {
141+
const std::vector<std::string> allowed_users = {
142+
"playbook",
143+
"sports-client",
144+
"anonymous-betting-client",
145+
"pump"
146+
};
147+
148+
return std::find(allowed_users.begin(), allowed_users.end(), username) != allowed_users.end();
159149
}
160150

161151
std::string get_env_var( std::string const & key )
@@ -184,31 +174,26 @@ std::string base64_decode(const std::string &in) {
184174
BIO_free_all(bio);
185175
return out;
186176
}
177+
187178
AuthResult flashmq_plugin_login_check(void *thread_data, const std::string &clientid, const std::string &username, const std::string &password,
188179
const std::vector<std::pair<std::string, std::string>> *userProperties, const std::weak_ptr<Client> &client)
189180
{
190181
(void)clientid;
191182
(void)userProperties;
192183
(void)client;
193-
(void)username;
194-
(void)password;
184+
195185

196186
flashmq_logf(LOG_INFO, "username: %s", username.c_str());
197187

198-
for (const auto &grant : {Allowed_Access::playbook, Allowed_Access::sportsClient,
199-
Allowed_Access::bettingClient, Allowed_Access::pump})
200-
{
201-
if (username == allowed_user_access_check(grant))
202-
{
203-
return AuthResult::success;
204-
}
188+
if (allow_user_access(username)){
189+
return AuthResult::success;
205190
}
206191

207192
// base64 decode the environment variable AUTH_PUBLICKEY
208193
const std::string rsa_pub_env_key = get_env_var("AUTH_PUBLICKEY");
209194
const std::string rsa_pub_key = base64_decode(rsa_pub_env_key);
210195

211-
const std::string token = username; // In a real application, you would get the token from the username or password field.
196+
const std::string token = password;
212197
if (token.empty())
213198
{
214199
flashmq_logf(LOG_ERR, "No token found for username: %s", username.c_str());
@@ -222,19 +207,11 @@ AuthResult flashmq_plugin_login_check(void *thread_data, const std::string &clie
222207
auto verify = jwt::verify()
223208
// We only need an RSA public key to verify tokens
224209
.allow_algorithm(jwt::algorithm::rs256(rsa_pub_key, "", "", ""));
225-
/* [allow rsa algorithm] */
226-
227-
// Decode the JWT token
228210
/* [decode jwt token] */
229-
auto decoded = jwt::decode(username);
211+
auto decoded = jwt::decode(token);
230212
flashmq_logf(LOG_INFO, "Decoded JWT token successfully");
231213
verify.verify(decoded);
232214
flashmq_logf(LOG_INFO, "Verified JWT token successfully with public key");
233-
// for (auto& e : decoded.get_header_json())
234-
// std::cout << e.first << " = " << e.second << '\n';
235-
// for (auto& e : decoded.get_payload_json())
236-
// std::cout << e.first << " = " << e.second << '\n';
237-
238215

239216
return AuthResult::success;
240217
} catch (const std::exception &e) {
@@ -249,8 +226,10 @@ AuthResult flashmq_plugin_login_check(void *thread_data, const std::string &clie
249226
}
250227

251228
AuthResult flashmq_plugin_acl_check(void *thread_data, const AclAccess access, const std::string &clientid, const std::string &username,
252-
const std::string &topic, const std::vector<std::string> &subtopics, std::string_view payload, const uint8_t qos,
253-
const bool retain, const std::vector<std::pair<std::string, std::string>> *userProperties)
229+
const std::string &topic, const std::vector<std::string> &subtopics, const std::string &shareName,
230+
std::string_view payload, const uint8_t qos, const bool retain,
231+
const std::optional<std::string> &correlationData, const std::optional<std::string> &responseTopic,
232+
const std::vector<std::pair<std::string, std::string>> *userProperties)
254233
{
255234
(void)thread_data;
256235
(void)access;
@@ -262,6 +241,9 @@ AuthResult flashmq_plugin_acl_check(void *thread_data, const AclAccess access, c
262241
(void)userProperties;
263242
(void)topic;
264243
(void)payload;
244+
(void)shareName;
245+
(void)correlationData;
246+
(void)responseTopic;
265247

266248
return AuthResult::success;
267249
}

0 commit comments

Comments
 (0)