2
0

AccessKeyFetcher.h 949 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #pragma once
  2. #include <functional> // for function
  3. #include <memory> // for shared_ptr
  4. #include <string> // for string
  5. #include <atomic>
  6. namespace bell {
  7. class WrappedSemaphore;
  8. };
  9. namespace cspot {
  10. struct Context;
  11. class AccessKeyFetcher {
  12. public:
  13. AccessKeyFetcher(std::shared_ptr<cspot::Context> ctx);
  14. /**
  15. * @brief Checks if key is expired
  16. * @returns true when currently held access key is not valid
  17. */
  18. bool isExpired();
  19. /**
  20. * @brief Fetches a new access key
  21. * @remark In case the key is expired, this function blocks until a refresh is done.
  22. * @returns access key
  23. */
  24. std::string getAccessKey();
  25. /**
  26. * @brief Forces a refresh of the access key
  27. */
  28. void updateAccessKey();
  29. private:
  30. std::shared_ptr<cspot::Context> ctx;
  31. std::shared_ptr<bell::WrappedSemaphore> updateSemaphore;
  32. std::atomic<bool> keyPending = false;
  33. std::string accessKey;
  34. long long int expiresAt;
  35. };
  36. } // namespace cspot