Search in sources :

Example 6 with WormholeUserDetails

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;
}
Also used : WormholeRequestException(com.vmware.flowgate.exception.WormholeRequestException) Cookie(javax.servlet.http.Cookie) WormholeUserDetails(com.vmware.flowgate.util.WormholeUserDetails) AuthorityUtil(com.vmware.flowgate.util.AuthorityUtil) ArrayList(java.util.ArrayList) AuthToken(com.vmware.flowgate.common.model.AuthToken) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with WormholeUserDetails

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;
}
Also used : WormholeRequestException(com.vmware.flowgate.exception.WormholeRequestException) WormholeUserDetails(com.vmware.flowgate.util.WormholeUserDetails) Optional(java.util.Optional) WormholeUser(com.vmware.flowgate.common.model.WormholeUser) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 8 with WormholeUserDetails

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;
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) WormholeUserDetails(com.vmware.flowgate.util.WormholeUserDetails) ArrayList(java.util.ArrayList) FacilitySoftwareConfig(com.vmware.flowgate.common.model.FacilitySoftwareConfig) SoftwareType(com.vmware.flowgate.common.model.FacilitySoftwareConfig.SoftwareType) WormholeUser(com.vmware.flowgate.common.model.WormholeUser) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 9 with WormholeUserDetails

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;
}
Also used : WormholeRequestException(com.vmware.flowgate.exception.WormholeRequestException) WormholeUserDetails(com.vmware.flowgate.util.WormholeUserDetails) Authentication(org.springframework.security.core.Authentication) AuthToken(com.vmware.flowgate.common.model.AuthToken) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException)

Example 10 with WormholeUserDetails

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;
}
Also used : SDDCSoftwareConfig(com.vmware.flowgate.common.model.SDDCSoftwareConfig) WormholeUserDetails(com.vmware.flowgate.util.WormholeUserDetails) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

WormholeUserDetails (com.vmware.flowgate.util.WormholeUserDetails)29 WormholeUser (com.vmware.flowgate.common.model.WormholeUser)18 ArrayList (java.util.ArrayList)14 Test (org.junit.Test)12 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)12 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)10 WormholeRequestException (com.vmware.flowgate.exception.WormholeRequestException)9 FacilitySoftwareConfig (com.vmware.flowgate.common.model.FacilitySoftwareConfig)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 AuthToken (com.vmware.flowgate.common.model.AuthToken)3 SDDCSoftwareConfig (com.vmware.flowgate.common.model.SDDCSoftwareConfig)3 AuthorityUtil (com.vmware.flowgate.util.AuthorityUtil)3 IOException (java.io.IOException)3 MvcResult (org.springframework.test.web.servlet.MvcResult)3 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)3 DecodedJWT (com.auth0.jwt.interfaces.DecodedJWT)2 IntegrationStatus (com.vmware.flowgate.common.model.IntegrationStatus)2 Optional (java.util.Optional)2 Cookie (javax.servlet.http.Cookie)2 PageRequest (org.springframework.data.domain.PageRequest)2