1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2016 Realm Inc.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
17 ////////////////////////////////////////////////////////////////////////////
19 #import "RLMSyncCredentials.h"
20 #import "RLMSyncUtil_Private.h"
22 /// A Twitter account as an identity provider.
23 //extern RLMIdentityProvider const RLMIdentityProviderTwitter;
25 RLMIdentityProvider const RLMIdentityProviderDebug = @"debug";
26 RLMIdentityProvider const RLMIdentityProviderRealm = @"realm";
27 RLMIdentityProvider const RLMIdentityProviderUsernamePassword = @"password";
28 RLMIdentityProvider const RLMIdentityProviderFacebook = @"facebook";
29 RLMIdentityProvider const RLMIdentityProviderTwitter = @"twitter";
30 RLMIdentityProvider const RLMIdentityProviderGoogle = @"google";
31 RLMIdentityProvider const RLMIdentityProviderCloudKit = @"cloudkit";
32 RLMIdentityProvider const RLMIdentityProviderJWT = @"jwt";
33 RLMIdentityProvider const RLMIdentityProviderAnonymous = @"anonymous";
34 RLMIdentityProvider const RLMIdentityProviderNickname = @"nickname";
36 @interface RLMSyncCredentials ()
38 - (instancetype)initWithCustomToken:(RLMSyncCredentialsToken)token
39 provider:(RLMIdentityProvider)provider
40 userInfo:(NSDictionary *)userInfo NS_DESIGNATED_INITIALIZER;
42 @property (nonatomic, readwrite) RLMSyncCredentialsToken token;
43 @property (nonatomic, readwrite) RLMIdentityProvider provider;
44 @property (nonatomic, readwrite) NSDictionary *userInfo;
48 @implementation RLMSyncCredentials
50 + (instancetype)credentialsWithFacebookToken:(RLMSyncCredentialsToken)token {
51 return [[self alloc] initWithCustomToken:token provider:RLMIdentityProviderFacebook userInfo:nil];
54 + (instancetype)credentialsWithGoogleToken:(RLMSyncCredentialsToken)token {
55 return [[self alloc] initWithCustomToken:token provider:RLMIdentityProviderGoogle userInfo:nil];
58 + (instancetype)credentialsWithCloudKitToken:(RLMSyncCredentialsToken)token {
59 return [[self alloc] initWithCustomToken:token provider:RLMIdentityProviderCloudKit userInfo:nil];
62 + (instancetype)credentialsWithUsername:(NSString *)username
63 password:(NSString *)password
64 register:(BOOL)shouldRegister {
65 return [[self alloc] initWithCustomToken:username
66 provider:RLMIdentityProviderUsernamePassword
67 userInfo:@{kRLMSyncPasswordKey: password,
68 kRLMSyncRegisterKey: @(shouldRegister)}];
71 + (instancetype)credentialsWithJWT:(NSString *)token {
72 return [[self alloc] initWithCustomToken:token provider:RLMIdentityProviderJWT userInfo:nil];
75 + (instancetype)anonymousCredentials {
76 return [[self alloc] initWithCustomToken:@"" provider:RLMIdentityProviderAnonymous userInfo:nil];
79 + (instancetype)credentialsWithNickname:(NSString *)nickname isAdmin:(BOOL)isAdmin {
80 return [[self alloc] initWithCustomToken:nickname
81 provider:RLMIdentityProviderNickname
82 userInfo:@{kRLMSyncIsAdminKey: @(isAdmin), kRLMSyncDataKey: nickname}];
85 /// Intended only for testing use. Will only work if the ROS is started with the `debug` provider enabled.
86 + (instancetype)credentialsWithDebugUserID:(NSString *)userID isAdmin:(BOOL)isAdmin {
87 return [[self alloc] initWithCustomToken:userID
88 provider:RLMIdentityProviderDebug
89 userInfo:@{kRLMSyncIsAdminKey: @(isAdmin)}];
92 + (instancetype)credentialsWithAccessToken:(RLMServerToken)accessToken identity:(NSString *)identity {
93 return [[self alloc] initWithCustomToken:accessToken
94 provider:RLMIdentityProviderAccessToken
95 userInfo:@{kRLMSyncIdentityKey: identity}];
98 - (BOOL)isEqual:(id)object {
99 if (![object isKindOfClass:[RLMSyncCredentials class]]) {
102 RLMSyncCredentials *that = (RLMSyncCredentials *)object;
103 return ([self.token isEqualToString:that.token]
104 && [self.provider isEqualToString:that.provider]
105 && [self.userInfo isEqual:that.userInfo]);
108 - (instancetype)initWithCustomToken:(RLMSyncCredentialsToken)token
109 provider:(RLMIdentityProvider)provider
110 userInfo:(NSDictionary *)userInfo {
111 if (self = [super init]) {
113 self.provider = provider;
114 self.userInfo = userInfo;