CBOE Emulator  1.0
authorizer.hpp
1 // The credentials manager for the order entry system.
2 // Copyright 2020 Christian Kauten
3 //
4 // Author: Christian Kauten (kautenja@auburn.edu)
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 
18 #ifndef ORDER_ENTRY_AUTHORIZER_HPP
19 #define ORDER_ENTRY_AUTHORIZER_HPP
20 
21 #include "system_account.hpp"
22 #include <string>
23 #include <map>
24 
26 namespace OrderEntry {
27 
31 template <typename Handler>
32 class Authorizer {
33  private:
35  std::map<std::string, SystemAccount<Handler>> accounts;
36 
37  public:
39  inline std::size_t size() const { return accounts.size(); }
40 
46  void new_user(std::string username, std::string password) {
47  if (accounts.find(username) != accounts.end()) // username found
48  throw "username already exists in system";
49  accounts[username] = {username, password};
50  }
51 
57  inline bool has_account(std::string username) const {
58  return accounts.find(username) != accounts.end();
59  }
60 
66  inline SystemAccount<Handler>* get_account(std::string username) {
67  return &accounts.at(username);
68  }
69 
76  inline bool is_valid(
77  const std::string& username,
78  const std::string& password
79  ) const {
80  // make sure the username exists before looking up the password
81  if (not accounts.count(username)) return false;
82  // check if the password matches for the given username
83  return accounts.at(username).password.compare(password) == 0;
84  }
85 
92  inline friend std::ostream& operator<<(
93  std::ostream& stream,
94  const Authorizer& authorizer
95  ) {
96  stream << SystemAccount<Handler>::table_header() << std::endl;
97  for (auto pair : authorizer.accounts) stream << pair.second << std::endl;
98  return stream;
99  }
100 };
101 
102 } // namespace OrderEntry
103 
104 #endif // ORDER_ENTRY_AUTHORIZER_HPP
OrderEntry
Logic for sending/receiving application messages in a financial market.
Definition: authorizer.hpp:26
OrderEntry::Authorizer::size
std::size_t size() const
Return the number of accounts in the manager.
Definition: authorizer.hpp:39
OrderEntry::Authorizer::has_account
bool has_account(std::string username) const
Return true if the username exists in the database.
Definition: authorizer.hpp:57
OrderEntry::Authorizer
Logic for looking up and validating credentials for new connections.
Definition: authorizer.hpp:32
OrderEntry::Authorizer::get_account
SystemAccount< Handler > * get_account(std::string username)
Return the account for the given username.
Definition: authorizer.hpp:66
OrderEntry::Authorizer::is_valid
bool is_valid(const std::string &username, const std::string &password) const
Return true if the username and password combination is valid.
Definition: authorizer.hpp:76
OrderEntry::Authorizer::new_user
void new_user(std::string username, std::string password)
Create a new user in the account manager.
Definition: authorizer.hpp:46
OrderEntry::SystemAccount
A subclass of the LOB::Account that manages client state on the market server.
Definition: system_account.hpp:35
OrderEntry::Authorizer::operator<<
friend std::ostream & operator<<(std::ostream &stream, const Authorizer &authorizer)
Write the data from the authorizer to a stream.
Definition: authorizer.hpp:92