17#include <rapidjson/document.h>
18#include <rapidjson/writer.h>
40const std::string_view
uriPrefix =
"audacity://link";
47 rapidjson::Document& document, std::string_view grantType, std::string_view
scope)
49 using namespace rapidjson;
52 "grant_type", StringRef(grantType.data(), grantType.size()),
53 document.GetAllocator());
60 Value(clientID.data(), clientID.size(), document.GetAllocator()),
61 document.GetAllocator());
65 Value(clientSecret.data(), clientSecret.size(), document.GetAllocator()),
66 document.GetAllocator());
69 "scope", StringRef(
scope.data(),
scope.size()), document.GetAllocator());
72bool IsPrefixed(std::string_view hay, std::string_view prefix)
74 if (hay.length() < prefix.length())
78 prefix.begin(), prefix.end(), hay.begin(),
79 [](
auto a,
auto b) { return a == std::tolower(b); })
80 .first == prefix.end();
86 std::function<
void(std::string_view)> completedHandler)
99 std::string_view uri, std::function<
void(std::string_view)> completedHandler)
103 if (completedHandler)
104 completedHandler({});
110 const auto argsStart = uri.find(
"?");
114 if (completedHandler)
115 completedHandler({});
120 auto args = uri.substr(argsStart + 1);
122 std::string_view token;
123 std::string_view username;
124 std::string_view password;
125 std::string_view authorizationCode;
127 while (!args.empty())
129 const auto nextArg = args.find(
'&');
131 const auto arg = args.substr(0, nextArg);
145 if (!authorizationCode.empty())
150 else if (!token.empty())
155 else if (!username.empty() && !password.empty())
161 std::move(completedHandler));
165 if (completedHandler)
166 completedHandler({});
172 std::lock_guard<std::recursive_mutex> lock(
mMutex);
185 std::string_view password,
186 std::function<
void(std::string_view)> completedHandler)
188 using namespace rapidjson;
191 document.SetObject();
197 document.GetAllocator());
200 "password", StringRef(password.data(), password.size()),
201 document.GetAllocator());
203 rapidjson::StringBuffer buffer;
204 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
205 document.Accept(writer);
208 config, { buffer.GetString(), buffer.GetSize() },
209 std::move(completedHandler));
214 std::function<
void(std::string_view)> completedHandler)
216 using namespace rapidjson;
219 document.SetObject();
224 "refresh_token", StringRef(token.data(), token.size()),
225 document.GetAllocator());
227 rapidjson::StringBuffer buffer;
228 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
229 document.Accept(writer);
232 config, { buffer.GetString(), buffer.GetSize() },
233 std::move(completedHandler));
238 std::function<
void(std::string_view)> completedHandler)
240 std::lock_guard<std::recursive_mutex> lock(
mMutex);
244 std::move(completedHandler));
248 const ServiceConfig& config, std::string_view authorizationCode,
249 std::function<
void(std::string_view)> completedHandler)
251 using namespace rapidjson;
254 document.SetObject();
259 "code", StringRef(authorizationCode.data(), authorizationCode.size()),
260 document.GetAllocator());
265 "redirect_uri", StringRef(redirectURI.data(), redirectURI.size()),
266 document.GetAllocator());
268 rapidjson::StringBuffer buffer;
269 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
270 document.Accept(writer);
273 config, { buffer.GetString(), buffer.GetSize() },
274 std::move(completedHandler));
284 std::lock_guard<std::recursive_mutex> lock(
mMutex);
290 std::lock_guard<std::recursive_mutex> lock(
mMutex);
300 std::function<
void(std::string_view)> completedHandler)
313 request, payload.data(), payload.size());
315 response->setRequestFinishedCallback(
316 [response,
this,
handler = std::move(completedHandler)](
auto)
318 const auto httpCode = response->getHTTPCode();
319 const auto body = response->readAll<std::string>();
335 rapidjson::Document document;
336 document.Parse(body.data(), body.size());
338 if (!document.IsObject())
347 const auto tokenType = document[
"token_type"].GetString();
348 const auto accessToken = document[
"access_token"].GetString();
349 const auto expiresIn = document[
"expires_in"].GetInt64();
350 const auto newRefreshToken = document[
"refresh_token"].GetString();
353 std::lock_guard<std::recursive_mutex> lock(
mMutex);
355 mAccessToken = std::string(tokenType) +
" " + accessToken;
357 Clock::now() + std::chrono::seconds(expiresIn);
361 [token = std::string(newRefreshToken)]()
Toolkit-neutral facade for basic user interface services.
Declare functions to perform UTF-8 to std::wstring conversions.
Declare an interface for HTTP response.
Declare a class for performing HTTP requests.
audacity::BasicSettings * gPrefs
Declare a class for constructing HTTP requests.
Declare a function to decode an URL encode string.
CallbackReturn Publish(const AuthStateChangedMessage &message)
Send a message to connected callbacks.
bool Write(const T &value)
Write value to config and return true if successful.
bool Read(T *pVar) const
overload of Read returning a boolean that is true if the value was previously defined */
Specialization of Setting for strings.
virtual bool Flush() noexcept=0
Request & setHeader(const std::string &name, std::string value)
Service responsible for OAuth authentication against the audio.com service.
void AuthoriseCode(const ServiceConfig &config, std::string_view authorizationCode, std::function< void(std::string_view)> completedHandler)
std::string GetAccessToken() const
Return the current access token, if any.
void AuthoriseRefreshToken(const ServiceConfig &config, std::string_view refreshToken, std::function< void(std::string_view)> completedHandler)
void DoAuthorise(const ServiceConfig &config, std::string_view payload, std::function< void(std::string_view)> completedHandler)
void UnlinkAccount()
Removes access and refresh token, notifies about the logout.
bool HasRefreshToken() const
void HandleLinkURI(std::string_view uri, std::function< void(std::string_view)> completedHandler)
Handle the OAuth callback.
std::recursive_mutex mMutex
Clock::time_point mTokenExpirationTime
void SafePublish(const AuthStateChangedMessage &message)
void AuthorisePassword(const ServiceConfig &config, std::string_view userName, std::string_view password, std::function< void(std::string_view)> completedHandler)
bool HasAccessToken() const
Indicates, that service has a valid access token, i. e. that the user is authorized.
void ValidateAuth(std::function< void(std::string_view)> completedHandler)
Attempt to authorize the user.
Configuration for the audio.com.
std::string GetOAuthClientID() const
OAuth2 client ID.
std::string GetOAuthRedirectURL() const
OAuth2 redirect URL. Only used to satisfy the protocol.
std::string GetAPIUrl(std::string_view apiURI) const
Helper to construct the full URLs for the API.
std::string GetOAuthClientSecret() const
OAuth2 client secret.
void CallAfter(Action action)
Schedule an action to be done later, and in the main thread.
constexpr size_t npos(-1)
FrameStatistics & GetInstance() noexcept
static CommandContext::TargetFactory::SubstituteInUnique< InteractiveOutputTargets > scope
const std::string ApplicationJson
std::string ToUTF8(const std::wstring &wstr)
std::string UrlDecode(const std::string &url)
AuthorizationHandler handler
const std::string_view tokenPrefix
const std::string_view uriPrefix
const std::string_view usernamePrefix
StringSetting refreshToken
void WriteCommonFields(rapidjson::Document &document, std::string_view grantType, std::string_view scope)
const std::string_view authorizationCodePrefix
bool IsPrefixed(std::string_view hay, std::string_view prefix)
const std::string_view passwordPrefix
const ServiceConfig & GetServiceConfig()
Returns the instance of the ServiceConfig.
OAuthService & GetOAuthService()
Returns the instance of the OAuthService.
Message that is sent when authorization state changes.