RLMAPIKeyAuth.mm 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. @implementation RLMAPIKeyAuth
  24. - (realm::app::App::UserAPIKeyProviderClient)client {
  25. return self.app._realmApp->provider_client<realm::app::App::UserAPIKeyProviderClient>();
  26. }
  27. - (std::shared_ptr<realm::SyncUser>)currentUser {
  28. return self.app._realmApp->current_user();
  29. }
  30. - (void)createAPIKeyWithName:(NSString *)name
  31. completion:(RLMOptionalUserAPIKeyBlock)completion {
  32. self.client.create_api_key(name.UTF8String, self.currentUser,
  33. ^(realm::util::Optional<realm::app::App::UserAPIKey> userAPIKey,
  34. realm::util::Optional<realm::app::AppError> error) {
  35. if (error && error->error_code) {
  36. return completion(nil, RLMAppErrorToNSError(*error));
  37. }
  38. if (userAPIKey) {
  39. return completion([[RLMUserAPIKey alloc] initWithUserAPIKey:userAPIKey.value()], nil);
  40. }
  41. return completion(nil, nil);
  42. });
  43. }
  44. - (void)fetchAPIKey:(RLMObjectId *)objectId
  45. completion:(RLMOptionalUserAPIKeyBlock)completion {
  46. self.client.fetch_api_key(objectId.value,
  47. self.currentUser,
  48. ^(realm::util::Optional<realm::app::App::UserAPIKey> userAPIKey,
  49. realm::util::Optional<realm::app::AppError> error) {
  50. if (error && error->error_code) {
  51. return completion(nil, RLMAppErrorToNSError(*error));
  52. }
  53. if (userAPIKey) {
  54. return completion([[RLMUserAPIKey alloc] initWithUserAPIKey:userAPIKey.value()], nil);
  55. }
  56. return completion(nil, nil);
  57. });
  58. }
  59. - (void)fetchAPIKeysWithCompletion:(RLMUserAPIKeysBlock)completion {
  60. self.client.fetch_api_keys(self.currentUser,
  61. ^(const std::vector<realm::app::App::UserAPIKey>& userAPIKeys,
  62. realm::util::Optional<realm::app::AppError> error) {
  63. if (error && error->error_code) {
  64. return completion(nil, RLMAppErrorToNSError(*error));
  65. }
  66. NSMutableArray *apiKeys = [[NSMutableArray alloc] init];
  67. for(auto &userAPIKey : userAPIKeys) {
  68. [apiKeys addObject:[[RLMUserAPIKey alloc] initWithUserAPIKey:userAPIKey]];
  69. }
  70. return completion(apiKeys, nil);
  71. });
  72. }
  73. - (void)deleteAPIKey:(RLMObjectId *)objectId
  74. completion:(RLMAPIKeyAuthOptionalErrorBlock)completion {
  75. self.client.delete_api_key(objectId.value,
  76. self.currentUser,
  77. ^(realm::util::Optional<realm::app::AppError> error) {
  78. [self handleResponse:error completion:completion];
  79. });
  80. }
  81. - (void)enableAPIKey:(RLMObjectId *)objectId
  82. completion:(RLMAPIKeyAuthOptionalErrorBlock)completion {
  83. self.client.enable_api_key(objectId.value,
  84. self.currentUser,
  85. ^(realm::util::Optional<realm::app::AppError> error) {
  86. [self handleResponse:error completion:completion];
  87. });
  88. }
  89. - (void)disableAPIKey:(RLMObjectId *)objectId
  90. completion:(RLMAPIKeyAuthOptionalErrorBlock)completion {
  91. self.client.disable_api_key(objectId.value,
  92. self.currentUser,
  93. ^(realm::util::Optional<realm::app::AppError> error) {
  94. [self handleResponse:error completion:completion];
  95. });
  96. }
  97. @end