Search in sources :

Example 11 with AuthenticationSuccessEvent

use of org.springframework.security.authentication.event.AuthenticationSuccessEvent in project pentaho-platform by pentaho.

the class BackingRepositoryLifecycleManagerAuthenticationSuccessListener method onApplicationEvent.

// ~ Methods
// =========================================================================================================
public void onApplicationEvent(final ApplicationEvent event) {
    if (event instanceof AuthenticationSuccessEvent || event instanceof InteractiveAuthenticationSuccessEvent) {
        // $NON-NLS-1$
        logger.debug("received AbstractAuthenticationEvent");
        // Get the lifecycle manager for this event
        final IBackingRepositoryLifecycleManager lifecycleManager = getLifecycleManager();
        // Execute new tenant with the tenant id from the logged in user
        final AbstractAuthenticationEvent aEvent = (AbstractAuthenticationEvent) event;
        final String principalName = aEvent.getAuthentication().getName();
        try {
            getSecurityHelper().runAsSystem(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    // Execute new tenant with the tenant id from the logged in user
                    lifecycleManager.newTenant(JcrTenantUtils.getTenant(principalName, true));
                    return null;
                }
            });
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage(), e);
        }
        try {
            getSecurityHelper().runAsSystem(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    // Execute new tenant with the tenant id from the logged in user
                    lifecycleManager.newUser(JcrTenantUtils.getTenant(principalName, true), JcrTenantUtils.getPrincipalName(principalName, true));
                    return null;
                }
            });
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage(), e);
        }
        try {
            // The newTenant() call should be executed as the system (or more correctly the tenantAdmin)
            getSecurityHelper().runAsSystem(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    lifecycleManager.newTenant();
                    return null;
                }
            });
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage(), e);
        }
        try {
            // run as user to populate SecurityContextHolder and PentahoSessionHolder since Spring Security events are
            // fired
            // before SecurityContextHolder is set
            getSecurityHelper().runAsUser(principalName, new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    lifecycleManager.newUser();
                    return null;
                }
            });
        } catch (Exception e) {
            logger.error(e.getLocalizedMessage(), e);
        }
        logger.info("The user \"" + principalName + "\"" + " connected to repository");
    }
}
Also used : InteractiveAuthenticationSuccessEvent(org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent) InteractiveAuthenticationSuccessEvent(org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent) AuthenticationSuccessEvent(org.springframework.security.authentication.event.AuthenticationSuccessEvent) AbstractAuthenticationEvent(org.springframework.security.authentication.event.AbstractAuthenticationEvent) IBackingRepositoryLifecycleManager(org.pentaho.platform.api.repository2.unified.IBackingRepositoryLifecycleManager)

Example 12 with AuthenticationSuccessEvent

use of org.springframework.security.authentication.event.AuthenticationSuccessEvent in project spring-security-oauth by spring-projects.

the class OAuth2ClientAuthenticationProcessingFilter method attemptAuthentication.

@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
    OAuth2AccessToken accessToken;
    try {
        accessToken = restTemplate.getAccessToken();
    } catch (OAuth2Exception e) {
        BadCredentialsException bad = new BadCredentialsException("Could not obtain access token", e);
        publish(new OAuth2AuthenticationFailureEvent(bad));
        throw bad;
    }
    try {
        OAuth2Authentication result = tokenServices.loadAuthentication(accessToken.getValue());
        if (authenticationDetailsSource != null) {
            request.setAttribute(OAuth2AuthenticationDetails.ACCESS_TOKEN_VALUE, accessToken.getValue());
            request.setAttribute(OAuth2AuthenticationDetails.ACCESS_TOKEN_TYPE, accessToken.getTokenType());
            result.setDetails(authenticationDetailsSource.buildDetails(request));
        }
        publish(new AuthenticationSuccessEvent(result));
        return result;
    } catch (InvalidTokenException e) {
        BadCredentialsException bad = new BadCredentialsException("Could not obtain user details from token", e);
        publish(new OAuth2AuthenticationFailureEvent(bad));
        throw bad;
    }
}
Also used : InvalidTokenException(org.springframework.security.oauth2.common.exceptions.InvalidTokenException) OAuth2AccessToken(org.springframework.security.oauth2.common.OAuth2AccessToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) AuthenticationSuccessEvent(org.springframework.security.authentication.event.AuthenticationSuccessEvent) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) OAuth2Exception(org.springframework.security.oauth2.common.exceptions.OAuth2Exception)

Aggregations

AuthenticationSuccessEvent (org.springframework.security.authentication.event.AuthenticationSuccessEvent)12 Date (java.util.Date)3 AbstractAuthenticationFailureEvent (org.springframework.security.authentication.event.AbstractAuthenticationFailureEvent)3 InteractiveAuthenticationSuccessEvent (org.springframework.security.authentication.event.InteractiveAuthenticationSuccessEvent)3 Test (org.junit.jupiter.api.Test)2 EventBuilder (org.opennms.netmgt.model.events.EventBuilder)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 AbstractAuthenticationEvent (org.springframework.security.authentication.event.AbstractAuthenticationEvent)2 Authentication (org.springframework.security.core.Authentication)2 LoginEvent (com.dappermoose.stsimplefinance.data.LoginEvent)1 LoginUser (com.dappermoose.stsimplefinance.data.LoginUser)1 User (de.tudarmstadt.ukp.clarin.webanno.security.model.User)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpSession (javax.servlet.http.HttpSession)1 Transactional (javax.transaction.Transactional)1 Event (org.opennms.netmgt.xml.event.Event)1 OrcidProfileUserDetails (org.orcid.core.oauth.OrcidProfileUserDetails)1 IPentahoSession (org.pentaho.platform.api.engine.IPentahoSession)1 IBackingRepositoryLifecycleManager (org.pentaho.platform.api.repository2.unified.IBackingRepositoryLifecycleManager)1 AuditApplicationEvent (org.springframework.boot.actuate.audit.listener.AuditApplicationEvent)1