Skip to content

Commit 9a57547

Browse files
committed
ANDROID: implemented IP based authentication for the web portal
1 parent 1e362ed commit 9a57547

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

src/platform/android/app/src/main/java/net/sourceforge/smallbasic/MainActivity.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -772,8 +772,12 @@ private void installSamples() {
772772
}
773773
}
774774

775-
private boolean isHostPermitted(String remoteHost) {
776-
return (remoteHost != null && permittedHost.get(remoteHost) != null && Boolean.TRUE.equals(permittedHost.get(remoteHost)));
775+
private boolean isHostDenied(String remoteHost) {
776+
return (remoteHost != null && permittedHost.get(remoteHost) != null && Boolean.FALSE.equals(permittedHost.get(remoteHost)));
777+
}
778+
779+
private boolean isHostNonPermitted(String remoteHost) {
780+
return (remoteHost == null || permittedHost.get(remoteHost) == null || !Boolean.TRUE.equals(permittedHost.get(remoteHost)));
777781
}
778782

779783
private boolean locationPermitted() {
@@ -889,8 +893,8 @@ private void setupStorageEnvironment() {
889893
}
890894

891895
private void validateAccess(String remoteHost) throws IOException {
892-
if (!isHostPermitted(remoteHost)) {
893-
throw new IOException("Access denied");
896+
if (isHostNonPermitted(remoteHost)) {
897+
throw new IOException(getString(R.string.PORTAL_DENIED));
894898
}
895899
}
896900

@@ -990,8 +994,15 @@ protected void deleteFile(String remoteHost, String fileName) throws IOException
990994
}
991995

992996
@Override
993-
protected void execStream(InputStream inputStream) throws IOException {
994-
MainActivity.this.execStream(inputStream);
997+
protected void execStream(String remoteHost, InputStream inputStream) throws IOException {
998+
if (isHostDenied(remoteHost)) {
999+
throw new IOException(getString(R.string.PORTAL_DENIED));
1000+
}
1001+
if (isHostNonPermitted(remoteHost)) {
1002+
requestHostPermission(remoteHost);
1003+
} else {
1004+
MainActivity.this.execStream(inputStream);
1005+
}
9951006
}
9961007

9971008
@Override
@@ -1002,7 +1013,7 @@ protected Response getFile(String remoteHost, String path, boolean asset) throws
10021013
long length = getFileLength(name);
10031014
log("Opened " + name + " " + length + " bytes");
10041015
result = new Response(getAssets().open(name), length);
1005-
if ("index.html".equals(path) && !isHostPermitted(remoteHost)) {
1016+
if ("index.html".equals(path) && isHostNonPermitted(remoteHost)) {
10061017
requestHostPermission(remoteHost);
10071018
}
10081019
} else {

src/platform/android/app/src/main/java/net/sourceforge/smallbasic/WebServer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void run() {
6464
}
6565

6666
protected abstract void deleteFile(String remoteHost, String fileName) throws IOException;
67-
protected abstract void execStream(InputStream inputStream) throws IOException;
67+
protected abstract void execStream(String remoteHost, InputStream inputStream) throws IOException;
6868
protected abstract Response getFile(String remoteHost, String path, boolean asset) throws IOException;
6969
protected abstract Collection<FileData> getFileData(String remoteHost) throws IOException;
7070
protected abstract byte[] decodeBase64(String data);
@@ -546,7 +546,7 @@ private Response handleRename(Map<String, FormField> data) throws IOException {
546546

547547
private void handleRun(InputStream inputStream) throws IOException {
548548
if (tokenKey.equals(requestToken)) {
549-
execStream(inputStream);
549+
execStream(remoteHost, inputStream);
550550
} else {
551551
log("Invalid token");
552552
}

src/platform/android/app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
33
<string name="app_name">SmallBASIC</string>
4+
<string name="PORTAL_DENIED">Access denied</string>
45
<string name="PORTAL_PROMPT">Allow Web portal access?</string>
56
<string name="PORTAL_QUESTION">Do you wish to allow anyone with IP address [%s] to gain access to the portal?</string>
67
<string name="OK">OK</string>

src/platform/android/webui/server/src/main/java/net/sourceforge/smallbasic/Server.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected void deleteFile(String hostName, String fileName) throws IOException {
3737
}
3838

3939
@Override
40-
protected void execStream(InputStream inputStream) {
40+
protected void execStream(String remoteHost, InputStream inputStream) {
4141
try {
4242
byte[] data = IOUtils.readAllBytes(inputStream);
4343
log(new String(data));

0 commit comments

Comments
 (0)