Leosac  0.8.0
Open Source Access Control
AuthSourceBuilder.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero 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 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include "AuthSourceBuilder.hpp"
21 #include "Auth.hpp"
25 #include "tools/enforce.hpp"
26 #include "tools/log.hpp"
27 #include <boost/algorithm/string.hpp>
28 
29 namespace Leosac
30 {
31 namespace Auth
32 {
34  zmqpp::message *msg)
35 {
36  // card id
37  assert(msg && msg->remaining() == 1);
38 
39  std::string card_id;
40 
41  *msg >> card_id;
42  INFO("Building an AuthSource object (SIMPLE_CSN):" << card_id);
43  auto raw_csn = boost::replace_all_copy(card_id, ":", "");
44  LEOSAC_ENFORCE(raw_csn.length() % 2 == 0, "CSN has invalid length.");
45 
46  auto bits = raw_csn.length() * 8;
47  ASSERT_LOG(bits < std::numeric_limits<int>::max(), "Too many bits.");
48 
49  return std::make_shared<Cred::RFIDCard>(card_id, bits);
50 }
51 
53 {
54  // Auth spec say at least 2 frames: source and type.
55  assert(msg && msg->parts() >= 2);
56  std::string source_name;
57  SourceType type;
58  *msg >> source_name >> type;
59 
60  bool ret = extract_source_name(source_name, &source_name);
61  ASSERT_LOG(
62  ret,
63  "Failed to extract source name: cannot construct the AuthSource object. "
64  << "Source name was {" << source_name << "}");
65 
66  if (type == SourceType::SIMPLE_WIEGAND)
67  return create_simple_wiegand(source_name, msg);
68  else if (type == SourceType::WIEGAND_PIN)
69  return create_pincode(source_name, msg);
70  else if (type == SourceType::WIEGAND_CARD_PIN)
71  return create_wiegand_card_pin(source_name, msg);
72  else if (type == SourceType::SIMPLE_CSN)
73  return create_simple_csn(source_name, msg);
74  LEOSAC_ENFORCE(0, "Unknown auth source type.");
75 
76  return nullptr;
77 }
78 
79 bool AuthSourceBuilder::extract_source_name(const std::string &input,
80  std::string *output) const
81 {
82  assert(output);
83  if (input.size() > 2 && input.substr(0, 2) == "S_")
84  {
85  *output = input.substr(2);
86  return true;
87  }
88  return false;
89 }
90 
93  zmqpp::message *msg)
94 {
95  // card id and number of bit shall be left.
96  assert(msg && msg->remaining() == 2);
97 
98  std::string card_id;
99  int bits;
100 
101  *msg >> card_id >> bits;
102 
103  INFO("Building a Credential object (RFIDCard): "
104  << card_id << " with " << bits
105  << " significant bits. Source name = " << name);
106  return std::make_shared<Cred::RFIDCard>(card_id, bits);
107 }
108 
110  zmqpp::message *msg)
111 {
112  // pin code only
113  assert(msg && msg->remaining() == 1);
114 
115  std::string pin;
116  *msg >> pin;
117 
118  INFO("Building a Credential object (PinCode): " << pin
119  << ". Source name = " << name);
120  auto p = std::make_shared<Cred::PinCode>();
121  p->pin_code(pin);
122  return p;
123 }
124 
127  zmqpp::message *msg)
128 {
129  // card id; nb bits; pin
130  assert(msg && msg->remaining() == 3);
131 
132  std::string card_id;
133  int bits;
134  std::string pin_code;
135 
136  *msg >> card_id >> bits >> pin_code;
137  INFO("Building a Credential object (RFIDCardPin):"
138  << card_id << ", " << pin_code << ". Source name = " << name);
139 
140  auto c = std::make_shared<Cred::RFIDCard>(card_id, bits);
141  auto p = std::make_shared<Cred::PinCode>();
142  p->pin_code(pin_code);
143 
144  return std::make_shared<Cred::RFIDCardPin>(c, p);
145 }
146 }
147 }
Leosac::Auth::AuthSourceBuilder::create
virtual Cred::ICredentialPtr create(zmqpp::message *msg)
Create a Credential object from a message.
Definition: AuthSourceBuilder.cpp:52
Leosac::Auth::AuthSourceBuilder::create_simple_csn
Cred::ICredentialPtr create_simple_csn(const std::string &name, zmqpp::message *msg)
Definition: AuthSourceBuilder.cpp:33
Auth.hpp
Leosac::Auth::AuthSourceBuilder::extract_source_name
bool extract_source_name(const std::string &input, std::string *output) const
Extract the source name from the frame.
Definition: AuthSourceBuilder.cpp:79
Leosac::Auth::SourceType::WIEGAND_PIN
@ WIEGAND_PIN
Message formatting when using a simple PIN code.
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::Auth::AuthSourceBuilder::create_pincode
Cred::ICredentialPtr create_pincode(const std::string &name, zmqpp::message *msg)
Create an auth source from WIEGAND_PIN data type.
Definition: AuthSourceBuilder.cpp:109
AuthSourceBuilder.hpp
RFIDCard.hpp
INFO
@ INFO
Definition: log.hpp:34
Leosac::Auth::SourceType::WIEGAND_CARD_PIN
@ WIEGAND_CARD_PIN
When reading both a card an a PIN code.
enforce.hpp
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Auth::AuthSourceBuilder::create_simple_wiegand
Cred::ICredentialPtr create_simple_wiegand(const std::string &name, zmqpp::message *msg)
Create an auth source from SIMPLE_WIEGAND data type.
Definition: AuthSourceBuilder.cpp:92
RFIDCardPin.hpp
Leosac::Auth::SourceType::SIMPLE_CSN
@ SIMPLE_CSN
A simple Card Serial Number.
PinCode.hpp
Leosac::Cred::ICredentialPtr
std::shared_ptr< ICredential > ICredentialPtr
Definition: CredentialFwd.hpp:32
log.hpp
Leosac::Auth::SourceType
SourceType
Definition: Auth.hpp:29
LEOSAC_ENFORCE
#define LEOSAC_ENFORCE(cond,...)
Similar to enforce, except that it will throw a LEOSACException.
Definition: enforce.hpp:47
Leosac::Auth::SourceType::SIMPLE_WIEGAND
@ SIMPLE_WIEGAND
This define message formatting for data source SIMPLE_WIEGAND.
Leosac::Auth::AuthSourceBuilder::create_wiegand_card_pin
Cred::ICredentialPtr create_wiegand_card_pin(const std::string &name, zmqpp::message *msg)
Create an auth source from a WiegandCard and PIN Code.
Definition: AuthSourceBuilder.cpp:126