@Test public void test_login_successfully() { UserLogin user = new UserLogin("testuser", "testuser"); String jsonStr = "{\"username\":\"testuser\",\"password\":\"testuser\"}"; String responseJson = "{\"client_token\":\"1sGWOpjPOuZezcIgxVFAm1Oh\",\"admin\":\"no\",\"access\":{},\"policies\":[\"default\"],\"lease_duration\":1800000}"; Response response = getMockResponse(HttpStatus.OK, true, responseJson); response.setAdminPolicies(null); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body(response.toString()); when(JSONUtil.getJSON(user)).thenReturn(jsonStr); when(reqProcessor.process("/auth/userpass/login",jsonStr,"")).thenReturn(response); ResponseEntity<String> responseEntity = userPassService.login(user); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
public ResponseEntity<String> login(UserLogin user) { String jsonStr = JSONUtil.getJSON(user); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, user.getUsername()). put(LogMessage.ACTION, "User Login"). put(LogMessage.MESSAGE, "Trying to authenticate"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); Response response = reqProcessor.process("/auth/userpass/login",jsonStr,""); List<String> adminPolicies = null; try { ObjectMapper objMapper = new ObjectMapper(); JsonNode policiesJsonNode = objMapper.readTree(response.getResponse().toString()).get("policies"); adminPolicies = safeUtils.getPoliciesForManagedSafes(policiesJsonNode); } catch (Exception e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, user.getUsername()). put(LogMessage.ACTION, "User Login"). put(LogMessage.MESSAGE, "Unable to retrieve the admin (sudo) policies associated with the user"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); } if(HttpStatus.OK.equals(response.getHttpstatus())){ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, user.getUsername()). put(LogMessage.ACTION, "User Login"). put(LogMessage.MESSAGE, "User 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.toString()); }else{ if (HttpStatus.BAD_REQUEST.equals(response.getHttpstatus())) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, user.getUsername()). put(LogMessage.ACTION, "User 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, user.getUsername()). put(LogMessage.ACTION, "User 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, user.getUsername()). put(LogMessage.ACTION, "User 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.\"]}"); } }
UserPassService { public ResponseEntity<String> login(UserLogin user) { String jsonStr = JSONUtil.getJSON(user); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, user.getUsername()). put(LogMessage.ACTION, "User Login"). put(LogMessage.MESSAGE, "Trying to authenticate"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); Response response = reqProcessor.process("/auth/userpass/login",jsonStr,""); List<String> adminPolicies = null; try { ObjectMapper objMapper = new ObjectMapper(); JsonNode policiesJsonNode = objMapper.readTree(response.getResponse().toString()).get("policies"); adminPolicies = safeUtils.getPoliciesForManagedSafes(policiesJsonNode); } catch (Exception e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, user.getUsername()). put(LogMessage.ACTION, "User Login"). put(LogMessage.MESSAGE, "Unable to retrieve the admin (sudo) policies associated with the user"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); } if(HttpStatus.OK.equals(response.getHttpstatus())){ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, user.getUsername()). put(LogMessage.ACTION, "User Login"). put(LogMessage.MESSAGE, "User 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.toString()); }else{ if (HttpStatus.BAD_REQUEST.equals(response.getHttpstatus())) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, user.getUsername()). put(LogMessage.ACTION, "User 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, user.getUsername()). put(LogMessage.ACTION, "User 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, user.getUsername()). put(LogMessage.ACTION, "User 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.\"]}"); } } }
UserPassService { public ResponseEntity<String> login(UserLogin user) { String jsonStr = JSONUtil.getJSON(user); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, user.getUsername()). put(LogMessage.ACTION, "User Login"). put(LogMessage.MESSAGE, "Trying to authenticate"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); Response response = reqProcessor.process("/auth/userpass/login",jsonStr,""); List<String> adminPolicies = null; try { ObjectMapper objMapper = new ObjectMapper(); JsonNode policiesJsonNode = objMapper.readTree(response.getResponse().toString()).get("policies"); adminPolicies = safeUtils.getPoliciesForManagedSafes(policiesJsonNode); } catch (Exception e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, user.getUsername()). put(LogMessage.ACTION, "User Login"). put(LogMessage.MESSAGE, "Unable to retrieve the admin (sudo) policies associated with the user"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); } if(HttpStatus.OK.equals(response.getHttpstatus())){ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, user.getUsername()). put(LogMessage.ACTION, "User Login"). put(LogMessage.MESSAGE, "User 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.toString()); }else{ if (HttpStatus.BAD_REQUEST.equals(response.getHttpstatus())) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, user.getUsername()). put(LogMessage.ACTION, "User 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, user.getUsername()). put(LogMessage.ACTION, "User 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, user.getUsername()). put(LogMessage.ACTION, "User 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.\"]}"); } }  }
UserPassService { public ResponseEntity<String> login(UserLogin user) { String jsonStr = JSONUtil.getJSON(user); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, user.getUsername()). put(LogMessage.ACTION, "User Login"). put(LogMessage.MESSAGE, "Trying to authenticate"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); Response response = reqProcessor.process("/auth/userpass/login",jsonStr,""); List<String> adminPolicies = null; try { ObjectMapper objMapper = new ObjectMapper(); JsonNode policiesJsonNode = objMapper.readTree(response.getResponse().toString()).get("policies"); adminPolicies = safeUtils.getPoliciesForManagedSafes(policiesJsonNode); } catch (Exception e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, user.getUsername()). put(LogMessage.ACTION, "User Login"). put(LogMessage.MESSAGE, "Unable to retrieve the admin (sudo) policies associated with the user"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); } if(HttpStatus.OK.equals(response.getHttpstatus())){ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, user.getUsername()). put(LogMessage.ACTION, "User Login"). put(LogMessage.MESSAGE, "User 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.toString()); }else{ if (HttpStatus.BAD_REQUEST.equals(response.getHttpstatus())) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, user.getUsername()). put(LogMessage.ACTION, "User 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, user.getUsername()). put(LogMessage.ACTION, "User 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, user.getUsername()). put(LogMessage.ACTION, "User 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> createUser(String token, UserpassUser user); ResponseEntity<String> readUser(String token, String username); ResponseEntity<String> deleteUser(String token, String username); ResponseEntity<String> updatePassword( String token, UserpassUser user); ResponseEntity<String> listUsers(String token); ResponseEntity<String> login(UserLogin user); }
UserPassService { public ResponseEntity<String> login(UserLogin user) { String jsonStr = JSONUtil.getJSON(user); log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, user.getUsername()). put(LogMessage.ACTION, "User Login"). put(LogMessage.MESSAGE, "Trying to authenticate"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); Response response = reqProcessor.process("/auth/userpass/login",jsonStr,""); List<String> adminPolicies = null; try { ObjectMapper objMapper = new ObjectMapper(); JsonNode policiesJsonNode = objMapper.readTree(response.getResponse().toString()).get("policies"); adminPolicies = safeUtils.getPoliciesForManagedSafes(policiesJsonNode); } catch (Exception e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, user.getUsername()). put(LogMessage.ACTION, "User Login"). put(LogMessage.MESSAGE, "Unable to retrieve the admin (sudo) policies associated with the user"). put(LogMessage.STATUS, response.getHttpstatus().toString()). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL).toString()). build())); } if(HttpStatus.OK.equals(response.getHttpstatus())){ log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, user.getUsername()). put(LogMessage.ACTION, "User Login"). put(LogMessage.MESSAGE, "User 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.toString()); }else{ if (HttpStatus.BAD_REQUEST.equals(response.getHttpstatus())) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, user.getUsername()). put(LogMessage.ACTION, "User 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, user.getUsername()). put(LogMessage.ACTION, "User 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, user.getUsername()). put(LogMessage.ACTION, "User 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> createUser(String token, UserpassUser user); ResponseEntity<String> readUser(String token, String username); ResponseEntity<String> deleteUser(String token, String username); ResponseEntity<String> updatePassword( String token, UserpassUser user); ResponseEntity<String> listUsers(String token); ResponseEntity<String> login(UserLogin user);  }