Skip to content
Merged
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
48 changes: 0 additions & 48 deletions Parse/src/main/java/com/parse/AnonymousAuthenticationProvider.java

This file was deleted.

31 changes: 31 additions & 0 deletions Parse/src/main/java/com/parse/AuthenticationCallback.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (c) 2015-present, Parse, LLC.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.parse;

import java.util.Map;

/**
* Provides a general interface for delegation of third party authentication callbacks.
*/
public interface AuthenticationCallback {
/**
* Called when restoring third party authentication credentials that have been serialized,
* such as session keys, etc.
* <p />
* <strong>Note:</strong> This will be executed on a background thread.
*
* @param authData
* The auth data for the provider. This value may be {@code null} when
* unlinking an account.
*
* @return {@code true} iff the {@code authData} was successfully synchronized or {@code false}
* if user should no longer be associated because of bad {@code authData}.
*/
boolean onRestore(Map<String, String> authData);
}
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,8 @@ public ParseUser then(Task<ParseUser> task) throws Exception {
}

private ParseUser lazyLogIn() {
AnonymousAuthenticationProvider provider = ParseAnonymousUtils.getProvider();
String authType = provider.getAuthType();
Map<String, String> authData = provider.getAuthData();
return lazyLogIn(authType, authData);
Map<String, String> authData = ParseAnonymousUtils.getAuthData();
return lazyLogIn(ParseAnonymousUtils.AUTH_TYPE, authData);
}

/* package for tests */ ParseUser lazyLogIn(String authType, Map<String, String> authData) {
Expand Down
26 changes: 9 additions & 17 deletions Parse/src/main/java/com/parse/ParseAnonymousUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
*/
package com.parse;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import bolts.Continuation;
import bolts.Task;
Expand All @@ -35,18 +37,8 @@
* </ul>
*/
public final class ParseAnonymousUtils {
private static AnonymousAuthenticationProvider provider;

/* package */ static final String AUTH_TYPE = "anonymous";

/* package */ static AnonymousAuthenticationProvider getProvider() {
if (provider == null) {
provider = new AnonymousAuthenticationProvider();
ParseUser.registerAuthenticationProvider(provider);
}
return provider;
}

/**
* Whether the user is logged in anonymously.
*
Expand All @@ -65,13 +57,7 @@ public static boolean isLinked(ParseUser user) {
* @return A Task that will be resolved when logging in is completed.
*/
public static Task<ParseUser> logInInBackground() {
final AnonymousAuthenticationProvider provider = getProvider();
return provider.authenticateAsync().onSuccessTask(new Continuation<Map<String, String>, Task<ParseUser>>() {
@Override
public Task<ParseUser> then(Task<Map<String, String>> task) throws Exception {
return ParseUser.logInWithInBackground(provider.getAuthType(), task.getResult());
}
});
return ParseUser.logInWithInBackground(AUTH_TYPE, getAuthData());
}

/**
Expand All @@ -84,6 +70,12 @@ public static void logIn(LogInCallback callback) {
ParseTaskUtils.callbackOnMainThreadAsync(logInInBackground(), callback);
}

/* package */ static Map<String, String> getAuthData() {
Map<String, String> authData = new HashMap<>();
authData.put("id", UUID.randomUUID().toString());
return authData;
}

private ParseAnonymousUtils() {
// do nothing
}
Expand Down
49 changes: 30 additions & 19 deletions Parse/src/main/java/com/parse/ParseAuthenticationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,40 @@

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

import bolts.Continuation;
import bolts.Task;

/** package */ class ParseAuthenticationManager {

private final Object lock = new Object();
private final Map<String, ParseAuthenticationProvider> authenticationProviders = new HashMap<>();
private final Map<String, AuthenticationCallback> callbacks = new HashMap<>();
private final ParseCurrentUserController controller;

public ParseAuthenticationManager(ParseCurrentUserController controller) {
this.controller = controller;
}

public void register(ParseAuthenticationProvider provider) {
final String authType = provider.getAuthType();
public void register(final String authType, AuthenticationCallback callback) {
if (authType == null) {
throw new IllegalArgumentException("Invalid authType: " + null);
}

synchronized (lock) {
if (authenticationProviders.containsKey(authType)) {
throw new IllegalStateException("Another " + authType + " provider was already registered: "
+ authenticationProviders.get(authType));
if (this.callbacks.containsKey(authType)) {
throw new IllegalStateException("Callback already registered for <" + authType + ">: "
+ this.callbacks.get(authType));
}
authenticationProviders.put(provider.getAuthType(), provider);
this.callbacks.put(authType, callback);
}

if (provider instanceof AnonymousAuthenticationProvider) {
if (ParseAnonymousUtils.AUTH_TYPE.equals(authType)) {
// There's nothing to synchronize
return;
}

// Synchronize the current user with the auth provider.
// Synchronize the current user with the auth callback.
controller.getAsync(false).onSuccessTask(new Continuation<ParseUser, Task<Void>>() {
@Override
public Task<Void> then(Task<ParseUser> task) throws Exception {
Expand All @@ -56,24 +56,35 @@ public Task<Void> then(Task<ParseUser> task) throws Exception {
});
}

public Task<Void> restoreAuthenticationAsync(String authType, Map<String, String> authData) {
ParseAuthenticationProvider provider;
public Task<Boolean> restoreAuthenticationAsync(String authType, final Map<String, String> authData) {
final AuthenticationCallback callback;
synchronized (lock) {
provider = authenticationProviders.get(authType);
callback = this.callbacks.get(authType);
}
if (provider == null) {
return Task.forResult(null);
if (callback == null) {
return Task.forResult(true);
}
return provider.restoreAuthenticationInBackground(authData);
return Task.call(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return callback.onRestore(authData);
}
}, ParseExecutors.io());
}

public Task<Void> deauthenticateAsync(String authType) {
ParseAuthenticationProvider provider;
final AuthenticationCallback callback;
synchronized (lock) {
provider = authenticationProviders.get(authType);
callback = this.callbacks.get(authType);
}
if (provider != null) {
return provider.deauthenticateInBackground();
if (callback != null) {
return Task.call(new Callable<Void>() {
@Override
public Void call() throws Exception {
callback.onRestore(null);
return null;
}
}, ParseExecutors.io());
}
return Task.forResult(null);
}
Expand Down
46 changes: 0 additions & 46 deletions Parse/src/main/java/com/parse/ParseAuthenticationProvider.java

This file was deleted.

Loading