Search in sources :

Example 6 with WormholeRequestException

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;
}
Also used : WormholeRequestException(com.vmware.flowgate.exception.WormholeRequestException) Cookie(javax.servlet.http.Cookie) AuthToken(com.vmware.flowgate.common.model.AuthToken) DecodedJWT(com.auth0.jwt.interfaces.DecodedJWT) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with WormholeRequestException

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

Example 8 with WormholeRequestException

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;
}
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 9 with WormholeRequestException

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;
}
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 10 with WormholeRequestException

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

Aggregations

WormholeRequestException (com.vmware.flowgate.exception.WormholeRequestException)27 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)21 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)14 WormholeUserDetails (com.vmware.flowgate.util.WormholeUserDetails)9 IOException (java.io.IOException)9 SDDCSoftwareConfig (com.vmware.flowgate.common.model.SDDCSoftwareConfig)4 EventMessage (com.vmware.flowgate.common.model.redis.message.EventMessage)4 AuthToken (com.vmware.flowgate.common.model.AuthToken)3 FacilityAdapter (com.vmware.flowgate.common.model.FacilityAdapter)3 WormholeUser (com.vmware.flowgate.common.model.WormholeUser)3 WormholeException (com.vmware.flowgate.common.exception.WormholeException)2 AdapterJobCommand (com.vmware.flowgate.common.model.AdapterJobCommand)2 Asset (com.vmware.flowgate.common.model.Asset)2 IntegrationStatus (com.vmware.flowgate.common.model.IntegrationStatus)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 Date (java.util.Date)2 NoSuchElementException (java.util.NoSuchElementException)2 Optional (java.util.Optional)2 Cookie (javax.servlet.http.Cookie)2