Leosac  0.7.0
OpenSourceAccessControl
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 }
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
static void unserialize(Cred::ICredential &out, const json &in, const SecurityContext &sc)
nlohmann::json json
This is the header file for a generated source file, GitSHA1.cpp.
Interface for RFIDCard credential.
Definition: IRFIDCard.hpp:32
static json serialize(const Cred::ICredential &in, const SecurityContext &sc)
static void unserialize(Cred::IRFIDCard &out, const json &in, const SecurityContext &sc)
static json serialize(const Cred::IRFIDCard &in, const SecurityContext &sc)
A SecurityContext is used to query permission while doing an operation.
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:221
virtual const std::string & card_id() const =0
virtual int nb_bits() const =0