forked from hrantzsch/keychain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keychain.h
131 lines (116 loc) · 4.72 KB
/
keychain.h
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
126
127
128
129
130
131
/*
* Copyright (c) 2019 Hannes Rantzsch, René Meusel
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#ifndef XPLATFORM_KEYCHAIN_WRAPPER_H_
#define XPLATFORM_KEYCHAIN_WRAPPER_H_
#include <string>
/*! \brief A thin wrapper to provide cross-platform access to the operating
* system's credentials storage.
*
* keychain provides the functions getPassword, setPassword, and deletePassword.
*
* All of these functions require three input parameters to identify the
* credential that should be retrieved or manipulated: `package`, `service`, and
* `user`. These identifiers will be mangled differently on each OS to
* correspond to the OS API.
* While none of the supported OSes has specific requirements to the format
* identifiers, the reverse domain name format is recommended for the `package`
* parameter in order to correspond with conventions.
*
* In addition, each function expects an instance of `keychain::Error` as an
* output parameter to indicate success or failure. Note that previous states of
* the Error are ignored and potentially overwritten.
*
* Also note that all three functions are blocking (potentially indefinitely)
* for example if the OS prompts the user to unlock their credentials storage.
*/
namespace keychain {
struct Error;
/*! \brief Retrieve a password
*
* \param package, service, user Used to identify the password to get
* \param err Output parameter communicating success or error details
*
* \return The password, if the function was successful
*/
std::string getPassword(const std::string &package, const std::string &service,
const std::string &user, Error &err);
/*! \brief Insert or update a password
*
* Existing passwords will be overwritten.
*
* \param package, service, user Used to identify the password to set
* \param password The new password
* \param err Output parameter communicating success or error details
*/
void setPassword(const std::string &package, const std::string &service,
const std::string &user, const std::string &password,
Error &err);
/*! \brief Insert or update a password
*
* Trying to delete a password that does not exist will result in a NotFound
* error.
*
* \param package, service, user Used to identify the password to delete
* \param err Output parameter communicating success or error details
*/
void deletePassword(const std::string &package, const std::string &service,
const std::string &user, Error &err);
enum class ErrorType {
// update CATCH_REGISTER_ENUM in tests.cpp when changing this
NoError = 0,
GenericError,
NotFound,
// OS-specific errors
PasswordTooLong = 10, // Windows only
AccessDenied, // macOS only
};
/*! \brief A struct to collect error information
*
* An instance of this struct is used as an output parameter to indicate success
* or failure.
*/
struct Error {
Error() : type(ErrorType::NoError) {}
/*! \brief The type or reason of the error
*
* Note that some types of errors can only occur on certain platforms. In
* cases where a platform-specific error occurs on one platform, both
* NoError or some other (more generic) error might occur on others.
*/
ErrorType type;
/*! \brief A human-readable explanatory error message
*
* In most cases this message is obtained from the operating system.
*/
std::string message;
/*! \brief The "native" error code set by the operating system
*
* Even for the same type of error this value will differ across platforms.
*/
int code;
//! \brief Checks if the error type is not NoError
operator bool() const { return ErrorType::NoError != type; }
};
} // namespace keychain
#endif