diff --git a/pom.xml b/pom.xml index d18b986..486d7df 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.bwssystems.HABridge ha-bridge - 4.5.0alpha + 4.5.0alpha-2 jar HA Bridge diff --git a/src/main/java/com/bwssystems/HABridge/AuthFramework.java b/src/main/java/com/bwssystems/HABridge/AuthFramework.java deleted file mode 100644 index 12a6547..0000000 --- a/src/main/java/com/bwssystems/HABridge/AuthFramework.java +++ /dev/null @@ -1,25 +0,0 @@ -package com.bwssystems.HABridge; - -import spark.Request; - -public abstract class AuthFramework { - private static final String USER_SESSION_ID = "user"; - - public AuthFramework() { - // TODO Auto-generated constructor stub - } - - public void addAuthenticatedUser(Request request, User u) { - request.session().attribute(USER_SESSION_ID, u); - - } - - public void removeAuthenticatedUser(Request request) { - request.session().removeAttribute(USER_SESSION_ID); - - } - - public User getAuthenticatedUser(Request request) { - return request.session().attribute(USER_SESSION_ID); - } -} diff --git a/src/main/java/com/bwssystems/HABridge/BridgeSecurity.java b/src/main/java/com/bwssystems/HABridge/BridgeSecurity.java index 454d7c1..204643e 100644 --- a/src/main/java/com/bwssystems/HABridge/BridgeSecurity.java +++ b/src/main/java/com/bwssystems/HABridge/BridgeSecurity.java @@ -18,20 +18,26 @@ import org.slf4j.LoggerFactory; import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; -public class BridgeSecurity extends AuthFramework { +import spark.Request; + +public class BridgeSecurity { private static final Logger log = LoggerFactory.getLogger(BridgeSecurity.class); - private char[] habridgeKey; + private static final String USER_SESSION_ID = "user"; private static final byte[] SALT = { (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12, (byte) 0xde, (byte) 0x33, (byte) 0x10, (byte) 0x12, }; + private char[] habridgeKey; private BridgeSecurityDescriptor securityDescriptor; private boolean settingsChanged; - public BridgeSecurity(char[] theKey, String theData) { + public BridgeSecurity(char[] theKey) { habridgeKey = theKey; securityDescriptor = null; settingsChanged = false; + } + + public void setSecurityData(String theData) { String anError = null; if(theData != null && !theData.isEmpty()) { try { @@ -211,4 +217,47 @@ public class BridgeSecurity extends AuthFramework { private static byte[] base64Decode(String property) throws IOException { return Base64.getDecoder().decode(property); } -} + + public void addAuthenticatedUser(Request request, User u) { + request.session().attribute(USER_SESSION_ID, u); + + } + + public void removeAuthenticatedUser(Request request) { + request.session().removeAttribute(USER_SESSION_ID); + + } + + public User getAuthenticatedUser(Request request) { + User theUser = request.session().attribute(USER_SESSION_ID); + if(theUser == null) { + String authHeader = request.headers("Authorization"); + if(authHeader != null) { + byte[] authData; + try { + authData = base64Decode(authHeader.substring(6)); + } catch (IOException e1) { + // TODO Auto-generated catch block + return theUser; + } + String[] credentials = new String(authData).split(":"); + String username = credentials[0]; + String password = credentials[1]; + theUser = new User(); + theUser.setUsername(username); + theUser.setPassword(password); + LoginResult theResult = null; + try { + theResult = validatePassword(theUser); + } catch (IOException e) { + // TODO Auto-generated catch block + return null; + } + if(theResult != null && theResult.getError() == null) { + addAuthenticatedUser(request, theUser); + } + } + } + return theUser; + } +} \ No newline at end of file diff --git a/src/main/java/com/bwssystems/HABridge/BridgeSettings.java b/src/main/java/com/bwssystems/HABridge/BridgeSettings.java index 567c61b..cf3617a 100644 --- a/src/main/java/com/bwssystems/HABridge/BridgeSettings.java +++ b/src/main/java/com/bwssystems/HABridge/BridgeSettings.java @@ -35,6 +35,10 @@ public class BridgeSettings extends BackupHandler { bridgeControl = new BridgeControlDescriptor(); theBridgeSettings = new BridgeSettingsDescriptor(); bridgeSecurity = null; + String theKey = System.getProperty("security.key"); + if(theKey == null) + theKey = "IWantMyPasswordsToBeAbleToBeDecodedPleaseSeeTheReadme"; + bridgeSecurity = new BridgeSecurity(theKey.toCharArray()); String ipV6Stack = System.getProperty("ipV6Stack"); if(ipV6Stack == null || !ipV6Stack.equalsIgnoreCase("true")) { System.setProperty("java.net.preferIPv4Stack" , "true"); @@ -183,10 +187,7 @@ public class BridgeSettings extends BackupHandler { setupInternalTestUser(); - String theKey = System.getProperty("security.key"); - if(theKey == null) - theKey = "IWantMyPasswordsToBeAbleToBeDecodedPleaseSeeTheReadme"; - bridgeSecurity = new BridgeSecurity(theKey.toCharArray(), theBridgeSettings.getSecurityData()); + bridgeSecurity.setSecurityData(theBridgeSettings.getSecurityData()); } public void loadConfig() { @@ -273,7 +274,8 @@ public class BridgeSettings extends BackupHandler { perms.add(PosixFilePermission.OWNER_WRITE); try { - if(System.getProperty("os.name").toLowerCase().indexOf("win") <= 0) + String osName = System.getProperty("os.name"); + if(osName.toLowerCase().indexOf("win") < 0) Files.setPosixFilePermissions(filePath, perms); } catch(UnsupportedOperationException e) { log.info("Cannot set permissions for config file on this system as it is not supported. Continuing"); diff --git a/src/main/resources/public/index.html b/src/main/resources/public/index.html index a12ec99..76eff64 100644 --- a/src/main/resources/public/index.html +++ b/src/main/resources/public/index.html @@ -41,7 +41,6 @@ +
  • Login/Logout
  • diff --git a/src/main/resources/public/scripts/app.js b/src/main/resources/public/scripts/app.js index 8bec0be..18dde82 100644 --- a/src/main/resources/public/scripts/app.js +++ b/src/main/resources/public/scripts/app.js @@ -87,6 +87,7 @@ app.run( async function ($rootScope, $location, Auth, bridgeService) { bridgeService.getTestUser(); bridgeService.getSecurityInfo(); bridgeService.viewMapTypes(); + bridgeService.viewConfigs(); $location.path("/"); } else { event.preventDefault(); @@ -94,12 +95,33 @@ app.run( async function ($rootScope, $location, Auth, bridgeService) { } }); + $rootScope.$on('securityReview', function(event, data) { + if(Auth.isLoggedIn()) { + bridgeService.loadBridgeSettings(); + bridgeService.getTestUser(); + bridgeService.getSecurityInfo(); + bridgeService.viewMapTypes(); + bridgeService.viewConfigs(); + $location.path("/"); + } else { + event.preventDefault(); + $location.path("/login"); + } + }); + + $rootScope.$on('securityReinit', function(event, data) { + event.preventDefault(); + Auth.logout(); + $location.path("/login"); + }); + $rootScope.$on('$routeChangeStart', function (event, next) { if(Auth.isLoggedIn()) { bridgeService.loadBridgeSettings(); bridgeService.getTestUser(); bridgeService.getSecurityInfo(); bridgeService.viewMapTypes(); + bridgeService.viewConfigs(); } if (!Auth.checkPermissionForView(next)){ event.preventDefault(); @@ -870,10 +892,9 @@ app.service ('bridgeService', function ($rootScope, $http, $base64, $location, n return $http.get(this.state.bridgelocation + "/description.xml").then( function (response) { ngToast.dismiss(self.state.myToastMsg); - self.viewConfigs(); self.state.myToastMsg = null; self.state.isInControl = false; - window.location.reload(); + $rootScope.$broadcast('securityReinit', 'done'); }, function (error) { setTimeout(function(){ @@ -3213,6 +3234,7 @@ app.filter('configuredSomfyDevices', function (bridgeService) { app.controller('LoginController', function ($scope, $location, Auth) { $scope.failed = false; + $scope.loggedIn = Auth.isLoggedIn(); $scope.login = function(username, password) { Auth.login(username, password) .then(function() { @@ -3224,6 +3246,8 @@ app.controller('LoginController', function ($scope, $location, Auth) { $scope.logout = function() { Auth.logout(); + $scope.loggedIn = Auth.isLoggedIn(); + $location.path("/login"); }; }); diff --git a/src/main/resources/public/views/login.html b/src/main/resources/public/views/login.html index 2ebefe3..0853b14 100644 --- a/src/main/resources/public/views/login.html +++ b/src/main/resources/public/views/login.html @@ -3,7 +3,7 @@

    Login

    -
    +
    Enter Credentials @@ -18,10 +18,12 @@
    -
    +
    + +
    \ No newline at end of file diff --git a/src/main/resources/public/views/system.html b/src/main/resources/public/views/system.html index 266c3f7..811af09 100644 --- a/src/main/resources/public/views/system.html +++ b/src/main/resources/public/views/system.html @@ -48,16 +48,9 @@ + type="submit" ng-click="bridgeReinit()">Bridge Reinitialize - -