Skip to content

Commit 799d288

Browse files
committed
Merge pull request #128 from ParsePlatform/grantland.auth_callbacks
Clean up third party authentication APIs
2 parents 6d51a01 + 51ada3e commit 799d288

9 files changed

+148
-221
lines changed

Parse/src/main/java/com/parse/AnonymousAuthenticationProvider.java

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2015-present, Parse, LLC.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
package com.parse;
10+
11+
import java.util.Map;
12+
13+
/**
14+
* Provides a general interface for delegation of third party authentication callbacks.
15+
*/
16+
public interface AuthenticationCallback {
17+
/**
18+
* Called when restoring third party authentication credentials that have been serialized,
19+
* such as session keys, etc.
20+
* <p />
21+
* <strong>Note:</strong> This will be executed on a background thread.
22+
*
23+
* @param authData
24+
* The auth data for the provider. This value may be {@code null} when
25+
* unlinking an account.
26+
*
27+
* @return {@code true} iff the {@code authData} was successfully synchronized or {@code false}
28+
* if user should no longer be associated because of bad {@code authData}.
29+
*/
30+
boolean onRestore(Map<String, String> authData);
31+
}

Parse/src/main/java/com/parse/CachedCurrentUserController.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,8 @@ public ParseUser then(Task<ParseUser> task) throws Exception {
268268
}
269269

270270
private ParseUser lazyLogIn() {
271-
AnonymousAuthenticationProvider provider = ParseAnonymousUtils.getProvider();
272-
String authType = provider.getAuthType();
273-
Map<String, String> authData = provider.getAuthData();
274-
return lazyLogIn(authType, authData);
271+
Map<String, String> authData = ParseAnonymousUtils.getAuthData();
272+
return lazyLogIn(ParseAnonymousUtils.AUTH_TYPE, authData);
275273
}
276274

277275
/* package for tests */ ParseUser lazyLogIn(String authType, Map<String, String> authData) {

Parse/src/main/java/com/parse/ParseAnonymousUtils.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
*/
99
package com.parse;
1010

11+
import java.util.HashMap;
1112
import java.util.Map;
13+
import java.util.UUID;
1214

1315
import bolts.Continuation;
1416
import bolts.Task;
@@ -35,18 +37,8 @@
3537
* </ul>
3638
*/
3739
public final class ParseAnonymousUtils {
38-
private static AnonymousAuthenticationProvider provider;
39-
4040
/* package */ static final String AUTH_TYPE = "anonymous";
4141

42-
/* package */ static AnonymousAuthenticationProvider getProvider() {
43-
if (provider == null) {
44-
provider = new AnonymousAuthenticationProvider();
45-
ParseUser.registerAuthenticationProvider(provider);
46-
}
47-
return provider;
48-
}
49-
5042
/**
5143
* Whether the user is logged in anonymously.
5244
*
@@ -65,13 +57,7 @@ public static boolean isLinked(ParseUser user) {
6557
* @return A Task that will be resolved when logging in is completed.
6658
*/
6759
public static Task<ParseUser> logInInBackground() {
68-
final AnonymousAuthenticationProvider provider = getProvider();
69-
return provider.authenticateAsync().onSuccessTask(new Continuation<Map<String, String>, Task<ParseUser>>() {
70-
@Override
71-
public Task<ParseUser> then(Task<Map<String, String>> task) throws Exception {
72-
return ParseUser.logInWithInBackground(provider.getAuthType(), task.getResult());
73-
}
74-
});
60+
return ParseUser.logInWithInBackground(AUTH_TYPE, getAuthData());
7561
}
7662

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

73+
/* package */ static Map<String, String> getAuthData() {
74+
Map<String, String> authData = new HashMap<>();
75+
authData.put("id", UUID.randomUUID().toString());
76+
return authData;
77+
}
78+
8779
private ParseAnonymousUtils() {
8880
// do nothing
8981
}

Parse/src/main/java/com/parse/ParseAuthenticationManager.java

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,40 @@
1010

1111
import java.util.HashMap;
1212
import java.util.Map;
13+
import java.util.concurrent.Callable;
1314

1415
import bolts.Continuation;
1516
import bolts.Task;
1617

1718
/** package */ class ParseAuthenticationManager {
1819

1920
private final Object lock = new Object();
20-
private final Map<String, ParseAuthenticationProvider> authenticationProviders = new HashMap<>();
21+
private final Map<String, AuthenticationCallback> callbacks = new HashMap<>();
2122
private final ParseCurrentUserController controller;
2223

2324
public ParseAuthenticationManager(ParseCurrentUserController controller) {
2425
this.controller = controller;
2526
}
2627

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

3333
synchronized (lock) {
34-
if (authenticationProviders.containsKey(authType)) {
35-
throw new IllegalStateException("Another " + authType + " provider was already registered: "
36-
+ authenticationProviders.get(authType));
34+
if (this.callbacks.containsKey(authType)) {
35+
throw new IllegalStateException("Callback already registered for <" + authType + ">: "
36+
+ this.callbacks.get(authType));
3737
}
38-
authenticationProviders.put(provider.getAuthType(), provider);
38+
this.callbacks.put(authType, callback);
3939
}
4040

41-
if (provider instanceof AnonymousAuthenticationProvider) {
41+
if (ParseAnonymousUtils.AUTH_TYPE.equals(authType)) {
4242
// There's nothing to synchronize
4343
return;
4444
}
4545

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

59-
public Task<Void> restoreAuthenticationAsync(String authType, Map<String, String> authData) {
60-
ParseAuthenticationProvider provider;
59+
public Task<Boolean> restoreAuthenticationAsync(String authType, final Map<String, String> authData) {
60+
final AuthenticationCallback callback;
6161
synchronized (lock) {
62-
provider = authenticationProviders.get(authType);
62+
callback = this.callbacks.get(authType);
6363
}
64-
if (provider == null) {
65-
return Task.forResult(null);
64+
if (callback == null) {
65+
return Task.forResult(true);
6666
}
67-
return provider.restoreAuthenticationInBackground(authData);
67+
return Task.call(new Callable<Boolean>() {
68+
@Override
69+
public Boolean call() throws Exception {
70+
return callback.onRestore(authData);
71+
}
72+
}, ParseExecutors.io());
6873
}
6974

7075
public Task<Void> deauthenticateAsync(String authType) {
71-
ParseAuthenticationProvider provider;
76+
final AuthenticationCallback callback;
7277
synchronized (lock) {
73-
provider = authenticationProviders.get(authType);
78+
callback = this.callbacks.get(authType);
7479
}
75-
if (provider != null) {
76-
return provider.deauthenticateInBackground();
80+
if (callback != null) {
81+
return Task.call(new Callable<Void>() {
82+
@Override
83+
public Void call() throws Exception {
84+
callback.onRestore(null);
85+
return null;
86+
}
87+
}, ParseExecutors.io());
7788
}
7889
return Task.forResult(null);
7990
}

Parse/src/main/java/com/parse/ParseAuthenticationProvider.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)