blob: ba126c07a504427f391329294ea7fc39fab3d836 [file] [log] [blame]
weijia yuan82cf9142018-10-21 12:25:02 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento11904062022-04-14 22:33:28 -04003 * Copyright (c) 2014-2022, 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
Davide Pesavento11904062022-04-14 22:33:28 -040022#include <optional>
weijia yuan82cf9142018-10-21 12:25:02 -070023
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 */
Davide Pesavento11904062022-04-14 22:33:28 -040032static std::optional<std::string>
weijia yuan82cf9142018-10-21 12:25:02 -070033getSignerFromTag(const ndn::Interest& interest)
34{
Davide Pesavento11904062022-04-14 22:33:28 -040035 auto signerTag = interest.getTag<SignerTag>();
weijia yuan82cf9142018-10-21 12:25:02 -070036 if (signerTag == nullptr) {
Davide Pesavento11904062022-04-14 22:33:28 -040037 return std::nullopt;
weijia yuan82cf9142018-10-21 12:25:02 -070038 }
39 else {
40 return signerTag->get().toUri();
41 }
42}
43
Davide Pesavento11904062022-04-14 22:33:28 -040044CommandBaseHandle::CommandBaseHandle(Face& face, RepoStorage& storage,
45 Scheduler& sched, ndn::security::Validator& validator)
weijia yuan82cf9142018-10-21 12:25:02 -070046 : face(face)
Davide Pesavento11904062022-04-14 22:33:28 -040047 , storageHandle(storage)
48 , scheduler(sched)
weijia yuan82cf9142018-10-21 12:25:02 -070049 , m_validator(validator)
50{
51}
52
53ndn::mgmt::Authorization
54CommandBaseHandle::makeAuthorization()
55{
Davide Pesavento11904062022-04-14 22:33:28 -040056 return [=] (const ndn::Name&, const auto& interest,
57 const ndn::mgmt::ControlParameters*,
58 const ndn::mgmt::AcceptContinuation& accept,
59 const ndn::mgmt::RejectContinuation& reject) {
weijia yuan82cf9142018-10-21 12:25:02 -070060 m_validator.validate(interest,
Davide Pesavento11904062022-04-14 22:33:28 -040061 [accept] (const auto& request) {
62 auto signer = getSignerFromTag(request).value_or("*");
weijia yuan82cf9142018-10-21 12:25:02 -070063 accept(signer);
64 },
Davide Pesavento11904062022-04-14 22:33:28 -040065 [reject] (auto&&...) {
weijia yuan82cf9142018-10-21 12:25:02 -070066 reject(ndn::mgmt::RejectReply::STATUS403);
67 });
68 };
69}
70
71} // namespace repo