mirror of
https://github.com/bwssytems/ha-bridge.git
synced 2025-12-16 18:24:36 +00:00
Took care of dynamic test user creation
This commit is contained in:
2
pom.xml
2
pom.xml
@@ -5,7 +5,7 @@
|
|||||||
|
|
||||||
<groupId>com.bwssystems.HABridge</groupId>
|
<groupId>com.bwssystems.HABridge</groupId>
|
||||||
<artifactId>ha-bridge</artifactId>
|
<artifactId>ha-bridge</artifactId>
|
||||||
<version>4.5.0rc1</version>
|
<version>4.5.0rc2</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>HA Bridge</name>
|
<name>HA Bridge</name>
|
||||||
|
|||||||
@@ -51,7 +51,8 @@ public class BridgeSecurity {
|
|||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
anError = e.getMessage();
|
anError = e.getMessage();
|
||||||
}
|
}
|
||||||
log.warn("Cound not get security data, using default security (none): " + anError);
|
if(anError != null)
|
||||||
|
log.warn("Cound not get security data, using default security (none): " + anError);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(theData == null || anError != null) {
|
if(theData == null || anError != null) {
|
||||||
|
|||||||
@@ -186,8 +186,6 @@ public class BridgeSettings extends BackupHandler {
|
|||||||
theBridgeSettings.setWebaddress(serverIpOverride);
|
theBridgeSettings.setWebaddress(serverIpOverride);
|
||||||
setupParams(Paths.get(theBridgeSettings.getConfigfile()), ".cfgbk", "habridge.config-");
|
setupParams(Paths.get(theBridgeSettings.getConfigfile()), ".cfgbk", "habridge.config-");
|
||||||
|
|
||||||
setupInternalTestUser();
|
|
||||||
|
|
||||||
bridgeSecurity.setSecurityData(theBridgeSettings.getSecurityData());
|
bridgeSecurity.setSecurityData(theBridgeSettings.getSecurityData());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -338,9 +336,4 @@ public class BridgeSettings extends BackupHandler {
|
|||||||
}
|
}
|
||||||
return addressString;
|
return addressString;
|
||||||
}
|
}
|
||||||
private void setupInternalTestUser() {
|
|
||||||
theBridgeSettings.setupInternalTestUser();
|
|
||||||
if(theBridgeSettings.isSettingsChanged())
|
|
||||||
this.updateConfigFile();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import java.util.HashMap;
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Map.Entry;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.StringTokenizer;
|
import java.util.StringTokenizer;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
@@ -14,8 +15,8 @@ import com.bwssystems.HABridge.api.hue.HueErrorResponse;
|
|||||||
import com.bwssystems.HABridge.api.hue.WhitelistEntry;
|
import com.bwssystems.HABridge.api.hue.WhitelistEntry;
|
||||||
|
|
||||||
public class BridgeSettingsDescriptor {
|
public class BridgeSettingsDescriptor {
|
||||||
private static final String DEFAULT_INTERNAL_USER = "thehabridgeuser";
|
private static final String DEPRACATED_INTERNAL_USER = "thehabridgeuser";
|
||||||
private static final String DEFAULT_USER_DESCRIPTION = "default_test_user";
|
private static final String TEST_USER_TYPE = "test_ha_bridge";
|
||||||
private String upnpconfigaddress;
|
private String upnpconfigaddress;
|
||||||
private Integer serverport;
|
private Integer serverport;
|
||||||
private Integer upnpresponseport;
|
private Integer upnpresponseport;
|
||||||
@@ -403,11 +404,17 @@ public class BridgeSettingsDescriptor {
|
|||||||
if (!found) {
|
if (!found) {
|
||||||
return HueErrorResponse.createResponse("1", "/api/" + aUser, "unauthorized user", null, null, null).getTheErrors();
|
return HueErrorResponse.createResponse("1", "/api/" + aUser, "unauthorized user", null, null, null).getTheErrors();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Object anUser = whitelist.remove(DEPRACATED_INTERNAL_USER);
|
||||||
|
if(anUser != null)
|
||||||
|
setSettingsChanged(true);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void newWhitelistUser(String aUser, String userDescription) {
|
public void newWhitelistUser(String aUser, String userDescription) {
|
||||||
|
if(aUser.equals(DEPRACATED_INTERNAL_USER))
|
||||||
|
return;
|
||||||
if (whitelist == null) {
|
if (whitelist == null) {
|
||||||
whitelist = new HashMap<>();
|
whitelist = new HashMap<>();
|
||||||
}
|
}
|
||||||
@@ -434,23 +441,22 @@ public class BridgeSettingsDescriptor {
|
|||||||
|
|
||||||
return newUser;
|
return newUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void removeTestUsers() {
|
||||||
|
if (whitelist != null) {
|
||||||
|
Object anUser = whitelist.remove(DEPRACATED_INTERNAL_USER);
|
||||||
|
if(anUser != null)
|
||||||
|
setSettingsChanged(true);
|
||||||
|
|
||||||
public String getInternalTestUser() {
|
Iterator<Entry<String, WhitelistEntry>> it = whitelist.entrySet().iterator();
|
||||||
return DEFAULT_INTERNAL_USER;
|
while (it.hasNext()) {
|
||||||
}
|
Map.Entry<String, WhitelistEntry> pair = it.next();
|
||||||
|
it.remove(); // avoids a ConcurrentModificationException
|
||||||
public void setupInternalTestUser() {
|
if(pair.getValue().getName().equals(TEST_USER_TYPE)) {
|
||||||
boolean found = false;
|
whitelist.remove(pair.getKey());
|
||||||
if(whitelist != null) {
|
setSettingsChanged(true);
|
||||||
for (String key : whitelist.keySet()) {
|
}
|
||||||
if(key.equals(DEFAULT_INTERNAL_USER)) {
|
}
|
||||||
found = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(!found) {
|
|
||||||
newWhitelistUser(DEFAULT_INTERNAL_USER, DEFAULT_USER_DESCRIPTION);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -102,6 +102,9 @@ public class HABridge {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
bridgeSettings.getBridgeSettingsDescriptor().removeTestUsers();
|
||||||
|
if(bridgeSettings.getBridgeSettingsDescriptor().isSettingsChanged())
|
||||||
|
bridgeSettings.updateConfigFile();
|
||||||
log.info("HA Bridge (v" + theVersion.getVersion() + ") exiting....");
|
log.info("HA Bridge (v" + theVersion.getVersion() + ") exiting....");
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,14 +78,6 @@ public class SystemControl {
|
|||||||
return "{\"version\":\"" + version.getVersion() + "\",\"isSecure\":" + bridgeSettings.getBridgeSecurity().isSecure() + "}";
|
return "{\"version\":\"" + version.getVersion() + "\",\"isSecure\":" + bridgeSettings.getBridgeSecurity().isSecure() + "}";
|
||||||
});
|
});
|
||||||
|
|
||||||
// http://ip_address:port/system/habridge/testuser gets the valid test user for calling the api
|
|
||||||
get (SYSTEM_CONTEXT + "/habridge/testuser", (request, response) -> {
|
|
||||||
log.debug("Get HA Bridge testuser: " + bridgeSettings.getBridgeSettingsDescriptor().getInternalTestUser());
|
|
||||||
response.status(HttpStatus.SC_OK);
|
|
||||||
response.type("application/json");
|
|
||||||
return "{\"user\":\"" + bridgeSettings.getBridgeSettingsDescriptor().getInternalTestUser() + "\"}";
|
|
||||||
});
|
|
||||||
|
|
||||||
// http://ip_address:port/system/logmsgs gets the log messages for the bridge
|
// http://ip_address:port/system/logmsgs gets the log messages for the bridge
|
||||||
get (SYSTEM_CONTEXT + "/logmsgs", (request, response) -> {
|
get (SYSTEM_CONTEXT + "/logmsgs", (request, response) -> {
|
||||||
log.debug("Get logmsgs.");
|
log.debug("Get logmsgs.");
|
||||||
|
|||||||
@@ -84,7 +84,6 @@ app.run( async function ($rootScope, $location, Auth, bridgeService) {
|
|||||||
Auth.init();
|
Auth.init();
|
||||||
if(Auth.isLoggedIn()) {
|
if(Auth.isLoggedIn()) {
|
||||||
bridgeService.loadBridgeSettings();
|
bridgeService.loadBridgeSettings();
|
||||||
bridgeService.getTestUser();
|
|
||||||
bridgeService.getSecurityInfo();
|
bridgeService.getSecurityInfo();
|
||||||
bridgeService.viewMapTypes();
|
bridgeService.viewMapTypes();
|
||||||
$location.path("/");
|
$location.path("/");
|
||||||
@@ -237,12 +236,25 @@ app.service ('bridgeService', function ($rootScope, $http, $base64, $location, n
|
|||||||
};
|
};
|
||||||
|
|
||||||
this.getTestUser = function () {
|
this.getTestUser = function () {
|
||||||
return $http.get(this.state.systemsbase + "/habridge/testuser").then(
|
if(self.state.testuser === undefined || self.state.testuser === "") {
|
||||||
|
return $http.put(this.state.systemsbase + "/presslinkbutton").then(
|
||||||
|
function (response) {
|
||||||
|
self.getAUser();
|
||||||
|
},
|
||||||
|
function (error) {
|
||||||
|
self.displayWarn("Cannot get testuser: ", error);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
this.getAUser = function () {
|
||||||
|
return $http.post(this.state.huebase, "{\"devicetype\":\"test_ha_bridge\"}").then(
|
||||||
function (response) {
|
function (response) {
|
||||||
self.state.testuser = response.data.user;
|
self.state.testuser = response.data[0].success.username;
|
||||||
},
|
},
|
||||||
function (error) {
|
function (error) {
|
||||||
self.displayWarn("Cannot get testuser: ", error);
|
self.displayWarn("Cannot get a user: ", error);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@@ -251,6 +263,7 @@ app.service ('bridgeService', function ($rootScope, $http, $base64, $location, n
|
|||||||
return $http.get(this.state.systemsbase + "/securityinfo").then(
|
return $http.get(this.state.systemsbase + "/securityinfo").then(
|
||||||
function (response) {
|
function (response) {
|
||||||
self.state.securityInfo = response.data;
|
self.state.securityInfo = response.data;
|
||||||
|
self.getTestUser();
|
||||||
},
|
},
|
||||||
function (error) {
|
function (error) {
|
||||||
self.displayWarn("Cannot get security info: ", error);
|
self.displayWarn("Cannot get security info: ", error);
|
||||||
|
|||||||
Reference in New Issue
Block a user