All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.parse.ParseAuthenticationManager Maven / Gradle / Ivy

Go to download

A library that gives you access to the powerful Parse cloud platform from your Android app.

There is a newer version: 1.17.3
Show newest version
/*
 * 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.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 callbacks = new HashMap<>();
  private final ParseCurrentUserController controller;

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

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

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

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

    // Synchronize the current user with the auth callback.
    controller.getAsync(false).onSuccessTask(new Continuation>() {
      @Override
      public Task then(Task task) throws Exception {
        ParseUser user = task.getResult();
        if (user != null) {
          return user.synchronizeAuthDataAsync(authType);
        }
        return null;
      }
    });
  }

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

  public Task deauthenticateAsync(String authType) {
    final AuthenticationCallback callback;
    synchronized (lock) {
      callback = this.callbacks.get(authType);
    }
    if (callback != null) {
      return Task.call(new Callable() {
        @Override
        public Void call() throws Exception {
          callback.onRestore(null);
          return null;
        }
      }, ParseExecutors.io());
    }
    return Task.forResult(null);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy