Skip to content

Commit 4684053

Browse files
committed
Merge pull request #125 from ParsePlatform/grantland.authentication_public
Publicize Third Party Authentication methods
2 parents da458dd + c79d8c1 commit 4684053

File tree

7 files changed

+197
-146
lines changed

7 files changed

+197
-146
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
*/
2121
/** package */ class AnonymousAuthenticationProvider implements ParseAuthenticationProvider {
2222

23-
@Override
2423
public Task<Map<String, String>> authenticateAsync() {
2524
return Task.forResult(getAuthData());
2625
}
@@ -32,14 +31,14 @@ public Map<String, String> getAuthData() {
3231
}
3332

3433
@Override
35-
public Task<Void> deauthenticateAsync() {
34+
public Task<Void> deauthenticateInBackground() {
3635
// do nothing
3736
return Task.forResult(null);
3837
}
3938

4039
@Override
41-
public Task<Boolean> restoreAuthenticationAsync(Map<String, String> authData) {
42-
return Task.forResult(true);
40+
public Task<Void> restoreAuthenticationInBackground(Map<String, String> authData) {
41+
return Task.forResult(null);
4342
}
4443

4544
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ public static boolean isLinked(ParseUser user) {
6565
* @return A Task that will be resolved when logging in is completed.
6666
*/
6767
public static Task<ParseUser> logInInBackground() {
68-
final ParseAuthenticationProvider provider = getProvider();
68+
final AnonymousAuthenticationProvider provider = getProvider();
6969
return provider.authenticateAsync().onSuccessTask(new Continuation<Map<String, String>, Task<ParseUser>>() {
7070
@Override
7171
public Task<ParseUser> then(Task<Map<String, String>> task) throws Exception {
72-
return ParseUser.logInWithAsync(provider.getAuthType(), task.getResult());
72+
return ParseUser.logInWithInBackground(provider.getAuthType(), task.getResult());
7373
}
7474
});
7575
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,15 @@ public Task<Void> then(Task<ParseUser> task) throws Exception {
5656
});
5757
}
5858

59-
public Task<Boolean> restoreAuthenticationAsync(String authType, Map<String, String> authData) {
59+
public Task<Void> restoreAuthenticationAsync(String authType, Map<String, String> authData) {
6060
ParseAuthenticationProvider provider;
6161
synchronized (lock) {
6262
provider = authenticationProviders.get(authType);
6363
}
6464
if (provider == null) {
65-
return Task.forResult(true);
65+
return Task.forResult(null);
6666
}
67-
return provider.restoreAuthenticationAsync(authData);
67+
return provider.restoreAuthenticationInBackground(authData);
6868
}
6969

7070
public Task<Void> deauthenticateAsync(String authType) {
@@ -73,7 +73,7 @@ public Task<Void> deauthenticateAsync(String authType) {
7373
provider = authenticationProviders.get(authType);
7474
}
7575
if (provider != null) {
76-
return provider.deauthenticateAsync();
76+
return provider.deauthenticateInBackground();
7777
}
7878
return Task.forResult(null);
7979
}

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

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,41 +13,34 @@
1313
import bolts.Task;
1414

1515
/**
16-
* Provides a general interface for delegation of the authentication process.
16+
* Provides a general interface for delegation of third party authentication.
1717
*/
18-
/** package */ interface ParseAuthenticationProvider {
18+
public interface ParseAuthenticationProvider {
1919

2020
/**
21-
* Provides a unique name for the type of authentication the provider does.
22-
* For example, the FacebookAuthenticationProvider would return "facebook".
21+
* @return A unique name for the type of authentication the provider does.
22+
* <p />
23+
* For example, the {@code FacebookAuthenticationProvider} would return {@code "facebook"}.
2324
*/
2425
String getAuthType();
2526

2627
/**
27-
* Authenticates with the service.
28+
* Deauthenticates (logs out) the user associated with this provider.
2829
*
29-
* @return A {@code Task} that will be resolved when authentication is complete.
30+
* @return A {@link Task} that resolves when deauthentication completes.
3031
*/
31-
Task<Map<String, String>> authenticateAsync();
32+
Task<Void> deauthenticateInBackground();
3233

3334
/**
34-
* Deauthenticates (logs out) the user associated with this provider. This call may block.
35-
*
36-
* @return A {@link Task} that resolves when deauthentication is complete.
37-
*/
38-
Task<Void> deauthenticateAsync();
39-
40-
/**
41-
* Restores authentication that has been serialized, such as session keys,
42-
* etc.
35+
* Restores authentication that has been serialized, such as session keys, etc.
4336
*
4437
* @param authData
45-
* the auth data for the provider. This value may be null when
38+
* The auth data for the provider. This value may be {@code null} when
4639
* unlinking an account.
4740
*
4841
* @return A {@link Task} that resolves to {@code true} iff the {@code authData} was successfully
4942
* synchronized or {@code false} if user should no longer be associated because of bad
5043
* {@code authData}.
5144
*/
52-
Task<Boolean> restoreAuthenticationAsync(Map<String, String> authData);
45+
Task<Void> restoreAuthenticationInBackground(Map<String, String> authData);
5346
}

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

Lines changed: 143 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,80 +1078,33 @@ public ParseUser fetchIfNeeded() throws ParseException {
10781078

10791079
//region Third party authentication
10801080

1081-
/* package */ boolean isLinked(String authType) {
1082-
Map<String, Map<String, String>> authData = getAuthData();
1083-
return authData.containsKey(authType) && authData.get(authType) != null;
1084-
}
1085-
10861081
/**
1087-
* Ensures that all auth providers have auth data (e.g. access tokens, etc.) that matches this
1088-
* user.
1082+
* Registers a third party authentication provider.
1083+
* <p />
1084+
* <strong>Note: This shouldn't be called directly unless developing a third party authentication
1085+
* provider.</strong>
1086+
*
1087+
* @param provider The third party authentication provider to be registered.
1088+
*
1089+
* @see ParseAuthenticationProvider
10891090
*/
1090-
/* package */ Task<Void> synchronizeAllAuthDataAsync() {
1091-
Map<String, Map<String, String>> authData;
1092-
synchronized (mutex) {
1093-
if (!isCurrentUser()) {
1094-
return Task.forResult(null);
1095-
}
1096-
authData = getAuthData();
1097-
}
1098-
List<Task<Void>> tasks = new ArrayList<>(authData.size());
1099-
for (String authType : authData.keySet()) {
1100-
tasks.add(synchronizeAuthDataAsync(authType));
1101-
}
1102-
return Task.whenAll(tasks);
1103-
}
1104-
1105-
/* package */ Task<Void> synchronizeAuthDataAsync(String authType) {
1106-
Map<String, String> authData;
1107-
synchronized (mutex) {
1108-
if (!isCurrentUser()) {
1109-
return Task.forResult(null);
1110-
}
1111-
authData = getAuthData(authType);
1112-
}
1113-
return synchronizeAuthDataAsync(getAuthenticationManager(), authType, authData);
1114-
}
1115-
1116-
private Task<Void> synchronizeAuthDataAsync(
1117-
ParseAuthenticationManager manager, final String authType, Map<String, String> authData) {
1118-
return manager.restoreAuthenticationAsync(authType, authData).onSuccessTask(new Continuation<Boolean, Task<Void>>() {
1119-
@Override
1120-
public Task<Void> then(Task<Boolean> task) throws Exception {
1121-
boolean success = task.getResult();
1122-
if (!success) {
1123-
return unlinkFromAsync(authType);
1124-
}
1125-
return task.makeVoid();
1126-
}
1127-
});
1128-
}
1129-
1130-
/* package */ Task<Void> unlinkFromAsync(final String authType) {
1131-
synchronized (mutex) {
1132-
if (authType == null) {
1133-
return Task.forResult(null);
1134-
}
1135-
return Task.<Void> forResult(null).continueWithTask(new Continuation<Void, Task<Void>>() {
1136-
@Override
1137-
public Task<Void> then(Task<Void> task) throws Exception {
1138-
synchronized (mutex) {
1139-
if (getAuthData().containsKey(authType)) {
1140-
putAuthData(authType, null);
1141-
return saveInBackground();
1142-
}
1143-
return Task.forResult(null);
1144-
}
1145-
}
1146-
});
1147-
}
1148-
}
1149-
1150-
/* package */ static void registerAuthenticationProvider(ParseAuthenticationProvider provider) {
1091+
public static void registerAuthenticationProvider(ParseAuthenticationProvider provider) {
11511092
getAuthenticationManager().register(provider);
11521093
}
11531094

1154-
/* package */ static Task<ParseUser> logInWithAsync(
1095+
/**
1096+
* Logs in a user with third party authentication credentials.
1097+
* <p />
1098+
* <strong>Note: This shouldn't be called directly unless developing a third party authentication
1099+
* provider.</strong>
1100+
*
1101+
* @param authType The name of the third party authentication provider.
1102+
* @param authData The user credentials of the third party authentication provider.
1103+
* @return A {@code Task} is resolved when logging in completes.
1104+
*
1105+
* @see ParseAuthenticationProvider
1106+
*/
1107+
public static Task<ParseUser> logInWithInBackground(
11551108
final String authType, final Map<String, String> authData) {
11561109
if (authType == null) {
11571110
throw new IllegalArgumentException("Invalid authType: " + null);
@@ -1223,24 +1176,25 @@ public Task<ParseUser> then(Task<Void> task) throws Exception {
12231176
// Try to link the current user with third party user, unless a user is already linked
12241177
// to that third party user, then we'll just create a new user and link it with the
12251178
// third party user. New users will not be linked to the previous user's data.
1226-
return user.linkWithAsync(authType, authData).continueWithTask(new Continuation<Void, Task<ParseUser>>() {
1227-
@Override
1228-
public Task<ParseUser> then(Task<Void> task) throws Exception {
1229-
if (task.isFaulted()) {
1230-
Exception error = task.getError();
1231-
if (error instanceof ParseException
1232-
&& ((ParseException) error).getCode() == ParseException.ACCOUNT_ALREADY_LINKED) {
1233-
// An account that's linked to the given authData already exists, so log in
1234-
// instead of trying to claim.
1235-
return Task.<Void>forResult(null).continueWithTask(logInWithTask);
1179+
return user.linkWithInBackground(authType, authData)
1180+
.continueWithTask(new Continuation<Void, Task<ParseUser>>() {
1181+
@Override
1182+
public Task<ParseUser> then(Task<Void> task) throws Exception {
1183+
if (task.isFaulted()) {
1184+
Exception error = task.getError();
1185+
if (error instanceof ParseException
1186+
&& ((ParseException) error).getCode() == ParseException.ACCOUNT_ALREADY_LINKED) {
1187+
// An account that's linked to the given authData already exists, so log in
1188+
// instead of trying to claim.
1189+
return Task.<Void>forResult(null).continueWithTask(logInWithTask);
1190+
}
1191+
}
1192+
if (task.isCancelled()) {
1193+
return Task.cancelled();
1194+
}
1195+
return Task.forResult(user);
12361196
}
1237-
}
1238-
if (task.isCancelled()) {
1239-
return Task.cancelled();
1240-
}
1241-
return Task.forResult(user);
1242-
}
1243-
});
1197+
});
12441198
}
12451199
}
12461200
}
@@ -1250,6 +1204,65 @@ public Task<ParseUser> then(Task<Void> task) throws Exception {
12501204
});
12511205
}
12521206

1207+
/**
1208+
* Indicates whether this user is linked with a third party authentication provider.
1209+
* <p />
1210+
* <strong>Note: This shouldn't be called directly unless developing a third party authentication
1211+
* provider.</strong>
1212+
*
1213+
* @param authType The name of the third party authentication provider.
1214+
* @return {@code true} if linked, otherwise {@code false}.
1215+
*
1216+
* @see ParseAuthenticationProvider
1217+
*/
1218+
public boolean isLinked(String authType) {
1219+
Map<String, Map<String, String>> authData = getAuthData();
1220+
return authData.containsKey(authType) && authData.get(authType) != null;
1221+
}
1222+
1223+
/**
1224+
* Ensures that all auth providers have auth data (e.g. access tokens, etc.) that matches this
1225+
* user.
1226+
*/
1227+
/* package */ Task<Void> synchronizeAllAuthDataAsync() {
1228+
Map<String, Map<String, String>> authData;
1229+
synchronized (mutex) {
1230+
if (!isCurrentUser()) {
1231+
return Task.forResult(null);
1232+
}
1233+
authData = getAuthData();
1234+
}
1235+
List<Task<Void>> tasks = new ArrayList<>(authData.size());
1236+
for (String authType : authData.keySet()) {
1237+
tasks.add(synchronizeAuthDataAsync(authType));
1238+
}
1239+
return Task.whenAll(tasks);
1240+
}
1241+
1242+
/* package */ Task<Void> synchronizeAuthDataAsync(String authType) {
1243+
Map<String, String> authData;
1244+
synchronized (mutex) {
1245+
if (!isCurrentUser()) {
1246+
return Task.forResult(null);
1247+
}
1248+
authData = getAuthData(authType);
1249+
}
1250+
return synchronizeAuthDataAsync(getAuthenticationManager(), authType, authData);
1251+
}
1252+
1253+
private Task<Void> synchronizeAuthDataAsync(
1254+
ParseAuthenticationManager manager, final String authType, Map<String, String> authData) {
1255+
return manager.restoreAuthenticationAsync(authType, authData).continueWithTask(new Continuation<Void, Task<Void>>() {
1256+
@Override
1257+
public Task<Void> then(Task<Void> task) throws Exception {
1258+
if (task.isFaulted()) {
1259+
return unlinkFromInBackground(authType);
1260+
}
1261+
return task;
1262+
}
1263+
});
1264+
}
1265+
12531266
private Task<Void> linkWithAsync(
12541267
final String authType,
12551268
final Map<String, String> authData,
@@ -1289,14 +1302,57 @@ public Task<Void> then(Task<Void> task) throws Exception {
12891302
});
12901303
}
12911304

1292-
/* package */ Task<Void> linkWithAsync(
1305+
/**
1306+
* Links this user to a third party authentication provider.
1307+
* <p />
1308+
* <strong>Note: This shouldn't be called directly unless developing a third party authentication
1309+
* provider.</strong>
1310+
*
1311+
* @param authType The name of the third party authentication provider.
1312+
* @param authData The user credentials of the third party authentication provider.
1313+
* @return A {@code Task} is resolved when linking completes.
1314+
*
1315+
* @see ParseAuthenticationProvider
1316+
*/
1317+
public Task<Void> linkWithInBackground(
12931318
String authType, Map<String, String> authData) {
12941319
if (authType == null) {
12951320
throw new IllegalArgumentException("Invalid authType: " + null);
12961321
}
12971322
return linkWithAsync(authType, authData, getSessionToken());
12981323
}
12991324

1325+
/**
1326+
* Unlinks this user from a third party authentication provider.
1327+
* <p />
1328+
* <strong>Note: This shouldn't be called directly unless developing a third party authentication
1329+
* provider.</strong>
1330+
*
1331+
* @param authType The name of the third party authentication provider.
1332+
* @return A {@code Task} is resolved when unlinking completes.
1333+
*
1334+
* @see ParseAuthenticationProvider
1335+
*/
1336+
public Task<Void> unlinkFromInBackground(final String authType) {
1337+
synchronized (mutex) {
1338+
if (authType == null) {
1339+
return Task.forResult(null);
1340+
}
1341+
return Task.<Void> forResult(null).continueWithTask(new Continuation<Void, Task<Void>>() {
1342+
@Override
1343+
public Task<Void> then(Task<Void> task) throws Exception {
1344+
synchronized (mutex) {
1345+
if (getAuthData().containsKey(authType)) {
1346+
putAuthData(authType, null);
1347+
return saveInBackground();
1348+
}
1349+
return Task.forResult(null);
1350+
}
1351+
}
1352+
});
1353+
}
1354+
}
1355+
13001356
//endregion
13011357

13021358
/**

0 commit comments

Comments
 (0)