weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California. |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 4 | * |
| 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 Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 22 | #include <optional> |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 23 | |
| 24 | namespace repo { |
| 25 | |
| 26 | /** \brief an Interest tag to indicate command signer |
| 27 | */ |
| 28 | using SignerTag = ndn::SimpleTag<ndn::Name, 20>; |
| 29 | |
| 30 | /** \brief obtain signer from SignerTag attached to Interest, if available |
| 31 | */ |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 32 | static std::optional<std::string> |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 33 | getSignerFromTag(const ndn::Interest& interest) |
| 34 | { |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 35 | auto signerTag = interest.getTag<SignerTag>(); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 36 | if (signerTag == nullptr) { |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 37 | return std::nullopt; |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 38 | } |
| 39 | else { |
| 40 | return signerTag->get().toUri(); |
| 41 | } |
| 42 | } |
| 43 | |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 44 | CommandBaseHandle::CommandBaseHandle(Face& face, RepoStorage& storage, |
| 45 | Scheduler& sched, ndn::security::Validator& validator) |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 46 | : face(face) |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 47 | , storageHandle(storage) |
| 48 | , scheduler(sched) |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 49 | , m_validator(validator) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | ndn::mgmt::Authorization |
| 54 | CommandBaseHandle::makeAuthorization() |
| 55 | { |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 56 | 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 yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 60 | m_validator.validate(interest, |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 61 | [accept] (const auto& request) { |
| 62 | auto signer = getSignerFromTag(request).value_or("*"); |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 63 | accept(signer); |
| 64 | }, |
Davide Pesavento | 1190406 | 2022-04-14 22:33:28 -0400 | [diff] [blame] | 65 | [reject] (auto&&...) { |
weijia yuan | 82cf914 | 2018-10-21 12:25:02 -0700 | [diff] [blame] | 66 | reject(ndn::mgmt::RejectReply::STATUS403); |
| 67 | }); |
| 68 | }; |
| 69 | } |
| 70 | |
| 71 | } // namespace repo |