58 lines
1.5 KiB
C++
58 lines
1.5 KiB
C++
#pragma once
|
|
|
|
#include <chrono>
|
|
#include <cstdint>
|
|
#include <filesystem>
|
|
#include <optional>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
#include <Spotify/Client.hpp> // for ProxyConfig + CurlDeleter + ProxyType
|
|
|
|
namespace Spotify {
|
|
|
|
struct TokenSet {
|
|
std::string access_token;
|
|
std::string token_type;
|
|
std::string scope;
|
|
std::string refresh_token;
|
|
std::int64_t expires_at_utc{}; // unix seconds
|
|
|
|
bool IsExpired(std::chrono::seconds skew = std::chrono::seconds{60}) const;
|
|
};
|
|
|
|
class PkceAuth {
|
|
public:
|
|
PkceAuth(std::string clientId, std::string redirectUri, std::filesystem::path tokenCachePath);
|
|
|
|
void SetProxy(ProxyConfig p) { m_Proxy = std::move(p); }
|
|
void SetVerbose(bool v) { m_Verbose = v; }
|
|
|
|
// One-time interactive step: prints URL and waits for redirect; stores refresh token.
|
|
void AuthorizeAndStore(const std::vector<std::string>& scopes,
|
|
std::chrono::milliseconds timeout = std::chrono::minutes(5));
|
|
|
|
// Headless: load from cache and refresh if needed.
|
|
TokenSet LoadOrRefresh();
|
|
|
|
private:
|
|
std::string m_ClientId;
|
|
std::string m_RedirectUri;
|
|
std::filesystem::path m_TokenCachePath;
|
|
ProxyConfig m_Proxy;
|
|
bool m_Verbose = false;
|
|
|
|
TokenSet ExchangeCodeForToken(const std::string& code, const std::string& codeVerifier);
|
|
TokenSet RefreshAccessToken(const TokenSet& current);
|
|
|
|
void Save(const TokenSet& t) const;
|
|
std::optional<TokenSet> Load() const;
|
|
|
|
static std::int64_t NowUnixSeconds();
|
|
friend TokenSet;
|
|
};
|
|
|
|
} // namespace ts6bot
|