50 explicit FileTokenStore(std::string folder, std::chrono::milliseconds lock_wait = std::chrono::seconds(30))
51 : folder_(std::move(folder)), lock_wait_(lock_wait) {}
61 if (
auto error = open_folder(identity, folder)) {
64 const auto name = identity +
".lock";
65 Descriptor file(::openat(folder.get(), name.c_str(), FILE_FLAGS | O_RDWR | O_CREAT, 0600));
66 if (file.get() < 0 && errno == ENOENT) {
68 file.reset(::openat(folder.get(), name.c_str(), FILE_FLAGS | O_RDWR | O_CREAT, 0600));
70 if (
auto error = check_file(file)) {
73 const auto deadline = std::chrono::steady_clock::now() + lock_wait_;
74 while (::flock(file.get(), LOCK_EX | LOCK_NB) != 0) {
75 if (errno != EWOULDBLOCK && errno != EINTR) {
78 if (std::chrono::steady_clock::now() >= deadline) {
81 std::this_thread::sleep_for(std::chrono::milliseconds(10));
83 std::unique_ptr<Lock> held = std::make_unique<FileLock>(std::move(file));
94 if (
auto error = open_folder(identity, folder)) {
97 std::optional<std::string> text;
98 if (
auto error = read_token(folder, identity, text)) {
101 return text ? parse(*text) : std::optional<StoredToken>();
112 if (
auto error = open_folder(identity, folder)) {
115 std::optional<std::string> text;
116 if (
auto error = read_token(folder, identity, text)) {
120 if (
const auto stored = parse(*text); stored && stored->expires_at_ > token.
expires_at_) {
124 return write_token(folder, identity, token);
133 std::optional<BrokerError>
discard(
const std::string& identity,
const std::string& access_token)
override {
135 if (
auto error = open_folder(identity, folder)) {
138 std::optional<std::string> text;
139 if (
auto error = read_token(folder, identity, text)) {
145 if (
const auto stored = parse(*text); !stored || stored->access_token_ != access_token) {
148 return unlink_token(folder, identity);
156 std::optional<BrokerError>
remove(
const std::string& identity)
override {
158 if (
auto error = open_folder(identity, folder)) {
161 return unlink_token(folder, identity);
166 static constexpr int FILE_FLAGS = O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK;
168 static constexpr std::size_t MAXIMUM_TOKEN_FILE = 64 * 1024;
173 explicit Descriptor(
int descriptor = -1) noexcept : descriptor_(descriptor) {}
176 if (descriptor_ >= 0) {
177 ::close(descriptor_);
181 Descriptor(Descriptor&& other) noexcept : descriptor_(std::exchange(other.descriptor_, -1)) {}
182 Descriptor& operator=(Descriptor&&) =
delete;
183 Descriptor(
const Descriptor&) =
delete;
184 Descriptor& operator=(
const Descriptor&) =
delete;
186 int get() const noexcept {
190 void reset(
int descriptor)
noexcept {
191 if (descriptor_ >= 0) {
192 ::close(descriptor_);
194 descriptor_ = descriptor;
202 class FileLock :
public Lock {
204 explicit FileLock(Descriptor file) noexcept : file_(std::move(file)) {}
211 static bool file_safe(
const std::string& identity)
noexcept {
212 if (identity.empty() || identity.size() > 200) {
215 for (
const char ch : identity) {
217 (ch >=
'a' && ch <=
'z') || (ch >=
'A' && ch <=
'Z') || (ch >=
'0' && ch <=
'9') || ch ==
'-' || ch ==
'_';
226 static bool owned_privately(
int descriptor, mode_t type)
noexcept {
227 struct stat status{};
228 return ::fstat(descriptor, &status) == 0 && (status.st_mode & S_IFMT) == type && status.st_uid == ::geteuid() &&
229 (status.st_mode & 077) == 0;
233 std::optional<BrokerError> open_folder(
const std::string& identity, Descriptor& folder)
const {
234 if (!file_safe(identity)) {
237 if (::mkdir(folder_.c_str(), 0700) != 0 && errno != EEXIST) {
240 folder.reset(::open(folder_.c_str(), O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC));
241 if (folder.get() < 0) {
242 struct stat status{};
243 const bool link = ::lstat(folder_.c_str(), &status) == 0 && S_ISLNK(status.st_mode);
245 link ?
"token store: the folder is a symbolic link" :
"token store: the folder could not be opened"
248 if (!owned_privately(folder.get(), S_IFDIR)) {
249 return token_store_error(
"token store: the folder is not private (owner or permissions)");
255 static std::optional<BrokerError> check_file(
const Descriptor& file) {
256 if (file.get() < 0) {
258 errno == ELOOP ?
"token store: a file is a symbolic link" :
"token store: a file could not be opened"
261 if (!owned_privately(file.get(), S_IFREG)) {
262 return token_store_error(
"token store: a file is not private (owner, permissions or type)");
268 static std::optional<BrokerError> read_token(
269 const Descriptor& folder,
const std::string& identity, std::optional<std::string>& text
271 Descriptor file(::openat(folder.get(), (identity +
".token").c_str(), FILE_FLAGS | O_RDONLY));
272 if (file.get() < 0 && errno == ENOENT) {
275 if (
auto error = check_file(file)) {
278 std::string contents;
280 while (contents.size() <= MAXIMUM_TOKEN_FILE) {
281 const auto count = ::read(file.get(), buffer,
sizeof(buffer));
283 text = std::move(contents);
286 if (count < 0 && errno != EINTR) {
290 contents.append(buffer,
static_cast<std::size_t
>(count));
293 text = std::string();
298 static std::optional<StoredToken> parse(
const std::string& text) {
299 boost::system::error_code ec;
300 const auto document = boost::json::parse(text, ec);
301 if (ec || !document.is_object()) {
304 const auto* token = document.as_object().if_contains(
"access_token");
305 const auto* expiry = document.as_object().if_contains(
"expires_at_ns");
306 if (!token || !token->is_string() || token->as_string().empty() || !expiry || !expiry->is_int64()) {
309 const auto since_epoch = std::chrono::nanoseconds(expiry->as_int64());
311 std::string(token->as_string()),
312 std::chrono::system_clock::time_point(
313 std::chrono::duration_cast<std::chrono::system_clock::duration>(since_epoch)
319 static std::optional<BrokerError> write_token(
320 const Descriptor& folder,
const std::string& identity,
const StoredToken& token
322 boost::json::object document;
323 document[
"access_token"] = token.access_token_;
324 document[
"expires_at_ns"] =
325 std::chrono::duration_cast<std::chrono::nanoseconds>(token.expires_at_.time_since_epoch()).count();
326 const auto text = boost::json::serialize(document);
328 const auto temporary = identity +
".tmp";
330 ::unlinkat(folder.get(), temporary.c_str(), 0);
331 Descriptor file(::openat(folder.get(), temporary.c_str(), FILE_FLAGS | O_WRONLY | O_CREAT | O_EXCL, 0600));
332 bool written = file.get() >= 0;
333 for (std::size_t offset = 0; written && offset < text.size();) {
334 const auto count = ::write(file.get(), text.data() + offset, text.size() - offset);
336 offset +=
static_cast<std::size_t
>(count);
337 }
else if (count == 0 || errno != EINTR) {
341 written = written && ::fsync(file.get()) == 0 &&
342 ::renameat(folder.get(), temporary.c_str(), folder.get(), (identity +
".token").c_str()) == 0;
344 if (file.get() >= 0) {
345 ::unlinkat(folder.get(), temporary.c_str(), 0);
353 static std::optional<BrokerError> unlink_token(
const Descriptor& folder,
const std::string& identity) {
354 if (::unlinkat(folder.get(), (identity +
".token").c_str(), 0) != 0 && errno != ENOENT) {
361 std::chrono::milliseconds lock_wait_;