forked from fullstackreact/react-native-oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOAuthManager.m
717 lines (611 loc) · 25.7 KB
/
OAuthManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
//
// OAuthManager.m
// Created by Ari Lerner on 5/31/16.
// Copyright © 2016 Facebook. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
#import <SafariServices/SafariServices.h>
#import "OAuthManager.h"
#import "DCTAuth.h"
#import "DCTAuthAccountStore.h"
#import "OAuthClient.h"
#import "OAuth1Client.h"
#import "OAuth2Client.h"
#import "XMLReader.h"
@interface OAuthManager()
@property (nonatomic) NSArray *pendingClients;
@property BOOL pendingAuthentication;
@end
@implementation OAuthManager
@synthesize callbackUrls = _callbackUrls;
static NSString *const AUTH_MANAGER_TAG = @"AUTH_MANAGER";
static OAuthManager *manager;
static dispatch_once_t onceToken;
static SFSafariViewController *safariViewController = nil;
RCT_EXPORT_MODULE(OAuthManager);
// Run on a different thread
- (dispatch_queue_t)methodQueue
{
return dispatch_queue_create("io.fullstack.oauth", DISPATCH_QUEUE_SERIAL);
}
+ (instancetype)sharedManager {
dispatch_once(&onceToken, ^{
manager = [self new];
});
return manager;
}
+ (void) reset {
onceToken = nil;
manager = nil;
}
- (instancetype) init {
self = [super init];
if (self != nil) {
_callbackUrls = [[NSArray alloc] init];
_pendingClients = [[NSArray alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
}
return self;
}
- (void) dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void) didBecomeActive:(NSNotification *)notification
{
// TODO?
}
/*
Call this from your AppDelegate.h
*/
+ (BOOL)setupOAuthHandler:(UIApplication *)application
{
OAuthManager *sharedManager = [OAuthManager sharedManager];
DCTAuthPlatform *authPlatform = [DCTAuthPlatform sharedPlatform];
[authPlatform setURLOpener: ^void(NSURL *URL, DCTAuthPlatformCompletion completion) {
// [sharedManager setPendingAuthentication:YES];
if ([SFSafariViewController class] != nil) {
safariViewController = [[SFSafariViewController alloc] initWithURL:URL];
UIViewController *viewController = application.keyWindow.rootViewController;
[viewController presentViewController:safariViewController animated:YES completion: nil];
} else {
[application openURL:URL];
}
completion(YES);
}];
// Check for plist file
NSString *path = [[NSBundle mainBundle] pathForResource:kAuthConfig ofType:@"plist"];
if (path != nil) {
// plist exists
NSDictionary *initialConfig = [NSDictionary dictionaryWithContentsOfFile:path];
for (NSString *name in [initialConfig allKeys]) {
NSDictionary *cfg = [initialConfig objectForKey:name];
[sharedManager _configureProvider:name andConfig:cfg];
}
} else {
[sharedManager setProviderConfig:[NSDictionary dictionary]];
}
return YES;
}
+ (BOOL)handleOpenUrl:(UIApplication *)application openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
OAuthManager *manager = [OAuthManager sharedManager];
NSString *strUrl = [manager stringHost:url];
if ([manager.callbackUrls indexOfObject:strUrl] != NSNotFound) {
if(safariViewController != nil) {
[safariViewController dismissViewControllerAnimated:YES completion:nil];
}
return [DCTAuth handleURL:url];
}
// [manager clearPending];
return [RCTLinkingManager application:application openURL:url
sourceApplication:sourceApplication annotation:annotation];
}
- (BOOL) _configureProvider:(NSString *)providerName andConfig:(NSDictionary *)config
{
if (self.providerConfig == nil) {
self.providerConfig = [[NSDictionary alloc] init];
}
NSMutableDictionary *providerCfgs = [self.providerConfig mutableCopy];
NSMutableDictionary *objectProps = [[NSMutableDictionary alloc] init];
// Save the callback url for checking later
NSMutableArray *arr = [_callbackUrls mutableCopy];
NSString *callbackUrlStr = [config valueForKey:@"callback_url"];
NSURL *callbackUrl = [NSURL URLWithString:callbackUrlStr];
NSString *saveCallbackUrl = [[self stringHost:callbackUrl] lowercaseString];
if ([arr indexOfObject:saveCallbackUrl] == NSNotFound) {
[arr addObject:saveCallbackUrl];
_callbackUrls = [arr copy];
NSLog(@"Saved callback url: %@ in %@", saveCallbackUrl, _callbackUrls);
}
// Convert objects of url type
for (NSString *name in [config allKeys]) {
if ([name rangeOfString:@"_url"].location != NSNotFound) {
// This is a URL representation
NSString *urlStr = [config valueForKey:name];
NSURL *url = [NSURL URLWithString:urlStr];
[objectProps setObject:url forKey:name];
} else {
NSString *str = [NSString stringWithString:[config valueForKey:name]];
[objectProps setValue:str forKey:name];
}
}
[providerCfgs setObject:objectProps forKey:providerName];
self.providerConfig = providerCfgs;
return YES;
}
- (NSDictionary *) getConfigForProvider:(NSString *)name
{
return [self.providerConfig objectForKey:name];
}
/**
* configure provider
*
* @param {string} providerName - name of the provider we are configuring
* @param [object] props - properties to set on the configuration object
*/
RCT_EXPORT_METHOD(configureProvider:
(NSString *)providerName
props:(NSDictionary *)props
callback:(RCTResponseSenderBlock)callback)
{
OAuthManager *sharedManager = [OAuthManager sharedManager];
if ([sharedManager _configureProvider:providerName andConfig:props]) {
callback(@[[NSNull null], @{
@"status": @"ok"
}]);
} else {
// Error?
callback(@[@{
@"status": @"error"
}]);
}
}
#pragma mark OAuth
// TODO: Remove opts
RCT_EXPORT_METHOD(getSavedAccounts:(NSDictionary *) opts
callback:(RCTResponseSenderBlock) callback)
{
OAuthManager *manager = [OAuthManager sharedManager];
DCTAuthAccountStore *store = [manager accountStore];
NSSet *accounts = [store accounts];
NSMutableArray *respAccounts = [[NSMutableArray alloc] init];
for (DCTAuthAccount *account in [accounts allObjects]) {
NSString *providerName = account.type;
NSMutableDictionary *cfg = [[manager getConfigForProvider:providerName] mutableCopy];
NSMutableDictionary *acc = [[manager getAccountResponse:account cfg:cfg] mutableCopy];
[acc setValue:providerName forKey:@"provider"];
[respAccounts addObject:acc];
}
callback(@[[NSNull null], @{
@"status": @"ok",
@"accounts": respAccounts
}]);
}
// TODO: Remove opts
RCT_EXPORT_METHOD(getSavedAccount:(NSString *)providerName
opts:(NSDictionary *) opts
callback:(RCTResponseSenderBlock)callback)
{
OAuthManager *manager = [OAuthManager sharedManager];
NSMutableDictionary *cfg = [[manager getConfigForProvider:providerName] mutableCopy];
DCTAuthAccount *existingAccount = [manager accountForProvider:providerName];
if (existingAccount != nil) {
if ([existingAccount isAuthorized]) {
NSDictionary *accountResponse = [manager getAccountResponse:existingAccount cfg:cfg];
callback(@[[NSNull null], @{
@"status": @"ok",
@"provider": providerName,
@"response": accountResponse
}]);
return;
} else {
DCTAuthAccountStore *store = [manager accountStore];
[store deleteAccount:existingAccount];
NSDictionary *errResp = @{
@"status": @"error",
@"response": @{
@"msg": @"Account not authorized"
}
};
callback(@[errResp]);
}
} else {
NSDictionary *errResp = @{
@"status": @"error",
@"response": @{
@"msg": @"No saved account"
}
};
callback(@[errResp]);
}
}
RCT_EXPORT_METHOD(deauthorize:(NSString *) providerName
callback:(RCTResponseSenderBlock) callback)
{
OAuthManager *manager = [OAuthManager sharedManager];
DCTAuthAccountStore *store = [manager accountStore];
DCTAuthAccount *existingAccount = [manager accountForProvider:providerName];
if (existingAccount != nil) {
[store deleteAccount:existingAccount];
callback(@[[NSNull null], @{
@"status": @"ok"
}]);
} else {
NSDictionary *resp = @{
@"status": @"error",
@"msg": [NSString stringWithFormat:@"No account found for %@", providerName]
};
callback(@[resp]);
}
}
/**
* authorize with url
* provider, url, scope, state, params
**/
RCT_EXPORT_METHOD(authorize:(NSString *)providerName
opts:(NSDictionary *) opts
callback:(RCTResponseSenderBlock)callback)
{
OAuthManager *manager = [OAuthManager sharedManager];
[manager clearPending];
NSMutableDictionary *cfg = [[manager getConfigForProvider:providerName] mutableCopy];
DCTAuthAccount *existingAccount = [manager accountForProvider:providerName];
NSString *clientID = ((DCTOAuth2Credential *) existingAccount).clientID;
if (([providerName isEqualToString:@"google"] && existingAccount && clientID != nil)
|| (![providerName isEqualToString:@"google"] && existingAccount != nil)) {
if ([existingAccount isAuthorized]) {
NSDictionary *accountResponse = [manager getAccountResponse:existingAccount cfg:cfg];
callback(@[[NSNull null], @{
@"status": @"ok",
@"provider": providerName,
@"response": accountResponse
}]);
return;
} else {
DCTAuthAccountStore *store = [manager accountStore];
[store deleteAccount:existingAccount];
}
}
NSString *callbackUrl;
NSURL *storedCallbackUrl = [cfg objectForKey:@"callback_url"];
if (storedCallbackUrl != nil) {
callbackUrl = [storedCallbackUrl absoluteString];
} else {
NSString *appName = [cfg valueForKey:@"app_name"];
callbackUrl = [NSString
stringWithFormat:@"%@://oauth-response/%@",
appName,
providerName];
}
NSString *version = [cfg valueForKey:@"auth_version"];
[cfg addEntriesFromDictionary:opts];
OAuthClient *client;
if ([version isEqualToString:@"1.0"]) {
// OAuth 1
client = (OAuthClient *)[[OAuth1Client alloc] init];
} else if ([version isEqualToString:@"2.0"]) {
client = (OAuthClient *)[[OAuth2Client alloc] init];
} else {
return callback(@[@{
@"status": @"error",
@"msg": @"Unknown provider"
}]);
}
// Store pending client
[manager addPending:client];
_pendingAuthentication = YES;
NSLog(@"Calling authorizeWithUrl: %@ with callbackURL: %@\n %@", providerName, callbackUrl, cfg);
[client authorizeWithUrl:providerName
url:callbackUrl
cfg:cfg
onSuccess:^(DCTAuthAccount *account) {
NSLog(@"on success called with account: %@", account);
NSDictionary *accountResponse = [manager getAccountResponse:account cfg:cfg];
_pendingAuthentication = NO;
[manager removePending:client];
[[manager accountStore] saveAccount:account]; // <~
callback(@[[NSNull null], @{
@"status": @"ok",
@"response": accountResponse
}]);
} onError:^(NSError *error) {
NSLog(@"Error in authorizeWithUrl: %@", error);
_pendingAuthentication = NO;
callback(@[@{
@"status": @"error",
@"msg": [error localizedDescription],
@"userInfo": error.userInfo
}]);
[manager removePending:client];
}];
}
RCT_EXPORT_METHOD(makeRequest:(NSString *)providerName
urlOrPath:(NSString *) urlOrPath
opts:(NSDictionary *) opts
callback:(RCTResponseSenderBlock)callback)
{
OAuthManager *manager = [OAuthManager sharedManager];
NSMutableDictionary *cfg = [[manager getConfigForProvider:providerName] mutableCopy];
DCTAuthAccount *existingAccount = [manager accountForProvider:providerName];
if (existingAccount == nil) {
NSDictionary *errResp = @{
@"status": @"error",
@"msg": [NSString stringWithFormat:@"No account found for %@", providerName]
};
callback(@[errResp]);
return;
}
NSDictionary *creds = [self credentialForAccount:providerName cfg:cfg];
// If we have the http in the string, use it as the URL, otherwise create one
// with the configuration
NSURL *apiUrl;
if ([urlOrPath hasPrefix:@"http"]) {
apiUrl = [NSURL URLWithString:urlOrPath];
} else {
NSURL *apiHost = [cfg objectForKey:@"api_url"];
apiUrl = [NSURL URLWithString:[[apiHost absoluteString] stringByAppendingString:urlOrPath]];
}
// If there are params
NSMutableArray *items = [NSMutableArray array];
NSDictionary *params = [opts objectForKey:@"params"];
if (params != nil) {
for (NSString *key in params) {
NSString *value = [params valueForKey:key];
if ([value isEqualToString:@"access_token"]) {
value = [creds valueForKey:@"access_token"];
}
NSURLQueryItem *item = [NSURLQueryItem queryItemWithName:key value:value];
if (item != nil) {
[items addObject:item];
}
}
}
NSString *methodStr = [opts valueForKey:@"method"];
DCTAuthRequestMethod method = [manager getRequestMethodByString:methodStr];
DCTAuthRequest *request =
[[DCTAuthRequest alloc]
initWithRequestMethod:method
URL:apiUrl
items:items];
NSDictionary *body = [opts objectForKey:@"body"];
if (body != nil) {
for (NSString *key in body) {
NSData *data = [[NSString stringWithFormat:@"%@", [body valueForKey:key]] dataUsingEncoding:NSUTF8StringEncoding];
[request addMultiPartData:data withName:key type:@"application/json"]; // TODO: How should we handle different body types?
}
}
request.account = existingAccount;
// If there are headers
NSDictionary *headers = [opts objectForKey:@"headers"];
if (headers != nil) {
NSMutableDictionary *existingHeaders = [request.HTTPHeaders mutableCopy];
for (NSString *header in headers) {
[existingHeaders setValue:[headers valueForKey:header] forKey:header];
}
request.HTTPHeaders = existingHeaders;
}
[request performRequestWithHandler:^(DCTAuthResponse *response, NSError *error) {
if (error != nil) {
NSDictionary *errorDict = @{
@"status": @"error",
@"msg": [error localizedDescription]
};
callback(@[errorDict]);
} else {
NSInteger statusCode = response.statusCode;
NSData *rawData = response.data;
NSError *err;
NSArray *data;
// Check if returned data is a valid JSON
// != nil returned if the rawdata is not a valid JSON
if ((data = [NSJSONSerialization JSONObjectWithData:rawData
options:kNilOptions
error:&err]) == nil) {
// Resetting err variable.
err = nil;
// Parse XML
data = [XMLReader dictionaryForXMLData:rawData
options:XMLReaderOptionsProcessNamespaces
error:&err];
}
if (err != nil) {
NSDictionary *errResp = @{
@"status": @"error",
@"msg": [NSString stringWithFormat:@"JSON parsing error: %@", [err localizedDescription]]
};
callback(@[errResp]);
} else {
NSDictionary *resp = @{
@"status": @(statusCode),
@"data": data
};
callback(@[[NSNull null], resp]);
}
}
}];
}
#pragma mark - private
- (DCTAuthAccount *) accountForProvider:(NSString *) providerName
{
DCTAuthAccountStore *store = [self accountStore];
NSSet *accounts = [store accountsWithType:providerName];
if ([accounts count] == 0) {
return nil;
} else {
NSArray *allAccounts = [accounts allObjects];
if ([allAccounts count] == 0) {
return nil;
} else {
return [allAccounts lastObject];
}
}
}
- (NSDictionary *) credentialForAccount:(NSString *)providerName
cfg:(NSDictionary *)cfg
{
DCTAuthAccount *account = [self accountForProvider:providerName];
if (!account) {
return nil;
}
NSString *version = [cfg valueForKey:@"auth_version"];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
if ([version isEqualToString:@"1.0"]) {
DCTOAuth1Credential *credentials = [account credential];
if (credentials) {
if (credentials.oauthToken) {
NSString *token = credentials.oauthToken;
[dict setObject:token forKey:@"access_token"];
}
if (credentials.oauthTokenSecret) {
NSString *secret = credentials.oauthTokenSecret;
[dict setObject:secret forKey:@"access_token_secret"];
}
}
} else if ([version isEqualToString:@"2.0"]) {
DCTOAuth2Credential *credentials = [account credential];
if (credentials) {
if (credentials.accessToken) {
NSString *token = credentials.accessToken;
[dict setObject:token forKey:@"access_token"];
}
}
}
return dict;
}
- (DCTAuthRequestMethod) getRequestMethodByString:(NSString *) method
{
if ([method compare:@"get" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
return DCTAuthRequestMethodGET;
} else if ([method compare:@"post" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
return DCTAuthRequestMethodPOST;
} else if ([method compare:@"put" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
return DCTAuthRequestMethodPUT;
} else if ([method compare:@"delete" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
return DCTAuthRequestMethodDELETE;
} else if ([method compare:@"head" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
return DCTAuthRequestMethodHEAD;
} else if ([method compare:@"options" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
return DCTAuthRequestMethodOPTIONS;
} else if ([method compare:@"patch" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
return DCTAuthRequestMethodPATCH;
} else if ([method compare:@"trace" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
return DCTAuthRequestMethodTRACE;
} else {
return DCTAuthRequestMethodGET;
}
}
- (NSDictionary *) getAccountResponse:(DCTAuthAccount *) account
cfg:(NSDictionary *)cfg
{
NSArray *ignoredCredentialProperties = @[@"superclass", @"hash", @"description", @"debugDescription"];
NSString *version = [cfg valueForKey:@"auth_version"];
NSMutableDictionary *accountResponse = [@{
@"authorized": @(account.authorized),
@"uuid": account.identifier
} mutableCopy];
if ([version isEqualToString:@"1.0"]) {
DCTOAuth1Credential *credential = account.credential;
if (credential != nil) {
NSDictionary *cred = @{
@"access_token": credential.oauthToken,
@"access_token_secret": credential.oauthTokenSecret
};
[accountResponse setObject:cred forKey:@"credentials"];
}
} else if ([version isEqualToString:@"2.0"]) {
DCTOAuth2Credential *credential = account.credential;
if (credential != nil) {
NSMutableDictionary *cred = [self dictionaryForCredentialKeys: credential];
DCTOAuth2Account *oauth2Account = (DCTOAuth2Account *) account;
if (oauth2Account.scopes) {
[cred setObject:oauth2Account.scopes forKey:@"scopes"];
}
[accountResponse setObject:cred forKey:@"credentials"];
}
}
[accountResponse setValue:[account identifier] forKey:@"identifier"];
if (account.userInfo != nil) {
[accountResponse setObject:[account userInfo] forKey:@"user_info"];
}
return accountResponse;
}
- (NSDictionary *) dictionaryForCredentialKeys:(NSObject *) credential
{
NSArray *ignoredCredentialProperties = @[@"superclass", @"hash", @"description", @"debugDescription"];
unsigned int count = 0;
NSMutableDictionary *cred = [NSMutableDictionary new];
objc_property_t *properties = class_copyPropertyList([credential class], &count);
for (int i = 0; i < count; i++) {
NSString *key = [NSString stringWithUTF8String:property_getName(properties[i])];
if ([ignoredCredentialProperties containsObject:key]) {
NSLog(@"Ignoring credentials key: %@", key);
} else {
id value = [credential valueForKey:key];
if (value == nil) {
} else if ([value isKindOfClass:[NSNumber class]]
|| [value isKindOfClass:[NSString class]]
|| [value isKindOfClass:[NSDictionary class]] || [value isKindOfClass:[NSMutableArray class]]) {
// TODO: extend to other types
[cred setObject:value forKey:key];
} else if ([value isKindOfClass:[NSObject class]]) {
[cred setObject:[value dictionary] forKey:key];
} else {
NSLog(@"Invalid type for %@ (%@)", NSStringFromClass([self class]), key);
}
}
}
return cred;
}
- (void) clearPending
{
OAuthManager *manager = [OAuthManager sharedManager];
for (OAuthClient *client in manager.pendingClients) {
[manager removePending:client];
}
manager.pendingClients = [NSArray array];
_pendingAuthentication = NO;
}
- (void) addPending:(OAuthClient *) client
{
OAuthManager *manager = [OAuthManager sharedManager];
NSMutableArray *newPendingClients = [manager.pendingClients mutableCopy];
[newPendingClients addObject:client];
manager.pendingClients = newPendingClients;
}
- (void) removePending:(OAuthClient *) client
{
[client clearPendingAccount];
OAuthManager *manager = [OAuthManager sharedManager];
NSUInteger idx = [manager.pendingClients indexOfObject:client];
if ([manager.pendingClients count] <= idx) {
NSMutableArray *newPendingClients = [manager.pendingClients mutableCopy];
[newPendingClients removeObjectAtIndex:idx];
manager.pendingClients = newPendingClients;
}
}
- (DCTAuthAccountStore *) accountStore
{
NSString *name = [NSString stringWithFormat:@"%@", AUTH_MANAGER_TAG];
return [DCTAuthAccountStore accountStoreWithName:name];
}
- (NSString *) stringHost:(NSURL *)url
{
NSString *str;
if (url.host != nil) {
str = [NSString stringWithFormat:@"%@://%@%@", url.scheme, url.host, url.path];
} else {
str = [NSString stringWithFormat:@"%@%@", url.scheme, url.path];
}
if ([str hasSuffix:@"/"]) {
str = [str substringToIndex:str.length - 1];
}
return str;
}
@end