added iOS source code
[wl-app.git] / iOS / Pods / Realm / Realm / RLMSyncCredentials.m
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
19 #import "RLMSyncCredentials.h"
20 #import "RLMSyncUtil_Private.h"
21
22 /// A Twitter account as an identity provider.
23 //extern RLMIdentityProvider const RLMIdentityProviderTwitter;
24
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";
35
36 @interface RLMSyncCredentials ()
37
38 - (instancetype)initWithCustomToken:(RLMSyncCredentialsToken)token
39                            provider:(RLMIdentityProvider)provider
40                            userInfo:(NSDictionary *)userInfo NS_DESIGNATED_INITIALIZER;
41
42 @property (nonatomic, readwrite) RLMSyncCredentialsToken token;
43 @property (nonatomic, readwrite) RLMIdentityProvider provider;
44 @property (nonatomic, readwrite) NSDictionary *userInfo;
45
46 @end
47
48 @implementation RLMSyncCredentials
49
50 + (instancetype)credentialsWithFacebookToken:(RLMSyncCredentialsToken)token {
51     return [[self alloc] initWithCustomToken:token provider:RLMIdentityProviderFacebook userInfo:nil];
52 }
53
54 + (instancetype)credentialsWithGoogleToken:(RLMSyncCredentialsToken)token {
55     return [[self alloc] initWithCustomToken:token provider:RLMIdentityProviderGoogle userInfo:nil];
56 }
57
58 + (instancetype)credentialsWithCloudKitToken:(RLMSyncCredentialsToken)token {
59     return [[self alloc] initWithCustomToken:token provider:RLMIdentityProviderCloudKit userInfo:nil];
60 }
61
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)}];
69 }
70
71 + (instancetype)credentialsWithJWT:(NSString *)token {
72     return [[self alloc] initWithCustomToken:token provider:RLMIdentityProviderJWT userInfo:nil];
73 }
74     
75 + (instancetype)anonymousCredentials {
76     return [[self alloc] initWithCustomToken:@"" provider:RLMIdentityProviderAnonymous userInfo:nil];
77 }
78     
79 + (instancetype)credentialsWithNickname:(NSString *)nickname isAdmin:(BOOL)isAdmin {
80     return [[self alloc] initWithCustomToken:nickname
81                                     provider:RLMIdentityProviderNickname
82                                     userInfo:@{kRLMSyncIsAdminKey: @(isAdmin), kRLMSyncDataKey: nickname}];
83 }
84
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)}];
90 }
91
92 + (instancetype)credentialsWithAccessToken:(RLMServerToken)accessToken identity:(NSString *)identity {
93     return [[self alloc] initWithCustomToken:accessToken
94                                     provider:RLMIdentityProviderAccessToken
95                                     userInfo:@{kRLMSyncIdentityKey: identity}];
96 }
97
98 - (BOOL)isEqual:(id)object {
99     if (![object isKindOfClass:[RLMSyncCredentials class]]) {
100         return NO;
101     }
102     RLMSyncCredentials *that = (RLMSyncCredentials *)object;
103     return ([self.token isEqualToString:that.token]
104             && [self.provider isEqualToString:that.provider]
105             && [self.userInfo isEqual:that.userInfo]);
106 }
107
108 - (instancetype)initWithCustomToken:(RLMSyncCredentialsToken)token
109                            provider:(RLMIdentityProvider)provider
110                            userInfo:(NSDictionary *)userInfo {
111     if (self = [super init]) {
112         self.token = token;
113         self.provider = provider;
114         self.userInfo = userInfo;
115         return self;
116     }
117     return nil;
118 }
119
120 @end