|
| 1 | +//////////////////////////////////////////////////////////////////////////// |
| 2 | +// |
| 3 | +// Copyright 2022 Realm Inc. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +// you may not use this file except in compliance with the License. |
| 7 | +// You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, software |
| 12 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +// See the License for the specific language governing permissions and |
| 15 | +// limitations under the License. |
| 16 | +// |
| 17 | +//////////////////////////////////////////////////////////////////////////// |
| 18 | + |
| 19 | +#ifndef CPPREALM_APP_HPP |
| 20 | +#define CPPREALM_APP_HPP |
| 21 | + |
| 22 | +#include <cpprealm/bson.hpp> |
| 23 | +#include <cpprealm/db.hpp> |
| 24 | + |
| 25 | +#include <cpprealm/internal/bridge/realm.hpp> |
| 26 | +#include <cpprealm/internal/bridge/sync_error.hpp> |
| 27 | +#include <cpprealm/internal/bridge/sync_manager.hpp> |
| 28 | +#include <cpprealm/internal/bridge/sync_session.hpp> |
| 29 | +#include <cpprealm/internal/bridge/utils.hpp> |
| 30 | + |
| 31 | +#include <future> |
| 32 | +#include <utility> |
| 33 | + |
| 34 | +namespace realm { |
| 35 | + using proxy_config = sync_config::proxy_config; |
| 36 | + using sync_session = internal::bridge::sync_session; |
| 37 | + class SyncUser; |
| 38 | + |
| 39 | + namespace app { |
| 40 | + class App; |
| 41 | + struct AppError; |
| 42 | + }// namespace app |
| 43 | + namespace internal::bridge { |
| 44 | + struct sync_error; |
| 45 | + } |
| 46 | + |
| 47 | +// MARK: User |
| 48 | + |
| 49 | +// Represents an error state from the server. |
| 50 | +struct app_error { |
| 51 | + app_error() = delete; |
| 52 | + app_error(const app_error& other); |
| 53 | + app_error& operator=(const app_error& other) ; |
| 54 | + app_error(app_error&& other); |
| 55 | + app_error& operator=(app_error&& other); |
| 56 | + ~app_error(); |
| 57 | + app_error(realm::app::AppError&& error); //NOLINT(google-explicit-constructor) |
| 58 | + |
| 59 | + [[nodiscard]] std::string_view message() const; |
| 60 | + |
| 61 | + [[nodiscard]] std::string_view link_to_server_logs() const; |
| 62 | + |
| 63 | + [[nodiscard]] bool is_json_error() const; |
| 64 | + |
| 65 | + [[nodiscard]] bool is_service_error() const; |
| 66 | + |
| 67 | + [[nodiscard]] bool is_http_error() const; |
| 68 | + |
| 69 | + [[nodiscard]] bool is_custom_error() const; |
| 70 | + |
| 71 | + [[nodiscard]] bool is_client_error() const; |
| 72 | +private: |
| 73 | +#ifdef CPPREALM_HAVE_GENERATED_BRIDGE_TYPES |
| 74 | + internal::bridge::storage::AppError m_error[1]; |
| 75 | +#else |
| 76 | + std::shared_ptr<realm::app::AppError> m_error; |
| 77 | +#endif |
| 78 | +}; |
| 79 | + |
| 80 | +/** |
| 81 | + A `User` instance represents a single Realm App user account. |
| 82 | +
|
| 83 | + A user may have one or more credentials associated with it. These credentials |
| 84 | + uniquely identify the user to the authentication provider, and are used to sign |
| 85 | + into a MongoDB Realm user account. |
| 86 | +
|
| 87 | + User objects can be accessed from any thread. |
| 88 | + */ |
| 89 | +struct user { |
| 90 | + user() = default; |
| 91 | + user(const user&) = default; |
| 92 | + user(user&&) = default; |
| 93 | + user& operator=(const user&) = default; |
| 94 | + user& operator=(user&&) = default; |
| 95 | + explicit user(std::shared_ptr<SyncUser> user); |
| 96 | + bool is_logged_in() const; |
| 97 | + |
| 98 | + /** |
| 99 | + The state of the user object. |
| 100 | + */ |
| 101 | + enum class state : uint8_t { |
| 102 | + logged_out, |
| 103 | + logged_in, |
| 104 | + removed, |
| 105 | + }; |
| 106 | + |
| 107 | + /** |
| 108 | + The unique MongoDB Realm string identifying this user. |
| 109 | + Note this is different from an identitiy: A user may have multiple identities but has a single indentifier. See RLMUserIdentity. |
| 110 | + */ |
| 111 | + [[nodiscard]] std::string identifier() const; |
| 112 | + |
| 113 | + /** |
| 114 | + The current state of the user. |
| 115 | + */ |
| 116 | + [[nodiscard]] state state() const; |
| 117 | + |
| 118 | + /** |
| 119 | + The user's refresh token used to access the Realm Application. |
| 120 | +
|
| 121 | + This is required to make HTTP requests to MongoDB Realm's REST API |
| 122 | + for functionality not exposed natively. It should be treated as sensitive data. |
| 123 | + */ |
| 124 | + [[nodiscard]] std::string access_token() const; |
| 125 | + |
| 126 | + /** |
| 127 | + The user's refresh token used to access the Realm Applcation. |
| 128 | +
|
| 129 | + This is required to make HTTP requests to the Realm App's REST API |
| 130 | + for functionality not exposed natively. It should be treated as sensitive data. |
| 131 | + */ |
| 132 | + [[nodiscard]] std::string refresh_token() const; |
| 133 | + |
| 134 | + struct internal::bridge::sync_manager sync_manager() const; |
| 135 | + |
| 136 | + [[nodiscard]] db_config flexible_sync_configuration() const |
| 137 | + { |
| 138 | + db_config config; |
| 139 | + config.set_sync_config(sync_config(m_user)); |
| 140 | + config.sync_config().set_error_handler([](const sync_session& session, const internal::bridge::sync_error& error) { |
| 141 | + std::cerr<<"sync error: "<<error.message()<<std::endl; |
| 142 | + }); |
| 143 | + config.set_path(sync_manager().path_for_realm(config.sync_config())); |
| 144 | + config.sync_config().set_stop_policy(realm::internal::bridge::realm::sync_session_stop_policy::after_changes_uploaded); |
| 145 | + config.set_schema_mode(realm::internal::bridge::realm::config::schema_mode::additive_discovered); |
| 146 | + return config; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + Logs out the current user |
| 151 | +
|
| 152 | + The users state will be set to `Removed` is they are an anonymous user or `LoggedOut` |
| 153 | + if they are authenticated by an email / password or third party auth clients |
| 154 | + If the logout request fails, this method will still clear local authentication state. |
| 155 | + */ |
| 156 | + void log_out(std::function<void(std::optional<app_error>)>&& callback) const; |
| 157 | + |
| 158 | + [[nodiscard]] std::future<void> log_out() const; |
| 159 | + |
| 160 | + |
| 161 | + [[deprecated("Replaced by `get_custom_data()`. This method will be removed in a future release.")]] |
| 162 | + [[nodiscard]] std::optional<std::string> custom_data() const; |
| 163 | + |
| 164 | + /** |
| 165 | + The custom data of the user. |
| 166 | + This is configured in your Atlas App Services app. |
| 167 | + */ |
| 168 | + [[nodiscard]] std::optional<bsoncxx::document> get_custom_data() const; |
| 169 | + |
| 170 | + /** |
| 171 | + Calls the Atlas App Services function with the provided name and arguments. |
| 172 | +
|
| 173 | + @param name The name of the Atlas App Services function to be called. |
| 174 | + @param arguments The string represented extended json to be provided to the function. |
| 175 | + @param callback The completion handler to call when the function call is complete. |
| 176 | + This handler is executed on the thread the method was called from. |
| 177 | + */ |
| 178 | + [[deprecated("This function is deprecated and will replaced by the BSON based call_function API.")]] |
| 179 | + void call_function(const std::string& name, const std::string& args_ejson, |
| 180 | + std::function<void(std::optional<std::string>, std::optional<app_error>)> callback) const; |
| 181 | + |
| 182 | + /** |
| 183 | + Calls the Atlas App Services function with the provided name and arguments. |
| 184 | +
|
| 185 | + @param name The name of the Atlas App Services function to be called. |
| 186 | + @param arguments The string represented extended json to be provided to the function. |
| 187 | + @return A future containing an optional std::string once the operation has completed. |
| 188 | + This handler is executed on the thread the method was called from. |
| 189 | + */ |
| 190 | + [[deprecated("This function is deprecated and will replaced by the BSON based call_function API.")]] |
| 191 | + [[nodiscard]] std::future<std::optional<std::string>> call_function(const std::string& name, |
| 192 | + const std::string& args_ejson) const; |
| 193 | + |
| 194 | + /** |
| 195 | + Calls the Atlas App Services function with the provided name and arguments. |
| 196 | +
|
| 197 | + @param name The name of the Atlas App Services function to be called. |
| 198 | + @param arguments The BSON array to be provided to the function. |
| 199 | + @param callback The completion handler to call when the function call is complete. |
| 200 | + This handler is executed on the thread the method was called from. |
| 201 | + */ |
| 202 | + void call_function(const std::string& name, const std::vector<bsoncxx>& args_bson, |
| 203 | + std::function<void(std::optional<bsoncxx>, std::optional<app_error>)> callback) const; |
| 204 | + |
| 205 | + /** |
| 206 | + Calls the Atlas App Services function with the provided name and arguments. |
| 207 | +
|
| 208 | + @param name The name of the Atlas App Services function to be called. |
| 209 | + @param arguments The BSON array to be provided to the function. |
| 210 | + @return A future containing optional BSON once the operation has completed. |
| 211 | + */ |
| 212 | + [[nodiscard]] std::future<std::optional<bsoncxx>> call_function(const std::string& name, const std::vector<bsoncxx>& args_bson) const; |
| 213 | + |
| 214 | + /** |
| 215 | + Refresh a user's custom data. This will, in effect, refresh the user's auth session. |
| 216 | + */ |
| 217 | + void refresh_custom_user_data(std::function<void(std::optional<app_error>)> callback); |
| 218 | + |
| 219 | + /** |
| 220 | + Refresh a user's custom data. This will, in effect, refresh the user's auth session. |
| 221 | + */ |
| 222 | + [[nodiscard]] std::future<void> refresh_custom_user_data() const; |
| 223 | + |
| 224 | + std::shared_ptr<SyncUser> m_user; |
| 225 | +}; |
| 226 | + |
| 227 | +bool operator==(const user& lhs, const user& rhs); |
| 228 | +bool operator!=(const user& lhs, const user& rhs); |
| 229 | + |
| 230 | +namespace util { |
| 231 | + template <class T> |
| 232 | + class TaggedString; |
| 233 | +} |
| 234 | +namespace app { |
| 235 | + struct AppCredentials; |
| 236 | +} |
| 237 | + |
| 238 | +class App { |
| 239 | +public: |
| 240 | + struct configuration { |
| 241 | + std::string app_id; |
| 242 | + std::optional<std::string> base_url; |
| 243 | + std::optional<std::string> path; |
| 244 | + std::optional<std::map<std::string, std::string>> custom_http_headers; |
| 245 | + std::optional<std::array<char, 64>> metadata_encryption_key; |
| 246 | + std::optional<sync_config::proxy_config> proxy_configuration; |
| 247 | + }; |
| 248 | + |
| 249 | + [[deprecated("Use App(const configuration&) instead.")]] |
| 250 | + explicit App(const std::string& app_id, |
| 251 | + const std::optional<std::string>& base_url = {}, |
| 252 | + const std::optional<std::string>& path = {}, |
| 253 | + const std::optional<std::map<std::string, std::string>>& custom_http_headers = {}); |
| 254 | + |
| 255 | + App(const configuration&); |
| 256 | + |
| 257 | + struct credentials { |
| 258 | + static credentials anonymous(); |
| 259 | + static credentials api_key(const std::string& key); |
| 260 | + static credentials facebook(const std::string& access_token); |
| 261 | + static credentials apple(const std::string& id_token); |
| 262 | + static credentials google_auth_code(const std::string& auth_code); |
| 263 | + static credentials google_id_token(const std::string& id_token); |
| 264 | + static credentials custom(const std::string& token); |
| 265 | + static credentials username_password(const std::string& username, const std::string& password); |
| 266 | + static credentials function(const std::string& payload); |
| 267 | + credentials(); |
| 268 | + credentials(const credentials& other) ; |
| 269 | + credentials& operator=(const credentials& other) ; |
| 270 | + credentials(credentials&& other); |
| 271 | + credentials& operator=(credentials&& other); |
| 272 | + ~credentials(); |
| 273 | + private: |
| 274 | + credentials(app::AppCredentials&& credentials) noexcept; |
| 275 | + operator app::AppCredentials() const; |
| 276 | + friend class App; |
| 277 | + |
| 278 | +#ifdef CPPREALM_HAVE_GENERATED_BRIDGE_TYPES |
| 279 | + internal::bridge::storage::AppCredentials m_credentials[1]; |
| 280 | +#else |
| 281 | + std::shared_ptr<app::AppCredentials> m_credentials; |
| 282 | +#endif |
| 283 | + }; |
| 284 | + |
| 285 | + std::future<void> register_user(const std::string& username, const std::string& password); |
| 286 | + std::future<user> login(const credentials &credentials); |
| 287 | + void login(const credentials& credentials, std::function<void(user, std::optional<app_error>)>&& callback); |
| 288 | + [[nodiscard]] internal::bridge::sync_manager get_sync_manager() const; |
| 289 | + [[nodiscard]] std::optional<user> get_current_user() const; |
| 290 | + void clear_cached_apps(); |
| 291 | + std::optional<App> get_cached_app(const std::string& app_id, const std::optional<std::string>& base_url); |
| 292 | +private: |
| 293 | + std::shared_ptr<app::App> m_app; |
| 294 | + App(std::shared_ptr<app::App>&& a) : m_app(std::move(a)) { } |
| 295 | +}; |
| 296 | + |
| 297 | +} |
| 298 | +#endif /* CPPREALM_APP_HPP */ |
0 commit comments