Add session timeout page
[clamp.git] / src / main / java / org / onap / clamp / clds / config / spring / CldsSecurityConfigUsers.java
index d9e5ef2..961cc6b 100644 (file)
@@ -18,7 +18,7 @@
  * limitations under the License.
  * ============LICENSE_END============================================
  * ===================================================================
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
+ * 
  */
 
 package org.onap.clamp.clds.config.spring;
@@ -30,6 +30,7 @@ import java.io.IOException;
 
 import org.onap.clamp.clds.config.ClampProperties;
 import org.onap.clamp.clds.config.CldsUserJsonDecoder;
+import org.onap.clamp.clds.exception.CldsConfigException;
 import org.onap.clamp.clds.exception.CldsUsersException;
 import org.onap.clamp.clds.service.CldsUser;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -40,6 +41,8 @@ import org.springframework.security.config.annotation.authentication.builders.Au
 import org.springframework.security.config.annotation.web.builders.HttpSecurity;
 import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
 import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
+import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
+import org.springframework.security.crypto.password.PasswordEncoder;
 
 /**
  * This class is used to enable the HTTP authentication to login. It requires a
@@ -59,6 +62,10 @@ public class CldsSecurityConfigUsers extends WebSecurityConfigurerAdapter {
     private String cldsPersmissionTypeCl;
     @Value("${CLDS_PERMISSION_INSTANCE:dev}")
     private String cldsPermissionInstance;
+    @Value("${clamp.config.security.encoder:bcrypt}")
+    private String cldsEncoderMethod;
+    @Value("${clamp.config.security.encoder.bcrypt.strength:10}")
+    private Integer cldsBcryptEncoderStrength;
 
     /**
      * This method configures on which URL the authorization will be enabled.
@@ -67,7 +74,11 @@ public class CldsSecurityConfigUsers extends WebSecurityConfigurerAdapter {
     protected void configure(HttpSecurity http) {
         try {
             http.csrf().disable().httpBasic().and().authorizeRequests().antMatchers("/restservices/clds/v1/user/**")
-                    .authenticated().anyRequest().permitAll().and().logout();
+                    .authenticated().anyRequest().permitAll().and().logout()
+            .and().sessionManagement()
+                .maximumSessions(1)
+            .and().invalidSessionUrl("/designer/timeout.html");
+
         } catch (Exception e) {
             logger.error("Exception occurred during the setup of the Web users in memory", e);
             throw new CldsUsersException("Exception occurred during the setup of the Web users in memory", e);
@@ -83,6 +94,9 @@ public class CldsSecurityConfigUsers extends WebSecurityConfigurerAdapter {
      */
     @Autowired
     public void configureGlobal(AuthenticationManagerBuilder auth) {
+        // configure algorithm used for password hashing
+        final PasswordEncoder passwordEncoder = getPasswordEncoder();
+
         try {
             CldsUser[] usersList = loadUsers();
             // no users defined
@@ -92,7 +106,7 @@ public class CldsSecurityConfigUsers extends WebSecurityConfigurerAdapter {
             }
             for (CldsUser user : usersList) {
                 auth.inMemoryAuthentication().withUser(user.getUser()).password(user.getPassword())
-                        .roles(user.getPermissionsString());
+                    .roles(user.getPermissionsString()).and().passwordEncoder(passwordEncoder);
             }
         } catch (Exception e) {
             logger.error("Exception occurred during the setup of the Web users in memory", e);
@@ -112,4 +126,15 @@ public class CldsSecurityConfigUsers extends WebSecurityConfigurerAdapter {
         logger.info("Load from clds-users.properties");
         return CldsUserJsonDecoder.decodeJson(refProp.getFileContent("files.cldsUsers"));
     }
+
+    /**
+     * This methods returns the chosen encoder for password hashing.
+     */
+    private PasswordEncoder getPasswordEncoder() {
+        if ("bcrypt".equals(cldsEncoderMethod)) {
+            return new BCryptPasswordEncoder(cldsBcryptEncoderStrength);
+        } else {
+            throw new CldsConfigException("Invalid clamp.config.security.encoder value. 'bcrypt' is the only option at this time.");
+        }
+    }
 }