File: authz-request.cpp

package info (click to toggle)
gdbuspp 3-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,164 kB
  • sloc: cpp: 9,462; python: 477; sh: 215; makefile: 11
file content (125 lines) | stat: -rw-r--r-- 3,545 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//  GDBus++ - glib2 GDBus C++ wrapper
//
//  SPDX-License-Identifier: AGPL-3.0-only
//
//  Copyright (C)  OpenVPN Inc <sales@openvpn.net>
//  Copyright (C)  David Sommerseth <davids@openvpn.net>
//

/**
 * @file authz-request.cpp
 *
 * @brief  Implementation of the Authz::Request and related APIs
 */

#include <string>

#include "authz-request.hpp"
#include "object/base.hpp"
#include "object/operation.hpp"
#include "object/path.hpp"


namespace DBus {
namespace Authz {

Exception::Exception(const Request::Ptr req, const std::string &errmsg)
    : DBus::Exception("AuthzReq", compose_error(req, errmsg))
{
}


const std::string Exception::compose_error(const Request::Ptr req,
                                           const std::string &errmsg)
{
    if (!errmsg.empty())
    {
        return errmsg;
    }

    std::ostringstream r;
    r << "Authorization failed for " << req->caller;
    switch (req->operation)
    {
    case Object::Operation::METHOD_CALL:
        r << " performing method call ";
        break;
    case Object::Operation::PROPERTY_GET:
        r << " reading property ";
        break;
    case Object::Operation::PROPERTY_SET:
        r << " setting property ";
        break;
    case Object::Operation::NONE:
        r << " [NO OPERATION] ";
    }
    r << req->target << " in object " << req->object_path;
    return std::string(r.str());
}



/**
 *  Internal helper function to extract an operation target string from
 *  an AsyncProcess::Request object
 *
 *  This is basically just helping the Authz::Request constructor to
 *  extract information which can be stored as constant variables

 * @param t    AsyncProcess::ReqType value to conviert
 * @return std::string with readable authorization request information
 */
static inline const std::string extract_authzreq_target(const Object::Operation reqtype,
                                                        const std::string &interface,
                                                        const std::string &method,
                                                        const std::string &property)
{
    std::string trgt = interface + ".";
    switch (reqtype)
    {
    case Object::Operation::METHOD_CALL:
        trgt += method;
        break;
    case Object::Operation::PROPERTY_GET:
    case Object::Operation::PROPERTY_SET:
        trgt += property;
        break;
    default:
        throw DBus::Exception("Authz::Request::Create",
                              "Invalid request type conversion");
    }
    return trgt;
}


Authz::Request::Request(const std::string &caller_,
                        const Object::Operation operation_,
                        const Object::Path &object_path_,
                        const std::string &interace_,
                        const std::string &target_)
    : caller(caller_), operation(operation_),
      object_path(object_path_), interface(interace_), target(target_)
{
}


Authz::Request::Request(const AsyncProcess::Request::UPtr &req)
    : caller(req->sender),
      operation(req->request_type),
      object_path(req->object->GetPath()),
      interface(req->object->GetInterface()),
      target(extract_authzreq_target(req->request_type,
                                     req->object->GetInterface(),
                                     req->method,
                                     req->property))
{
}


const std::string Authz::Request::OperationString() const noexcept
{
    return Object::OperationString(operation);
}

} // namespace Authz
} // namespace DBus