protected Locale resolveLocale(@Nullable Locale requestLocale) { Map<String, Locale> locales = globalConfig.getAvailableLocales(); if (globalConfig.getLocaleSelectVisible()) { String lastLocale = getCookieValue(COOKIE_LOCALE); if (lastLocale != null) { for (Locale locale : locales.values()) { if (locale.toLanguageTag().equals(lastLocale)) { return locale; } } } } if (requestLocale != null) { Locale requestTrimmedLocale = messageTools.trimLocale(requestLocale); if (locales.containsValue(requestTrimmedLocale)) { return requestTrimmedLocale; } // if not found and application locale contains country, try to match by language only if (!StringUtils.isEmpty(requestLocale.getCountry())) { Locale appLocale = Locale.forLanguageTag(requestLocale.getLanguage()); for (Locale locale : locales.values()) { if (Locale.forLanguageTag(locale.getLanguage()).equals(appLocale)) { return locale; } } } } // return default locale return messageTools.getDefaultLocale(); }
protected Locale resolveLocale(HttpServletRequest req, Messages messages, GlobalConfig globalConfig) { Map<String, Locale> locales = globalConfig.getAvailableLocales(); if (globalConfig.getLocaleSelectVisible()) { String lastLocale = getLocaleFromCookie(req); if (lastLocale != null) { for (Locale locale : locales.values()) { if (locale.toLanguageTag().equals(lastLocale)) { return locale; } } } } Locale requestLocale = req.getLocale(); if (requestLocale != null) { Locale requestTrimmedLocale = messages.getTools().trimLocale(requestLocale); if (locales.containsValue(requestTrimmedLocale)) { return requestTrimmedLocale; } // if not found and application locale contains country, try to match by language only if (!StringUtils.isEmpty(requestLocale.getCountry())) { Locale appLocale = Locale.forLanguageTag(requestLocale.getLanguage()); for (Locale locale : locales.values()) { if (Locale.forLanguageTag(locale.getLanguage()).equals(appLocale)) { return locale; } } } } return messages.getTools().getDefaultLocale(); }
protected Locale getCurrentLocale() { Locale locale; if (userSessionSource.checkCurrentUserSession()) { locale = userSessionSource.getLocale(); } else { locale = messages.getTools().getDefaultLocale(); } return locale; } }
@Override protected Locale getUserLocale() { return userSessionSource.checkCurrentUserSession() ? userSessionSource.getUserSession().getLocale() : messageTools.getDefaultLocale(); }
@Override protected Locale getUserLocale() { return userSessionSource.checkCurrentUserSession() ? userSessionSource.getUserSession().getLocale() : messageTools.getDefaultLocale(); }
protected Locale getUserLocale() { return userSessionSource.checkCurrentUserSession() ? userSessionSource.getUserSession().getLocale() : messages.getTools().getDefaultLocale(); } }
@MetaProperty public String getCaption() { Locale defaultLocale = AppBeans.get(MessageTools.class).getDefaultLocale(); String formattedDeploymentDate = Datatypes.getNN(Date.class).format(deploymentDate, defaultLocale); return this.name + " (" + this.code + " - " + formattedDeploymentDate + ")"; } }
protected Locale getUserLocale(LocalizedCredentials credentials, User user) { Locale userLocale = null; if (credentials.isOverrideLocale()) { userLocale = credentials.getLocale(); } if (userLocale == null) { if (user.getLanguage() != null) { userLocale = LocaleUtils.toLocale(user.getLanguage()); } else { userLocale = messages.getTools().trimLocale(messages.getTools().getDefaultLocale()); } } return userLocale; }
protected void checkTrustedClientCredentials(String trustedClientPassword) throws LoginException { RemoteClientInfo remoteClientInfo = RemoteClientInfo.get(); if (remoteClientInfo != null && remoteClientInfo.getAddress() != null) { // reject request from not permitted client ip if (!trustedLoginHandler.checkAddress(remoteClientInfo.getAddress())) { log.warn("Attempt trusted access from not permitted IP address: {}", remoteClientInfo.getAddress()); throw new LoginException("Trusted access denied"); } } if (!trustedLoginHandler.checkPassword(trustedClientPassword)) { throw new LoginException(getInvalidCredentialsMessage(serverConfig.getJmxUserLogin(), messages.getTools().getDefaultLocale())); } } }
@Override public void check(Credentials credentials, AuthenticationDetails authenticationDetails) throws LoginException { if (credentials instanceof AbstractClientCredentials) { AbstractClientCredentials clientCredentials = (AbstractClientCredentials) credentials; if (clientCredentials.isCheckClientPermissions()) { ClientType clientType = clientCredentials.getClientType(); if (ClientType.DESKTOP == clientType || ClientType.WEB == clientType) { if (!authenticationDetails.getSession().isSpecificPermitted("cuba.gui.loginToClient")) { log.warn("Attempt of login to {} for user '{}' without cuba.gui.loginToClient permission", clientType, clientCredentials); Locale userLocale; if (clientCredentials.getLocale() != null) { userLocale = clientCredentials.getLocale(); } else { userLocale = messages.getTools().getDefaultLocale(); } throw new LoginException(getInvalidCredentialsMessage(clientCredentials.getUserIdentifier(), userLocale)); } } } } }
/** * Called when <em>the first</em> UI of the session is initialized. */ protected void init(Locale requestLocale) { VaadinSession vSession = VaadinSession.getCurrent(); vSession.setAttribute(App.class, this); vSession.setLocale(messageTools.getDefaultLocale()); // set root error handler for all session vSession.setErrorHandler(event -> { try { getExceptionHandlers().handle(event); getAppLog().log(event); } catch (Throwable e) { log.error("Error handling exception\nOriginal exception:\n{}\nException in handlers:\n{}", ExceptionUtils.getStackTrace(event.getThrowable()), ExceptionUtils.getStackTrace(e) ); } }); log.debug("Initializing application"); appLog = new AppLog(webConfig.getAppLogMaxItemsCount(), beanLocator.get(TimeSource.NAME)); connection = createConnection(); exceptionHandlers = new ExceptionHandlers(this, beanLocator); cookies = new AppCookies(); themeConstants = loadTheme(); // get default locale from config Locale targetLocale = resolveLocale(requestLocale); setLocale(targetLocale); }
protected UserSession loginAsAnonymous() { String login = portalConfig.getAnonymousUserLogin(); String password = portalConfig.getTrustedClientPassword(); UserSession userSession; try { String portalLocationString = getPortalNetworkLocation(); String portalClientInfo = "Portal Anonymous Session"; if (StringUtils.isNotBlank(portalLocationString)) { portalClientInfo += " (" + portalLocationString + ")"; } TrustedClientCredentials credentials = new TrustedClientCredentials(login, password, messagesTools.getDefaultLocale()); credentials.setClientType(ClientType.PORTAL); credentials.setClientInfo(portalClientInfo); credentials.setParams(ParamsMap.of( ClientType.class.getName(), AppContext.getProperty("cuba.clientType"), SessionParams.CLIENT_INFO.getId(), portalClientInfo )); userSession = authenticationService.login(credentials).getSession(); } catch (LoginException e) { throw new NoMiddlewareConnectionException("Unable to login as anonymous portal user", e); } catch (Exception e) { throw new NoMiddlewareConnectionException("Unable to connect to middleware services", e); } return userSession; }
@Override public AuthenticationDetails authenticate(Credentials credentials) throws LoginException { SystemUserCredentials systemLogin = (SystemUserCredentials) credentials; String login = systemLogin.getLogin(); Locale credentialsLocale = systemLogin.getLocale() == null ? messages.getTools().getDefaultLocale() : systemLogin.getLocale(); User user = loadUser(login); if (user == null) { throw new LoginException(getInvalidCredentialsMessage(login, credentialsLocale)); } Locale userLocale = getUserLocale(systemLogin, user); UserSession session = userSessionManager.createSession(user, userLocale, true); return new SimpleAuthenticationDetails(session); }
@Override public UserSession getSystemSession(String trustedClientPassword) throws LoginException { if (!trustedLoginHandler.checkPassword(trustedClientPassword)) { Locale locale = messages.getTools().getDefaultLocale(); throw new LoginException(messages.formatMessage(MSG_PACK, "LoginException.InvalidLoginOrPassword", locale, serverConfig.getJmxUserLogin())); } SecurityContext currentSecContext = AppContext.getSecurityContext(); UserSession userSession; try { // we need to reset security context to prevent reusing current session AppContext.setSecurityContext(null); userSession = authentication.begin(); authentication.end(); } finally { AppContext.setSecurityContext(currentSecContext); } return userSession; }
protected AuthenticationDetails loginClient(LoginPasswordCredentials credentials) { String login = credentials.getLogin(); Locale credentialsLocale = credentials.getLocale() == null ? messages.getTools().getDefaultLocale() : credentials.getLocale(); if (Strings.isNullOrEmpty(login)) { // empty login is not valid throw new LoginException(getInvalidCredentialsMessage(login, credentialsLocale)); } UserSession systemSession = trustedClientService.getSystemSession(restApiConfig.getTrustedClientPassword()); User user = AppContext.withSecurityContext(new SecurityContext(systemSession), () -> usersRepository.findUserByLogin(login)); if (user == null) { throw new LoginException(getInvalidCredentialsMessage(login, credentialsLocale)); } if (!passwordEncryption.checkPassword(user, credentials.getPassword())) { throw new LoginException(getInvalidCredentialsMessage(login, credentialsLocale)); } return authenticationService.login(createTrustedCredentials(credentials)); }
protected AuthenticationDetails loginClient(LoginPasswordCredentials credentials) { String login = credentials.getLogin(); Locale credentialsLocale = credentials.getLocale() == null ? messages.getTools().getDefaultLocale() : credentials.getLocale(); if (Strings.isNullOrEmpty(login)) { // empty login is not valid throw new LoginException(getInvalidCredentialsMessage(login, credentialsLocale)); } UserSession systemSession = trustedClientService.getSystemSession(portalConfig.getTrustedClientPassword()); User user = AppContext.withSecurityContext(new SecurityContext(systemSession), () -> usersRepository.findUserByLogin(login)); if (user == null) { throw new LoginException(getInvalidCredentialsMessage(login, credentialsLocale)); } if (!passwordEncryption.checkPassword(user, credentials.getPassword())) { throw new LoginException(getInvalidCredentialsMessage(login, credentialsLocale)); } return authenticationService.login(createTrustedCredentials(credentials)); }
protected AuthenticationDetails loginClient(LoginPasswordCredentials credentials) { String login = credentials.getLogin(); Locale credentialsLocale = credentials.getLocale() == null ? messages.getTools().getDefaultLocale() : credentials.getLocale(); if (Strings.isNullOrEmpty(login)) { // empty login is not valid throw new LoginException(getInvalidCredentialsMessage(login, credentialsLocale)); } UserSession systemSession = trustedClientService.getSystemSession(webAuthConfig.getTrustedClientPassword()); User user = AppContext.withSecurityContext(new SecurityContext(systemSession), () -> usersRepository.findUserByLogin(login)); if (user == null) { throw new LoginException(getInvalidCredentialsMessage(login, credentialsLocale)); } if (!passwordEncryption.checkPassword(user, credentials.getPassword())) { throw new LoginException(getInvalidCredentialsMessage(login, credentialsLocale)); } return authenticationService.login(createTrustedCredentials(credentials)); }
@Override public AuthenticationDetails authenticate(Credentials credentials) throws LoginException { AnonymousUserCredentials anonymous = (AnonymousUserCredentials) credentials; String login = serverConfig.getAnonymousLogin(); Locale credentialsLocale = anonymous.getLocale() == null ? messages.getTools().trimLocale(messages.getTools().getDefaultLocale()) : anonymous.getLocale(); User user = loadUser(login); if (user == null) { throw new LoginException(getInvalidCredentialsMessage(login, credentialsLocale)); } Locale userLocale = getUserLocale(anonymous, user); UUID anonymousSessionId = globalConfig.getAnonymousSessionId(); UserSession session = createSession(anonymous, user, userLocale, anonymousSessionId); session.setClientInfo("System anonymous session"); return new SimpleAuthenticationDetails(session); }
@Override public AuthenticationDetails authenticate(Credentials credentials) throws LoginException { LoginPasswordCredentials loginAndPassword = (LoginPasswordCredentials) credentials; String login = loginAndPassword.getLogin(); Locale credentialsLocale = loginAndPassword.getLocale() == null ? messages.getTools().getDefaultLocale() : loginAndPassword.getLocale(); if (Strings.isNullOrEmpty(login)) { // empty login is not valid throw new LoginException(getInvalidCredentialsMessage(login, credentialsLocale)); } checkUserCredentials(credentials); User user = loadUser(login); if (user == null) { throw new LoginException(getInvalidCredentialsMessage(login, credentialsLocale)); } if (!passwordEncryption.checkPassword(user, loginAndPassword.getPassword())) { throw new LoginException(getInvalidCredentialsMessage(login, credentialsLocale)); } Locale userLocale = getUserLocale(loginAndPassword, user); UserSession session = createSession(loginAndPassword, user, userLocale); setClientSessionParams(loginAndPassword, session); AuthenticationDetails authenticationDetails = new SimpleAuthenticationDetails(session); checkUserAccess(loginAndPassword, authenticationDetails); return authenticationDetails; }
@Override public AuthenticationDetails authenticate(Credentials credentials) throws LoginException { RememberMeCredentials rememberMe = (RememberMeCredentials) credentials; String login = rememberMe.getLogin(); Locale credentialsLocale = rememberMe.getLocale() == null ? messages.getTools().getDefaultLocale() : rememberMe.getLocale(); if (Strings.isNullOrEmpty(login)) { // empty login is not valid throw new LoginException(getInvalidCredentialsMessage(login, credentialsLocale)); } checkUserCredentials(credentials); User user = loadUser(login); if (user == null) { throw new LoginException(getInvalidCredentialsMessage(login, credentialsLocale)); } RememberMeToken loginToken = loadRememberMeToken(user, rememberMe.getRememberMeToken()); if (loginToken == null) { throw new LoginException(getInvalidCredentialsMessage(login, credentialsLocale)); } Locale userLocale = getUserLocale(rememberMe, user); UserSession session = createSession(rememberMe, user, userLocale); setClientSessionParams(rememberMe, session); AuthenticationDetails authenticationDetails = new SimpleAuthenticationDetails(session); checkUserAccess(rememberMe, authenticationDetails); return authenticationDetails; }