1 ////////////////////////////////////////////////////////////////////////////
3 // Copyright 2017 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 "RLMJSONModels.h"
20 #import "RLMSyncUtil_Private.h"
21 #import "RLMSyncUser.h"
23 #pragma mark - Constants
25 static const NSString *const kRLMSyncAccessTokenKey = @"access_token";
26 static const NSString *const kRLMSyncAccountsKey = @"accounts";
27 static const NSString *const kRLMSyncErrorCodeKey = @"code";
28 static const NSString *const kRLMSyncExpiresKey = @"expires";
29 static const NSString *const kRLMSyncErrorHintKey = @"hint";
30 static const NSString *const kRLMSyncIdKey = @"id";
31 static const NSString *const kRLMSyncKeyKey = @"key";
32 static const NSString *const kRLMSyncMetadataKey = @"metadata";
33 static const NSString *const kRLMSyncRefreshTokenKey = @"refresh_token";
34 static const NSString *const kRLMSyncErrorStatusKey = @"status";
35 static const NSString *const kRLMSyncErrorTitleKey = @"title";
36 static const NSString *const kRLMSyncTokenDataKey = @"token_data";
37 static const NSString *const kRLMSyncUserKey = @"user";
38 static const NSString *const kRLMSyncValueKey = @"value";
40 #pragma mark - RLMTokenDataModel
42 @interface RLMTokenDataModel ()
44 @property (nonatomic, readwrite) NSString *identity;
45 @property (nonatomic, readwrite) NSString *appID;
46 @property (nonatomic, readwrite) NSString *path;
47 @property (nonatomic, readwrite) NSTimeInterval expires;
48 @property (nonatomic, readwrite) BOOL isAdmin;
49 //@property (nonatomic, readwrite) NSArray *access;
53 @implementation RLMTokenDataModel
55 - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
56 if (self = [super init]) {
58 RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncIdentityKey, identity);
59 RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncAppIDKey, appID);
60 RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncPathKey, path);
61 RLM_SYNC_PARSE_OPTIONAL_BOOL(jsonDictionary, kRLMSyncIsAdminKey, isAdmin);
62 RLM_SYNC_PARSE_DOUBLE_OR_ABORT(jsonDictionary, kRLMSyncExpiresKey, expires);
70 #pragma mark - RLMTokenModel
72 @interface RLMTokenModel ()
74 @property (nonatomic, readwrite) NSString *token;
75 @property (nonatomic, nullable, readwrite) NSString *path;
76 @property (nonatomic, readwrite) RLMTokenDataModel *tokenData;
80 @implementation RLMTokenModel
82 - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
83 if (self = [super init]) {
84 RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncTokenKey, token);
85 RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncPathKey, path);
86 RLM_SYNC_PARSE_MODEL_OR_ABORT(jsonDictionary, kRLMSyncTokenDataKey, RLMTokenDataModel, tokenData);
94 #pragma mark - RLMAuthResponseModel
96 @interface RLMAuthResponseModel ()
98 @property (nonatomic, readwrite) RLMTokenModel *accessToken;
99 @property (nonatomic, readwrite) RLMTokenModel *refreshToken;
103 @implementation RLMAuthResponseModel
105 - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary
106 requireAccessToken:(BOOL)requireAccessToken
107 requireRefreshToken:(BOOL)requireRefreshToken {
108 if (self = [super init]) {
109 // Get the access token.
110 if (requireAccessToken) {
111 RLM_SYNC_PARSE_MODEL_OR_ABORT(jsonDictionary, kRLMSyncAccessTokenKey, RLMTokenModel, accessToken);
113 RLM_SYNC_PARSE_OPTIONAL_MODEL(jsonDictionary, kRLMSyncAccessTokenKey, RLMTokenModel, accessToken);
115 // Get the refresh token.
116 if (requireRefreshToken) {
117 RLM_SYNC_PARSE_MODEL_OR_ABORT(jsonDictionary, kRLMSyncRefreshTokenKey, RLMTokenModel, refreshToken);
119 RLM_SYNC_PARSE_OPTIONAL_MODEL(jsonDictionary, kRLMSyncRefreshTokenKey, RLMTokenModel, refreshToken);
128 #pragma mark - RLMUserInfoResponseModel
130 @interface RLMSyncUserAccountInfo ()
131 @property (nonatomic, readwrite) NSString *provider;
132 @property (nonatomic, readwrite) NSString *providerUserIdentity;
135 @implementation RLMSyncUserAccountInfo
137 - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
138 if (self = [super init]) {
139 RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncProviderKey, provider);
140 RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncProviderIDKey, providerUserIdentity);
148 @interface RLMUserResponseModel ()
150 @property (nonatomic, readwrite) NSString *identity;
151 @property (nonatomic, readwrite) NSArray *accounts;
152 @property (nonatomic, readwrite) NSDictionary *metadata;
153 @property (nonatomic, readwrite) BOOL isAdmin;
157 @implementation RLMUserResponseModel
159 - (void)parseMetadataFromJSON:(NSDictionary *)jsonDictionary {
160 NSMutableDictionary *buffer = [NSMutableDictionary dictionary];
161 NSArray *metadataArray = jsonDictionary[kRLMSyncMetadataKey];
162 if (![metadataArray isKindOfClass:[NSArray class]]) {
166 for (NSDictionary *object in metadataArray) {
167 if (![object isKindOfClass:[NSDictionary class]]) {
170 NSString *key = object[kRLMSyncKeyKey];
171 NSString *value = object[kRLMSyncValueKey];
172 if (!key || !value) {
177 self.metadata = [buffer copy];
180 - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
181 if (self = [super init]) {
183 RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncUserIDKey, identity);
184 RLM_SYNC_PARSE_OPTIONAL_BOOL(jsonDictionary, kRLMSyncIsAdminKey, isAdmin);
185 RLM_SYNC_PARSE_MODEL_ARRAY_OR_ABORT(jsonDictionary, kRLMSyncAccountsKey, RLMSyncUserAccountInfo, accounts);
186 [self parseMetadataFromJSON:jsonDictionary];
194 #pragma mark - RLMSyncErrorResponseModel
196 @interface RLMSyncErrorResponseModel ()
198 @property (nonatomic, readwrite) NSInteger status;
199 @property (nonatomic, readwrite) NSInteger code;
200 @property (nonatomic, readwrite) NSString *title;
201 @property (nonatomic, readwrite) NSString *hint;
205 @implementation RLMSyncErrorResponseModel
207 - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
208 if (self = [super init]) {
209 RLM_SYNC_PARSE_DOUBLE_OR_ABORT(jsonDictionary, kRLMSyncErrorStatusKey, status);
210 RLM_SYNC_PARSE_DOUBLE_OR_ABORT(jsonDictionary, kRLMSyncErrorCodeKey, code);
211 RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncErrorTitleKey, title);
212 RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncErrorHintKey, hint);