RLMSyncCredentials.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 "RLMSyncCredentials.h"
  19. #import "RLMSyncUtil_Private.h"
  20. /// A Twitter account as an identity provider.
  21. //extern RLMIdentityProvider const RLMIdentityProviderTwitter;
  22. RLMIdentityProvider const RLMIdentityProviderDebug = @"debug";
  23. RLMIdentityProvider const RLMIdentityProviderRealm = @"realm";
  24. RLMIdentityProvider const RLMIdentityProviderUsernamePassword = @"password";
  25. RLMIdentityProvider const RLMIdentityProviderFacebook = @"facebook";
  26. RLMIdentityProvider const RLMIdentityProviderTwitter = @"twitter";
  27. RLMIdentityProvider const RLMIdentityProviderGoogle = @"google";
  28. RLMIdentityProvider const RLMIdentityProviderCloudKit = @"cloudkit";
  29. RLMIdentityProvider const RLMIdentityProviderJWT = @"jwt";
  30. RLMIdentityProvider const RLMIdentityProviderAnonymous = @"anonymous";
  31. RLMIdentityProvider const RLMIdentityProviderNickname = @"nickname";
  32. @interface RLMSyncCredentials ()
  33. - (instancetype)initWithCustomToken:(RLMSyncCredentialsToken)token
  34. provider:(RLMIdentityProvider)provider
  35. userInfo:(NSDictionary *)userInfo NS_DESIGNATED_INITIALIZER;
  36. @property (nonatomic, readwrite) RLMSyncCredentialsToken token;
  37. @property (nonatomic, readwrite) RLMIdentityProvider provider;
  38. @property (nonatomic, readwrite) NSDictionary *userInfo;
  39. @end
  40. @implementation RLMSyncCredentials
  41. + (instancetype)credentialsWithFacebookToken:(RLMSyncCredentialsToken)token {
  42. return [[self alloc] initWithCustomToken:token provider:RLMIdentityProviderFacebook userInfo:nil];
  43. }
  44. + (instancetype)credentialsWithGoogleToken:(RLMSyncCredentialsToken)token {
  45. return [[self alloc] initWithCustomToken:token provider:RLMIdentityProviderGoogle userInfo:nil];
  46. }
  47. + (instancetype)credentialsWithCloudKitToken:(RLMSyncCredentialsToken)token {
  48. return [[self alloc] initWithCustomToken:token provider:RLMIdentityProviderCloudKit userInfo:nil];
  49. }
  50. + (instancetype)credentialsWithUsername:(NSString *)username
  51. password:(NSString *)password
  52. register:(BOOL)shouldRegister {
  53. return [[self alloc] initWithCustomToken:username
  54. provider:RLMIdentityProviderUsernamePassword
  55. userInfo:@{kRLMSyncPasswordKey: password,
  56. kRLMSyncRegisterKey: @(shouldRegister)}];
  57. }
  58. + (instancetype)credentialsWithJWT:(NSString *)token {
  59. return [[self alloc] initWithCustomToken:token provider:RLMIdentityProviderJWT userInfo:nil];
  60. }
  61. + (instancetype)anonymousCredentials {
  62. return [[self alloc] initWithCustomToken:@"" provider:RLMIdentityProviderAnonymous userInfo:nil];
  63. }
  64. + (instancetype)credentialsWithNickname:(NSString *)nickname isAdmin:(BOOL)isAdmin {
  65. return [[self alloc] initWithCustomToken:nickname
  66. provider:RLMIdentityProviderNickname
  67. userInfo:@{kRLMSyncIsAdminKey: @(isAdmin), kRLMSyncDataKey: nickname}];
  68. }
  69. /// Intended only for testing use. Will only work if the ROS is started with the `debug` provider enabled.
  70. + (instancetype)credentialsWithDebugUserID:(NSString *)userID isAdmin:(BOOL)isAdmin {
  71. return [[self alloc] initWithCustomToken:userID
  72. provider:RLMIdentityProviderDebug
  73. userInfo:@{kRLMSyncIsAdminKey: @(isAdmin)}];
  74. }
  75. + (instancetype)credentialsWithAccessToken:(RLMServerToken)accessToken identity:(NSString *)identity {
  76. return [[self alloc] initWithCustomToken:accessToken
  77. provider:RLMIdentityProviderAccessToken
  78. userInfo:@{kRLMSyncIdentityKey: identity}];
  79. }
  80. - (BOOL)isEqual:(id)object {
  81. if (![object isKindOfClass:[RLMSyncCredentials class]]) {
  82. return NO;
  83. }
  84. RLMSyncCredentials *that = (RLMSyncCredentials *)object;
  85. return ([self.token isEqualToString:that.token]
  86. && [self.provider isEqualToString:that.provider]
  87. && [self.userInfo isEqual:that.userInfo]);
  88. }
  89. - (instancetype)initWithCustomToken:(RLMSyncCredentialsToken)token
  90. provider:(RLMIdentityProvider)provider
  91. userInfo:(NSDictionary *)userInfo {
  92. if (self = [super init]) {
  93. self.token = token;
  94. self.provider = provider;
  95. self.userInfo = userInfo;
  96. return self;
  97. }
  98. return nil;
  99. }
  100. @end