Skip to content

Instantly share code, notes, and snippets.

@Muneefm
Created April 1, 2017 16:40
Show Gist options
  • Save Muneefm/f4c08b2aa3accd57fa890156f74e619a to your computer and use it in GitHub Desktop.
Save Muneefm/f4c08b2aa3accd57fa890156f74e619a to your computer and use it in GitHub Desktop.
Woocommerce API Http Authentication Code.
package appilder.coffye.com.woocommerceadmin.Tools;
import android.content.Context;
import android.util.Base64;
import android.util.Log;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import appilder.coffye.com.woocommerceadmin.BaseUrl;
import appilder.coffye.com.woocommerceadmin.Models.ParamsModel;
import appilder.coffye.com.woocommerceadmin.Models.SalesReport.TotalModel;
import appilder.coffye.com.woocommerceadmin.PreferensHandler;
/**
* Created by Muneef on 07/09/15.
*/
public class HttpAuthentication{
List<ParamsModel> paramsList;
BaseUrl b= new BaseUrl();
String baseUrl= b.baseUrlhttp.toString();
PreferensHandler pref;
public String AtuhenticateRequestGetUrl(Context c,List<ParamsModel> listMod,String urlSiffix,boolean method){
baseUrl = baseUrl+urlSiffix;
Log.e("baser"," base + siffix = "+baseUrl);
this.paramsList = listMod;
Log.e("baser"," before sorting = "+paramsList);
pref = new PreferensHandler(c);
String cs=pref.getConsSecret();//"cs_a20c564a17ab1503a77aec329b3615464275a905";//"cs_cb61b3962e701ba66124f8c9bfe50aad";//pref.getConsSecret();
String ck=pref.getConsKey();//"ck_ddfe5eedb9004bc82ef353edc12ba72ab2dc6861";//"ck_1ab64cb9fe50d2841e9430b60e12d9f4e9a478c4";//"ck_61729dbafbecb99cbd697779085dcbef";// pref.getConsKey();
String timeStamp = getTimeStamp();
paramsList.add(new ParamsModel("oauth_nonce", generateNonce()));
paramsList.add(new ParamsModel("oauth_signature_method", "HMAC-SHA1"));
paramsList.add(new ParamsModel("oauth_timestamp", timeStamp));
paramsList.add(new ParamsModel("oauth_consumer_key", ck));
paramsList = getSortedParams(paramsList);
String paramsUrl = getParamsUrl(paramsList);
String encodedUrlParams = getParamsUrlencoded(paramsList);
Log.e("baser","encoded url params is = "+encodedUrlParams);
String nonEnUrl = baseUrl+"?"+paramsUrl;
baseUrl = Strings.urlEncode(baseUrl);
String encodedParams = Strings.urlEncode(paramsUrl);
baseUrl = baseUrl+"&"+encodedUrlParams;
String encodedUrl = "GET&" + baseUrl;
if(!method) {
encodedUrl = "POST&" + baseUrl;
Log.e("baser"," method is post");
}
Log.e("baser","encoded url is = "+encodedUrl);
String signature="nothing";
try {
signature = hmacSha1(encodedUrl, cs);
// signature = Base64.encodeToString(signature.getBytes(), Base64.DEFAULT);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
Log.e("baser","before encoding signature = "+signature);
signature = Strings.urlEncode(signature);
Log.e("baser","after encoding signature = "+signature);
signature = signature.replaceAll("%0A","");
Log.e("baser","singature in auth is = "+signature);
nonEnUrl = nonEnUrl+"&oauth_signature="+signature;
return nonEnUrl;
}
//login url generate
public String getLoginUrl(Context c,String ck,String cs,String suffix){
baseUrl = baseUrl+suffix;
Log.e("tagLogin","uerl recived + suffix = "+baseUrl);
String tiStamp = getTimeStamp();
paramsList = new ArrayList<>();
paramsList.add(new ParamsModel("oauth_nonce", generateNonce()));
paramsList.add(new ParamsModel("oauth_signature_method", "HMAC-SHA1"));
paramsList.add(new ParamsModel("oauth_timestamp", tiStamp));
paramsList.add(new ParamsModel("oauth_consumer_key", ck));
paramsList = getSortedParams(paramsList);
for (ParamsModel mod : paramsList) {
Log.e("tagLogin"," sorted array is = "+mod.key);
}
Log.e("tagLogin","login url sorted list is = "+paramsList);
// String UrlParamsB = getParamsUrl(paramsList);
String UrlParamsB = getParamsUrlencoded(paramsList);
String UrlParamNonEncode = getParamsUrl(paramsList);
Log.e("tagLogin","login url params encoded = "+UrlParamsB);
// String signatureUrl = hmacSha1()
Log.e("tagLog","login url prams not encoded = "+UrlParamNonEncode);
String finalReqUrl = baseUrl+"?"+UrlParamNonEncode;
Log.e("tagLogin","final url is = "+finalReqUrl);
baseUrl = Strings.urlEncode(baseUrl);
//UrlParamsB = Strings.urlEncode(UrlParamsB);
baseUrl = baseUrl+"&"+UrlParamsB;
String encodedUrlLogin = "GET&"+baseUrl;
Log.e("tagLog","final encoded is = "+encodedUrlLogin);
String signatureUrl ="";
try {
signatureUrl = hmacSha1(encodedUrlLogin, cs);
// signature = Base64.encodeToString(signature.getBytes(), Base64.DEFAULT);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
signatureUrl = Strings.urlEncode(signatureUrl);
signatureUrl = signatureUrl.replaceAll("%0A", "");
finalReqUrl = finalReqUrl+"&oauth_signature="+signatureUrl;
return finalReqUrl;
}
//Tools for auth
public String getParamsUrlencoded(List<ParamsModel> params){
List<ParamsModel> tempParamsList= params;
String paramsUrl="";
Log.e("TAGp"," after sorting = "+params);
if(params.size()>0) {
// baseUrl = baseUrl+"?";
Log.e("baser"," base + ? = "+baseUrl);
for (int i = 0; i < params.size(); i++) {
if(i==0) {
paramsUrl = paramsUrl + Strings.urlEncode(Strings.urlEncode(params.get(i).key))+"%3D"+paramsList.get(i).value;
}else{
paramsUrl= paramsUrl+"%26"+Strings.urlEncode(Strings.urlEncode(params.get(i).key))+"%3D"+paramsList.get(i).value;
}
}
}
return paramsUrl;
}
public String getParamsUrl(List<ParamsModel> params){
List<ParamsModel> tempParamsList= params;
String paramsUrl="";
Log.e("TAGp"," after sorting = "+params);
if(params.size()>0) {
// baseUrl = baseUrl+"?";
Log.e("baser"," base + ? = "+baseUrl);
for (int i = 0; i < params.size(); i++) {
if(i==0) {
paramsUrl = paramsUrl + params.get(i).key+"="+paramsList.get(i).value;
}else{
paramsUrl= paramsUrl+"&"+params.get(i).key+"="+paramsList.get(i).value;
}
}
}
return paramsUrl;
}
public List<ParamsModel> getSortedParams(List<ParamsModel> params){
Collections.sort(params, new Comparator<ParamsModel>() {
@Override
public int compare(ParamsModel totalModel, ParamsModel t1) {
// Log.e("TAGS", "inside comapare: 1=" + totalModel.key + " 2=" + t1.key + " return val = " + totalModel.key.compareToIgnoreCase(t1.key));
//return totalModel.key.compareToIgnoreCase(t1.key);
/*if (totalModel.key.length() > t1.key.length()) {
return -1;
} else if (totalModel.key.length() < t1.key.length()) {
return 1;
} else {
return 0;
}*/
return totalModel.key.compareTo(t1.key);
//totalModel.key.com
}
});
return params;
}
public String getTimeStamp(){
long unixTime = System.currentTimeMillis() / 1000L;
return Long.toString(unixTime);
}
private static String hmacSha1(String value, String key)
throws UnsupportedEncodingException, NoSuchAlgorithmException,
InvalidKeyException {
//value = "te";
String type = "HmacSHA1";
SecretKeySpec secret = new SecretKeySpec(key.getBytes(), type);
Mac mac = Mac.getInstance(type);
mac.init(secret);
byte[] bytes = mac.doFinal(value.getBytes());
return Base64.encodeToString(bytes, Base64.DEFAULT);
//return bytesToHex(bytes);
}
private final static char[] hexArray = "0123456789abcdef".toCharArray();
private static String bytesToHex(byte[] bytes) {
char[] hexChars = new char[bytes.length * 2];
int v;
for (int j = 0; j < bytes.length; j++) {
v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
//nonce
public static String generateNonce() {
return new BigInteger(130, new SecureRandom()).toString(32);
//int n = 100000 + random_float() * 900000;
/* Random rnd = new Random();
int n = 100000 + rnd.nextInt(900000);
return Integer.toString(n);*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment