Leosac  0.8.0
Open Source Access Control
PinCodeSerializer.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 {
32  // First we serialize the base ICredential structure.
33  auto serialized = CredentialJSONSerializer::serialize(in, sc);
34  // Now we override the type.
35  ASSERT_LOG(serialized.at("type").is_string(),
36  "Base credential serialization did something unexpected.");
37  serialized["type"] = "pin-code";
38 
39  serialized["attributes"]["code"] = in.pin_code();
40  return serialized;
41 }
42 
44  const SecurityContext &sc)
45 {
47 
48  using namespace JSONUtil;
49  out.pin_code(extract_with_default(in, "code", out.pin_code()));
50 }
Leosac::PinCodeJSONSerializer::serialize
static json serialize(const Cred::IPinCode &in, const SecurityContext &sc)
Definition: PinCodeSerializer.cpp:30
Leosac::Cred::IPinCode::pin_code
virtual const std::string & pin_code() const =0
Leosac::json
nlohmann::json json
Definition: AuditSerializer.hpp:29
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
CredentialSerializer.hpp
SecurityContext.hpp
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
PinCodeSerializer.hpp
Leosac::CredentialJSONSerializer::serialize
static json serialize(const Cred::ICredential &in, const SecurityContext &sc)
Definition: CredentialSerializer.cpp:34
JSONUtils.hpp
Leosac::CredentialJSONSerializer::unserialize
static void unserialize(Cred::ICredential &out, const json &in, const SecurityContext &sc)
Definition: CredentialSerializer.cpp:71
Leosac::Cred
Definition: Credential.hpp:31
log.hpp
Leosac::Cred::IPinCode
Interface for PinCode credentials.
Definition: IPinCode.hpp:32
Leosac::SecurityContext
A SecurityContext is used to query permission while doing an operation.
Definition: SecurityContext.hpp:40
Leosac::PinCodeJSONSerializer::unserialize
static void unserialize(Cred::IPinCode &out, const json &in, const SecurityContext &sc)
Definition: PinCodeSerializer.cpp:43
IPinCode.hpp