Android Open Source - Facebook-Contact-Sync Authenticator






From Project

Back to project page Facebook-Contact-Sync.

License

The source code is released under:

GNU General Public License

If you think the Android project Facebook-Contact-Sync listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2012 Danut Chereches//w w  w.j av a2s  .c  om
 *
 * Contact: Danut Chereches <admin@weednet.ro>
 *
 * This file is part of Facebook Contact Sync.
 * 
 * Facebook Contact Sync is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Facebook Contact Sync.
 * If not, see <http://www.gnu.org/licenses/>.
 *
 */
package ro.weednet.contactssync.authenticator;

import ro.weednet.contactssync.Constants;
import ro.weednet.contactssync.client.NetworkUtilities;
import android.accounts.AbstractAccountAuthenticator;
import android.accounts.Account;
import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.accounts.NetworkErrorException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

/**
 * This class is an implementation of AbstractAccountAuthenticator for
 * authenticating accounts in the com.example.android.sync domain. The
 * interesting thing that this class demonstrates is the use of authTokens as
 * part of the authentication process. In the account setup UI, the user enters
 * their username and password. But for our subsequent calls off to the service
 * for syncing, we want to use an authtoken instead - so we're not continually
 * sending the password over the wire. getAuthToken() will be called when
 * SyncAdapter calls AccountManager.blockingGetAuthToken(). When we get called,
 * we need to return the appropriate authToken for the specified account. If we
 * already have an authToken stored in the account, we return that authToken. If
 * we don't, but we do have a username and password, then we'll attempt to talk
 * to the  service to fetch an authToken. If that fails (or we didn't have
 * a username/password), then we need to prompt the user - so we create an
 * AuthenticatorActivity intent and return that. That will display the dialog
 * that prompts the user for their login information.
 */
public class Authenticator extends AbstractAccountAuthenticator {
  public static final String[] REQUIRED_PERMISSIONS = {"email", "friends_photos"};
  private static final String TAG = "Authenticator";
  private final Context mContext;
  
  public Authenticator(Context context) {
    super(context);
    mContext = context;
  }
  
  @Override
  public Bundle addAccount(AccountAuthenticatorResponse response, String accountType, String authTokenType, String[] requiredFeatures, Bundle options) {
    Log.v(TAG, "addAccount()");
    final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
    intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
    final Bundle bundle = new Bundle();
    bundle.putParcelable(AccountManager.KEY_INTENT, intent);
    return bundle;
  }
  
  @Override
  public Bundle confirmCredentials(AccountAuthenticatorResponse response, Account account, Bundle options) {
    return null;
  }
  
  @Override
  public Bundle editProperties(AccountAuthenticatorResponse response, String accountType) {
    throw new UnsupportedOperationException();
  }
  
  @Override
  public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account, String authTokenType, Bundle loginOptions) throws NetworkErrorException {
    Log.v(TAG, "getAuthToken()");
    
    // If the caller requested an authToken type we don't support, then
    // return an error
    if (!authTokenType.equals(Constants.AUTHTOKEN_TYPE)) {
      final Bundle result = new Bundle();
      result.putString(AccountManager.KEY_ERROR_MESSAGE, "invalid authTokenType");
      return result;
    }
    
    // Extract the username and password from the Account Manager, and ask
    // the server for an appropriate AuthToken.
    final AccountManager am = AccountManager.get(mContext);
    final String authToken = am.getPassword(account);
    if (authToken != null) {
      NetworkUtilities nu = new NetworkUtilities(authToken, mContext);
      if (nu.checkAccessToken()) {
        Log.v(TAG, "getAuthToken() GOOD");
        final Bundle result = new Bundle();
        result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
        result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
        result.putString(AccountManager.KEY_AUTHTOKEN, authToken);
        return result;
      } else {
        Log.v(TAG, "getAuthToken() BAD");
      }
    }
    
    // If we get here, then we couldn't access the user's password - so we
    // need to re-prompt them for their credentials. We do that by creating
    // an intent to display our AuthenticatorActivity panel.
    final Intent intent = new Intent(mContext, AuthenticatorActivity.class);
    intent.putExtra(AuthenticatorActivity.PARAM_USERNAME, account.name);
    intent.putExtra(AuthenticatorActivity.PARAM_AUTHTOKEN_TYPE, authTokenType);
    intent.putExtra(AccountManager.KEY_ACCOUNT_AUTHENTICATOR_RESPONSE, response);
    final Bundle bundle = new Bundle();
    bundle.putParcelable(AccountManager.KEY_INTENT, intent);
    return bundle;
  }
  
  @Override
  public String getAuthTokenLabel(String authTokenType) {
    // null means we don't support multiple authToken types
    Log.v(TAG, "getAuthTokenLabel()");
    return null;
  }
  
  @Override
  public Bundle hasFeatures(AccountAuthenticatorResponse response,
      Account account, String[] features) {
    // This call is used to query whether the Authenticator supports
    // specific features. We don't expect to get called, so we always
    // return false (no) for any queries.
    Log.v(TAG, "hasFeatures()");
    final Bundle result = new Bundle();
    result.putBoolean(AccountManager.KEY_BOOLEAN_RESULT, false);
    return result;
  }
  
  @Override
  public Bundle updateCredentials(AccountAuthenticatorResponse response,
      Account account, String authTokenType, Bundle loginOptions) {
    Log.v(TAG, "updateCredentials()");
    return null;
  }
}




Java Source Code List

ro.weednet.ContactsSync.java
ro.weednet.contactssync.Constants.java
ro.weednet.contactssync.activities.Preferences.java
ro.weednet.contactssync.activities.Profile.java
ro.weednet.contactssync.activities.TestFacebookApi.java
ro.weednet.contactssync.authenticator.AuthenticationService.java
ro.weednet.contactssync.authenticator.AuthenticatorActivity.java
ro.weednet.contactssync.authenticator.Authenticator.java
ro.weednet.contactssync.client.ContactPhoto.java
ro.weednet.contactssync.client.ContactStreamItem.java
ro.weednet.contactssync.client.NetworkUtilities.java
ro.weednet.contactssync.client.RawContact.java
ro.weednet.contactssync.iap.Base64DecoderException.java
ro.weednet.contactssync.iap.Base64.java
ro.weednet.contactssync.iap.IabException.java
ro.weednet.contactssync.iap.IabHelper.java
ro.weednet.contactssync.iap.IabResult.java
ro.weednet.contactssync.iap.Inventory.java
ro.weednet.contactssync.iap.Purchase.java
ro.weednet.contactssync.iap.Security.java
ro.weednet.contactssync.iap.SkuDetails.java
ro.weednet.contactssync.notifier.NotifierService.java
ro.weednet.contactssync.platform.BatchOperation.java
ro.weednet.contactssync.platform.ContactManager.java
ro.weednet.contactssync.platform.ContactOperations.java
ro.weednet.contactssync.platform.SyncAdapterColumns.java
ro.weednet.contactssync.preferences.GlobalFragment.java
ro.weednet.contactssync.syncadapter.SyncAdapter.java
ro.weednet.contactssync.syncadapter.SyncService.java