Search in sources :

Example 1 with UserInfo

use of org.usermanagement.model.UserInfo in project open-kilda by telstra.

the class BaseController method getLoggedInUser.

/**
 * Return logged in user information.
 *
 * @param request HttpServletRequest to retrieve logged in user information.
 * @return logged in user information.
 */
protected UserInfo getLoggedInUser(final HttpServletRequest request) {
    HttpSession session = request.getSession();
    UserInfo userInfo = null;
    try {
        userInfo = (UserInfo) session.getAttribute(IConstants.SESSION_OBJECT);
    } catch (IllegalStateException ex) {
        LOGGER.warn("Exception while retrieving user information from session. Exception: " + ex.getLocalizedMessage(), ex);
    } finally {
        if (userInfo == null) {
            session = request.getSession(false);
            userInfo = new UserInfo();
            session.setAttribute(IConstants.SESSION_OBJECT, userInfo);
        }
    }
    return userInfo;
}
Also used : HttpSession(javax.servlet.http.HttpSession) UserInfo(org.usermanagement.model.UserInfo)

Example 2 with UserInfo

use of org.usermanagement.model.UserInfo in project open-kilda by telstra.

the class LoginController method authenticate.

/**
 * Authenticate.
 *
 * @param username the username
 * @param password the password
 * @param request the request
 * @return the model and view
 */
@RequestMapping(value = "/authenticate", method = RequestMethod.POST)
public ModelAndView authenticate(@RequestParam("username") String username, @RequestParam("password") final String password, final HttpServletRequest request, RedirectAttributes redir) {
    ModelAndView modelAndView = new ModelAndView(IConstants.View.LOGIN);
    String error = null;
    username = username != null ? username.toLowerCase() : null;
    UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(username, password);
    CustomWebAuthenticationDetails customWebAuthenticationDetails = new CustomWebAuthenticationDetails(request);
    token.setDetails(customWebAuthenticationDetails);
    try {
        HttpSession sessionOld = request.getSession(false);
        if (sessionOld != null && !sessionOld.isNew()) {
            sessionOld.invalidate();
        }
        Authentication authenticate = authenticationManager.authenticate(token);
        if (authenticate.isAuthenticated()) {
            modelAndView.setViewName(IConstants.View.REDIRECT_HOME);
            UserInfo userInfo = getLoggedInUser(request);
            userService.populateUserInfo(userInfo, username);
            request.getSession(true).setAttribute(IConstants.SESSION_OBJECT, userInfo);
            SecurityContextHolder.getContext().setAuthentication(authenticate);
            userService.updateLoginDetail(username);
        } else {
            error = "Login failed; Invalid email or password.";
            LOGGER.warn("Authentication failure for user: '" + username + "'");
            modelAndView.setViewName(IConstants.View.REDIRECT_LOGIN);
        }
    } catch (TwoFaKeyNotSetException e) {
        LOGGER.warn("2 FA Key not set for user: '" + username + "'");
        modelAndView.addObject("username", username);
        modelAndView.addObject("password", password);
        String secretKey = TwoFactorUtility.getBase32EncryptedKey();
        modelAndView.addObject("key", secretKey);
        userService.updateUser2FaKey(username, secretKey);
        modelAndView.addObject("applicationName", applicationName);
        modelAndView.setViewName(IConstants.View.TWO_FA_GENERATOR);
    } catch (OtpRequiredException e) {
        LOGGER.warn("OTP required for user: '" + username + "'");
        modelAndView.addObject("username", username);
        modelAndView.addObject("password", password);
        modelAndView.addObject("applicationName", applicationName);
        modelAndView.setViewName(IConstants.View.OTP);
    } catch (InvalidOtpException e) {
        LOGGER.warn("Authentication code is invalid for user: '" + username + "'");
        error = "Authentication code is invalid";
        modelAndView.addObject("username", username);
        modelAndView.addObject("password", password);
        modelAndView.addObject("applicationName", applicationName);
        if (customWebAuthenticationDetails.isConfigure2Fa()) {
            UserEntity userInfo = userService.getUserByUsername(username);
            modelAndView.addObject("key", userInfo.getTwoFaKey());
            modelAndView.setViewName(IConstants.View.TWO_FA_GENERATOR);
        } else {
            modelAndView.setViewName(IConstants.View.OTP);
        }
    } catch (BadCredentialsException e) {
        LOGGER.warn("Authentication failure", e);
        error = e.getMessage();
        modelAndView.setViewName(IConstants.View.REDIRECT_LOGIN);
    } catch (LockedException e) {
        error = e.getMessage();
        modelAndView.setViewName(IConstants.View.REDIRECT_LOGIN);
    } catch (Exception e) {
        LOGGER.warn("Authentication failure", e);
        error = "Login Failed. Error: " + e.getMessage() + ".";
        modelAndView.setViewName(IConstants.View.REDIRECT_LOGIN);
    }
    if (error != null) {
        redir.addFlashAttribute("error", error);
    }
    return modelAndView;
}
Also used : LockedException(org.springframework.security.authentication.LockedException) CustomWebAuthenticationDetails(org.openkilda.security.CustomWebAuthenticationDetails) HttpSession(javax.servlet.http.HttpSession) ModelAndView(org.springframework.web.servlet.ModelAndView) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) UserInfo(org.usermanagement.model.UserInfo) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) TwoFaKeyNotSetException(org.openkilda.exception.TwoFaKeyNotSetException) OtpRequiredException(org.openkilda.exception.OtpRequiredException) UserEntity(org.usermanagement.dao.entity.UserEntity) OtpRequiredException(org.openkilda.exception.OtpRequiredException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) LockedException(org.springframework.security.authentication.LockedException) InvalidOtpException(org.openkilda.exception.InvalidOtpException) TwoFaKeyNotSetException(org.openkilda.exception.TwoFaKeyNotSetException) Authentication(org.springframework.security.core.Authentication) InvalidOtpException(org.openkilda.exception.InvalidOtpException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with UserInfo

use of org.usermanagement.model.UserInfo in project open-kilda by telstra.

the class FlowService method getAllFlows.

/**
 * get All Flows.
 *
 * @return SwitchRelationData
 */
public List<FlowInfo> getAllFlows(List<String> statuses, boolean controller) {
    List<FlowInfo> flows = new ArrayList<FlowInfo>();
    if (!CollectionUtil.isEmpty(statuses)) {
        statuses = statuses.stream().map((status) -> status.toLowerCase()).collect(Collectors.toList());
    }
    if (CollectionUtil.isEmpty(statuses) || statuses.contains("active")) {
        flows = flowsIntegrationService.getFlows();
        if (flows == null) {
            flows = new ArrayList<FlowInfo>();
        }
    }
    if (!controller) {
        if (storeService.getLinkStoreConfig().getUrls().size() > 0) {
            try {
                UserInfo userInfo = userService.getLoggedInUserInfo();
                if (userInfo.getPermissions().contains(IConstants.Permission.FW_FLOW_INVENTORY)) {
                    List<InventoryFlow> inventoryFlows = new ArrayList<InventoryFlow>();
                    String status = "";
                    for (String statusObj : statuses) {
                        if (StringUtil.isNullOrEmpty(status)) {
                            status += statusObj;
                        } else {
                            status += "," + statusObj;
                        }
                    }
                    inventoryFlows = flowStoreService.getFlowsWithParams(status);
                    processInventoryFlow(flows, inventoryFlows);
                }
            } catch (Exception ex) {
                LOGGER.error("Error occurred while retrieving flows from store", ex);
            }
        }
    }
    return flows;
}
Also used : FlowInfo(org.openkilda.model.FlowInfo) InventoryFlow(org.openkilda.integration.source.store.dto.InventoryFlow) ArrayList(java.util.ArrayList) UserInfo(org.usermanagement.model.UserInfo) InvalidResponseException(org.openkilda.integration.exception.InvalidResponseException) IntegrationException(org.openkilda.integration.exception.IntegrationException) RequestValidationException(org.usermanagement.exception.RequestValidationException) AccessDeniedException(java.nio.file.AccessDeniedException)

Example 4 with UserInfo

use of org.usermanagement.model.UserInfo in project open-kilda by telstra.

the class ContractService method getContracts.

/**
 * get contracts.
 *
 * @param linkId
 *            the link id
 * @throws AccessDeniedException the access denied exception
 */
public List<Contract> getContracts(String linkId) throws AccessDeniedException {
    LOGGER.info("Inside ContractService method getContracts");
    UserInfo userInfo = userService.getLoggedInUserInfo();
    if (userInfo.getPermissions().contains(IConstants.Permission.FW_FLOW_INVENTORY)) {
        if (userInfo.getPermissions().contains(IConstants.Permission.FW_FLOW_CONTRACT)) {
            List<Contract> contracts = flowStoreService.getContracts(linkId);
            return contracts;
        }
    }
    return null;
}
Also used : UserInfo(org.usermanagement.model.UserInfo) Contract(org.openkilda.integration.source.store.dto.Contract)

Example 5 with UserInfo

use of org.usermanagement.model.UserInfo in project open-kilda by telstra.

the class SamlController method samlAuthenticate.

/**
 * Saml Authenticate.
 *
 * @param request the request
 * @return the model and view
 */
@RequestMapping(value = "/authenticate")
public ModelAndView samlAuthenticate(final HttpServletRequest request, RedirectAttributes redir) {
    ModelAndView modelAndView = null;
    String error = null;
    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    if (null != authentication) {
        boolean isValid = (authentication.isAuthenticated() && !(authentication instanceof AnonymousAuthenticationToken));
        if (isValid) {
            SAMLCredential saml = (SAMLCredential) authentication.getCredentials();
            SamlConfig samlConfig = samlService.getConfigByEntityId(saml.getRemoteEntityID());
            NameID nameId = (NameID) authentication.getPrincipal();
            String username = nameId.getValue();
            UserInfo userInfo = userService.getUserInfoByUsername(username);
            if (userInfo != null && userInfo.getStatus().equalsIgnoreCase(Status.ACTIVE.name())) {
                userService.populateUserInfo(userInfo, username);
                request.getSession().setAttribute(IConstants.SESSION_OBJECT, userInfo);
                userService.updateLoginDetail(username);
                modelAndView = new ModelAndView(IConstants.View.REDIRECT_HOME);
            } else if (userInfo != null && userInfo.getStatus().equalsIgnoreCase(Status.INACTIVE.name())) {
                error = messageUtil.getAttributeUserInactive();
                request.getSession(false);
                modelAndView = new ModelAndView(IConstants.View.REDIRECT_LOGIN);
            } else if (userInfo == null && samlConfig.isUserCreation()) {
                Set<RoleEntity> roleEntities = roleService.getRoleByIds(samlConfig.getRoles());
                userService.createSamlUser(nameId.getValue(), roleEntities);
                UserInfo userInfo1 = getLoggedInUser(request);
                userService.populateUserInfo(userInfo1, username);
                userService.updateLoginDetail(username);
                modelAndView = new ModelAndView(IConstants.View.REDIRECT_HOME);
            } else {
                error = messageUtil.getAttributeUserDoesNotExist();
                LOGGER.warn("User is not logged in, redirected to login page. Requested view name: ");
                request.getSession(false);
                modelAndView = new ModelAndView(IConstants.View.REDIRECT_LOGIN);
            }
        }
    } else {
        error = messageUtil.getAttributeAuthenticationFailure();
        LOGGER.warn("User is not logged in, redirected to login page. Requested view name: ");
        modelAndView = new ModelAndView(IConstants.View.LOGIN);
    }
    if (error != null) {
        redir.addFlashAttribute("error", error);
    }
    return modelAndView;
}
Also used : RoleEntity(org.usermanagement.dao.entity.RoleEntity) SAMLCredential(org.springframework.security.saml.SAMLCredential) NameID(org.opensaml.saml2.core.NameID) Authentication(org.springframework.security.core.Authentication) ModelAndView(org.springframework.web.servlet.ModelAndView) UserInfo(org.usermanagement.model.UserInfo) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken) SamlConfig(org.openkilda.saml.model.SamlConfig) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

UserInfo (org.usermanagement.model.UserInfo)17 AccessDeniedException (java.nio.file.AccessDeniedException)6 RequestValidationException (org.usermanagement.exception.RequestValidationException)6 IntegrationException (org.openkilda.integration.exception.IntegrationException)4 InvalidResponseException (org.openkilda.integration.exception.InvalidResponseException)4 UserEntity (org.usermanagement.dao.entity.UserEntity)4 ArrayList (java.util.ArrayList)3 HttpSession (javax.servlet.http.HttpSession)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ModelAndView (org.springframework.web.servlet.ModelAndView)3 RoleEntity (org.usermanagement.dao.entity.RoleEntity)3 RequestContext (org.openkilda.auth.model.RequestContext)2 InvalidOtpException (org.openkilda.exception.InvalidOtpException)2 OtpRequiredException (org.openkilda.exception.OtpRequiredException)2 TwoFaKeyNotSetException (org.openkilda.exception.TwoFaKeyNotSetException)2 StoreIntegrationException (org.openkilda.integration.exception.StoreIntegrationException)2 InventoryFlow (org.openkilda.integration.source.store.dto.InventoryFlow)2 InventorySwitch (org.openkilda.integration.source.store.dto.InventorySwitch)2 FlowInfo (org.openkilda.model.FlowInfo)2 SwitchInfo (org.openkilda.model.SwitchInfo)2