17#include <rapidjson/document.h>
18#include <rapidjson/writer.h>
42const std::string_view
uriPrefix =
"audacity://link";
49 rapidjson::Document& document, std::string_view grantType, std::string_view
scope)
51 using namespace rapidjson;
54 "grant_type", StringRef(grantType.data(), grantType.size()),
55 document.GetAllocator());
62 Value(clientID.data(), clientID.size(), document.GetAllocator()),
63 document.GetAllocator());
67 Value(clientSecret.data(), clientSecret.size(), document.GetAllocator()),
68 document.GetAllocator());
71 "scope", StringRef(
scope.data(),
scope.size()), document.GetAllocator());
77 std::function<
void(std::string_view)> completedHandler,
bool silent)
90 std::string_view uri, std::function<
void(std::string_view)> completedHandler)
101 const auto argsStart = uri.find(
"?");
105 if (completedHandler)
106 completedHandler({});
111 auto args = uri.substr(argsStart + 1);
113 std::string_view token;
114 std::string_view username;
115 std::string_view password;
116 std::string_view authorizationCode;
118 while (!args.empty())
120 const auto nextArg = args.find(
'&');
122 const auto arg = args.substr(0, nextArg);
136 if (!authorizationCode.empty())
141 else if (!token.empty())
146 else if (!username.empty() && !password.empty())
152 std::move(completedHandler));
156 if (completedHandler)
157 completedHandler({});
167 std::lock_guard<std::recursive_mutex> lock(
mMutex);
180 std::string_view password,
181 std::function<
void(std::string_view)> completedHandler)
183 using namespace rapidjson;
186 document.SetObject();
192 document.GetAllocator());
195 "password", StringRef(password.data(), password.size()),
196 document.GetAllocator());
198 rapidjson::StringBuffer buffer;
199 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
200 document.Accept(writer);
203 config, { buffer.GetString(), buffer.GetSize() },
204 std::move(completedHandler),
false);
209 std::function<
void(std::string_view)> completedHandler,
bool silent)
211 using namespace rapidjson;
214 document.SetObject();
219 "refresh_token", StringRef(token.data(), token.size()),
220 document.GetAllocator());
222 rapidjson::StringBuffer buffer;
223 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
224 document.Accept(writer);
227 config, { buffer.GetString(), buffer.GetSize() },
228 std::move(completedHandler), silent);
233 std::function<
void(std::string_view)> completedHandler,
bool silent)
235 std::lock_guard<std::recursive_mutex> lock(
mMutex);
239 std::move(completedHandler), silent);
243 const ServiceConfig& config, std::string_view authorizationCode,
244 std::function<
void(std::string_view)> completedHandler)
246 using namespace rapidjson;
249 document.SetObject();
254 "code", StringRef(authorizationCode.data(), authorizationCode.size()),
255 document.GetAllocator());
260 "redirect_uri", StringRef(redirectURI.data(), redirectURI.size()),
261 document.GetAllocator());
263 rapidjson::StringBuffer buffer;
264 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
265 document.Accept(writer);
268 config, { buffer.GetString(), buffer.GetSize() },
269 std::move(completedHandler),
false);
279 std::lock_guard<std::recursive_mutex> lock(
mMutex);
285 std::lock_guard<std::recursive_mutex> lock(
mMutex);
295 std::function<
void(std::string_view)> completedHandler,
bool silent)
308 request, payload.data(), payload.size());
310 response->setRequestFinishedCallback(
311 [response,
this,
handler = std::move(completedHandler), silent](
auto)
313 const auto httpCode = response->getHTTPCode();
314 const auto body = response->readAll<std::string>();
330 rapidjson::Document document;
331 document.Parse(body.data(), body.size());
333 if (!document.IsObject())
342 const auto tokenType = document[
"token_type"].GetString();
343 const auto accessToken = document[
"access_token"].GetString();
344 const auto expiresIn = document[
"expires_in"].GetInt64();
345 const auto newRefreshToken = document[
"refresh_token"].GetString();
348 std::lock_guard<std::recursive_mutex> lock(
mMutex);
350 mAccessToken = std::string(tokenType) +
" " + accessToken;
352 Clock::now() + std::chrono::seconds(expiresIn);
356 [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.
bool IsPrefixedInsensitive(const HayType &hay, const PrefixType &prefix)
bool IsPrefixed(const HayType &hay, const PrefixType &prefix)
Declare a function to decode an URL encode string.
CallbackReturn Publish(const AuthStateChangedMessage &message)
Send a message to connected callbacks.
Allows custom logic for preferences reset event.
bool Write(const T &value)
Write value to config and return true if successful.
void Invalidate() override
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
Service responsible for OAuth authentication against the audio.com service.
bool HasRefreshToken() const
void AuthoriseCode(const ServiceConfig &config, std::string_view authorizationCode, std::function< void(std::string_view)> completedHandler)
void UnlinkAccount()
Removes access and refresh token, notifies about the logout.
void SafePublish(const AuthStateChangedMessage &message)
void ValidateAuth(std::function< void(std::string_view)> completedHandler, bool silent)
Attempt to authorize the user.
bool HandleLinkURI(std::string_view uri, std::function< void(std::string_view)> completedHandler)
Handle the OAuth callback.
void AuthorisePassword(const ServiceConfig &config, std::string_view userName, std::string_view password, std::function< void(std::string_view)> completedHandler)
void DoAuthorise(const ServiceConfig &config, std::string_view payload, std::function< void(std::string_view)> completedHandler, bool silent)
Clock::time_point mTokenExpirationTime
bool HasAccessToken() const
Indicates, that service has a valid access token, i. e. that the user is authorized.
void AuthoriseRefreshToken(const ServiceConfig &config, std::string_view refreshToken, std::function< void(std::string_view)> completedHandler, bool silent)
std::recursive_mutex mMutex
std::string GetAccessToken() const
Return the current access token, if any.
Configuration for the audio.com.
std::string GetOAuthRedirectURL() const
OAuth2 redirect URL. Only used to satisfy the protocol.
std::string GetOAuthClientSecret() const
OAuth2 client secret.
std::string GetAPIUrl(std::string_view apiURI) const
Helper to construct the full URLs for the API.
std::string GetOAuthClientID() const
OAuth2 client ID.
void OnSettingResetBegin() override
Happens before preferences reset.
void OnSettingResetEnd() override
Happens after preferences reset.
static NetworkManager & GetInstance()
ResponsePtr doPost(const Request &request, const void *data, size_t size)
Request & setHeader(const std::string &name, std::string value)
void CallAfter(Action action)
Schedule an action to be done later, and in the main thread.
constexpr size_t npos(-1)
static CommandContext::TargetFactory::SubstituteInUnique< InteractiveOutputTargets > scope
AuthorizationHandler handler
static PreferencesResetHandler::Registration< OAuthServiceSettingsResetHandler > resetHandler
const std::string_view usernamePrefix
const std::string_view passwordPrefix
StringSetting refreshToken
const std::string_view tokenPrefix
const std::string_view authorizationCodePrefix
const std::string_view uriPrefix
void WriteCommonFields(rapidjson::Document &document, std::string_view grantType, std::string_view scope)
OAuthService & GetOAuthService()
Returns the instance of the OAuthService.
const ServiceConfig & GetServiceConfig()
Returns the instance of the ServiceConfig.
const std::string ApplicationJson
std::string ToUTF8(const std::wstring &wstr)
std::string UrlDecode(const std::string &url)
Performs single-time global handler registration.
Message that is sent when authorization state changes.