RLMSyncCredentials.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. RLMIdentityProvider const RLMIdentityProviderCustomRefreshToken = @"customrefreshtoken";
  33. @interface RLMSyncCredentials ()
  34. - (instancetype)initWithCustomToken:(RLMSyncCredentialsToken)token
  35. provider:(RLMIdentityProvider)provider
  36. userInfo:(NSDictionary *)userInfo NS_DESIGNATED_INITIALIZER;
  37. @property (nonatomic, readwrite) RLMSyncCredentialsToken token;
  38. @property (nonatomic, readwrite) RLMIdentityProvider provider;
  39. @property (nonatomic, readwrite) NSDictionary *userInfo;
  40. @end
  41. @implementation RLMSyncCredentials
  42. + (instancetype)credentialsWithFacebookToken:(RLMSyncCredentialsToken)token {
  43. return [[self alloc] initWithCustomToken:token provider:RLMIdentityProviderFacebook userInfo:nil];
  44. }
  45. + (instancetype)credentialsWithGoogleToken:(RLMSyncCredentialsToken)token {
  46. return [[self alloc] initWithCustomToken:token provider:RLMIdentityProviderGoogle userInfo:nil];
  47. }
  48. + (instancetype)credentialsWithCloudKitToken:(RLMSyncCredentialsToken)token {
  49. return [[self alloc] initWithCustomToken:token provider:RLMIdentityProviderCloudKit userInfo:nil];
  50. }
  51. + (instancetype)credentialsWithUsername:(NSString *)username
  52. password:(NSString *)password
  53. register:(BOOL)shouldRegister {
  54. return [[self alloc] initWithCustomToken:username
  55. provider:RLMIdentityProviderUsernamePassword
  56. userInfo:@{kRLMSyncPasswordKey: password,
  57. kRLMSyncRegisterKey: @(shouldRegister)}];
  58. }
  59. + (instancetype)credentialsWithJWT:(NSString *)token {
  60. return [[self alloc] initWithCustomToken:token provider:RLMIdentityProviderJWT userInfo:nil];
  61. }
  62. + (instancetype)anonymousCredentials {
  63. return [[self alloc] initWithCustomToken:@"" provider:RLMIdentityProviderAnonymous userInfo:nil];
  64. }
  65. + (instancetype)credentialsWithNickname:(NSString *)nickname isAdmin:(BOOL)isAdmin {
  66. return [[self alloc] initWithCustomToken:nickname
  67. provider:RLMIdentityProviderNickname
  68. userInfo:@{kRLMSyncIsAdminKey: @(isAdmin), kRLMSyncDataKey: nickname}];
  69. }
  70. /// Intended only for testing use. Will only work if the ROS is started with the `debug` provider enabled.
  71. + (instancetype)credentialsWithDebugUserID:(NSString *)userID isAdmin:(BOOL)isAdmin {
  72. return [[self alloc] initWithCustomToken:userID
  73. provider:RLMIdentityProviderDebug
  74. userInfo:@{kRLMSyncIsAdminKey: @(isAdmin)}];
  75. }
  76. + (instancetype)credentialsWithAccessToken:(RLMServerToken)accessToken identity:(NSString *)identity {
  77. return [[self alloc] initWithCustomToken:accessToken
  78. provider:RLMIdentityProviderAccessToken
  79. userInfo:@{kRLMSyncIdentityKey: identity}];
  80. }
  81. + (instancetype)credentialsWithCustomRefreshToken:(NSString *)token identity:(NSString *)identity isAdmin:(BOOL)isAdmin {
  82. NSDictionary *userInfo = @{
  83. kRLMSyncIdentityKey: identity,
  84. kRLMSyncIsAdminKey: @(isAdmin)
  85. };
  86. return [[self alloc] initWithCustomToken:token
  87. provider:RLMIdentityProviderCustomRefreshToken
  88. userInfo:userInfo];
  89. }
  90. - (BOOL)isEqual:(id)object {
  91. if (![object isKindOfClass:[RLMSyncCredentials class]]) {
  92. return NO;
  93. }
  94. RLMSyncCredentials *that = (RLMSyncCredentials *)object;
  95. return ([self.token isEqualToString:that.token]
  96. && [self.provider isEqualToString:that.provider]
  97. && [self.userInfo isEqual:that.userInfo]);
  98. }
  99. - (instancetype)initWithCustomToken:(RLMSyncCredentialsToken)token
  100. provider:(RLMIdentityProvider)provider
  101. userInfo:(NSDictionary *)userInfo {
  102. if (self = [super init]) {
  103. self.token = token;
  104. self.provider = provider;
  105. self.userInfo = userInfo;
  106. return self;
  107. }
  108. return nil;
  109. }
  110. @end