Android Open Source - android-rest-client Token Authentication Provider






From Project

Back to project page android-rest-client.

License

The source code is released under:

Apache License

If you think the Android project android-rest-client 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 com.dg.libs.rest.authentication;
//from w  ww.ja  va2  s.  co  m
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import com.dg.libs.rest.requests.RestClientRequest;

public class TokenAuthenticationProvider implements AuthenticationProvider {

  private static final String TOKEN_KEY = "api_key";

  private static TokenAuthenticationProvider account;
  private final Context context;
  private String token;

  /**
   * <b> this object will be using a Reference to the application context via
   * getApplicationContext() NOT the Activity context</b> Recomended to be
   * initialized at the application startup or by initializing in your own class
   * extending application
   * <p>
   * <b> Dont forget to set the password on first init </b>
   *
   * @return
   */
  public static synchronized TokenAuthenticationProvider getInstance() {
    if (account == null) {
      throw new RuntimeException("Initialize the Provider first");
    }
    return account;
  }

  public static synchronized void init(Context context) {
    if (account == null) {
      account = new TokenAuthenticationProvider(context);
    }
  }

  private TokenAuthenticationProvider(final Context context) {
    this.context = context.getApplicationContext();
    initializeToken();
  }

  @Override
  public void authenticateRequest(RestClientRequest client) {
    if (TextUtils.isEmpty(token)) {
      return;
    }
    client.addHeader("Authorization", "OAuth " + token);
  }

  public String getToken() {
    return token;
  }

  public void setToken(String token) {
    this.token = token;
    saveApiKey(token);
  }

  public boolean isTokenValid() {
    return !TextUtils.isEmpty(token);
  }

  public boolean clearAuth() {
    Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
    editor.remove(TOKEN_KEY);
    boolean commit = editor.commit();
    return commit;
  }

  /**
   * Use as an alternative for saving the token to accounts (Note that using the
   * account manager is a preferred and safer method)
   * 
   * @param apiKey
   *          the token aqured from chute auth
   * @return if the save was successful
   */
  private boolean saveApiKey(final String apiKey) {
    Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
    editor.putString(TOKEN_KEY, apiKey);
    boolean commit = editor.commit();
    return commit;
  }

  private String restoreApiKey() {
    SharedPreferences savedSession = PreferenceManager
        .getDefaultSharedPreferences(context);
    return savedSession.getString(TOKEN_KEY, "");
  }

  private void initializeToken() {
    String apiKey = restoreApiKey();
    if (!TextUtils.isEmpty(apiKey)) {
      this.setToken(apiKey);
      return;
    }
    // Set a manual token for testing
    // this.setPassword("46e580a90085912ed11c565084f1f2465f28630bd58fa80cc98432f3078fc5ac");
  }
}




Java Source Code List

com.dg.examples.restclientdemo.MainActivity.java
com.dg.examples.restclientdemo.app.App.java
com.dg.examples.restclientdemo.communication.RestConstants.java
com.dg.examples.restclientdemo.communication.parsers.BlogsGoogleParser.java
com.dg.examples.restclientdemo.communication.requests.BlogsGoogleRequest.java
com.dg.examples.restclientdemo.communication.requests.CustomHandlersRequest.java
com.dg.examples.restclientdemo.communication.requests.PatchRequest.java
com.dg.examples.restclientdemo.domain.EntriesModel.java
com.dg.examples.restclientdemo.domain.ResponseDataModel.java
com.dg.examples.restclientdemo.domain.ResponseModel.java
com.dg.libs.rest.HttpRequest.java
com.dg.libs.rest.RestClientConfiguration.java
com.dg.libs.rest.authentication.AuthenticationProvider.java
com.dg.libs.rest.authentication.TokenAuthenticationProvider.java
com.dg.libs.rest.callbacks.HttpCallback.java
com.dg.libs.rest.client.RequestMethod.java
com.dg.libs.rest.domain.ResponseStatus.java
com.dg.libs.rest.entities.CountingInputStreamEntity.java
com.dg.libs.rest.entities.UnicodeBOMInputStream.java
com.dg.libs.rest.exceptions.HttpException.java
com.dg.libs.rest.exceptions.ParseException.java
com.dg.libs.rest.handlers.BackgroundThreadResponseHandler.java
com.dg.libs.rest.handlers.DefaultResponseStatusHandler.java
com.dg.libs.rest.handlers.ResponseHandler.java
com.dg.libs.rest.handlers.ResponseStatusHandler.java
com.dg.libs.rest.handlers.UIThreadResponseHandler.java
com.dg.libs.rest.parsers.BaseJacksonMapperResponseParser.java
com.dg.libs.rest.parsers.FileHttpResponseParser.java
com.dg.libs.rest.parsers.HttpResponseParser.java
com.dg.libs.rest.parsers.NoResponseParser.java
com.dg.libs.rest.parsers.StringHttpResponseParser.java
com.dg.libs.rest.requests.RequestBodyUtils.java
com.dg.libs.rest.requests.RestClientRequest.java