Leosac  0.8.0
Open Source Access Control
JSONUtils.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 "tools/JSONUtils.hpp"
22 #include "tools/MyTime.hpp"
23 #include <date/date.h>
24 
25 namespace Leosac
26 {
27 namespace JSONUtil
28 {
30  const std::string &base_key,
31  const Auth::ValidityInfo &def)
32 {
33  auto validity = def;
34 
35  validity.set_enabled(
36  extract_with_default(obj, base_key + "-enabled", def.is_enabled()));
37 
38  validity.start(extract_with_default(obj, base_key + "-start", def.start()));
39  validity.end(extract_with_default(obj, base_key + "-end", def.end()));
40 
41  return validity;
42 }
43 
44 std::chrono::system_clock::time_point
45 extract_with_default(const nlohmann::json &obj, const std::string &key,
46  const std::chrono::system_clock::time_point &tp)
47 {
48  std::string date_str = extract_with_default(obj, key, "");
49  if (date_str.length())
50  {
51  std::chrono::system_clock::time_point tp_out =
52  std::chrono::system_clock::time_point::max();
53  auto x = date::parse("%Y-%m-%dT%H:%M:%SZ", tp_out);
54  std::istringstream iss(date_str);
55  iss >> x;
56  assert(iss.good());
57  return tp_out;
58  }
59  return tp;
60 }
61 }
62 }
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
json
nlohmann::json json
Definition: WSServer.cpp:76
Leosac::Auth::ValidityInfo::is_enabled
bool is_enabled() const
Is the credential enabled ?
Definition: ValidityInfo.cpp:39
ValidityInfo.hpp
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Auth::ValidityInfo::set_enabled
void set_enabled(bool v)
Definition: ValidityInfo.cpp:93
Leosac::Auth::ValidityInfo::end
const TimePoint & end() const
Definition: ValidityInfo.cpp:103
JSONUtils.hpp
Leosac::JSONUtil::extract_validity_with_default
Auth::ValidityInfo extract_validity_with_default(const nlohmann::json &obj, const std::string &base_key, const Auth::ValidityInfo &def)
Extract fields representing a ValidityInfo object.
Definition: JSONUtils.cpp:29
Leosac::Auth::ValidityInfo::start
const TimePoint & start() const
Definition: ValidityInfo.cpp:98
Leosac::Auth::ValidityInfo
A simple class that stores (and can be queried for) the validity of some objects.
Definition: ValidityInfo.hpp:42
MyTime.hpp