Modified AuthIntercepter to correctly authenticate requests with parameters that are not oauth

This commit is contained in:
Gilbert Kimutai 2019-02-19 04:59:29 +03:00
parent 939afd1d9a
commit 31002b6169
9 changed files with 91 additions and 24 deletions

View File

@ -1,18 +1,8 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<AndroidXmlCodeStyleSettings>
<option name="USE_CUSTOM_SETTINGS" value="true" />
</AndroidXmlCodeStyleSettings>
<JetCodeStyleSettings>
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</JetCodeStyleSettings>
<codeStyleSettings language="XML">
<indentOptions>
<option name="INDENT_SIZE" value="2" />
<option name="CONTINUATION_INDENT_SIZE" value="4" />
<option name="USE_TAB_CHARACTER" value="true" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="kotlin">
<option name="CODE_STYLE_DEFAULTS" value="KOTLIN_OFFICIAL" />
</codeStyleSettings>

5
.idea/misc.xml generated
View File

@ -1,5 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeSettings">
<configurations>
<configuration PROFILE_NAME="Debug" CONFIG_NAME="Debug" />
</configurations>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>

2
.idea/vcs.xml generated
View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -12,7 +12,7 @@ open class BaseActivity : AppCompatActivity() {
val woocommerce = Woocommerce.Builder()
.setSiteUrl("http://157.230.131.179")
.setApiVersion(Woocommerce.API_V2)
.setApiVersion(Woocommerce.API_V3)
.setConsumerKey("ck_26c61abd7eeff238d87dc56585bf26cb2d1a1ec3")
.setConsumerSecret("cs_062e8e3a7ae0ce08fdebc0c39f8f834d5e87598e")
.build()

View File

@ -2,7 +2,9 @@ package me.gilo.wc.ui
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_shop.*
import kotlinx.android.synthetic.main.content_coupon.*
import kotlinx.android.synthetic.main.content_shop.*
import me.gilo.wc.R
import me.gilo.wc.adapter.ProductAdapter
@ -12,6 +14,7 @@ import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.util.*
import kotlin.collections.HashMap
class ShopActivity : BaseActivity() {
@ -42,19 +45,27 @@ class ShopActivity : BaseActivity() {
private fun products() {
val woocommerce = Woocommerce.Builder()
.setSiteUrl("http://157.230.131.179")
.setApiVersion(Woocommerce.API_V2)
.setApiVersion(Woocommerce.API_V3)
.setConsumerKey("ck_26c61abd7eeff238d87dc56585bf26cb2d1a1ec3")
.setConsumerSecret("cs_062e8e3a7ae0ce08fdebc0c39f8f834d5e87598e")
.build()
woocommerce.ProductRepository().products().enqueue(object : Callback<List<Product>> {
override fun onResponse(call: Call<List<Product>>, response: Response<List<Product>>) {
val productsResponse = response.body()
for (product in productsResponse!!) {
products.add(product)
}
val filters = HashMap<String, String>()
filters["search"] = "ship"
adapter.notifyDataSetChanged()
woocommerce.ProductRepository().filter(filters).enqueue(object : Callback<List<Product>> {
override fun onResponse(call: Call<List<Product>>, response: Response<List<Product>>) {
if (response.isSuccessful) {
val productsResponse = response.body()
for (product in productsResponse!!) {
products.add(product)
}
adapter.notifyDataSetChanged()
}else{
Toast.makeText(baseContext, "" + response.code() + " : " + response.message(), Toast.LENGTH_SHORT).show()
}
}
override fun onFailure(call: Call<List<Product>>, t: Throwable) {

View File

@ -12,6 +12,7 @@ public class Woocommerce {
public static final ApiVersion API_V1 = ApiVersion.API_VERSION1;
public static final ApiVersion API_V2 = ApiVersion.API_VERSION2;
public static final ApiVersion API_V3 = ApiVersion.API_VERSION3;
final OrderNoteRepository orderNoteRepository;
final RefundRepository refundRepository;

View File

@ -1,6 +1,7 @@
package me.gilo.woodroid.data.auth;
import android.util.Base64;
import android.util.Log;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
@ -13,11 +14,11 @@ import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.*;
public class AuthIntercepter implements Interceptor {
@ -58,6 +59,31 @@ public class AuthIntercepter implements Interceptor {
public ArrayList<NameValuePair> getOauthParams(Chain chain) {
ArrayList<NameValuePair> params = new ArrayList<>();
String request_url = chain.request().url().toString();
Iterator iterator = getQueryParams(request_url).entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry pair = (Map.Entry)iterator.next();
String key = (String) pair.getKey();
List<String> values = (List<String>) pair.getValue();
String value = "";
//why there would be multiple values for single key is not so clear to me, will keep this here though
if (values.size() == 1){
value = values.get(0);
}
params.add(new BasicNameValuePair(key, value));
iterator.remove();
}
if (request_url.contains("?")){
int request_url_end = request_url.indexOf("?");
request_url = request_url.substring(0, request_url_end);
}
oauth_nonce = getOauth_nonce();
oauth_timestamp = getOauth_timestamp();
@ -68,7 +94,7 @@ public class AuthIntercepter implements Interceptor {
Collections.sort(params, new AlphabeticSorter());
String encodedParams = URLEncodedUtils.format(params, "utf-8");
oauth_signature = getOauth_signature(chain.request().method(), chain.request().url().toString(), consumerSecret, encodedParams );
oauth_signature = getOauth_signature(chain.request().method(), request_url, consumerSecret, encodedParams );
params.add(new BasicNameValuePair("oauth_signature", oauth_signature));
@ -115,4 +141,33 @@ public class AuthIntercepter implements Interceptor {
long stamp = (long) (System.currentTimeMillis() / 1000D);
return (new StringBuilder(String.valueOf(stamp))).toString();
}
public static Map<String, List<String>> getQueryParams(String url) {
try {
Map<String, List<String>> params = new HashMap<String, List<String>>();
String[] urlParts = url.split("\\?");
if (urlParts.length > 1) {
String query = urlParts[1];
for (String param : query.split("&")) {
String[] pair = param.split("=");
String key = URLDecoder.decode(pair[0], "UTF-8");
String value = "";
if (pair.length > 1) {
value = URLDecoder.decode(pair[1], "UTF-8");
}
List<String> values = params.get(key);
if (values == null) {
values = new ArrayList<String>();
params.put(key, values);
}
values.add(value);
}
}
return params;
} catch (UnsupportedEncodingException ex) {
throw new AssertionError(ex);
}
}
}

View File

@ -6,6 +6,7 @@ import retrofit2.Call;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ProductRepository extends WooRepository {
@ -29,6 +30,10 @@ public class ProductRepository extends WooRepository {
return apiService.list();
}
public Call<List<Product>> filter(Map<String, String> filters) {
return apiService.filter(filters);
}
public Call<Product> update(int id, Product product) {
return apiService.update(id, product);
}

View File

@ -10,7 +10,7 @@ import java.util.concurrent.TimeUnit;
public class WooRepository {
//TODO Apply DI or single instance on this
//TODO ('Apply DI or single instance on this')
public Retrofit retrofit;
public WooRepository(String baseUrl, String consumerKey, String consumerSecret) {