RLMAPIKeyAuth.mm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2020 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. #import "RLMAPIKeyAuth.h"
  19. #import "RLMProviderClient_Private.hpp"
  20. #import "RLMApp_Private.hpp"
  21. #import "RLMUserAPIKey_Private.hpp"
  22. #import "RLMObjectId_Private.hpp"
  23. using namespace realm::app;
  24. @implementation RLMAPIKeyAuth
  25. - (App::UserAPIKeyProviderClient)client {
  26. return self.app._realmApp->provider_client<App::UserAPIKeyProviderClient>();
  27. }
  28. - (std::shared_ptr<realm::SyncUser>)currentUser {
  29. return self.app._realmApp->current_user();
  30. }
  31. static realm::util::UniqueFunction<void(App::UserAPIKey, std::optional<AppError>)>
  32. wrapCompletion(RLMOptionalUserAPIKeyBlock completion) {
  33. return [completion](App::UserAPIKey userAPIKey, std::optional<AppError> error) {
  34. if (error) {
  35. return completion(nil, makeError(*error));
  36. }
  37. return completion([[RLMUserAPIKey alloc] initWithUserAPIKey:userAPIKey], nil);
  38. };
  39. }
  40. - (void)createAPIKeyWithName:(NSString *)name
  41. completion:(RLMOptionalUserAPIKeyBlock)completion {
  42. self.client.create_api_key(name.UTF8String, self.currentUser, wrapCompletion(completion));
  43. }
  44. - (void)fetchAPIKey:(RLMObjectId *)objectId
  45. completion:(RLMOptionalUserAPIKeyBlock)completion {
  46. self.client.fetch_api_key(objectId.value, self.currentUser, wrapCompletion(completion));
  47. }
  48. - (void)fetchAPIKeysWithCompletion:(RLMUserAPIKeysBlock)completion {
  49. self.client.fetch_api_keys(self.currentUser,
  50. ^(const std::vector<App::UserAPIKey>& userAPIKeys,
  51. std::optional<AppError> error) {
  52. if (error) {
  53. return completion(nil, makeError(*error));
  54. }
  55. NSMutableArray *apiKeys = [[NSMutableArray alloc] init];
  56. for (auto &userAPIKey : userAPIKeys) {
  57. [apiKeys addObject:[[RLMUserAPIKey alloc] initWithUserAPIKey:userAPIKey]];
  58. }
  59. return completion(apiKeys, nil);
  60. });
  61. }
  62. - (void)deleteAPIKey:(RLMObjectId *)objectId
  63. completion:(RLMAPIKeyAuthOptionalErrorBlock)completion {
  64. self.client.delete_api_key(objectId.value, self.currentUser, RLMWrapCompletion(completion));
  65. }
  66. - (void)enableAPIKey:(RLMObjectId *)objectId
  67. completion:(RLMAPIKeyAuthOptionalErrorBlock)completion {
  68. self.client.enable_api_key(objectId.value, self.currentUser, RLMWrapCompletion(completion));
  69. }
  70. - (void)disableAPIKey:(RLMObjectId *)objectId
  71. completion:(RLMAPIKeyAuthOptionalErrorBlock)completion {
  72. self.client.disable_api_key(objectId.value, self.currentUser, RLMWrapCompletion(completion));
  73. }
  74. @end