Ditto 4.13.1
 
Loading...
Searching...
No Matches
Authenticator.hpp
1#ifndef DITTO_AUTHENTICATOR_H
2#define DITTO_AUTHENTICATOR_H
3
4#include "Ditto.hpp"
5
6#include "AuthenticationCallback.hpp"
7#include "AuthenticationStatus.hpp"
8#include "Observer.hpp"
9#include "SynchronizedValue.hpp"
10
11#include "authentication_status_handler.hpp"
12#include "dittoffi.hpp"
13
14#include <functional>
15#include <memory>
16#include <string>
17#include <vector>
18
19namespace ditto {
20class DittoConfig;
22class DittoError;
23class DittoHandleWrapper;
24
25// TODO(revamp-identity): add documentation
26using AuthenticationExpirationHandler =
27 std::function<void(Ditto &ditto, uint32_t sec_remaining)>;
28
34class Authenticator : public std::enable_shared_from_this<Authenticator> {
35 friend class AuthenticationStatusHandler;
36 friend class AuthenticationStatusObserver;
37 friend class Ditto;
38 friend class LoginProvider;
39 friend class OnlinePlaygroundIdentity;
40 friend class OnlineWithAuthenticationIdentity;
41
42public:
47
64 void login(std::string token, std::string provider,
65 std::function<void(std::unique_ptr<std::string>,
66 std::unique_ptr<DittoError>)>
67 login_handler) const;
68
79 DITTO_DEPRECATED_BECAUSE("Use `login` instead.")
81 std::string token, std::string provider,
82 std::function<void(std::unique_ptr<DittoError>)> login_handler) const;
83
96 std::string username, std::string password, std::string provider,
97 std::function<void(std::unique_ptr<DittoError>)> login_handler) const;
98
111 void logout(std::function<void(Ditto)> cleanup = [](Ditto ditto) {}) const;
112
118 std::shared_ptr<Observer>
119 observe_status(std::function<void(AuthenticationStatus status)> callback);
120
121 ~Authenticator() = default;
122
127 void set_expiration_handler(AuthenticationExpirationHandler handler) noexcept;
128
132 AuthenticationExpirationHandler get_expiration_handler() const noexcept;
133
134 // TODO(v5): Add an AuthenticationProvider class like the Swift SDK has.
141 static std::string get_development_provider();
142
143private:
144 AuthenticationStatus _status;
145 std::shared_ptr<DittoHandleWrapper> ditto_handle_wrapper;
146 std::weak_ptr<Ditto::Fields> weak_ditto_fields;
147 std::weak_ptr<AuthenticationCallback> callback;
148 std::vector<std::weak_ptr<AuthenticationStatusObserver>> status_observers;
149 internal::SynchronizedValue<AuthenticationExpirationHandler>
150 expiration_handler;
151
152 Authenticator(std::shared_ptr<DittoHandleWrapper> ditto_handle_wrapper,
153 std::weak_ptr<Ditto::Fields> ditto_fields,
154 std::shared_ptr<AuthenticationCallback> callback);
155
156 void stop_status_observer(
157 std::shared_ptr<AuthenticationStatusObserver> status_observer);
158 void authentication_expiring(uint32_t time_remaining);
159 void update_and_notify(bool should_notify);
160 void update_status_and_notify(AuthenticationStatus status);
161};
162
163} // namespace ditto
164
165#endif
Provides feedback to the developer about Ditto authentication status.
Definition AuthenticationCallback.hpp:13
Definition AuthenticationStatusObserver.hpp:15
void login(std::string token, std::string provider, std::function< void(std::unique_ptr< std::string >, std::unique_ptr< DittoError >)> login_handler) const
Log in to Ditto with a third-party token.
AuthenticationExpirationHandler get_expiration_handler() const noexcept
Get the authentication expiration callback.
static std::string get_development_provider()
Get the built-in development authentication provider to be used with development authentication token...
std::shared_ptr< Observer > observe_status(std::function< void(AuthenticationStatus status)> callback)
void logout(std::function< void(Ditto)> cleanup=[](Ditto ditto) {}) const
Log out of Ditto.
void login_with_token(std::string token, std::string provider, std::function< void(std::unique_ptr< DittoError >)> login_handler) const
Log in to Ditto with a third-party token.
AuthenticationStatus get_status() const
void login_with_credentials(std::string username, std::string password, std::string provider, std::function< void(std::unique_ptr< DittoError >)> login_handler) const
Log in to Ditto with a username and password.
void set_expiration_handler(AuthenticationExpirationHandler handler) noexcept
Set authentication expiration handler, typically provided via Identity but can be set directly.
All errors that are thrown by the Ditto SDK are wrapped as a DittoError.
Definition Errors.hpp:42
The entrypoint to the Ditto SDK.
Definition Ditto.hpp:39
Namespace for internal Ditto SDK functionality.
Definition Ditto.hpp:29
Namespace for the Ditto C++ SDK types and functions.
Definition AbstractDocumentPath.hpp:19
Provides info about the authentication status.
Definition AuthenticationStatus.hpp:10
Configuration options for initializing a Ditto instance.
Definition DittoConfig.hpp:30