Android Open Source - Mamytas Authorization Manager






From Project

Back to project page Mamytas.

License

The source code is released under:

GNU General Public License

If you think the Android project Mamytas 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

package mn.aug.restfulandroid.security;
// w  w  w.  j  ava  2  s.c  om
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import java.util.ArrayList;
import java.util.List;
import mn.aug.restfulandroid.rest.Request;


/**
 * OAuthManager handles OAuth authentication with the Twitter API.
 * 
 * @author jeremy
 * 
 */
public class AuthorizationManager implements RequestSigner {


  // Singleton instance of the OAuthManager
  private static AuthorizationManager mInstance;

  // Preferences in which to store the request and access tokens
  private final SharedPreferences prefs;

    public static final String  WUNDERLIST_TOKEN="WUNDERLIST_TOKEN";
    public static final String  WUNDERLIST_NAME="WUNDERLIST_NAME";

  private String mToken;
    private String mUser;


  /**
   * Returns the singleton instance of the OAuthManager
   * 
   * @return singleton instance of the OAuthManager
   */
  public static AuthorizationManager getInstance(Context context) {
    if (mInstance == null) {
      mInstance = new AuthorizationManager(context);
    }
    return mInstance;
  }

  /**
   * Private constructor for the OAuthManager. Initializes the persistent
   * storate and OAuthService
   */
  private AuthorizationManager(Context context) {

    prefs = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());

  }


  /**
   * Persists the token. Pass in <code>null</code> to clear the saved
   * token.
   * 
   * @param token
   *            the token to persist, or <code>null</code> to clear it
   * @return <code>true</code> if the save was successful
   */
  public boolean saveToken(String token, String user) {

    SharedPreferences.Editor editor = prefs.edit();

    if (token == null) {
      editor.remove(WUNDERLIST_TOKEN);
            editor.remove(WUNDERLIST_NAME);
    } else {
      editor.putString(WUNDERLIST_TOKEN, token);
            editor.putString(WUNDERLIST_NAME, user);
    }

    return editor.commit();
  }




  /**
   * Retrieves  the request token for authorizing the app
   */
  private void retrieveToken() {

        //Appel au service pour rcuprer le token

        String token = prefs.getString(WUNDERLIST_TOKEN,null);
        this.mToken=token;
  }

    /**
     * Retrieves the name of the usere
     */
    private void retrieveName() {

        //Appel au service pour rcuprer le token

        String name = prefs.getString(WUNDERLIST_NAME,null);
        this.mUser=name;
    }

  /**
   * Returns the token (may be null)
   *
   * @return saved  token (or null if it does not exist)
   */
    public String getToken() {
        retrieveToken();
        return mToken;
  }

    /**
     * Returns the user (may be null)
     *
     * @return saved user (or null if it does not exist)
     */
    public String getUser() {
        retrieveName();
        return mUser;
    }

  /**
   * Determines whether a user is currently logged in
   * 
   * @return <code>true</code> if user is logged in, <code>false</code>
   *         otherwise
   */
  public boolean loggedIn() {
    return getToken() != null;
  }

  /**
   * Log out of the application
   */
  public void logout() {
    mToken = null;
        mUser=null;
    saveToken(mToken,mUser);
  }

  /**
   * Authorizes aWunderlist request.
   *
   * Authorizing a Request</a> for authorization requirements and methods.
   */
  @Override
  public void authorize(Request request) {

        retrieveToken();


        List<String> values = new ArrayList<String>();
        values.add("Bearer " + mToken);

    request.addHeader("Authorization", values);

  }


}




Java Source Code List

mn.aug.restfulandroid.activity.AboutActivity.java
mn.aug.restfulandroid.activity.LoginActivity.java
mn.aug.restfulandroid.activity.ProjectEditor.java
mn.aug.restfulandroid.activity.ProjectsActivity.java
mn.aug.restfulandroid.activity.ProjectsArrayAdapter.java
mn.aug.restfulandroid.activity.TaskActivity.java
mn.aug.restfulandroid.activity.TaskEditor.java
mn.aug.restfulandroid.activity.TasksActivity.java
mn.aug.restfulandroid.activity.TasksArrayAdapter.java
mn.aug.restfulandroid.activity.TimerServiceHelper.java
mn.aug.restfulandroid.activity.TimerService.java
mn.aug.restfulandroid.activity.TimersArrayAdapter.java
mn.aug.restfulandroid.activity.base.RESTfulActivity.java
mn.aug.restfulandroid.activity.base.RESTfulListActivity.java
mn.aug.restfulandroid.activity.base.UndoBarController.java
mn.aug.restfulandroid.provider.CommentsDBAccess.java
mn.aug.restfulandroid.provider.ListsDBAccess.java
mn.aug.restfulandroid.provider.OwnershipDBAccess.java
mn.aug.restfulandroid.provider.ProviderDbHelper.java
mn.aug.restfulandroid.provider.RemindersDBAccess.java
mn.aug.restfulandroid.provider.TasksDBAccess.java
mn.aug.restfulandroid.provider.UsersDBAccess.java
mn.aug.restfulandroid.rest.AbstractRestMethod.java
mn.aug.restfulandroid.rest.DeleteListRestMethod.java
mn.aug.restfulandroid.rest.DeleteTaskRestMethod.java
mn.aug.restfulandroid.rest.GetListsRestMethod.java
mn.aug.restfulandroid.rest.GetTasksRestMethod.java
mn.aug.restfulandroid.rest.GetTimersRestMethod.java
mn.aug.restfulandroid.rest.LoginRestMethod.java
mn.aug.restfulandroid.rest.PostListRestMethod.java
mn.aug.restfulandroid.rest.PostTaskRestMethod.java
mn.aug.restfulandroid.rest.PostTimerRestMethod.java
mn.aug.restfulandroid.rest.PutListRestMethod.java
mn.aug.restfulandroid.rest.PutTaskRestMethod.java
mn.aug.restfulandroid.rest.PutTimerRestMethod.java
mn.aug.restfulandroid.rest.Request.java
mn.aug.restfulandroid.rest.Response.java
mn.aug.restfulandroid.rest.RestClient.java
mn.aug.restfulandroid.rest.RestMethodFactory.java
mn.aug.restfulandroid.rest.RestMethodResult.java
mn.aug.restfulandroid.rest.RestMethod.java
mn.aug.restfulandroid.rest.ShareListRestMethod.java
mn.aug.restfulandroid.rest.resource.Comment.java
mn.aug.restfulandroid.rest.resource.Lists.java
mn.aug.restfulandroid.rest.resource.Listw.java
mn.aug.restfulandroid.rest.resource.Login.java
mn.aug.restfulandroid.rest.resource.Reminder.java
mn.aug.restfulandroid.rest.resource.Resource.java
mn.aug.restfulandroid.rest.resource.TaskList.java
mn.aug.restfulandroid.rest.resource.Task.java
mn.aug.restfulandroid.rest.resource.Tasks.java
mn.aug.restfulandroid.rest.resource.Timer.java
mn.aug.restfulandroid.rest.resource.Timers.java
mn.aug.restfulandroid.security.AuthorizationManager.java
mn.aug.restfulandroid.security.RequestSigner.java
mn.aug.restfulandroid.service.ListProcessor.java
mn.aug.restfulandroid.service.ListsProcessor.java
mn.aug.restfulandroid.service.LoginProcessor.java
mn.aug.restfulandroid.service.ProcessorCallback.java
mn.aug.restfulandroid.service.ShareProcessor.java
mn.aug.restfulandroid.service.TaskProcessor.java
mn.aug.restfulandroid.service.TasksProcessor.java
mn.aug.restfulandroid.service.TimersProcessor.java
mn.aug.restfulandroid.service.WunderlistServiceHelper.java
mn.aug.restfulandroid.service.WunderlistService.java
mn.aug.restfulandroid.util.DateHelper.java
mn.aug.restfulandroid.util.DatePickerFragment.java
mn.aug.restfulandroid.util.Logger.java
mn.aug.restfulandroid.util.TimePickerFragment.java