Search in sources :

Example 1 with ClientDetails

use of io.jmix.core.security.ClientDetails in project jmix by jmix-framework.

the class LoginScreen method onSuccessfulAuthentication.

protected void onSuccessfulAuthentication(Authentication authentication) {
    if (cubaProperties.isLocaleSelectVisible()) {
        ClientDetails clientDetails = (ClientDetails) authentication.getDetails();
        app.addCookie(App.COOKIE_LOCALE, clientDetails.getLocale().toLanguageTag());
    }
}
Also used : ClientDetails(io.jmix.core.security.ClientDetails)

Example 2 with ClientDetails

use of io.jmix.core.security.ClientDetails in project jmix by jmix-framework.

the class AppLoginWindow method onSuccessfulAuthentication.

protected void onSuccessfulAuthentication(Authentication authentication) {
    if (cubaProperties.isLocaleSelectVisible()) {
        ClientDetails clientDetails = (ClientDetails) authentication.getDetails();
        app.addCookie(App.COOKIE_LOCALE, clientDetails.getLocale().toLanguageTag());
    }
}
Also used : ClientDetails(io.jmix.core.security.ClientDetails)

Example 3 with ClientDetails

use of io.jmix.core.security.ClientDetails in project jmix by jmix-framework.

the class UserSessionSourceImpl method updateUserSessionFromAuthentication.

protected void updateUserSessionFromAuthentication(Authentication authentication, UserSession session) {
    UserRepository userRepository = beanFactory.getBean(UserRepository.class);
    if (authentication instanceof UsernamePasswordAuthenticationToken || authentication instanceof RememberMeAuthenticationToken) {
        session.setUser((UserDetails) authentication.getPrincipal());
        if (authentication.getDetails() instanceof ClientDetails) {
            ClientDetails clientDetails = (ClientDetails) authentication.getDetails();
            session.setLocale(clientDetails.getLocale());
        } else {
            session.setLocale(Locale.getDefault());
        }
    } else if (authentication instanceof AnonymousAuthenticationToken || authentication instanceof SystemAuthenticationToken) {
        Object principal = authentication.getPrincipal();
        if (principal instanceof UserDetails) {
            session.setUser((UserDetails) authentication.getPrincipal());
            session.setLocale(Locale.getDefault());
        } else {
            session.setUser(userRepository.getSystemUser());
            session.setLocale(Locale.getDefault());
        }
    } else if (authentication instanceof OAuth2Authentication) {
        Authentication userAuthentication = ((OAuth2Authentication) authentication).getUserAuthentication();
        if (userAuthentication != authentication) {
            updateUserSessionFromAuthentication(userAuthentication, session);
        }
    } else if (authentication == null) {
        // todo MG should null authentication be possible?
        // todo MG what user to return?
        session.setUser(userRepository.getSystemUser());
        session.setLocale(Locale.getDefault());
    } else {
        throw new RuntimeException("Authentication type is not supported: " + authentication.getClass().getCanonicalName());
    }
}
Also used : UserRepository(io.jmix.core.security.UserRepository) ClientDetails(io.jmix.core.security.ClientDetails) UserDetails(org.springframework.security.core.userdetails.UserDetails) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) RememberMeAuthenticationToken(org.springframework.security.authentication.RememberMeAuthenticationToken) SystemAuthenticationToken(io.jmix.core.security.SystemAuthenticationToken) AnonymousAuthenticationToken(org.springframework.security.authentication.AnonymousAuthenticationToken)

Example 4 with ClientDetails

use of io.jmix.core.security.ClientDetails in project jmix by jmix-framework.

the class UserSessionControllerManager method setSessionLocale.

// @Autowired
// protected TokenStore tokenStore;
public void setSessionLocale(HttpServletRequest request) {
    Locale locale = restLocaleUtils.extractLocaleFromRequestHeader(request);
    if (locale != null) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication instanceof OAuth2Authentication) {
            OAuth2Authentication oAuth2Authentication = (OAuth2Authentication) authentication;
            // OAuth2AccessToken accessToken = tokenStore.getAccessToken(oAuth2Authentication);
            Authentication userAuthentication = oAuth2Authentication.getUserAuthentication();
            if (userAuthentication.getDetails() instanceof ClientDetails && userAuthentication instanceof AbstractAuthenticationToken) {
                ClientDetails clientDetails = (ClientDetails) userAuthentication.getDetails();
                ((AbstractAuthenticationToken) userAuthentication).setDetails(ClientDetails.builder().of(clientDetails).locale(locale).build());
                return;
            }
        }
    }
    throw new RestAPIException("Could not change user session locale", null, HttpStatus.UNPROCESSABLE_ENTITY);
}
Also used : Locale(java.util.Locale) AbstractAuthenticationToken(org.springframework.security.authentication.AbstractAuthenticationToken) ClientDetails(io.jmix.core.security.ClientDetails) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Authentication(org.springframework.security.core.Authentication) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) RestAPIException(io.jmix.rest.exception.RestAPIException)

Example 5 with ClientDetails

use of io.jmix.core.security.ClientDetails in project jmix by jmix-framework.

the class LoginScreenSupport method createAuthenticationToken.

protected Authentication createAuthenticationToken(String username, String password, @Nullable Locale locale, @Nullable TimeZone timeZone) {
    UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(username, password);
    VaadinServletRequest request = VaadinServletRequest.getCurrent();
    ClientDetails clientDetails = ClientDetails.builder().locale(locale != null ? locale : getDefaultLocale()).scope(SecurityScope.UI).sessionId(request.getSession().getId()).timeZone(timeZone == null ? getDeviceTimeZone() : timeZone).build();
    authenticationToken.setDetails(clientDetails);
    return authenticationToken;
}
Also used : ClientDetails(io.jmix.core.security.ClientDetails) VaadinServletRequest(com.vaadin.server.VaadinServletRequest) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken)

Aggregations

ClientDetails (io.jmix.core.security.ClientDetails)6 Authentication (org.springframework.security.core.Authentication)3 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)3 Locale (java.util.Locale)2 AbstractAuthenticationToken (org.springframework.security.authentication.AbstractAuthenticationToken)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 VaadinServletRequest (com.vaadin.server.VaadinServletRequest)1 SystemAuthenticationToken (io.jmix.core.security.SystemAuthenticationToken)1 UserRepository (io.jmix.core.security.UserRepository)1 RestAPIException (io.jmix.rest.exception.RestAPIException)1 AnonymousAuthenticationToken (org.springframework.security.authentication.AnonymousAuthenticationToken)1 RememberMeAuthenticationToken (org.springframework.security.authentication.RememberMeAuthenticationToken)1 UserDetails (org.springframework.security.core.userdetails.UserDetails)1