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
21enum class AudiocomTrace;
22
24{
25class ServiceConfig;
26
28struct CLOUD_AUDIOCOM_API AuthStateChangedMessage final
29{
31 std::string_view accessToken;
33 std::string_view errorMessage;
37 bool silent;
38};
39
41class CLOUD_AUDIOCOM_API OAuthService final :
42 public Observer::Publisher<AuthStateChangedMessage>
43{
44public:
45 using AuthSuccessCallback = std::function<void(std::string_view)>;
46 using AuthFailureCallback = std::function<void(unsigned, std::string_view)>;
47
49 bool HasAccessToken() const;
52 bool HasRefreshToken() const;
53
55 /*
56 If `HasAccessToken() || !HasRefreshToken()` invokes \p completedHandler synchronously,
57 if valid.
58
59 Otherwise, the service will attempt to authorize the user and invoke
60 \p completedHandler (if valid). Callback is invoked from the network thread.
61 \p silent indicates, that the service should not attempt to show any UI.
62
63 Callback argument will contain an access token, if any, or an empty view.
64 */
65 void ValidateAuth(
66 AuthSuccessCallback completedHandler, AudiocomTrace,
67 bool silent);
68
70 /*
71 This method supports the following URLs:
72 - audacity://link?authorization_code=code
73 - audacity://link?token=refresh_token
74 - audacity://link?username=user&password=password
75
76 This method will attempt to perform OAuth2 authorization and invoke \p completedHandler (if valid).
77 Callback is potentially called from the network thread.
78
79 \returns true if the URL was handled, false otherwise.
80 */
81 bool HandleLinkURI(
82 std::string_view uri, AudiocomTrace,
83 AuthSuccessCallback completedHandler);
84
86 void UnlinkAccount(AudiocomTrace);
87
89 std::string GetAccessToken() const;
90
92 // with selected OAuth provider
93 static std::string MakeOAuthRequestURL(std::string_view authClientId);
94
96 std::string MakeAudioComAuthorizeURL(std::string_view userId, std::string_view redirectUrl);
97
98 void Authorize(std::string_view email,
99 std::string_view password,
100 AuthSuccessCallback successCallback,
101 AuthFailureCallback failureCallback,
102 AudiocomTrace trace);
103
105 void Register(std::string_view email,
106 std::string_view password,
107 AuthSuccessCallback successCallback,
108 AuthFailureCallback failureCallback,
109 AudiocomTrace trace);
110
111private:
112 void AuthorisePassword(
113 const ServiceConfig& config, std::string_view userName,
114 std::string_view password, AudiocomTrace,
115 std::function<void(std::string_view)> completedHandler);
116
117 void AuthoriseRefreshToken(
118 const ServiceConfig& config, std::string_view refreshToken,
119 AudiocomTrace, std::function<void(std::string_view)> completedHandler,
120 bool silent);
121
122 void AuthoriseRefreshToken(
123 const ServiceConfig& config, AudiocomTrace,
124 std::function<void(std::string_view)> completedHandler, bool silent);
125
126 void AuthoriseCode(
127 const ServiceConfig& config, std::string_view authorizationCode, bool useAudioComRedirectURI,
128 AudiocomTrace, std::function<void(std::string_view)> completedHandler);
129
130 void DoAuthorise(
131 const ServiceConfig& config, std::string_view payload, AudiocomTrace,
132 std::function<void(std::string_view)> completedHandler, bool silent);
133
134 void ParseTokenResponse(std::string_view response,
135 AuthSuccessCallback successCallback,
136 AuthFailureCallback failureCallback,
137 AudiocomTrace trace,
138 bool silent);
139
140 void SafePublish(const AuthStateChangedMessage& message);
141
142 using Clock = std::chrono::steady_clock;
143
144 mutable std::recursive_mutex mMutex;
145
146 Clock::time_point mTokenExpirationTime;
147 std::string mAccessToken;
148};
149
150
152CLOUD_AUDIOCOM_API OAuthService& GetOAuthService();
153
154} // namespace audacity::cloud::audiocom
AudiocomTrace
Definition: ExportUtils.h:27
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:43
std::function< void(std::string_view)> AuthSuccessCallback
Definition: OAuthService.h:45
std::chrono::steady_clock Clock
Definition: OAuthService.h:142
std::function< void(unsigned, std::string_view)> AuthFailureCallback
Definition: OAuthService.h:46
Configuration for the audio.com.
Definition: ServiceConfig.h:25
OAuthService & GetOAuthService()
Returns the instance of the OAuthService.
Message that is sent when authorization state changes.
Definition: OAuthService.h:29
std::string_view accessToken
OAuth access token, valid for the current session or less.
Definition: OAuthService.h:31
bool authorised
Flag that indicates if user is authorised.
Definition: OAuthService.h:36
std::string_view errorMessage
Error message returned by the server in case of oauth error.
Definition: OAuthService.h:33