123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- ////////////////////////////////////////////////////////////////////////////
- //
- // Copyright 2017 Realm Inc.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- //
- ////////////////////////////////////////////////////////////////////////////
- #import "RLMJSONModels.h"
- #import "RLMSyncUtil_Private.h"
- #import "RLMSyncUser.h"
- #pragma mark - Constants
- static const NSString *const kRLMSyncAccessTokenKey = @"access_token";
- static const NSString *const kRLMSyncAccountsKey = @"accounts";
- static const NSString *const kRLMSyncErrorCodeKey = @"code";
- static const NSString *const kRLMSyncExpiresKey = @"expires";
- static const NSString *const kRLMSyncErrorHintKey = @"hint";
- static const NSString *const kRLMSyncIdKey = @"id";
- static const NSString *const kRLMSyncKeyKey = @"key";
- static const NSString *const kRLMSyncMetadataKey = @"metadata";
- static const NSString *const kRLMSyncRefreshTokenKey = @"refresh_token";
- static const NSString *const kRLMSyncErrorStatusKey = @"status";
- static const NSString *const kRLMSyncErrorTitleKey = @"title";
- static const NSString *const kRLMSyncTokenDataKey = @"token_data";
- static const NSString *const kRLMSyncUserKey = @"user";
- static const NSString *const kRLMSyncValueKey = @"value";
- #pragma mark - RLMTokenDataModel
- @interface RLMTokenDataModel ()
- @property (nonatomic, readwrite) NSString *identity;
- @property (nonatomic, readwrite) NSString *appID;
- @property (nonatomic, readwrite) NSString *path;
- @property (nonatomic, readwrite) NSTimeInterval expires;
- @property (nonatomic, readwrite) BOOL isAdmin;
- //@property (nonatomic, readwrite) NSArray *access;
- @end
- @implementation RLMTokenDataModel
- - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
- if (self = [super init]) {
- self.isAdmin = NO;
- RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncIdentityKey, identity);
- RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncAppIDKey, appID);
- RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncPathKey, path);
- RLM_SYNC_PARSE_OPTIONAL_BOOL(jsonDictionary, kRLMSyncIsAdminKey, isAdmin);
- RLM_SYNC_PARSE_DOUBLE_OR_ABORT(jsonDictionary, kRLMSyncExpiresKey, expires);
- return self;
- }
- return nil;
- }
- @end
- #pragma mark - RLMTokenModel
- @interface RLMTokenModel ()
- @property (nonatomic, readwrite) NSString *token;
- @property (nonatomic, nullable, readwrite) NSString *path;
- @property (nonatomic, readwrite) RLMTokenDataModel *tokenData;
- @end
- @implementation RLMTokenModel
- - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
- if (self = [super init]) {
- RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncTokenKey, token);
- RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncPathKey, path);
- RLM_SYNC_PARSE_MODEL_OR_ABORT(jsonDictionary, kRLMSyncTokenDataKey, RLMTokenDataModel, tokenData);
- return self;
- }
- return nil;
- }
- @end
- #pragma mark - RLMAuthResponseModel
- @interface RLMAuthResponseModel ()
- @property (nonatomic, readwrite) RLMTokenModel *accessToken;
- @property (nonatomic, readwrite) RLMTokenModel *refreshToken;
- @end
- @implementation RLMAuthResponseModel
- - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary
- requireAccessToken:(BOOL)requireAccessToken
- requireRefreshToken:(BOOL)requireRefreshToken {
- if (self = [super init]) {
- // Get the access token.
- if (requireAccessToken) {
- RLM_SYNC_PARSE_MODEL_OR_ABORT(jsonDictionary, kRLMSyncAccessTokenKey, RLMTokenModel, accessToken);
- } else {
- RLM_SYNC_PARSE_OPTIONAL_MODEL(jsonDictionary, kRLMSyncAccessTokenKey, RLMTokenModel, accessToken);
- }
- // Get the refresh token.
- if (requireRefreshToken) {
- RLM_SYNC_PARSE_MODEL_OR_ABORT(jsonDictionary, kRLMSyncRefreshTokenKey, RLMTokenModel, refreshToken);
- } else {
- RLM_SYNC_PARSE_OPTIONAL_MODEL(jsonDictionary, kRLMSyncRefreshTokenKey, RLMTokenModel, refreshToken);
- }
- return self;
- }
- return nil;
- }
- @end
- #pragma mark - RLMUserInfoResponseModel
- @interface RLMSyncUserAccountInfo ()
- @property (nonatomic, readwrite) NSString *provider;
- @property (nonatomic, readwrite) NSString *providerUserIdentity;
- @end
- @implementation RLMSyncUserAccountInfo
- - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
- if (self = [super init]) {
- RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncProviderKey, provider);
- RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncProviderIDKey, providerUserIdentity);
- return self;
- }
- return nil;
- }
- @end
- @interface RLMUserResponseModel ()
- @property (nonatomic, readwrite) NSString *identity;
- @property (nonatomic, readwrite) NSArray *accounts;
- @property (nonatomic, readwrite) NSDictionary *metadata;
- @property (nonatomic, readwrite) BOOL isAdmin;
- @end
- @implementation RLMUserResponseModel
- - (void)parseMetadataFromJSON:(NSDictionary *)jsonDictionary {
- NSMutableDictionary *buffer = [NSMutableDictionary dictionary];
- NSArray *metadataArray = jsonDictionary[kRLMSyncMetadataKey];
- if (![metadataArray isKindOfClass:[NSArray class]]) {
- self.metadata = @{};
- return;
- }
- for (NSDictionary *object in metadataArray) {
- if (![object isKindOfClass:[NSDictionary class]]) {
- continue;
- }
- NSString *key = object[kRLMSyncKeyKey];
- NSString *value = object[kRLMSyncValueKey];
- if (!key || !value) {
- continue;
- }
- buffer[key] = value;
- }
- self.metadata = [buffer copy];
- }
- - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
- if (self = [super init]) {
- self.isAdmin = NO;
- RLM_SYNC_PARSE_STRING_OR_ABORT(jsonDictionary, kRLMSyncUserIDKey, identity);
- RLM_SYNC_PARSE_OPTIONAL_BOOL(jsonDictionary, kRLMSyncIsAdminKey, isAdmin);
- RLM_SYNC_PARSE_MODEL_ARRAY_OR_ABORT(jsonDictionary, kRLMSyncAccountsKey, RLMSyncUserAccountInfo, accounts);
- [self parseMetadataFromJSON:jsonDictionary];
- return self;
- }
- return nil;
- }
- @end
- #pragma mark - RLMSyncErrorResponseModel
- @interface RLMSyncErrorResponseModel ()
- @property (nonatomic, readwrite) NSInteger status;
- @property (nonatomic, readwrite) NSInteger code;
- @property (nonatomic, readwrite) NSString *title;
- @property (nonatomic, readwrite) NSString *hint;
- @end
- @implementation RLMSyncErrorResponseModel
- - (instancetype)initWithDictionary:(NSDictionary *)jsonDictionary {
- if (self = [super init]) {
- RLM_SYNC_PARSE_DOUBLE_OR_ABORT(jsonDictionary, kRLMSyncErrorStatusKey, status);
- RLM_SYNC_PARSE_DOUBLE_OR_ABORT(jsonDictionary, kRLMSyncErrorCodeKey, code);
- RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncErrorTitleKey, title);
- RLM_SYNC_PARSE_OPTIONAL_STRING(jsonDictionary, kRLMSyncErrorHintKey, hint);
- NSString *detail = jsonDictionary[@"detail"];
- if ([detail isKindOfClass:[NSString class]]) {
- _title = detail;
- }
- for (NSDictionary<NSString *, NSString *> *problem in jsonDictionary[@"invalid_params"]) {
- NSString *name = problem[@"name"];
- NSString *reason = problem[@"reason"];
- if (name && reason) {
- _title = [NSString stringWithFormat:@"%@ %@: %@;", _title, name, reason];
- }
- }
- return self;
- }
- return nil;
- }
- @end
|