1 //
2 // Created by nullobsi on 2021/03/17.
3 //
4 
5 #ifndef PASS_FDO_SECRETS_SECRETSERVICE_H
6 #define PASS_FDO_SECRETS_SECRETSERVICE_H
7 
8 #include <sdbus-c++/sdbus-c++.h>
9 #include "../adaptors/secretsadaptor.h"
10 #include "../interop/PassStore.h"
11 #include "Collection.h"
12 
13 class Session;
14 
15 class SecretService
16 		: public sdbus::AdaptorInterfaces<org::freedesktop::Secret::Service_adaptor>,
17 		  public std::enable_shared_from_this<SecretService> {
18 public:
19 	SecretService(sdbus::IConnection &conn,
20 	              std::string path);
21 
22 	~SecretService();
23 
24 	void
25 	DiscardObjects();
26 
27 	void
28 	DiscardSession(const std::string &path);
29 
30 	void
31 	DiscardCollection(std::string id);
32 
33 	void
34 	InitCollections();
35 
36 	std::pair<std::vector<uint8_t>, std::vector<uint8_t>>
37 	EncryptSecret(const std::string &path, uint8_t *data, size_t len);
38 
39 protected:
40 	std::tuple<sdbus::Variant, sdbus::ObjectPath>
41 	OpenSession(const std::string &algorithm,
42 	            const sdbus::Variant &input) override;
43 
44 	std::tuple<sdbus::ObjectPath, sdbus::ObjectPath>
45 	CreateCollection(const std::map<std::string, sdbus::Variant> &properties,
46 	                 const std::string &alias) override;
47 
48 	std::tuple<std::vector<sdbus::ObjectPath>, std::vector<sdbus::ObjectPath>>
49 	SearchItems(const std::map<std::string, std::string> &attributes) override;
50 
51 	std::tuple<std::vector<sdbus::ObjectPath>, sdbus::ObjectPath>
52 	Unlock(const std::vector<sdbus::ObjectPath> &objects) override;
53 
54 	std::tuple<std::vector<sdbus::ObjectPath>, sdbus::ObjectPath>
55 	Lock(const std::vector<sdbus::ObjectPath> &objects) override;
56 
57 	void
58 	LockService() override;
59 
60 	sdbus::ObjectPath
61 	ChangeLock(const sdbus::ObjectPath &collection) override;
62 
63 	std::map<sdbus::ObjectPath, sdbus::Struct<sdbus::ObjectPath, std::vector<uint8_t>, std::vector<uint8_t>, std::string>>
64 	GetSecrets(const std::vector<sdbus::ObjectPath> &items,
65 	           const sdbus::ObjectPath &session) override;
66 
67 	sdbus::ObjectPath
68 	ReadAlias(const std::string &name) override;
69 
70 	void
71 	SetAlias(const std::string &name,
72 	         const sdbus::ObjectPath &collection) override;
73 
74 	std::vector<sdbus::ObjectPath>
75 	Collections() override;
76 
77 private:
78 	std::map<std::string, std::unique_ptr<Session>> sessions;
79 	std::vector<std::unique_ptr<Session>> discardedSessions;
80 
81 	PassStore store;
82 	std::map<std::string, std::shared_ptr<Collection>> collections;
83 	std::vector<std::shared_ptr<Collection>> discardedCollections;
84 
85 	std::vector<std::shared_ptr<Item>>
86 	fromObjectPath(const std::vector<std::string> &paths);
87 };
88 
89 #endif //PASS_FDO_SECRETS_SECRETSERVICE_H
90