From daed6b284dc72d30b4cfd702d77c32ac2fe61fc7 Mon Sep 17 00:00:00 2001 From: Christine Abernathy Date: Fri, 3 Feb 2012 14:12:34 -0800 Subject: [PATCH] Invalidate session only if access token errors when extending token Summary: Currently when there is a Platform API call that has any error or if there is a connection error, e.g. timeout, the access token is invalidated. This was due to diff D378197 where any errors call invalidSession. To fix this narrow the invalidate call to the scenario where the access token has expired when an API call is made. For now, only REST API error codes allow us to easily identify an invalid token (error code 190). A separate diff will be implemented by the Platform team to provide an error code for Graph API auth token errors. Test Plan: Case #1 1/ Login 2/ Tap Graph API 3/ Tap Get your friends 4/ Go off LAN and WiFi 5/ Tap Get your friends Case #2 1/ Turn WiFi back on 2/ Tap Get your friends 3/ See friends list 4/ Go back to get to Graph API page 5/ Remove app from https://www.facebook.com/settings/?tab=applications 6/ Tap Get your friends Case #3 1/ Temporarily modified code to force a Graph API error //[[delegate facebook] requestWithGraphPath:@"me" andParams:params andDelegate:self]; [[delegate facebook] requestWithGraphPath:@"000000" andParams:params andDelegate:self]; 2/ Tap Graph API 3/ Tap Get your information Before this change, here were the results Case #1: Session expires message, which is incorrect behavior Case #2: Session expires message, which is correct behavior Case #3: Session expires message, which is incorrect behavior After this change, here are the results Case #1: Get the Hackbook "Oops something went wrong" message, which is desired behavior Case #2: Get the Hackbook "Oops something went wrong" message, which is not ideal, user should also be logged out Case #3: Get the Hackbook "Oops something went wrong" message, which is desired behavior Reviewers: yariv, jimbru, brent, toddkrabach, jonathan Reviewed By: yariv CC: lshepard, selekman, beau, bgolub, danmuriello, jacl Differential Revision: https://phabricator.fb.com/D402481 Revert Plan: OK Task ID: 900407 --- src/FBRequest.h | 2 ++ src/FBRequest.m | 16 +++++++++------- src/Facebook.m | 12 ++++++------ 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/FBRequest.h b/src/FBRequest.h index 7205c10bf9..c1143493b9 100644 --- a/src/FBRequest.h +++ b/src/FBRequest.h @@ -39,6 +39,7 @@ typedef NSUInteger FBRequestState; NSMutableData* _responseText; FBRequestState _state; NSError* _error; + BOOL _sessionDidExpire; } @@ -64,6 +65,7 @@ typedef NSUInteger FBRequestState; @property(nonatomic,retain) NSURLConnection* connection; @property(nonatomic,retain) NSMutableData* responseText; @property(nonatomic,readonly) FBRequestState state; +@property(nonatomic,readonly) BOOL sessionDidExpire; /** * Error returned by the server in case of request's failure (or nil otherwise). diff --git a/src/FBRequest.m b/src/FBRequest.m index 593d3160dd..eb1b8104eb 100644 --- a/src/FBRequest.m +++ b/src/FBRequest.m @@ -23,6 +23,7 @@ static NSString* kUserAgent = @"FacebookConnect"; static NSString* kStringBoundary = @"3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f"; static const int kGeneralErrorCode = 10000; +static const int kRESTAPIAccessTokenErrorCode = 190; static const NSTimeInterval kTimeoutInterval = 180.0; @@ -30,6 +31,7 @@ @interface FBRequest () @property (nonatomic,readwrite) FBRequestState state; +@property (nonatomic,readwrite) BOOL sessionDidExpire; @end @implementation FBRequest @@ -41,6 +43,7 @@ @implementation FBRequest connection = _connection, responseText = _responseText, state = _state, + sessionDidExpire = _sessionDidExpire, error = _error; ////////////////////////////////////////////////////////////////////////////////////////////////// // class public @@ -239,10 +242,12 @@ - (id)parseJsonResponse:(NSData *)data error:(NSError **)error { * fails with error */ - (void)failWithError:(NSError *)error { + if ([error code] == kRESTAPIAccessTokenErrorCode) { + self.sessionDidExpire = YES; + } if ([_delegate respondsToSelector:@selector(request:didFailWithError:)]) { [_delegate request:self didFailWithError:error]; } - self.state = kFBRequestStateError; } /* @@ -313,6 +318,7 @@ - (void)connect { _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; self.state = kFBRequestStateLoading; + self.sessionDidExpire = NO; } /** @@ -355,10 +361,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection { self.responseText = nil; self.connection = nil; - - if (self.state != kFBRequestStateError) { - self.state = kFBRequestStateComplete; - } + self.state = kFBRequestStateComplete; } - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { @@ -366,8 +369,7 @@ - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)err self.responseText = nil; self.connection = nil; - - self.state = kFBRequestStateError; + self.state = kFBRequestStateComplete; } @end diff --git a/src/Facebook.m b/src/Facebook.m index 4380e84afa..e765ec5e86 100644 --- a/src/Facebook.m +++ b/src/Facebook.m @@ -182,13 +182,13 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N if (context == finishedContext) { FBRequest* _request = (FBRequest*)object; FBRequestState requestState = [_request state]; - if (requestState == kFBRequestStateError) { - [self invalidateSession]; - if ([self.sessionDelegate respondsToSelector:@selector(fbSessionInvalidated)]) { - [self.sessionDelegate fbSessionInvalidated]; + if (requestState == kFBRequestStateComplete) { + if ([_request sessionDidExpire]) { + [self invalidateSession]; + if ([self.sessionDelegate respondsToSelector:@selector(fbSessionInvalidated)]) { + [self.sessionDelegate fbSessionInvalidated]; + } } - } - if (requestState == kFBRequestStateComplete || requestState == kFBRequestStateError) { [_request removeObserver:self forKeyPath:requestFinishedKeyPath]; [_requests removeObject:_request]; }