RLMCredentials.mm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 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 "RLMCredentials_Private.hpp"
  19. #import "RLMBSON_Private.hpp"
  20. #import "RLMUtil.hpp"
  21. #import <realm/object-store/util/bson/bson.hpp>
  22. using namespace realm::app;
  23. @implementation RLMCredentials
  24. - (instancetype)initWithAppCredentials:(AppCredentials&&)credentials {
  25. if (self = [super init]) {
  26. _appCredentials = std::move(credentials);
  27. _provider = @(_appCredentials.provider_as_string().data());
  28. return self;
  29. }
  30. return nil;
  31. }
  32. + (instancetype)credentialsWithFacebookToken:(RLMCredentialsToken)token {
  33. return [[self alloc] initWithAppCredentials:AppCredentials::facebook(token.UTF8String)];
  34. }
  35. + (instancetype)credentialsWithGoogleAuthCode:(RLMCredentialsToken)token {
  36. return [[self alloc] initWithAppCredentials:AppCredentials::google(AuthCode(token.UTF8String))];
  37. }
  38. + (instancetype)credentialsWithGoogleIdToken:(RLMCredentialsToken)token {
  39. return [[self alloc] initWithAppCredentials:AppCredentials::google(IdToken(token.UTF8String))];
  40. }
  41. + (instancetype)credentialsWithAppleToken:(RLMCredentialsToken)token {
  42. return [[self alloc] initWithAppCredentials:AppCredentials::apple(token.UTF8String)];
  43. }
  44. + (instancetype)credentialsWithEmail:(NSString *)username
  45. password:(NSString *)password {
  46. return [[self alloc] initWithAppCredentials:AppCredentials::username_password(username.UTF8String,
  47. password.UTF8String)];
  48. }
  49. + (instancetype)credentialsWithJWT:(NSString *)token {
  50. return [[self alloc] initWithAppCredentials:AppCredentials::custom(token.UTF8String)];
  51. }
  52. + (instancetype)credentialsWithFunctionPayload:(NSDictionary<NSString *, id<RLMBSON>> *)payload {
  53. return [[self alloc] initWithAppCredentials:AppCredentials::function(static_cast<realm::bson::BsonDocument>(RLMConvertRLMBSONToBson(payload)))];
  54. }
  55. + (instancetype)credentialsWithUserAPIKey:(NSString *)apiKey {
  56. return [[self alloc] initWithAppCredentials:AppCredentials::user_api_key(apiKey.UTF8String)];
  57. }
  58. + (instancetype)credentialsWithServerAPIKey:(NSString *)apiKey {
  59. return [[self alloc] initWithAppCredentials:AppCredentials::server_api_key(apiKey.UTF8String)];
  60. }
  61. + (instancetype)anonymousCredentials {
  62. return [[self alloc] initWithAppCredentials:AppCredentials::anonymous()];
  63. }
  64. - (BOOL)isEqual:(id)object {
  65. if (auto that = RLMDynamicCast<RLMCredentials>(object)) {
  66. return [self.provider isEqualToString:that.provider]
  67. && self.appCredentials.serialize_as_json() == that.appCredentials.serialize_as_json();
  68. }
  69. return NO;
  70. }
  71. @end