LDAPAuthService { public ResponseEntity<String> authenticateLdap(UserLogin userLogin){ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, userLogin.getUsername()). put(LogMessage.ACTION, "LDAP Login"). put(LogMessage.MESSAGE, "Trying to authenticate"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); String jsonStr = JSONUtil.getJSON(userLogin); Response response = reqProcessor.process("/auth/ldap/login",jsonStr,""); if(HttpStatus.OK.equals(response.getHttpstatus())){ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, userLogin.getUsername()). put(LogMessage.ACTION, "LDAP Login"). put(LogMessage.MESSAGE, "Authentication Successful"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(response.getHttpstatus()).body(response.getResponse()); }else{ if (HttpStatus.BAD_REQUEST.equals(response.getHttpstatus())) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, userLogin.getUsername()). put(LogMessage.ACTION, "LDAP Login"). put(LogMessage.MESSAGE, "User Authentication failed. Invalid username or password."). put(LogMessage.RESPONSE, response.getResponse()). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(response.getHttpstatus()).body("{\"errors\": [\"User Authentication failed\", \"Invalid username or password. Please retry again after correcting username or password.\"]}"); } else if (HttpStatus.INTERNAL_SERVER_ERROR.equals(response.getHttpstatus())) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, userLogin.getUsername()). put(LogMessage.ACTION, "LDAP Login"). put(LogMessage.MESSAGE, "User Authentication failed. Vault Services could be down."). put(LogMessage.RESPONSE, response.getResponse()). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(response.getHttpstatus()).body("{\"errors\": [\"User Authentication failed\", \"This may be due to vault services are down or vault services are not reachable\"]}"); } log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, userLogin.getUsername()). put(LogMessage.ACTION, "LDAP Login"). put(LogMessage.MESSAGE, "User Authentication failed."). put(LogMessage.RESPONSE, response.getResponse()). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); return ResponseEntity.status(response.getHttpstatus()).body("{\"errors\":[\"Username Authentication Failed.\"]}"); } }  ResponseEntity<String> authenticateLdap(UserLogin userLogin); ResponseEntity<String> configureLdapGroup(String token, LDAPGroup ldapGroup); ResponseEntity<String> listLdapGroups(String token); ResponseEntity<String> fetchLdapGroup(String token, String groupname); ResponseEntity<String> deleteLdapGroup(String token, String groupname); ResponseEntity<String> configureLdapUser(String token, LDAPUser ldapUser); ResponseEntity<String> listLdapUsers(String token); ResponseEntity<String> fetchLdapUser(String token, String username); ResponseEntity<String> deleteLdapUser(String token, String username);  }
@Test public void test_authenticateLdap_successfully() { UserLogin userLogin = new UserLogin("safeadmin", "safeadmin"); Response response = getMockResponse(HttpStatus.OK, true, "{\"messages\":[\"Authentication Successful\"]}"); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Authentication Successful\"]}"); String jsonStr = "{ \"username\": \"safeadmin\", \"password\": \"safeadmin\"}"; when(JSONUtil.getJSON(userLogin)).thenReturn(jsonStr); when(reqProcessor.process("/auth/ldap/login",jsonStr,"")).thenReturn(response); ResponseEntity<String> responseEntity = ldapAuthService.authenticateLdap(userLogin); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_authenticateLdap_failure_400() { UserLogin userLogin = new UserLogin("safeadmin", "safeadmin"); Response response = getMockResponse(HttpStatus.BAD_REQUEST, true, "{\"errors\": [\"User Authentication failed\", \"Invalid username or password. Please retry again after correcting username or password.\"]}"); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.BAD_REQUEST).body("{\"errors\": [\"User Authentication failed\", \"Invalid username or password. Please retry again after correcting username or password.\"]}"); String jsonStr = "{ \"username\": \"safeadmin\", \"password\": \"safeadmin\"}"; when(JSONUtil.getJSON(userLogin)).thenReturn(jsonStr); when(reqProcessor.process("/auth/ldap/login",jsonStr,"")).thenReturn(response); ResponseEntity<String> responseEntity = ldapAuthService.authenticateLdap(userLogin); assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_authenticateLdap_failure_500() { UserLogin userLogin = new UserLogin("safeadmin", "safeadmin"); Response response = getMockResponse(HttpStatus.INTERNAL_SERVER_ERROR, true, "{\"errors\": [\"User Authentication failed\", \"This may be due to vault services are down or vault services are not reachable\"]}"); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"errors\": [\"User Authentication failed\", \"This may be due to vault services are down or vault services are not reachable\"]}"); String jsonStr = "{ \"username\": \"safeadmin\", \"password\": \"safeadmin\"}"; when(JSONUtil.getJSON(userLogin)).thenReturn(jsonStr); when(reqProcessor.process("/auth/ldap/login",jsonStr,"")).thenReturn(response); ResponseEntity<String> responseEntity = ldapAuthService.authenticateLdap(userLogin); assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_authenticateLdap_failure_204() { UserLogin userLogin = new UserLogin("safeadmin", "safeadmin"); Response response = getMockResponse(HttpStatus.NO_CONTENT, true, "{\"errors\":[\"Username Authentication Failed.\"]}"); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.NO_CONTENT).body("{\"errors\":[\"Username Authentication Failed.\"]}"); String jsonStr = "{ \"username\": \"safeadmin\", \"password\": \"safeadmin\"}"; when(JSONUtil.getJSON(userLogin)).thenReturn(jsonStr); when(reqProcessor.process("/auth/ldap/login",jsonStr,"")).thenReturn(response); ResponseEntity<String> responseEntity = ldapAuthService.authenticateLdap(userLogin); assertEquals(HttpStatus.NO_CONTENT, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }