diff --git a/libraries/lib-cloud-audiocom/OAuthService.cpp b/libraries/lib-cloud-audiocom/OAuthService.cpp index 983966976b72..76667a6d7068 100644 --- a/libraries/lib-cloud-audiocom/OAuthService.cpp +++ b/libraries/lib-cloud-audiocom/OAuthService.cpp @@ -53,13 +53,14 @@ void WriteCommonFields( document.GetAllocator()); const auto clientID = GetServiceConfig().GetOAuthClientID(); + const auto clientSecret = GetServiceConfig().GetOAuthClientSecret(); document.AddMember( "client_id", StringRef(clientID.data(), clientID.size()), document.GetAllocator()); document.AddMember( - "client_secret", StringRef("shKqnY2sLTfRK7hztwzNEVxnmhJfOy1i"), + "client_secret", StringRef(clientSecret.data(), clientSecret.size()), document.GetAllocator()); document.AddMember( diff --git a/libraries/lib-cloud-audiocom/ServiceConfig.cpp b/libraries/lib-cloud-audiocom/ServiceConfig.cpp index b77a6725bc19..dea603ded539 100644 --- a/libraries/lib-cloud-audiocom/ServiceConfig.cpp +++ b/libraries/lib-cloud-audiocom/ServiceConfig.cpp @@ -11,53 +11,135 @@ #include "ServiceConfig.h" #include "Languages.h" -#include +#include #include +#include + +#include + +#include "Prefs.h" +#include "CodeConversions.h" + + namespace cloud::audiocom { -std::string_view ServiceConfig::GetAPIEndpoint() const +namespace { - return "https://api.audio.com"; +StringSetting audioComApiEndpoint { + L"/CloudServices/AudioCom/ApiEndpoint", + L"https://api.audio.com" +}; + +StringSetting audioComOAuthClientID { + L"/CloudServices/AudioCom/OAuthClientID", + L"1741964426607541" +}; + +StringSetting audioComOAuthClientSecret { + L"/CloudServices/AudioCom/OAuthClientSecret", + L"shKqnY2sLTfRK7hztwzNEVxnmhJfOy1i" +}; + +StringSetting audioComOAuthRedirectURL { + L"/CloudServices/AudioCom/OAuthRedirectURL", + L"https://audio.com/auth/sign-in/success" +}; + +StringSetting audioComOAuthLoginPage { + L"/CloudServices/AudioCom/OAuthLoginPage", + L"https://audio.com/audacity/link?clientId={auth_client_id}" +}; + +StringSetting audioComFinishUploadPage { + L"/CloudServices/AudioCom/OAuthLoginPage", + L"https://audio.com/audacity/upload?audioId={audio_id}&token={auth_token}&clientId={auth_client_id}" +}; + +StringSetting audioComAudioURL { + L"/CloudServices/AudioCom/AudioURL", + L"https://audio.com/{user_slug}/audio/{audio_slug}/edit" +}; + +StringSetting audioComAudioPreferredMimeType { + L"/CloudServices/AudioCom/PreferredMimeType", + L"https://audio.com/{user_slug}/audio/{audio_slug}/edit" +}; + +std::string Substitute(std::string pattern, std::initializer_list> substitutions) +{ + for(auto& [key, value] : substitutions) + { + auto pos = pattern.find(key); + + assert(pos != std::string::npos); + + if(pos != std::string::npos) + pattern.replace(pos, key.size(), value); + } + + return std::move(pattern); +} + +} // namespace + +ServiceConfig::ServiceConfig() +{ + mApiEndpoint = audacity::ToUTF8(audioComApiEndpoint.Read()); + mOAuthClientID = audacity::ToUTF8(audioComOAuthClientID.Read()); + mOAuthClientSecret = audacity::ToUTF8(audioComOAuthClientSecret.Read()); + mOAuthRedirectURL = audacity::ToUTF8(audioComOAuthRedirectURL.Read()); + mOAuthLoginPage = audacity::ToUTF8(audioComOAuthLoginPage.Read()); + mFinishUploadPage = audacity::ToUTF8(audioComFinishUploadPage.Read()); + mAudioURL = audacity::ToUTF8(audioComAudioURL.Read()); + mPreferredMimeType = audacity::ToUTF8(audioComAudioPreferredMimeType.Read()); } -std::string_view ServiceConfig::GetOAuthLoginPage() const +std::string ServiceConfig::GetAPIEndpoint() const { - static const std::string loginPage = - std::string("https://audio.com/audacity/link?clientId=") + - std::string(GetOAuthClientID()); + return mApiEndpoint; +} - return loginPage; +std::string ServiceConfig::GetOAuthLoginPage() const +{ + return Substitute( + mOAuthClientID, { { "auth_client_id", GetOAuthClientID() } }); +} + +std::string ServiceConfig::GetOAuthClientID() const +{ + return mOAuthClientID; } -std::string_view ServiceConfig::GetOAuthClientID() const +std::string ServiceConfig::GetOAuthClientSecret() const { - return "1741964426607541"; + return mOAuthClientSecret; } -std::string_view ServiceConfig::GetOAuthRedirectURL() const +std::string ServiceConfig::GetOAuthRedirectURL() const { - //return "audacity://link"; - return "https://audio.com/auth/sign-in/success"; + return mOAuthRedirectURL; } std::string ServiceConfig::GetAPIUrl(std::string_view apiURI) const { - return std::string(GetAPIEndpoint()) + std::string(apiURI); + return mApiEndpoint + std::string(apiURI); } std::string ServiceConfig::GetFinishUploadPage( std::string_view audioID, std::string_view token) const { - return "https://audio.com/audacity/upload?audioId=" + std::string(audioID) + - "&token=" + std::string(token) + - "&clientId=" + std::string(GetOAuthClientID()); + return Substitute( + mFinishUploadPage, { { "audio_id", audioID }, + { "auth_token", token }, + { "auth_client_id", mOAuthClientID } }); } std::string ServiceConfig::GetAudioURL( std::string_view userSlug, std::string_view audioSlug) const { - return "https://audio.com/" + std::string(userSlug) + "/audio/" + std::string(audioSlug) + "/edit"; + return Substitute( + mAudioURL, { { "user_slug", userSlug }, { "audio_slug", audioSlug } }); } std::chrono::milliseconds ServiceConfig::GetProgressCallbackTimeout() const @@ -99,7 +181,7 @@ rapidjson::Document ServiceConfig::GetExportConfig(const std::string& mimeType) std::string ServiceConfig::GetDownloadMime() const { - return "audio/x-wav"; + return mPreferredMimeType; } std::string ServiceConfig::GetAcceptLanguageValue() const diff --git a/libraries/lib-cloud-audiocom/ServiceConfig.h b/libraries/lib-cloud-audiocom/ServiceConfig.h index b8e681bce38b..6309e4bcac8a 100644 --- a/libraries/lib-cloud-audiocom/ServiceConfig.h +++ b/libraries/lib-cloud-audiocom/ServiceConfig.h @@ -12,7 +12,6 @@ #include #include -#include #include #include @@ -23,14 +22,17 @@ namespace cloud::audiocom class CLOUD_AUDIOCOM_API ServiceConfig final { public: + ServiceConfig(); //! API endpoint - std::string_view GetAPIEndpoint() const; - //! Page to open in browser to initiate OAuth - std::string_view GetOAuthLoginPage() const; + std::string GetAPIEndpoint() const; + //! Page to open in browser to initiate OAuth + std::string GetOAuthLoginPage() const; //! OAuth2 client ID - std::string_view GetOAuthClientID() const; + std::string GetOAuthClientID() const; + //! OAuth2 client secret + std::string GetOAuthClientSecret() const; //! OAuth2 redirect URL. Only used to satisfy the protocol - std::string_view GetOAuthRedirectURL() const; + std::string GetOAuthRedirectURL() const; //! Helper to construct the full URLs for the API std::string GetAPIUrl(std::string_view apiURI) const; //! Helper to construct the page URL for the anonymous upload last stage @@ -43,10 +45,21 @@ class CLOUD_AUDIOCOM_API ServiceConfig final std::vector GetPreferredAudioFormats() const; //! Export configuration suitable for the mime type provided rapidjson::Document GetExportConfig(const std::string& exporterName) const; - //! Return the mime type server should store the file. This is a requirement from audiocom + //! Return the mime type server should store the file. This is a requirement + //! from audiocom std::string GetDownloadMime() const; //! Returns the preferred language std::string GetAcceptLanguageValue() const; + +private: + std::string mApiEndpoint; + std::string mOAuthClientID; + std::string mOAuthClientSecret; + std::string mOAuthRedirectURL; + std::string mOAuthLoginPage; + std::string mFinishUploadPage; + std::string mAudioURL; + std::string mPreferredMimeType; }; //! Returns the instance of the ServiceConfig