use of com.vmware.flowgate.exception.WormholeRequestException in project flowgate by vmware.
the class AuthController method refreshToken.
@RequestMapping(value = "/token/refresh", method = RequestMethod.GET)
public AuthToken refreshToken(HttpServletRequest request, HttpServletResponse response) {
String authToken = accessTokenService.getToken(request);
if (authToken == null || "".equals(authToken)) {
return null;
}
DecodedJWT jwt = jwtTokenUtil.getDecodedJwt(authToken);
String currentuser = accessTokenService.getCurrentUser(request).getUsername();
if (!jwt.getSubject().equals(currentuser)) {
throw new WormholeRequestException(HttpStatus.FORBIDDEN, "Forbidden", null);
}
AuthToken access_token = accessTokenService.refreshToken(authToken);
if (access_token != null) {
response.addHeader(InitializeConfigureData.Authentication_Header, access_token.getAccess_token());
Cookie cookie = new Cookie(JwtTokenUtil.Token_Name, access_token.getAccess_token());
cookie.setHttpOnly(true);
cookie.setPath("/");
cookie.setDomain(request.getServerName());
response.addCookie(cookie);
}
return access_token;
}
use of com.vmware.flowgate.exception.WormholeRequestException in project flowgate by vmware.
the class AuthController method updateRole.
// update a role
@ResponseStatus(HttpStatus.OK)
@RequestMapping(value = "/role", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
public void updateRole(@RequestBody WormholeRole role) {
Optional<WormholeRole> oldRoleOptional = roleRepository.findById(role.getId());
if (!oldRoleOptional.isPresent()) {
throw WormholeRequestException.NotFound("Role", "id", role.getId());
}
WormholeRole existingRole = roleRepository.findOneByRoleName(role.getRoleName());
if (existingRole != null && !existingRole.getId().equals(role.getId())) {
String message = "The role name: " + role.getRoleName() + " is already exsit.";
throw new WormholeRequestException(message);
}
WormholeRole old = oldRoleOptional.get();
if (role.getRoleName() != null && !"".equals(role.getRoleName().trim())) {
old.setRoleName(role.getRoleName());
}
if (role.getPrivilegeNames() != null) {
old.setPrivilegeNames(role.getPrivilegeNames());
}
roleRepository.save(old);
InitializeConfigureData.setPrivileges(role.getRoleName(), role.getPrivilegeNames());
}
use of com.vmware.flowgate.exception.WormholeRequestException in project flowgate by vmware.
the class AuthController method getToken.
@RequestMapping(value = "/token", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
public AuthToken getToken(@RequestBody(required = false) WormholeUser user, @RequestHeader(name = "serviceKey", required = false) String serviceKey, HttpServletRequest request, HttpServletResponse response) {
AuthToken access_token = null;
if (user == null && serviceKey == null) {
throw new WormholeRequestException(HttpStatus.UNAUTHORIZED, "Invalid username or password", null);
}
if (user != null) {
access_token = accessTokenService.createToken(user);
} else {
if (InitializeConfigureData.checkServiceKey(serviceKey) || accessTokenService.validateServiceKey(serviceKey)) {
List<String> roleNames = new ArrayList<String>();
roleNames.add(FlowgateConstant.Role_admin);
AuthorityUtil util = new AuthorityUtil();
WormholeUserDetails userDetails = new WormholeUserDetails(FlowgateConstant.systemUser, FlowgateConstant.systemUser, FlowgateConstant.systemUser, util.createGrantedAuthorities(roleNames));
access_token = jwtTokenUtil.generate(userDetails);
} else {
throw new WormholeRequestException(HttpStatus.UNAUTHORIZED, "Invalid username or password", null);
}
}
Cookie cookie = new Cookie(JwtTokenUtil.Token_Name, access_token.getAccess_token());
cookie.setHttpOnly(true);
cookie.setPath("/");
cookie.setDomain(request.getServerName());
cookie.setMaxAge(expiration);
response.addCookie(cookie);
return access_token;
}
use of com.vmware.flowgate.exception.WormholeRequestException in project flowgate by vmware.
the class AuthController method readUser.
// Read a user
@RequestMapping(value = "/user/{id}", method = RequestMethod.GET)
public WormholeUser readUser(@PathVariable(required = false) String id, HttpServletRequest request) {
WormholeUserDetails userDetail = accessTokenService.getCurrentUser(request);
Optional<WormholeUser> currentUserOptional = userRepository.findById(userDetail.getUserId());
WormholeUser user = null;
WormholeUser currentUser = currentUserOptional.get();
if (currentUser.getId().equals(id)) {
user = currentUser;
} else if (currentUser.getRoleNames().contains(FlowgateConstant.Role_admin)) {
Optional<WormholeUser> userOptional = userRepository.findById(id);
user = userOptional.get();
} else {
throw new WormholeRequestException(HttpStatus.FORBIDDEN, "Forbidden", null);
}
if (user != null) {
return DesensitizationUserData.desensitizationUser(user);
}
return user;
}
use of com.vmware.flowgate.exception.WormholeRequestException in project flowgate by vmware.
the class FacilityAdapterController method updateAdapterType.
@RequestMapping(method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
public void updateAdapterType(@RequestBody FacilityAdapter adapter) {
FacilityAdapter equalNameAdapter = facilityAdapterRepo.findByDisplayName(adapter.getDisplayName());
if (equalNameAdapter != null && !equalNameAdapter.getId().equals(adapter.getId())) {
throw new WormholeRequestException("Adapter with dispalyName : " + adapter.getDisplayName() + " is existed");
}
List<AdapterJobCommand> commands = adapter.getCommands();
if (commands == null || commands.isEmpty()) {
throw new WormholeRequestException("The Commands field is required.");
}
FacilityAdapter oldAdapterType = facilityAdapterService.findById(adapter.getId());
oldAdapterType.setDescription(adapter.getDescription());
oldAdapterType.setDisplayName(adapter.getDisplayName());
oldAdapterType.setCommands(commands);
facilityAdapterRepo.save(adapter);
}
Aggregations