Skip to content

Commit

Permalink
trust all ssl certs setting added
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Senchuk committed May 14, 2014
1 parent adb5eeb commit 4402fb2
Show file tree
Hide file tree
Showing 15 changed files with 152 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@

<activity android:name=".activity.AccountsManagementActivity" android:label="@string/account_management_activity_title" />
<activity android:name=".activity.AccountViewActivity" android:label="@string/account_view_activity_title" />
<activity android:name=".activity.SettingsActivity" android:label="@string/settings_activity_title" />
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,13 @@ public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMen
@Override
public boolean onContextItemSelected(android.view.MenuItem item) {
boolean result = true;
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
switch (item.getItemId()) {
case R.id.accounts_management_remove:
removePool((int)info.id);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
if(info != null) {
removePool((int)info.id);
}
break;
default:
result = super.onContextItemSelected(item);
break;
Expand All @@ -147,6 +150,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
IntentIntegrator integrator = new IntentIntegrator(this);
integrator.initiateScan();
break;
case R.id.accounts_management_settings:
startActivity(new Intent(this, SettingsActivity.class));
break;
default:
result = super.onOptionsItemSelected(item);
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package net.taviscaron.mposviewer.activity;

import android.os.Bundle;
import android.preference.PreferenceActivity;
import net.taviscaron.mposviewer.R;

/**
* Application preferences activity
* @author Andrei Senchuk
*/
public class SettingsActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
*/
public class Constants {
public static final String CURRENT_ACCOUNT_ID_PREF_KEY = "currentAccountId";
public static final String TRUST_ALL_SSL_CERTS_PREF_KEY = "trustAllSSLCerts";
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.os.Bundle;
import android.support.v4.app.Fragment;
import net.taviscaron.mposviewer.model.Account;
import net.taviscaron.mposviewer.rpc.RPCFactory;
import net.taviscaron.mposviewer.rpc.result.GetPublicResult;
import net.taviscaron.mposviewer.rpc.result.GetUserStatusResult;
import net.taviscaron.mposviewer.rpc.RPC;
Expand Down Expand Up @@ -71,7 +72,7 @@ protected Result doInBackground(Account... params) {
}

// init RPC; token and url is required
RPC rpc = new RPC();
RPC rpc = RPCFactory.createRPC(getActivity());
rpc.setUrl(account.getUrl());
rpc.setToken(account.getToken());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.taviscaron.mposviewer.R;
import net.taviscaron.mposviewer.model.Account;
import net.taviscaron.mposviewer.rpc.RPC;
import net.taviscaron.mposviewer.rpc.RPCFactory;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -167,7 +168,7 @@ protected void onPreExecute() {

@Override
protected Map<RPC.Method, RPC.RPCResult> doInBackground(RPC.Method... methods) {
RPC rpc = new RPC();
RPC rpc = RPCFactory.createRPC(getActivity());
rpc.setUrl(finalAccount.getUrl());
rpc.setToken(finalAccount.getToken());

Expand Down
9 changes: 3 additions & 6 deletions src/main/java/net/taviscaron/mposviewer/rpc/RPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public class RPC {

private static final String METHOD_ATTR_NAME = "action";
private static final String TOKEN_ATTR_NAME = "api_key";
private static final int DEFAULT_TIMEOUT = 30 * 1000;

/** supported RPC methods */
public enum Method {
Expand Down Expand Up @@ -95,13 +94,11 @@ public RPCResult(Object result, Error error) {

private String token;
private String url;
private final HttpClient httpClient = new DefaultHttpClient();
private final HttpClient httpClient;
private final Gson gson = new GsonBuilder().create();

public RPC() {
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, DEFAULT_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, DEFAULT_TIMEOUT);
public RPC(HttpClient httpClient) {
this.httpClient = httpClient;
}

public String getToken() {
Expand Down
97 changes: 97 additions & 0 deletions src/main/java/net/taviscaron/mposviewer/rpc/RPCFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package net.taviscaron.mposviewer.rpc;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
import net.taviscaron.mposviewer.core.Constants;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.net.Socket;
import java.security.KeyStore;
import java.security.cert.X509Certificate;

/**
* Creates RPC instances
* @author Andrei Senchuk
*/
public class RPCFactory {
private static final String TAG = "RPCFactory";
private static final int DEFAULT_TIMEOUT = 30 * 1000;

public static RPC createRPC(Context context) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
boolean trustAllSSLCerts = sp.getBoolean(Constants.TRUST_ALL_SSL_CERTS_PREF_KEY, false);
HttpClient client = (trustAllSSLCerts) ? createTrustAllSSLCertsHttpClient() : createDefaultHttpClient();
return new RPC(client);
}

private static HttpParams createHttpParams() {
HttpParams params = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(params, DEFAULT_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, DEFAULT_TIMEOUT);
return params;
}

private static HttpClient createDefaultHttpClient() {
return new DefaultHttpClient(createHttpParams());
}

private static HttpClient createTrustAllSSLCertsHttpClient() {
try {
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return null;
}

public void checkClientTrusted(X509Certificate[] certs, String authType) {
// noop
}

public void checkServerTrusted(X509Certificate[] certs, String authType) {
// noop
}
}}, null);

KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);

SSLSocketFactory sf = new SSLSocketFactory(trustStore) {
@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException {
return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
}

@Override
public Socket createSocket() throws IOException {
return sslContext.getSocketFactory().createSocket();
}
};

SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", sf, 443));

HttpParams params = createHttpParams();

return new DefaultHttpClient(new ThreadSafeClientConnManager(params, registry), params);
} catch (Exception e) {
Log.w(TAG, "Can't create trust all ssl certs http client. Create default instead.", e);
return createDefaultHttpClient();
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/main/res/menu/accounts_management.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@
android:icon="@drawable/ic_action_new"
android:title="@string/accounts_management_action_add"
android:showAsAction="ifRoom"/>

<item
android:id="@+id/accounts_management_settings"
android:icon="@drawable/ic_action_action_settings"
android:title="@string/accounts_management_action_settings"
android:showAsAction="ifRoom"/>
</menu>
6 changes: 6 additions & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
<!-- activities titles -->
<string name="account_management_activity_title">Account Management</string>
<string name="account_view_activity_title">MPOS Viewer</string>
<string name="settings_activity_title">MPOS Viewer Settings</string>

<!-- accounts management -->
<string name="accounts_management_action_add">Add</string>
<string name="accounts_management_action_settings">Settings</string>
<string name="accounts_management_context_action_remove">Remove</string>
<string name="accounts_management_unknown_qr_toast">Scanned code doesn\'t contain any MPOS account</string>
<string name="accounts_management_account_already_added">The account is already added</string>
Expand Down Expand Up @@ -68,4 +70,8 @@
<!-- formats -->
<string name="format_time_secs">%d sec</string>
<string name="format_time_mins_secs">%1$d min %2$d sec</string>

<!-- settings -->
<string name="setting_trust_all_ssl_certs_title">Trust all SSL certificates</string>
<string name="setting_trust_all_ssl_certs_summary">Check if you\'d like to trust all HTTPS hosts even they aren\'t trusted by default</string>
</resources>
9 changes: 9 additions & 0 deletions src/main/res/xml/settings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<CheckBoxPreference
android:key="trustAllSSLCerts"
android:title="@string/setting_trust_all_ssl_certs_title"
android:summary="@string/setting_trust_all_ssl_certs_summary" />

</PreferenceScreen>

0 comments on commit 4402fb2

Please sign in to comment.