Android Open Source - dw2020 X Auth Provider






From Project

Back to project page dw2020.

License

The source code is released under:

Apache License

If you think the Android project dw2020 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.fivehundredpx.api.auth;
//  w ww  .j a  v  a2s .  co m
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;

import android.net.Uri;
import android.util.Base64;

import com.fivehundredpx.api.FiveHundredException;

public class XAuthProvider implements OAuthProvider {

  private static final String HMAC_SHA1 = "HmacSHA1";

  // from xauth
  private String username;
  private String password;

  // from oauth
  private String consumerKey;
  private String consumerSecret;
  private String tokenKey;
  private String tokenSecret;

  protected XAuthProvider() {

  }

  public XAuthProvider(String username, String password) {
    super();
    this.username = username;
    this.password = password;
  }

  public void signForAccessToken(HttpPost req) throws FiveHundredException {

    OAuthParameters params = new OAuthParameters(this.consumerKey);
    final ArrayList<NameValuePair> tuples = buildParameters(params);
    
    params.put(OAuthConstants.TOKEN, this.tokenKey);

    // generate the base string for signature.
    String str = getSignatureBaseString(req, params);
    str = getSignature(str, consumerSecret, this.tokenSecret);
    params.put(OAuthConstants.SIGNATURE, str);
    // get the authorization header
    str = params.getAuthorizationHeaderValue();
    req.addHeader(OAuthConstants.HEADER, str);

    try {
      req.setEntity(new UrlEncodedFormEntity(tuples));
    } catch (UnsupportedEncodingException e) {
      throw new FiveHundredException(e);
    }

  }

  protected ArrayList<NameValuePair> buildParameters(OAuthParameters params) {
    ArrayList<NameValuePair> tuples = new ArrayList<NameValuePair>();
    tuples.add(new BasicNameValuePair(OAuthConstants.MODE, "client_auth"));
    params.put(OAuthConstants.MODE, "client_auth");
    tuples.add(new BasicNameValuePair(OAuthConstants.USERNAME, username));
    params.put(OAuthConstants.USERNAME, username);
    tuples.add(new BasicNameValuePair(OAuthConstants.PASSWORD, password));
    params.put(OAuthConstants.PASSWORD, password);

    return tuples;
  }

  private String getSignatureBaseString(HttpPost req, OAuthParameters params) {
    final String method = req.getMethod();
    final String url = Uri.encode(req.getURI().toString());

    final String sortedParams = Uri.encode(params
        .getSortedEncodedParamsAsString());
    return method + '&' + url + '&' + sortedParams;
  }

  private String getSignature(String data, String consumerSecret,
      String tokenSecret) {
    byte[] byteHMAC = null;
    try {
      Mac mac = Mac.getInstance(HMAC_SHA1);
      String oauthSignature = HttpParameterUtil.encode(consumerSecret)
          + "&" + HttpParameterUtil.encode(tokenSecret);
      SecretKeySpec spec = new SecretKeySpec(oauthSignature.getBytes(),
          HMAC_SHA1);
      mac.init(spec);
      byteHMAC = mac.doFinal(data.getBytes());
    } catch (InvalidKeyException ike) {
      throw new AssertionError(ike);
    } catch (NoSuchAlgorithmException nsae) {
      throw new AssertionError(nsae);
    }
    return Base64.encodeToString(byteHMAC, Base64.NO_WRAP);
  }

  @Override
  public void setOAuthConsumer(String consumerKey, String consumerSecret) {
    this.consumerKey = consumerKey;
    this.consumerSecret = consumerSecret;
  }

  @Override
  public void setOAuthRequestToken(String requestTokenKey,
      String requestTokenSecret) {
    this.tokenKey = requestTokenKey;
    this.tokenSecret = requestTokenSecret;
  }
}




Java Source Code List

com.fivehundredpx.api.FiveHundredException.java
com.fivehundredpx.api.PxApi.java
com.fivehundredpx.api.auth.AccessToken.java
com.fivehundredpx.api.auth.FacebookAuthProvider.java
com.fivehundredpx.api.auth.HttpParameterUtil.java
com.fivehundredpx.api.auth.OAuthAuthorization.java
com.fivehundredpx.api.auth.OAuthConstants.java
com.fivehundredpx.api.auth.OAuthParameters.java
com.fivehundredpx.api.auth.OAuthProvider.java
com.fivehundredpx.api.auth.PlusAuthProvider.java
com.fivehundredpx.api.auth.TwitterAuthProvider.java
com.fivehundredpx.api.auth.XAuthProvider.java
com.fivehundredpx.api.tasks.UserDetailTask.java
com.fivehundredpx.api.tasks.XAuth500pxTask.java
net.darkwire.example.BaseActivity.java
net.darkwire.example.BaseSpiceActivity.java
net.darkwire.example.MainActivity.java
net.darkwire.example.ObservableApp.java
net.darkwire.example.builder.FiveHundredPxAccessToken.java
net.darkwire.example.exception.AuthenticationError.java
net.darkwire.example.model.FiveHundredPxConfiguration.java
net.darkwire.example.model.FiveHundredPxImageMetadata.java
net.darkwire.example.model.FiveHundredPxPhotoContainer.java
net.darkwire.example.model.FiveHundredPxPhoto.java
net.darkwire.example.service.CacheableSpiceRequest.java
net.darkwire.example.service.FiveHundredPxGsonSpiceService.java
net.darkwire.example.service.FiveHundredPxJacksonSpiceService.java
net.darkwire.example.service.FiveHundredPxRecentPhotosSpiceRequest.java
net.darkwire.example.service.FiveHundredPxSearchSpiceRequest.java
net.darkwire.example.service.FiveHundredPxSpiceRequest.java
net.darkwire.example.service.FiveHundredPx.java
net.darkwire.example.service.catalog.UrlCatalog.java
net.darkwire.example.service.client.FiveHundredPxClient.java
net.darkwire.example.widgets.adapter.NumericPaginationBaseAdapter.java
net.darkwire.example.widgets.adapter.NumericPagination.java
net.darkwire.example.widgets.adapter.PhotoAdapter.java
net.darkwire.example.widgets.listener.InfiniteScrollListener.java
net.darkwire.example.widgets.view.SquareImageView.java
se.akerfeldt.signpost.retrofit.HttpRequestAdapter.java
se.akerfeldt.signpost.retrofit.RetrofitHttpOAuthConsumer.java
se.akerfeldt.signpost.retrofit.SigningOkClient.java