Debian Codegraph @

src/fdosecrets/objects/Session.cpp

raw
1 /*
2 * Copyright (C) 2018 Aetf <[email protected]>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 or (at your option)
7 * version 3 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "Session.h"
19
20 #include "fdosecrets/objects/Service.h"
21 #include "fdosecrets/objects/SessionCipher.h"
22
23 #include "core/Tools.h"
24
25 namespace FdoSecrets
26 {
27 Session* Session::Create(QSharedPointer<CipherPair> cipher, const QString& peer, Service* parent)
28 {
29 QScopedPointer<Session> res{new Session(std::move(cipher), peer, parent)};
30 if (!res->dbus()->registerObject(res.data())) {
31 return nullptr;
32 }
33
34 return res.take();
35 }
36
37 Session::Session(QSharedPointer<CipherPair> cipher, const QString& peer, Service* parent)
38 : DBusObject(parent)
39 , m_cipher(std::move(cipher))
40 , m_peer(peer)
41 , m_id(QUuid::createUuid())
42 {
43 }
44
45 DBusResult Session::close()
46 {
47 emit aboutToClose();
48 deleteLater();
49
50 return {};
51 }
52
53 QString Session::peer() const
54 {
55 return m_peer;
56 }
57
58 QString Session::id() const
59 {
60 return Tools::uuidToHex(m_id);
61 }
62
63 Service* Session::service() const
64 {
65 return qobject_cast<Service*>(parent());
66 }
67
68 Secret Session::encode(const Secret& input) const
69 {
70 auto output = m_cipher->encrypt(input);
71 output.session = this;
72 return output;
73 }
74
75 Secret Session::decode(const Secret& input) const
76 {
77 Q_ASSERT(input.session == this);
78 return m_cipher->decrypt(input);
79 }
80 } // namespace FdoSecrets