Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Resource BASIC_AUTH_ADMIN_PW that forces basic auth if not empty #213

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
return to original resValues
  • Loading branch information
magr committed Oct 7, 2021
commit 85342bc0054e9cb194c4d152fcda535d2c05a0b4
2 changes: 2 additions & 0 deletions debug-db-base/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ android {
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
resValue("string", "PORT_NUMBER", "8080")
resValue("string", "BASIC_AUTH_ADMIN_PW", "")
}
buildTypes {
release {
Expand Down
12 changes: 10 additions & 2 deletions debug-db-base/src/main/java/com/amitshekhar/DebugDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
public class DebugDB {

private static final String TAG = DebugDB.class.getSimpleName();
private static final int DEFAULT_PORT = 8080;
private static ClientServer clientServer;
private static String addressLog = "not available";

Expand All @@ -47,8 +48,15 @@ private DebugDB() {
}

public static void initialize(Context context, DBFactory dbFactory) {
int portNumber = Settings.PORT;

int portNumber;

try {
portNumber = Integer.valueOf(context.getString(R.string.PORT_NUMBER));
} catch (NumberFormatException ex) {
Log.e(TAG, "PORT_NUMBER should be integer", ex);
portNumber = DEFAULT_PORT;
Log.i(TAG, "Using Default port : " + DEFAULT_PORT);
}
clientServer = new ClientServer(context, portNumber, dbFactory);
clientServer.start();
addressLog = NetworkUtils.getAddressLog(context, portNumber);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import com.amitshekhar.utils.DatabaseFileProvider;
import com.amitshekhar.utils.DatabaseHelper;
import com.amitshekhar.utils.PrefHelper;
import com.amitshekhar.utils.Settings;
import com.amitshekhar.utils.ResourceManager;
import com.amitshekhar.utils.Utils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand Down Expand Up @@ -80,7 +80,10 @@ public RequestHandler(Context context, DBFactory dbFactory) {
mGson = new GsonBuilder().serializeNulls().create();
mDbFactory = dbFactory;

basicAuthAdminPW = Settings.BASIC_AUTH_ADMIN_PW;
basicAuthAdminPW = ResourceManager.getResourceString(context, "BASIC_AUTH_ADMIN_PW");
if (basicAuthAdminPW == null) {
basicAuthAdminPW = "";
}
}

public boolean checkHeader(PrintStream output, List<String> lines) {
Expand Down Expand Up @@ -145,7 +148,7 @@ public void handle(Socket socket) throws IOException {
// Output stream that we send the response to
output = new PrintStream(socket.getOutputStream());

if (basicAuthAdminPW != null && !checkHeader(output, lines)) {
if (!basicAuthAdminPW.isEmpty() && !checkHeader(output, lines)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.amitshekhar.utils;

import android.content.Context;

public class ResourceManager {

public static String getResourceString(Context context, String resourceName) {
int resourceId = context.getResources().getIdentifier(resourceName, "string", context.getPackageName());
if (resourceId != 0) {
return context.getString(resourceId);
} else {
return null;
}
}

}