diff --git a/src/files.cpp b/src/files.cpp index 61f00f3..e539a76 100644 --- a/src/files.cpp +++ b/src/files.cpp @@ -19,6 +19,8 @@ #include #include +#include +#include #include #include @@ -26,8 +28,10 @@ namespace FediBlock::files { using std::getenv; +using std::ifstream; using std::runtime_error; using std::string; +using std::stringstream; using std::uintmax_t; string _tmpdir; @@ -82,4 +86,18 @@ fs::path get_datadir() throw runtime_error{"Could not find data dir"}; } +string get_access_token() +{ + ifstream file(get_datadir() / "gitea_access_token"); + if (!file.good()) + { + throw runtime_error{"Could not read access token: " + + (get_datadir() / "gitea_access_token").string()}; + } + stringstream ss; + ss << file.rdbuf(); + + return ss.str().substr(0, ss.str().find('\n')); +} + } // namespace FediBlock::files diff --git a/src/files.hpp b/src/files.hpp index f2d8fe3..d78136a 100644 --- a/src/files.hpp +++ b/src/files.hpp @@ -19,9 +19,13 @@ #include "fs-compat.hpp" +#include + namespace FediBlock::files { +using std::string; + // Creates and returns the temporary directory. [[nodiscard]] fs::path get_tmpdir(); @@ -31,6 +35,9 @@ void remove_tmpdir(); // Returns the data directory and creates it if necessary. [[nodiscard]] fs::path get_datadir(); +// Read and return access token. +[[nodiscard]] string get_access_token(); + } // namespace FediBlock::files #endif // FEDIBLOCK_BACKEND_FILES_HPP