Audacity 3.2.0
OAuthService.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 OAuthService.h
7
8 Dmitry Vedenko
9
10**********************************************************************/
11#pragma once
12
13#include <chrono>
14#include <functional>
15#include <string_view>
16#include <string>
17#include <mutex>
18
19#include "Observer.h"
20
22{
23class ServiceConfig;
24
26struct CLOUD_AUDIOCOM_API AuthStateChangedMessage final
27{
29 std::string_view accessToken;
31 std::string_view errorMessage;
34 bool silent;
35};
36
38class CLOUD_AUDIOCOM_API OAuthService final :
39 public Observer::Publisher<AuthStateChangedMessage>
40{
41public:
43 bool HasAccessToken() const;
46 bool HasRefreshToken() const;
47
49 /*
50 If `HasAccessToken() || !HasRefreshToken()` invokes \p completedHandler synchronously,
51 if valid.
52
53 Otherwise, the service will attempt to authorize the user and invoke
54 \p completedHandler (if valid). Callback is invoked from the network thread.
55 \p silent indicates, that the service should not attempt to show any UI.
56
57 Callback argument will contain an access token, if any, or an empty view.
58 */
59 void ValidateAuth(std::function<void(std::string_view)> completedHandler, bool silent);
60
62 /*
63 This method supports the following URLs:
64 - audacity://link?authorization_code=code
65 - audacity://link?token=refresh_token
66 - audacity://link?username=user&password=password
67
68 This method will attempt to perform OAuth2 authorization and invoke \p completedHandler (if valid).
69 Callback is potentially called from the network thread.
70
71 \returns true if the URL was handled, false otherwise.
72 */
73 bool HandleLinkURI(
74 std::string_view uri,
75 std::function<void(std::string_view)> completedHandler);
76
78 void UnlinkAccount();
79
81 std::string GetAccessToken() const;
82
83private:
84 void AuthorisePassword(
85 const ServiceConfig& config, std::string_view userName,
86 std::string_view password,
87 std::function<void(std::string_view)> completedHandler);
88
89 void AuthoriseRefreshToken(
90 const ServiceConfig& config, std::string_view refreshToken,
91 std::function<void(std::string_view)> completedHandler, bool silent);
92
93 void AuthoriseRefreshToken(
94 const ServiceConfig& config,
95 std::function<void(std::string_view)> completedHandler, bool silent);
96
97 void AuthoriseCode(
98 const ServiceConfig& config, std::string_view authorizationCode,
99 std::function<void(std::string_view)> completedHandler);
100
101 void DoAuthorise(
102 const ServiceConfig& config, std::string_view payload,
103 std::function<void(std::string_view)> completedHandler, bool silent);
104
105 void SafePublish(const AuthStateChangedMessage& message);
106
107 using Clock = std::chrono::steady_clock;
108
109 mutable std::recursive_mutex mMutex;
110
111 Clock::time_point mTokenExpirationTime;
112 std::string mAccessToken;
113};
114
115
117CLOUD_AUDIOCOM_API OAuthService& GetOAuthService();
118
119} // namespace audacity::cloud::audiocom
An object that sends messages to an open-ended list of subscribed callbacks.
Definition: Observer.h:108
Service responsible for OAuth authentication against the audio.com service.
Definition: OAuthService.h:40
std::chrono::steady_clock Clock
Definition: OAuthService.h:107
Configuration for the audio.com.
Definition: ServiceConfig.h:23
OAuthService & GetOAuthService()
Returns the instance of the OAuthService.
Message that is sent when authorization state changes.
Definition: OAuthService.h:27
std::string_view accessToken
OAuth access token, valid for the current session or less.
Definition: OAuthService.h:29
bool authorised
Flag that indicates if user is authorised.
Definition: OAuthService.h:33
std::string_view errorMessage
Error message returned by the server in case of oauth error.
Definition: OAuthService.h:31