@@ -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