-
Notifications
You must be signed in to change notification settings - Fork 0
/
GTMOAuth2Authentication.m
1231 lines (1035 loc) · 39.3 KB
/
GTMOAuth2Authentication.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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2011 Google 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.
*/
#if GTM_INCLUDE_OAUTH2 || !GDATA_REQUIRE_SERVICE_INCLUDES
#define GTMOAUTH2AUTHENTICATION_DEFINE_GLOBALS 1
#import "GTMOAuth2Authentication.h"
// standard OAuth keys
static NSString *const kOAuth2AccessTokenKey = @"access_token";
static NSString *const kOAuth2RefreshTokenKey = @"refresh_token";
static NSString *const kOAuth2ClientIDKey = @"client_id";
static NSString *const kOAuth2ClientSecretKey = @"client_secret";
static NSString *const kOAuth2RedirectURIKey = @"redirect_uri";
static NSString *const kOAuth2ResponseTypeKey = @"response_type";
static NSString *const kOAuth2ScopeKey = @"scope";
static NSString *const kOAuth2ErrorKey = @"error";
static NSString *const kOAuth2TokenTypeKey = @"token_type";
static NSString *const kOAuth2ExpiresInKey = @"expires_in";
static NSString *const kOAuth2CodeKey = @"code";
static NSString *const kOAuth2AssertionKey = @"assertion";
static NSString *const kOAuth2RefreshScopeKey = @"refreshScope";
// additional persistent keys
static NSString *const kServiceProviderKey = @"serviceProvider";
static NSString *const kUserIDKey = @"userID";
static NSString *const kUserEmailKey = @"email";
static NSString *const kUserEmailIsVerifiedKey = @"isVerified";
// fetcher keys
static NSString *const kTokenFetchDelegateKey = @"delegate";
static NSString *const kTokenFetchSelectorKey = @"sel";
static NSString *const kRefreshFetchArgsKey = @"requestArgs";
// If GTMNSJSONSerialization is available, it is used for formatting JSON
#if (TARGET_OS_MAC && !TARGET_OS_IPHONE && (MAC_OS_X_VERSION_MAX_ALLOWED < 1070)) || \
(TARGET_OS_IPHONE && (__IPHONE_OS_VERSION_MAX_ALLOWED < 50000))
@interface GTMNSJSONSerialization : NSObject
+ (id)JSONObjectWithData:(NSData *)data options:(NSUInteger)opt error:(NSError **)error;
@end
#endif
@interface GTMOAuth2ParserClass : NSObject
// just enough of SBJSON to be able to parse
- (id)objectWithString:(NSString*)repr error:(NSError**)error;
@end
// wrapper class for requests needing authorization and their callbacks
@interface GTMOAuth2AuthorizationArgs : NSObject {
@private
NSMutableURLRequest *request_;
id delegate_;
SEL sel_;
id completionHandler_;
NSThread *thread_;
NSError *error_;
}
@property (retain) NSMutableURLRequest *request;
@property (retain) id delegate;
@property (assign) SEL selector;
@property (copy) id completionHandler;
@property (retain) NSThread *thread;
@property (retain) NSError *error;
+ (GTMOAuth2AuthorizationArgs *)argsWithRequest:(NSMutableURLRequest *)req
delegate:(id)delegate
selector:(SEL)sel
completionHandler:(id)completionHandler
thread:(NSThread *)thread;
@end
@implementation GTMOAuth2AuthorizationArgs
@synthesize request = request_,
delegate = delegate_,
selector = sel_,
completionHandler = completionHandler_,
thread = thread_,
error = error_;
+ (GTMOAuth2AuthorizationArgs *)argsWithRequest:(NSMutableURLRequest *)req
delegate:(id)delegate
selector:(SEL)sel
completionHandler:(id)completionHandler
thread:(NSThread *)thread {
GTMOAuth2AuthorizationArgs *obj;
obj = [[[GTMOAuth2AuthorizationArgs alloc] init] autorelease];
obj.request = req;
obj.delegate = delegate;
obj.selector = sel;
obj.completionHandler = completionHandler;
obj.thread = thread;
return obj;
}
- (void)dealloc {
[request_ release];
[delegate_ release];
[completionHandler_ release];
[thread_ release];
[error_ release];
[super dealloc];
}
@end
@interface GTMOAuth2Authentication ()
@property (retain) NSMutableArray *authorizationQueue;
- (void)setKeysForResponseJSONData:(NSData *)data;
- (BOOL)authorizeRequestArgs:(GTMOAuth2AuthorizationArgs *)args;
- (BOOL)authorizeRequestImmediateArgs:(GTMOAuth2AuthorizationArgs *)args;
- (BOOL)shouldRefreshAccessToken;
- (void)updateExpirationDate;
- (NSDictionary *)dictionaryWithJSONData:(NSData *)data;
- (void)tokenFetcher:(GTMHTTPFetcher *)fetcher
finishedWithData:(NSData *)data
error:(NSError *)error;
- (void)auth:(GTMOAuth2Authentication *)auth
finishedRefreshWithFetcher:(GTMHTTPFetcher *)fetcher
error:(NSError *)error;
- (void)invokeCallbackArgs:(GTMOAuth2AuthorizationArgs *)args;
+ (void)invokeDelegate:(id)delegate
selector:(SEL)sel
object:(id)obj1
object:(id)obj2
object:(id)obj3;
+ (NSString *)unencodedOAuthParameterForString:(NSString *)str;
+ (NSString *)encodedQueryParametersForDictionary:(NSDictionary *)dict;
+ (NSDictionary *)dictionaryWithResponseData:(NSData *)data;
@end
@implementation GTMOAuth2Authentication
@synthesize clientID = clientID_,
clientSecret = clientSecret_,
redirectURI = redirectURI_,
parameters = parameters_,
tokenURL = tokenURL_,
expirationDate = expirationDate_,
additionalTokenRequestParameters = additionalTokenRequestParameters_,
refreshFetcher = refreshFetcher_,
fetcherService = fetcherService_,
parserClass = parserClass_,
shouldAuthorizeAllRequests = shouldAuthorizeAllRequests_,
userData = userData_,
properties = properties_,
authorizationQueue = authorizationQueue_;
// Response parameters
@dynamic accessToken,
refreshToken,
code,
assertion,
refreshScope,
errorString,
tokenType,
scope,
expiresIn,
serviceProvider,
userEmail,
userEmailIsVerified;
@dynamic canAuthorize;
+ (id)authenticationWithServiceProvider:(NSString *)serviceProvider
tokenURL:(NSURL *)tokenURL
redirectURI:(NSString *)redirectURI
clientID:(NSString *)clientID
clientSecret:(NSString *)clientSecret {
GTMOAuth2Authentication *obj = [[[self alloc] init] autorelease];
obj.serviceProvider = serviceProvider;
obj.tokenURL = tokenURL;
obj.redirectURI = redirectURI;
obj.clientID = clientID;
obj.clientSecret = clientSecret;
return obj;
}
- (id)init {
self = [super init];
if (self) {
authorizationQueue_ = [[NSMutableArray alloc] init];
parameters_ = [[NSMutableDictionary alloc] init];
}
return self;
}
- (NSString *)description {
NSArray *props = [NSArray arrayWithObjects:@"accessToken", @"refreshToken",
@"code", @"assertion", @"expirationDate", @"errorString",
nil];
NSMutableString *valuesStr = [NSMutableString string];
NSString *separator = @"";
for (NSString *prop in props) {
id result = [self valueForKey:prop];
if (result) {
[valuesStr appendFormat:@"%@%@=\"%@\"", separator, prop, result];
separator = @", ";
}
}
return [NSString stringWithFormat:@"%@ %p: {%@}",
[self class], self, valuesStr];
}
- (void)dealloc {
[clientID_ release];
[clientSecret_ release];
[redirectURI_ release];
[parameters_ release];
[tokenURL_ release];
[expirationDate_ release];
[additionalTokenRequestParameters_ release];
[refreshFetcher_ release];
[authorizationQueue_ release];
[userData_ release];
[properties_ release];
[super dealloc];
}
#pragma mark -
- (void)setKeysForResponseDictionary:(NSDictionary *)dict {
if (dict == nil) return;
// If a new code or access token is being set, remove the old expiration
NSString *newCode = [dict objectForKey:kOAuth2CodeKey];
NSString *newAccessToken = [dict objectForKey:kOAuth2AccessTokenKey];
if (newCode || newAccessToken) {
self.expiresIn = nil;
}
BOOL didRefreshTokenChange = NO;
NSString *refreshToken = [dict objectForKey:kOAuth2RefreshTokenKey];
if (refreshToken) {
NSString *priorRefreshToken = self.refreshToken;
if (priorRefreshToken != refreshToken
&& (priorRefreshToken == nil
|| ![priorRefreshToken isEqual:refreshToken])) {
didRefreshTokenChange = YES;
}
}
[self.parameters addEntriesFromDictionary:dict];
[self updateExpirationDate];
if (didRefreshTokenChange) {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:kGTMOAuth2RefreshTokenChanged
object:self
userInfo:nil];
}
// NSLog(@"keys set ----------------------------\n%@", dict);
}
- (void)setKeysForResponseString:(NSString *)str {
NSDictionary *dict = [[self class] dictionaryWithResponseString:str];
[self setKeysForResponseDictionary:dict];
}
- (void)setKeysForResponseJSONData:(NSData *)data {
NSDictionary *dict = [self dictionaryWithJSONData:data];
[self setKeysForResponseDictionary:dict];
}
- (NSDictionary *)dictionaryWithJSONData:(NSData *)data {
NSMutableDictionary *obj = nil;
NSError *error = nil;
Class serializer = NSClassFromString(@"NSJSONSerialization");
if (serializer) {
const NSUInteger kOpts = (1UL << 0); // NSJSONReadingMutableContainers
obj = [serializer JSONObjectWithData:data
options:kOpts
error:&error];
#if DEBUG
if (error) {
NSString *str = [[[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"NSJSONSerialization error %@ parsing %@",
error, str);
}
#endif
return obj;
} else {
// try SBJsonParser or SBJSON
Class jsonParseClass = NSClassFromString(@"SBJsonParser");
if (!jsonParseClass) {
jsonParseClass = NSClassFromString(@"SBJSON");
}
if (jsonParseClass) {
GTMOAuth2ParserClass *parser = [[[jsonParseClass alloc] init] autorelease];
NSString *jsonStr = [[[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding] autorelease];
if (jsonStr) {
obj = [parser objectWithString:jsonStr error:&error];
#if DEBUG
if (error) {
NSLog(@"%@ error %@ parsing %@", NSStringFromClass(jsonParseClass),
error, jsonStr);
}
#endif
return obj;
}
} else {
#if DEBUG
NSAssert(0, @"GTMOAuth2Authentication: No parser available");
#endif
}
}
return nil;
}
#pragma mark Authorizing Requests
// General entry point for authorizing requests
#if NS_BLOCKS_AVAILABLE
// Authorizing with a completion block
- (void)authorizeRequest:(NSMutableURLRequest *)request
completionHandler:(void (^)(NSError *error))handler {
GTMOAuth2AuthorizationArgs *args;
args = [GTMOAuth2AuthorizationArgs argsWithRequest:request
delegate:nil
selector:NULL
completionHandler:handler
thread:[NSThread currentThread]];
[self authorizeRequestArgs:args];
}
#endif
// Authorizing with a callback selector
//
// Selector has the signature
// - (void)authentication:(GTMOAuth2Authentication *)auth
// request:(NSMutableURLRequest *)request
// finishedWithError:(NSError *)error;
- (void)authorizeRequest:(NSMutableURLRequest *)request
delegate:(id)delegate
didFinishSelector:(SEL)sel {
GTMAssertSelectorNilOrImplementedWithArgs(delegate, sel,
@encode(GTMOAuth2Authentication *),
@encode(NSMutableURLRequest *),
@encode(NSError *), 0);
GTMOAuth2AuthorizationArgs *args;
args = [GTMOAuth2AuthorizationArgs argsWithRequest:request
delegate:delegate
selector:sel
completionHandler:nil
thread:[NSThread currentThread]];
[self authorizeRequestArgs:args];
}
// Internal routine common to delegate and block invocations
- (BOOL)authorizeRequestArgs:(GTMOAuth2AuthorizationArgs *)args {
BOOL didAttempt = NO;
@synchronized(authorizationQueue_) {
BOOL shouldRefresh = [self shouldRefreshAccessToken];
if (shouldRefresh) {
// attempt to refresh now; once we have a fresh access token, we will
// authorize the request and call back to the user
didAttempt = YES;
if (self.refreshFetcher == nil) {
// there's not already a refresh pending
SEL finishedSel = @selector(auth:finishedRefreshWithFetcher:error:);
self.refreshFetcher = [self beginTokenFetchWithDelegate:self
didFinishSelector:finishedSel];
if (self.refreshFetcher) {
[authorizationQueue_ addObject:args];
}
} else {
// there's already a refresh pending
[authorizationQueue_ addObject:args];
}
}
if (!shouldRefresh || self.refreshFetcher == nil) {
// we're not fetching a new access token, so we can authorize the request
// now
didAttempt = [self authorizeRequestImmediateArgs:args];
}
}
return didAttempt;
}
- (void)auth:(GTMOAuth2Authentication *)auth
finishedRefreshWithFetcher:(GTMHTTPFetcher *)fetcher
error:(NSError *)error {
@synchronized(authorizationQueue_) {
// If there's an error, we want to try using the old access token anyway,
// in case it's a backend problem preventing refresh, in which case
// access tokens past their expiration date may still work
self.refreshFetcher = nil;
// Swap in a new auth queue in case the callbacks try to immediately auth
// another request
NSArray *pendingAuthQueue = [NSArray arrayWithArray:authorizationQueue_];
[authorizationQueue_ removeAllObjects];
BOOL hasAccessToken = ([self.accessToken length] > 0);
if (hasAccessToken && error == nil) {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc postNotificationName:kGTMOAuth2AccessTokenRefreshed
object:self
userInfo:nil];
}
for (GTMOAuth2AuthorizationArgs *args in pendingAuthQueue) {
if (!hasAccessToken && args.error == nil) {
args.error = error;
}
[self authorizeRequestImmediateArgs:args];
}
}
}
- (BOOL)isAuthorizingRequest:(NSURLRequest *)request {
BOOL wasFound = NO;
@synchronized(authorizationQueue_) {
for (GTMOAuth2AuthorizationArgs *args in authorizationQueue_) {
if ([args request] == request) {
wasFound = YES;
break;
}
}
}
return wasFound;
}
- (BOOL)isAuthorizedRequest:(NSURLRequest *)request {
NSString *authStr = [request valueForHTTPHeaderField:@"Authorization"];
return ([authStr length] > 0);
}
- (void)stopAuthorization {
@synchronized(authorizationQueue_) {
[authorizationQueue_ removeAllObjects];
[self.refreshFetcher stopFetching];
self.refreshFetcher = nil;
}
}
- (void)stopAuthorizationForRequest:(NSURLRequest *)request {
@synchronized(authorizationQueue_) {
NSUInteger argIndex = 0;
BOOL found = NO;
for (GTMOAuth2AuthorizationArgs *args in authorizationQueue_) {
if ([args request] == request) {
found = YES;
break;
}
argIndex++;
}
if (found) {
[authorizationQueue_ removeObjectAtIndex:argIndex];
// If the queue is now empty, go ahead and stop the fetcher.
if ([authorizationQueue_ count] == 0) {
[self stopAuthorization];
}
}
}
}
- (BOOL)authorizeRequestImmediateArgs:(GTMOAuth2AuthorizationArgs *)args {
// This authorization entry point never attempts to refresh the access token,
// but does call the completion routine
NSMutableURLRequest *request = args.request;
NSString *scheme = [[request URL] scheme];
BOOL isAuthorizableRequest = self.shouldAuthorizeAllRequests
|| [scheme caseInsensitiveCompare:@"https"] == NSOrderedSame;
if (!isAuthorizableRequest) {
// Request is not https, so may be insecure
//
// The NSError will be created below
#if DEBUG
NSLog(@"Cannot authorize request with scheme %@ (%@)", scheme, request);
#endif
}
NSString *accessToken = self.accessToken;
if (isAuthorizableRequest && [accessToken length] > 0) {
if (request) {
// we have a likely valid access token
NSString *value = [NSString stringWithFormat:@"%s %@",
GTM_OAUTH2_BEARER, accessToken];
[request setValue:value forHTTPHeaderField:@"Authorization"];
}
// We've authorized the request, even if the previous refresh
// failed with an error
args.error = nil;
} else if (args.error == nil) {
NSDictionary *userInfo = nil;
if (request) {
userInfo = [NSDictionary dictionaryWithObject:request
forKey:kGTMOAuth2ErrorRequestKey];
}
NSInteger code = (isAuthorizableRequest ?
kGTMOAuth2ErrorAuthorizationFailed :
kGTMOAuth2ErrorUnauthorizableRequest);
args.error = [NSError errorWithDomain:kGTMOAuth2ErrorDomain
code:code
userInfo:userInfo];
}
// Invoke any callbacks on the proper thread
if (args.delegate || args.completionHandler) {
NSThread *targetThread = args.thread;
BOOL isSameThread = [targetThread isEqual:[NSThread currentThread]];
if (isSameThread) {
[self invokeCallbackArgs:args];
} else {
SEL sel = @selector(invokeCallbackArgs:);
NSOperationQueue *delegateQueue = self.fetcherService.delegateQueue;
if (delegateQueue) {
NSInvocationOperation *op;
op = [[[NSInvocationOperation alloc] initWithTarget:self
selector:sel
object:args] autorelease];
[delegateQueue addOperation:op];
} else {
[self performSelector:sel
onThread:targetThread
withObject:args
waitUntilDone:NO];
}
}
}
BOOL didAuth = (args.error == nil);
return didAuth;
}
- (void)invokeCallbackArgs:(GTMOAuth2AuthorizationArgs *)args {
// Invoke the callbacks
NSError *error = args.error;
id delegate = args.delegate;
SEL sel = args.selector;
if (delegate && sel) {
NSMutableURLRequest *request = args.request;
NSMethodSignature *sig = [delegate methodSignatureForSelector:sel];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:sig];
[invocation setSelector:sel];
[invocation setTarget:delegate];
[invocation setArgument:&self atIndex:2];
[invocation setArgument:&request atIndex:3];
[invocation setArgument:&error atIndex:4];
[invocation invoke];
}
#if NS_BLOCKS_AVAILABLE
id handler = args.completionHandler;
if (handler) {
void (^authCompletionBlock)(NSError *) = handler;
authCompletionBlock(error);
}
#endif
}
- (BOOL)authorizeRequest:(NSMutableURLRequest *)request {
// Entry point for synchronous authorization mechanisms
GTMOAuth2AuthorizationArgs *args;
args = [GTMOAuth2AuthorizationArgs argsWithRequest:request
delegate:nil
selector:NULL
completionHandler:nil
thread:[NSThread currentThread]];
return [self authorizeRequestImmediateArgs:args];
}
- (BOOL)canAuthorize {
NSString *token = self.refreshToken;
if (token == nil) {
// For services which do not support refresh tokens, we'll just check
// the access token
token = self.accessToken;
}
BOOL canAuth = [token length] > 0;
return canAuth;
}
- (BOOL)shouldRefreshAccessToken {
// We should refresh the access token when it's missing or nearly expired
// and we have a refresh token
BOOL shouldRefresh = NO;
NSString *accessToken = self.accessToken;
NSString *refreshToken = self.refreshToken;
NSString *assertion = self.assertion;
NSString *code = self.code;
BOOL hasRefreshToken = ([refreshToken length] > 0);
BOOL hasAccessToken = ([accessToken length] > 0);
BOOL hasAssertion = ([assertion length] > 0);
BOOL hasCode = ([code length] > 0);
// Determine if we need to refresh the access token
if (hasRefreshToken || hasAssertion || hasCode) {
if (!hasAccessToken) {
shouldRefresh = YES;
} else {
// We'll consider the token expired if it expires 60 seconds from now
// or earlier
NSDate *expirationDate = self.expirationDate;
NSTimeInterval timeToExpire = [expirationDate timeIntervalSinceNow];
if (expirationDate == nil || timeToExpire < 60.0) {
// access token has expired, or will in a few seconds
shouldRefresh = YES;
}
}
}
return shouldRefresh;
}
- (void)waitForCompletionWithTimeout:(NSTimeInterval)timeoutInSeconds {
// If there is a refresh fetcher pending, wait for it.
//
// This is only intended for unit test or for use in command-line tools.
GTMHTTPFetcher *fetcher = self.refreshFetcher;
[fetcher waitForCompletionWithTimeout:timeoutInSeconds];
}
#pragma mark Token Fetch
- (NSString *)userAgent {
NSBundle *bundle = [NSBundle mainBundle];
NSString *appID = [bundle bundleIdentifier];
NSString *version = [bundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
if (version == nil) {
version = [bundle objectForInfoDictionaryKey:@"CFBundleVersion"];
}
if (appID && version) {
appID = [appID stringByAppendingFormat:@"/%@", version];
}
NSString *userAgent = @"gtm-oauth2";
if (appID) {
userAgent = [userAgent stringByAppendingFormat:@" %@", appID];
}
return userAgent;
}
- (GTMHTTPFetcher *)beginTokenFetchWithDelegate:(id)delegate
didFinishSelector:(SEL)finishedSel {
NSMutableDictionary *paramsDict = [NSMutableDictionary dictionary];
NSString *fetchType;
NSString *refreshToken = self.refreshToken;
NSString *code = self.code;
NSString *assertion = self.assertion;
if (refreshToken) {
// We have a refresh token
[paramsDict setObject:@"refresh_token" forKey:@"grant_type"];
[paramsDict setObject:refreshToken forKey:@"refresh_token"];
NSString *refreshScope = self.refreshScope;
if ([refreshScope length] > 0) {
[paramsDict setObject:refreshScope forKey:@"scope"];
}
fetchType = kGTMOAuth2FetchTypeRefresh;
} else if (code) {
// We have a code string
[paramsDict setObject:@"authorization_code" forKey:@"grant_type"];
[paramsDict setObject:code forKey:@"code"];
NSString *redirectURI = self.redirectURI;
if ([redirectURI length] > 0) {
[paramsDict setObject:redirectURI forKey:@"redirect_uri"];
}
NSString *scope = self.scope;
if ([scope length] > 0) {
[paramsDict setObject:scope forKey:@"scope"];
}
fetchType = kGTMOAuth2FetchTypeToken;
} else if (assertion) {
// We have an assertion string
[paramsDict setObject:assertion forKey:@"assertion"];
[paramsDict setObject:@"http://oauth.net/grant_type/jwt/1.0/bearer"
forKey:@"grant_type"];
fetchType = kGTMOAuth2FetchTypeAssertion;
} else {
#if DEBUG
NSAssert(0, @"unexpected lack of code or refresh token for fetching");
#endif
return nil;
}
NSString *clientID = self.clientID;
if ([clientID length] > 0) {
[paramsDict setObject:clientID forKey:@"client_id"];
}
NSString *clientSecret = self.clientSecret;
if ([clientSecret length] > 0) {
[paramsDict setObject:clientSecret forKey:@"client_secret"];
}
NSDictionary *additionalParams = self.additionalTokenRequestParameters;
if (additionalParams) {
[paramsDict addEntriesFromDictionary:additionalParams];
}
NSString *paramStr = [[self class] encodedQueryParametersForDictionary:paramsDict];
NSData *paramData = [paramStr dataUsingEncoding:NSUTF8StringEncoding];
NSURL *tokenURL = self.tokenURL;
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:tokenURL];
[request setValue:@"application/x-www-form-urlencoded"
forHTTPHeaderField:@"Content-Type"];
NSString *userAgent = [self userAgent];
[request setValue:userAgent forHTTPHeaderField:@"User-Agent"];
GTMHTTPFetcher *fetcher;
id <GTMHTTPFetcherServiceProtocol> fetcherService = self.fetcherService;
if (fetcherService) {
fetcher = [fetcherService fetcherWithRequest:request];
// Don't use an authorizer for an auth token fetch
fetcher.authorizer = nil;
} else {
fetcher = [GTMHTTPFetcher fetcherWithRequest:request];
}
NSString *const template = (refreshToken ? @"refresh token for %@" : @"fetch tokens for %@");
[fetcher setCommentWithFormat:template, [tokenURL host]];
fetcher.postData = paramData;
fetcher.retryEnabled = YES;
fetcher.maxRetryInterval = 15.0;
// Fetcher properties will retain the delegate
[fetcher setProperty:delegate forKey:kTokenFetchDelegateKey];
if (finishedSel) {
NSString *selStr = NSStringFromSelector(finishedSel);
[fetcher setProperty:selStr forKey:kTokenFetchSelectorKey];
}
if ([fetcher beginFetchWithDelegate:self
didFinishSelector:@selector(tokenFetcher:finishedWithData:error:)]) {
// Fetch began
[self notifyFetchIsRunning:YES fetcher:fetcher type:fetchType];
return fetcher;
} else {
// Failed to start fetching; typically a URL issue
NSError *error = [NSError errorWithDomain:kGTMHTTPFetcherStatusDomain
code:-1
userInfo:nil];
[[self class] invokeDelegate:delegate
selector:finishedSel
object:self
object:nil
object:error];
return nil;
}
}
- (void)tokenFetcher:(GTMHTTPFetcher *)fetcher
finishedWithData:(NSData *)data
error:(NSError *)error {
[self notifyFetchIsRunning:NO fetcher:fetcher type:nil];
NSDictionary *responseHeaders = [fetcher responseHeaders];
NSString *responseType = [responseHeaders valueForKey:@"Content-Type"];
BOOL isResponseJSON = [responseType hasPrefix:@"application/json"];
BOOL hasData = ([data length] > 0);
if (error) {
// Failed; if the error body is JSON, parse it and add it to the error's
// userInfo dictionary
if (hasData) {
if (isResponseJSON) {
NSDictionary *errorJson = [self dictionaryWithJSONData:data];
if ([errorJson count] > 0) {
#if DEBUG
NSLog(@"Error %@\nError data:\n%@", error, errorJson);
#endif
// Add the JSON error body to the userInfo of the error
NSMutableDictionary *userInfo;
userInfo = [NSMutableDictionary dictionaryWithObject:errorJson
forKey:kGTMOAuth2ErrorJSONKey];
NSDictionary *prevUserInfo = [error userInfo];
if (prevUserInfo) {
[userInfo addEntriesFromDictionary:prevUserInfo];
}
error = [NSError errorWithDomain:[error domain]
code:[error code]
userInfo:userInfo];
}
}
}
} else {
// Succeeded; we have an access token
#if DEBUG
NSAssert(hasData, @"data missing in token response");
#endif
if (hasData) {
if (isResponseJSON) {
[self setKeysForResponseJSONData:data];
} else {
// Support for legacy token servers that return form-urlencoded data
NSString *dataStr = [[[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding] autorelease];
[self setKeysForResponseString:dataStr];
}
#if DEBUG
// Watch for token exchanges that return a non-bearer or unlabeled token
NSString *tokenType = [self tokenType];
if (tokenType == nil
|| [tokenType caseInsensitiveCompare:@"bearer"] != NSOrderedSame) {
NSLog(@"GTMOAuth2: Unexpected token type: %@", tokenType);
}
#endif
}
}
id delegate = [fetcher propertyForKey:kTokenFetchDelegateKey];
SEL sel = NULL;
NSString *selStr = [fetcher propertyForKey:kTokenFetchSelectorKey];
if (selStr) sel = NSSelectorFromString(selStr);
[[self class] invokeDelegate:delegate
selector:sel
object:self
object:fetcher
object:error];
// Prevent a circular reference from retaining the delegate
[fetcher setProperty:nil forKey:kTokenFetchDelegateKey];
}
#pragma mark Fetch Notifications
- (void)notifyFetchIsRunning:(BOOL)isStarting
fetcher:(GTMHTTPFetcher *)fetcher
type:(NSString *)fetchType {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
NSString *name = (isStarting ? kGTMOAuth2FetchStarted : kGTMOAuth2FetchStopped);
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
fetcher, kGTMOAuth2FetcherKey,
fetchType, kGTMOAuth2FetchTypeKey, // fetchType may be nil
nil];
[nc postNotificationName:name
object:self
userInfo:dict];
}
#pragma mark Persistent Response Strings
- (void)setKeysForPersistenceResponseString:(NSString *)str {
// All persistence keys can be set directly as if returned by a server
[self setKeysForResponseString:str];
}
// This returns a "response string" that can be passed later to
// setKeysForResponseString: to reuse an old access token in a new auth object
- (NSString *)persistenceResponseString {
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:4];
NSString *refreshToken = self.refreshToken;
NSString *accessToken = nil;
if (refreshToken == nil) {
// We store the access token only for services that do not support refresh
// tokens; otherwise, we assume the access token is too perishable to
// be worth storing
accessToken = self.accessToken;
}
// Any nil values will not set a dictionary entry
[dict setValue:refreshToken forKey:kOAuth2RefreshTokenKey];
[dict setValue:accessToken forKey:kOAuth2AccessTokenKey];
[dict setValue:self.serviceProvider forKey:kServiceProviderKey];
[dict setValue:self.userID forKey:kUserIDKey];
[dict setValue:self.userEmail forKey:kUserEmailKey];
[dict setValue:self.userEmailIsVerified forKey:kUserEmailIsVerifiedKey];
[dict setValue:self.scope forKey:kOAuth2ScopeKey];
NSString *result = [[self class] encodedQueryParametersForDictionary:dict];
return result;
}
- (BOOL)primeForRefresh {
if (self.refreshToken == nil) {
// Cannot refresh without a refresh token
return NO;
}
self.accessToken = nil;
self.expiresIn = nil;
self.expirationDate = nil;
self.errorString = nil;
return YES;
}
- (void)reset {
// Reset all per-authorization values
self.code = nil;
self.accessToken = nil;
self.refreshToken = nil;
self.assertion = nil;
self.expiresIn = nil;
self.errorString = nil;
self.expirationDate = nil;
self.userEmail = nil;
self.userEmailIsVerified = nil;
}
#pragma mark Accessors for Response Parameters
- (NSString *)accessToken {
return [self.parameters objectForKey:kOAuth2AccessTokenKey];
}
- (void)setAccessToken:(NSString *)str {
[self.parameters setValue:str forKey:kOAuth2AccessTokenKey];
}
- (NSString *)refreshToken {
return [self.parameters objectForKey:kOAuth2RefreshTokenKey];
}
- (void)setRefreshToken:(NSString *)str {
[self.parameters setValue:str forKey:kOAuth2RefreshTokenKey];
}
- (NSString *)code {
return [self.parameters objectForKey:kOAuth2CodeKey];
}
- (void)setCode:(NSString *)str {
[self.parameters setValue:str forKey:kOAuth2CodeKey];
}
- (NSString *)assertion {
return [self.parameters objectForKey:kOAuth2AssertionKey];
}
- (void)setAssertion:(NSString *)str {
[self.parameters setValue:str forKey:kOAuth2AssertionKey];
}
- (NSString *)refreshScope {
return [self.parameters objectForKey:kOAuth2RefreshScopeKey];