Skip to content
This repository was archived by the owner on Feb 14, 2020. It is now read-only.

prevent asymmetric facebook session management with flag #28

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.2'
classpath 'com.android.tools.build:gradle:3.0.0'
}
}

Expand Down
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ buildscript {
}

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
compileSdkVersion 26
buildToolsVersion '26.0.2'

defaultConfig {
minSdkVersion 15
targetSdkVersion 25
targetSdkVersion 26
versionCode 1
versionName project.version
}
Expand Down
6 changes: 4 additions & 2 deletions library/src/main/java/com/parse/FacebookController.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,12 @@ public Map<String, String> getAuthData(AccessToken accessToken) {
return authData;
}

public void setAuthData(Map<String, String> authData)
public void setAuthData(Map<String, String> authData, boolean isFacebookLoginScopeExternallyManaged)
throws java.text.ParseException {
if (authData == null) {
facebookSdkDelegate.getLoginManager().logOut();
if (!isFacebookLoginScopeExternallyManaged) {
facebookSdkDelegate.getLoginManager().logOut();
}
return;
}

Expand Down
5 changes: 4 additions & 1 deletion library/src/main/java/com/parse/ParseFacebookUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public final class ParseFacebookUtils {

private static final Object lock = new Object();
/* package for tests */ static boolean isInitialized;
/* package for tests */ static boolean isFacebookLoginScopeExternallyManaged = false;
/* package for tests */ static FacebookController controller;
/* package for tests */ static ParseUserDelegate userDelegate = new ParseUserDelegateImpl();

Expand Down Expand Up @@ -111,7 +112,7 @@ public static void initialize(Context context, int callbackRequestCodeOffset) {
@Override
public boolean onRestore(Map<String, String> authData) {
try {
getController().setAuthData(authData);
getController().setAuthData(authData, isFacebookLoginScopeExternallyManaged);
return true;
} catch (Exception e) {
return false;
Expand Down Expand Up @@ -167,6 +168,7 @@ public static boolean onActivityResult(int requestCode, int resultCode, Intent d
* @return A task that will be resolved when logging in is complete.
*/
public static Task<ParseUser> logInInBackground(AccessToken accessToken) {
isFacebookLoginScopeExternallyManaged = true;
checkInitialization();
return userDelegate.logInWithInBackground(AUTH_TYPE, getController().getAuthData(accessToken));
}
Expand All @@ -180,6 +182,7 @@ public static Task<ParseUser> logInInBackground(AccessToken accessToken) {
* @return A task that will be resolved when logging in is complete.
*/
public static Task<ParseUser> logInInBackground(AccessToken accessToken, LogInCallback callback) {
isFacebookLoginScopeExternallyManaged = true;
return callbackOnMainThreadAsync(logInInBackground(accessToken), callback, true);
}

Expand Down
24 changes: 17 additions & 7 deletions library/src/test/java/com/parse/FacebookControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,20 +102,30 @@ public void testGetAuthData() {
//region testSetAuthData

@Test
public void testSetAuthDataWithNull() throws java.text.ParseException {
public void testSetAuthDataWithNullNotExternallyManaged() throws java.text.ParseException {
FacebookController.FacebookSdkDelegate facebookSdk =
mock(FacebookController.FacebookSdkDelegate.class);
mock(FacebookController.FacebookSdkDelegate.class);
LoginManager loginManager = mock(LoginManager.class);
when(facebookSdk.getLoginManager()).thenReturn(loginManager);
FacebookController controller = new FacebookController(facebookSdk);

controller.setAuthData(null);
controller.setAuthData(null, false);
verify(facebookSdk).getLoginManager();
verifyNoMoreInteractions(facebookSdk);
verify(loginManager).logOut();
verifyNoMoreInteractions(loginManager);
}

@Test
public void testSetAuthDataWithNullExternallyManaged() throws java.text.ParseException {
FacebookController.FacebookSdkDelegate facebookSdk =
mock(FacebookController.FacebookSdkDelegate.class);
FacebookController controller = new FacebookController(facebookSdk);

controller.setAuthData(null, true);
verifyNoMoreInteractions(facebookSdk);
}

@Test
public void testSetAuthData() throws ParseException {
Locale.setDefault(new Locale("ar")); // Mimic the device's locale
Expand All @@ -130,7 +140,7 @@ public void testSetAuthData() throws ParseException {
authData.put("id", "test_id");
authData.put("access_token", "test_token");
authData.put("expiration_date", "2015-07-03T07:00:00.000Z");
controller.setAuthData(authData);
controller.setAuthData(authData, true);
ArgumentCaptor<AccessToken> accessTokenCapture = ArgumentCaptor.forClass(AccessToken.class);
verify(facebookSdk).setCurrentAccessToken(accessTokenCapture.capture());
AccessToken accessToken = accessTokenCapture.getValue();
Expand All @@ -153,13 +163,13 @@ public void testSetAuthDataWithDifferentAccessToken() throws ParseException {
authData.put("id", "new_id");
authData.put("access_token", "test_token");
authData.put("expiration_date", "2015-07-03T07:00:00.000Z");
controller.setAuthData(authData);
controller.setAuthData(authData, true);
verify(facebookSdk, times(1)).setCurrentAccessToken(any(AccessToken.class));

authData.put("id", "new_id");
authData.put("access_token", "new_token");
authData.put("expiration_date", "2015-07-03T07:00:00.000Z");
controller.setAuthData(authData);
controller.setAuthData(authData, true);
verify(facebookSdk, times(2)).setCurrentAccessToken(any(AccessToken.class));
}

Expand All @@ -175,7 +185,7 @@ public void testSetAuthDataWithSameAccessToken() throws ParseException {
authData.put("id", "test_id");
authData.put("access_token", "test_token");
authData.put("expiration_date", "2015-07-03T07:00:00.000Z");
controller.setAuthData(authData);
controller.setAuthData(authData, true);
verify(facebookSdk, never()).setCurrentAccessToken(any(AccessToken.class));
}

Expand Down
7 changes: 4 additions & 3 deletions library/src/test/java/com/parse/ParseFacebookUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyListOf;
import static org.mockito.Matchers.anyMapOf;
Expand Down Expand Up @@ -102,7 +103,7 @@ public void testRestoreAuthentication() throws java.text.ParseException {
Map<String, String> authData = new HashMap<>();

assertTrue(callback.onRestore(authData));
verify(controller).setAuthData(authData);
verify(controller).setAuthData(authData, true);
}

@Test
Expand All @@ -115,10 +116,10 @@ public void testRestoreAuthenticationFailure() throws java.text.ParseException {
Map<String, String> authData = new HashMap<>();
doThrow(new RuntimeException())
.when(controller)
.setAuthData(anyMapOf(String.class, String.class));
.setAuthData(anyMapOf(String.class, String.class), anyBoolean());

assertFalse(callback.onRestore(authData));
verify(controller).setAuthData(authData);
verify(controller).setAuthData(authData, false);
}

//endregion
Expand Down