use of com.vmware.flowgate.util.WormholeUserDetails 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.util.WormholeUserDetails 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.util.WormholeUserDetails in project flowgate by vmware.
the class FacilitySoftwareController method queryFacilitySoftwareConfigByPage.
@RequestMapping(value = "/page/{pageNumber}/pagesize/{pageSize}", method = RequestMethod.GET)
public Page<FacilitySoftwareConfig> queryFacilitySoftwareConfigByPage(@PathVariable("pageNumber") int currentPage, @PathVariable("pageSize") int pageSize, HttpServletRequest request, @RequestParam(required = false) SoftwareType[] softwaretypes) {
WormholeUserDetails user = accessTokenService.getCurrentUser(request);
if (currentPage < FlowgateConstant.defaultPageNumber) {
currentPage = FlowgateConstant.defaultPageNumber;
} else if (pageSize <= 0) {
pageSize = FlowgateConstant.defaultPageSize;
} else if (pageSize > FlowgateConstant.maxPageSize) {
pageSize = FlowgateConstant.maxPageSize;
}
PageRequest pageRequest = PageRequest.of(currentPage - 1, pageSize);
Optional<WormholeUser> currentUserOptional = userRepository.findById(user.getUserId());
WormholeUser currentUser = currentUserOptional.get();
Page<FacilitySoftwareConfig> result = null;
List<String> types = new ArrayList<String>();
if (softwaretypes != null && softwaretypes.length > 0) {
for (SoftwareType type : softwaretypes) {
types.add(type.name());
}
}
if (currentUser.getRoleNames().contains(FlowgateConstant.Role_admin)) {
if (types.isEmpty()) {
result = repository.findAll(pageRequest);
} else {
result = repository.findAllByTypeIn(types, pageRequest);
}
} else {
if (types.isEmpty()) {
result = repository.findALlByUserId(user.getUserId(), pageRequest);
} else {
result = repository.findAllByUserIdAndTypeIn(user.getUserId(), types, pageRequest);
}
}
if (result != null) {
for (FacilitySoftwareConfig facilitySoftwareConfig : result.getContent()) {
facilitySoftwareConfig.setPassword(null);
}
}
return result;
}
use of com.vmware.flowgate.util.WormholeUserDetails in project flowgate by vmware.
the class AccessTokenService method createToken.
public AuthToken createToken(WormholeUser user) {
// Perform the security
AuthToken access_token = null;
try {
String username = user.getUserName();
final Authentication authentication = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, user.getPassword()));
SecurityContextHolder.getContext().setAuthentication(authentication);
// Reload password post-security so we can generate token
WormholeUserDetails userDetails = userDetailsService.loadUserByUsername(user.getUserName());
access_token = jwtTokenUtil.generate(userDetails);
} catch (BadCredentialsException e) {
throw new WormholeRequestException(HttpStatus.UNAUTHORIZED, "Invalid username or password", e.getCause());
}
return access_token;
}
use of com.vmware.flowgate.util.WormholeUserDetails in project flowgate by vmware.
the class SDDCSoftwareController method getServerConfigsByUser.
// get servers by user and type
@RequestMapping(value = "/type/{type}", method = RequestMethod.GET)
public List<SDDCSoftwareConfig> getServerConfigsByUser(@PathVariable("type") SoftwareType type, HttpServletRequest request) {
WormholeUserDetails user = accessTokenService.getCurrentUser(request);
List<SDDCSoftwareConfig> datas = sddcRepository.findAllByUserIdAndType(user.getUserId(), type.name());
for (SDDCSoftwareConfig sddcSoftwareConfig : datas) {
sddcSoftwareConfig.setPassword(null);
}
return datas;
}
Aggregations