Skip to content
Browse files

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

CC: lshepard, selekman

Differential Revision: https://phabricator.fb.com/D402481

Revert Plan: OK

Task ID: 900407
  • Loading branch information...
1 parent 4aa2a66 commit 82bc92c0bd173be86e939d9a456210b0dbf5de8a Christine Abernathy committed with Christine Abernathy Feb 3, 2012
Showing with 17 additions and 13 deletions.
  1. +2 −0 src/FBRequest.h
  2. +9 −7 src/FBRequest.m
  3. +6 −6 src/Facebook.m
View
2 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).
View
16 src/FBRequest.m
@@ -23,13 +23,15 @@
static NSString* kUserAgent = @"FacebookConnect";
static NSString* kStringBoundary = @"3i2ndDfv2rTHiSisAbouNdArYfORhtTPEefj3q2f";
static const int kGeneralErrorCode = 10000;
+static const int kRESTAPIAccessTokenErrorCode = 190;
static const NSTimeInterval kTimeoutInterval = 180.0;
///////////////////////////////////////////////////////////////////////////////////////////////////
@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,19 +361,15 @@ - (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 {
[self failWithError:error];
self.responseText = nil;
self.connection = nil;
-
- self.state = kFBRequestStateError;
+ self.state = kFBRequestStateComplete;
}
@end
View
12 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];
}

0 comments on commit 82bc92c

Please sign in to comment.
Something went wrong with that request. Please try again.