blob: a820e3bca1c578f7dbb8709d59bfa1ac64d34b6f [file] [log] [blame]
weijia yuan82cf9142018-10-21 12:25:02 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Alexander Afanasyev15f67df2020-06-03 14:22:24 -04003 * Copyright (c) 2014-2020, Regents of the University of California.
weijia yuan82cf9142018-10-21 12:25:02 -07004 *
5 * This file is part of NDN repo-ng (Next generation of NDN repository).
6 * See AUTHORS.md for complete list of repo-ng authors and contributors.
7 *
8 * repo-ng is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * repo-ng is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "command-base-handle.hpp"
21
22#include <ndn-cxx/util/random.hpp>
23
24namespace repo {
25
26/** \brief an Interest tag to indicate command signer
27 */
28using SignerTag = ndn::SimpleTag<ndn::Name, 20>;
29
30/** \brief obtain signer from SignerTag attached to Interest, if available
31 */
32static ndn::optional<std::string>
33getSignerFromTag(const ndn::Interest& interest)
34{
weijia yuan3aa8d2b2018-03-06 15:35:57 -080035 std::shared_ptr<SignerTag> signerTag = interest.getTag<SignerTag>();
weijia yuan82cf9142018-10-21 12:25:02 -070036 if (signerTag == nullptr) {
37 return ndn::nullopt;
38 }
39 else {
40 return signerTag->get().toUri();
41 }
42}
43
44CommandBaseHandle::CommandBaseHandle(Face& face, RepoStorage& storageHandle,
45 Scheduler& scheduler, Validator& validator)
46 : face(face)
47 , storageHandle(storageHandle)
48 , scheduler(scheduler)
49 , m_validator(validator)
50{
51}
52
53ndn::mgmt::Authorization
54CommandBaseHandle::makeAuthorization()
55{
56 return [=] (const ndn::Name& prefix, const ndn::Interest& interest,
57 const ndn::mgmt::ControlParameters* params,
58 const ndn::mgmt::AcceptContinuation& accept,
59 const ndn::mgmt::RejectContinuation& reject) {
60 m_validator.validate(interest,
61 [accept] (const ndn::Interest& request) {
62
63 auto signer1 = getSignerFromTag(request);
64 std::string signer = signer1.value_or("*");
weijia yuan82cf9142018-10-21 12:25:02 -070065 accept(signer);
66 },
67 [reject] (const ndn::Interest& request,
Alexander Afanasyev15f67df2020-06-03 14:22:24 -040068 const ndn::security::ValidationError& error) {
weijia yuan82cf9142018-10-21 12:25:02 -070069 reject(ndn::mgmt::RejectReply::STATUS403);
70 });
71 };
72}
73
74} // namespace repo