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 <mutex>
17
18#include "Observer.h"
19
20namespace cloud::audiocom
21{
22class ServiceConfig;
23
25struct CLOUD_AUDIOCOM_API AuthStateChangedMessage final
26{
28 std::string_view accessToken;
30 std::string_view errorMessage;
33};
34
36class CLOUD_AUDIOCOM_API OAuthService final :
37 public Observer::Publisher<AuthStateChangedMessage>
38{
39public:
41 bool HasAccessToken() const;
44 bool HasRefreshToken() const;
45
47 /*
48 If `HasAccessToken() || !HasRefreshToken()` invokes \p completedHandler synchronously,
49 if valid.
50
51 Otherwise, the service will attempt to authorize the user and invoke
52 \p completedHandler (if valid). Callback is invoked from the network thread.
53
54 Callback argument will contain an access token, if any, or an empty view.
55 */
56 void ValidateAuth(std::function<void(std::string_view)> completedHandler);
57
59 /*
60 This method supports the following URLs:
61 - audacity://link?authorization_code=code
62 - audacity://link?token=refresh_token
63 - audacity://link?username=user&password=password
64
65 This method will attempt to perform OAuth2 authorization and invoke \p completedHandler (if valid).
66 Callback is potentially called from the network thread.
67 */
68 void HandleLinkURI(
69 std::string_view uri,
70 std::function<void(std::string_view)> completedHandler);
71
73 void UnlinkAccount();
74
76 std::string GetAccessToken() const;
77
78private:
79 void AuthorisePassword(
80 const ServiceConfig& config, std::string_view userName,
81 std::string_view password,
82 std::function<void(std::string_view)> completedHandler);
83
84 void AuthoriseRefreshToken(
85 const ServiceConfig& config, std::string_view refreshToken,
86 std::function<void(std::string_view)> completedHandler);
87
88 void AuthoriseRefreshToken(
89 const ServiceConfig& config,
90 std::function<void(std::string_view)> completedHandler);
91
92 void AuthoriseCode(
93 const ServiceConfig& config, std::string_view authorizationCode,
94 std::function<void(std::string_view)> completedHandler);
95
96 void DoAuthorise(
97 const ServiceConfig& config, std::string_view payload,
98 std::function<void(std::string_view)> completedHandler);
99
100 void SafePublish(const AuthStateChangedMessage& message);
101
102 using Clock = std::chrono::steady_clock;
103
104 mutable std::recursive_mutex mMutex;
105
106 Clock::time_point mTokenExpirationTime;
107 std::string mAccessToken;
108};
109
110
112CLOUD_AUDIOCOM_API OAuthService& GetOAuthService();
113
114} // namespace 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:38
std::recursive_mutex mMutex
Definition: OAuthService.h:104
Clock::time_point mTokenExpirationTime
Definition: OAuthService.h:106
std::chrono::steady_clock Clock
Definition: OAuthService.h:102
Configuration for the audio.com.
Definition: ServiceConfig.h:24
OAuthService & GetOAuthService()
Returns the instance of the OAuthService.
Message that is sent when authorization state changes.
Definition: OAuthService.h:26
std::string_view errorMessage
Error message returned by the server in case of oauth error.
Definition: OAuthService.h:30
bool authorised
Flag that indicates if user is authorised.
Definition: OAuthService.h:32
std::string_view accessToken
OAuth access token, valid for the current session or less.
Definition: OAuthService.h:28