Leosac  0.8.0
Open Source Access Control
RFIDCardSerializer.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 
21 #include "core/SecurityContext.hpp"
24 #include "tools/JSONUtils.hpp"
25 #include "tools/log.hpp"
26 
27 using namespace Leosac;
28 using namespace Leosac::Cred;
29 
31  const SecurityContext &sc)
32 {
33  // First we serialize the base ICredential structure.
34  auto serialized = CredentialJSONSerializer::serialize(in, sc);
35  // Now we override the type.
36  ASSERT_LOG(serialized.at("type").is_string(),
37  "Base credential serialization did something unexpected.");
38  serialized["type"] = "rfid-card";
39 
40  serialized["attributes"]["nb-bits"] = in.nb_bits();
41  serialized["attributes"]["card-id"] = in.card_id();
42  return serialized;
43 }
44 
46  const SecurityContext &sc)
47 {
49 
50  using namespace JSONUtil;
51  out.card_id(extract_with_default(in, "card-id", out.card_id()));
52  out.nb_bits(extract_with_default(in, "nb-bits", out.nb_bits()));
53 }
Leosac::json
nlohmann::json json
Definition: AuditSerializer.hpp:29
Leosac::RFIDCardJSONSerializer::unserialize
static void unserialize(Cred::IRFIDCard &out, const json &in, const SecurityContext &sc)
Definition: RFIDCardSerializer.cpp:45
Leosac::JSONUtil::extract_with_default
std::chrono::system_clock::time_point extract_with_default(const nlohmann::json &obj, const std::string &key, const std::chrono::system_clock::time_point &tp)
Extract an ISO 8601 datetime string from a json object.
Definition: JSONUtils.cpp:45
ASSERT_LOG
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:190
Leosac::Cred::IRFIDCard::card_id
virtual const std::string & card_id() const =0
CredentialSerializer.hpp
SecurityContext.hpp
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Cred::IRFIDCard::nb_bits
virtual int nb_bits() const =0
Leosac::CredentialJSONSerializer::serialize
static json serialize(const Cred::ICredential &in, const SecurityContext &sc)
Definition: CredentialSerializer.cpp:34
JSONUtils.hpp
RFIDCardSerializer.hpp
Leosac::CredentialJSONSerializer::unserialize
static void unserialize(Cred::ICredential &out, const json &in, const SecurityContext &sc)
Definition: CredentialSerializer.cpp:71
Leosac::RFIDCardJSONSerializer::serialize
static json serialize(const Cred::IRFIDCard &in, const SecurityContext &sc)
Definition: RFIDCardSerializer.cpp:30
Leosac::Cred
Definition: Credential.hpp:31
log.hpp
IRFIDCard.hpp
Leosac::SecurityContext
A SecurityContext is used to query permission while doing an operation.
Definition: SecurityContext.hpp:40
Leosac::Cred::IRFIDCard
Interface for RFIDCard credential.
Definition: IRFIDCard.hpp:32