|
9 | 9 | #include <iterator> |
10 | 10 |
|
11 | 11 | #include "authentication.hpp" |
12 | | -#include "base64.hpp" |
| 12 | +#include "hash_utils.hpp" |
13 | 13 |
|
14 | 14 | namespace azure_proxy { |
15 | 15 |
|
16 | 16 | authentication::authentication() |
17 | 17 | { |
18 | 18 | } |
19 | 19 |
|
20 | | -auth_result authentication::auth_basic(const std::string::const_iterator begin, const std::string::const_iterator end) const |
| 20 | +bool authentication::auth(const auth_key_hash_t& auth_key_hash) const |
21 | 21 | { |
22 | | - std::string authorization; |
23 | | - try { |
24 | | - azure_proxy::encoding::base64_decode(begin, end, std::back_inserter(authorization)); |
25 | | - } |
26 | | - catch (const azure_proxy::encoding::decode_base64_error&) { |
27 | | - return auth_result::error; |
28 | | - } |
29 | | - auto colon_pos = authorization.find(':'); |
30 | | - if (colon_pos == std::string::npos) { |
31 | | - return auth_result::error; |
32 | | - } |
33 | | - std::string username(authorization.begin(), authorization.begin() + colon_pos); |
34 | | - std::string password(authorization.begin() + colon_pos + 1, authorization.end()); |
35 | | - auto iter = this->users_map.find(username); |
36 | | - if (iter != this->users_map.end() && std::get<1>(*iter) == password) { |
37 | | - return auth_result::ok; |
38 | | - } |
39 | | - return auth_result::incorrect; |
| 22 | + return this->auth_keys_map.find(auth_key_hash) != this->auth_keys_map.end(); |
40 | 23 | } |
41 | 24 |
|
42 | | -auth_result authentication::auth(const std::string& value) const |
| 25 | +void authentication::add_auth_key(const std::string& auth_key) |
43 | 26 | { |
44 | | - if (value.size() > 6 && std::equal(value.begin(), value.begin() + 6, "Basic ")) { |
45 | | - return this->auth_basic(value.begin() + 6, value.end()); |
46 | | - } |
47 | | - else { |
48 | | - return auth_result::error; |
49 | | - } |
| 27 | + auto auth_key_hash = hash_utils::sha256(reinterpret_cast<const unsigned char*>(auth_key.data()), auth_key.size()); |
| 28 | + this->auth_keys_map[auth_key_hash] = auth_key; |
50 | 29 | } |
51 | 30 |
|
52 | | -void authentication::add_user(const std::string& username, const std::string& password) |
| 31 | +void authentication::remove_all_auth_keys() |
53 | 32 | { |
54 | | - this->users_map[username] = password; |
55 | | -} |
56 | | - |
57 | | -void authentication::remove_all_users() |
58 | | -{ |
59 | | - this->users_map.clear(); |
| 33 | + this->auth_keys_map.clear(); |
60 | 34 | } |
61 | 35 |
|
62 | 36 | authentication& authentication::get_instance() |
|
0 commit comments