Search in sources :

Example 6 with DefaultAuthentication

use of org.craftercms.security.authentication.impl.DefaultAuthentication in project engine by craftercms.

the class ConfigAwareUrlAccessRestrictionCheckingProcessorTest method testProcessRequest.

@Test(expected = AccessDeniedException.class)
public void testProcessRequest() throws Exception {
    RequestContext requestContext = RequestContext.getCurrent();
    RequestSecurityProcessorChain chain = mock(RequestSecurityProcessorChain.class);
    SecurityUtils.setAuthentication(requestContext.getRequest(), new DefaultAuthentication(ObjectId.get().toString(), new Profile()));
    processor.processRequest(requestContext, chain);
}
Also used : DefaultAuthentication(org.craftercms.security.authentication.impl.DefaultAuthentication) RequestSecurityProcessorChain(org.craftercms.security.processors.RequestSecurityProcessorChain) RequestContext(org.craftercms.commons.http.RequestContext) Profile(org.craftercms.profile.api.Profile) Test(org.junit.Test)

Example 7 with DefaultAuthentication

use of org.craftercms.security.authentication.impl.DefaultAuthentication in project profile by craftercms.

the class CurrentAuthenticationResolvingProcessorTest method testGetAuthentication.

@Test
public void testGetAuthentication() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    RequestSecurityProcessorChain chain = mock(RequestSecurityProcessorChain.class);
    Date profileLastModified = new Date();
    Cookie ticketCookie = new Cookie(SecurityUtils.TICKET_COOKIE_NAME, TICKET);
    Cookie profileLastModifiedCookie = new Cookie(SecurityUtils.PROFILE_LAST_MODIFIED_COOKIE_NAME, String.valueOf(profileLastModified.getTime()));
    request.setCookies(ticketCookie, profileLastModifiedCookie);
    Profile profile = new Profile();
    profile.setLastModified(profileLastModified);
    Authentication auth = new DefaultAuthentication(TICKET, profile);
    when(authenticationManager.getAuthentication(TICKET, false)).thenReturn(auth);
    processor.processRequest(context, chain);
    verify(chain).processRequest(context);
    Authentication newAuth = SecurityUtils.getAuthentication(request);
    assertNotNull(newAuth);
    assertEquals(auth.getTicket(), newAuth.getTicket());
    assertEquals(auth.getProfile().getLastModified(), newAuth.getProfile().getLastModified());
}
Also used : Cookie(javax.servlet.http.Cookie) DefaultAuthentication(org.craftercms.security.authentication.impl.DefaultAuthentication) RequestSecurityProcessorChain(org.craftercms.security.processors.RequestSecurityProcessorChain) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.craftercms.security.authentication.Authentication) DefaultAuthentication(org.craftercms.security.authentication.impl.DefaultAuthentication) RequestContext(org.craftercms.commons.http.RequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Date(java.util.Date) Profile(org.craftercms.profile.api.Profile) Test(org.junit.Test)

Example 8 with DefaultAuthentication

use of org.craftercms.security.authentication.impl.DefaultAuthentication in project profile by craftercms.

the class LogoutProcessorTest method testLogout.

@Test
public void testLogout() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest(LogoutProcessor.DEFAULT_LOGOUT_METHOD, LogoutProcessor.DEFAULT_LOGOUT_URL);
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    RequestSecurityProcessorChain chain = mock(RequestSecurityProcessorChain.class);
    Profile profile = new Profile();
    profile.setUsername(USERNAME);
    Authentication auth = new DefaultAuthentication(new ObjectId().toString(), profile);
    SecurityUtils.setAuthentication(request, auth);
    processor.processRequest(context, chain);
    verify(chain, never()).processRequest(context);
    assertNull(SecurityUtils.getAuthentication(request));
    verify(logoutSuccessHandler).handle(context);
}
Also used : DefaultAuthentication(org.craftercms.security.authentication.impl.DefaultAuthentication) RequestSecurityProcessorChain(org.craftercms.security.processors.RequestSecurityProcessorChain) ObjectId(org.bson.types.ObjectId) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.craftercms.security.authentication.Authentication) DefaultAuthentication(org.craftercms.security.authentication.impl.DefaultAuthentication) RequestContext(org.craftercms.commons.http.RequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Profile(org.craftercms.profile.api.Profile) Test(org.junit.Test)

Example 9 with DefaultAuthentication

use of org.craftercms.security.authentication.impl.DefaultAuthentication in project profile by craftercms.

the class UrlAccessRestrictionCheckingProcessorTest method testAllowedAccess.

@Test
public void testAllowedAccess() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", URL);
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    RequestSecurityProcessorChain chain = mock(RequestSecurityProcessorChain.class);
    Profile profile = new Profile();
    profile.setRoles(SetUtils.asSet(ADMIN_ROLE));
    SecurityUtils.setAuthentication(request, new DefaultAuthentication(new ObjectId().toString(), profile));
    processor.processRequest(context, chain);
    verify(chain).processRequest(context);
}
Also used : DefaultAuthentication(org.craftercms.security.authentication.impl.DefaultAuthentication) RequestSecurityProcessorChain(org.craftercms.security.processors.RequestSecurityProcessorChain) ObjectId(org.bson.types.ObjectId) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestContext(org.craftercms.commons.http.RequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Profile(org.craftercms.profile.api.Profile) Test(org.junit.Test)

Example 10 with DefaultAuthentication

use of org.craftercms.security.authentication.impl.DefaultAuthentication in project profile by craftercms.

the class RefreshCurrentAuthenticationOnProfileUpdateAspect method refreshCurrentAuthentication.

@AfterReturning(value = "execution(* org.craftercms.profile.api.services.ProfileService.updateProfile(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.verifyProfile(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.enableProfile(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.disableProfile(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.addRoles(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.removeRoles(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.updateAttributes(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.removeAttributes(..)) || " + "execution(* org.craftercms.profile.api.services.ProfileService.changePassword(..))", returning = "updatedProfile")
public void refreshCurrentAuthentication(Profile updatedProfile) {
    Authentication auth = SecurityUtils.getCurrentAuthentication();
    if (auth != null) {
        Profile profile = auth.getProfile();
        if (profile.equals(updatedProfile)) {
            String ticket = auth.getTicket();
            auth = new DefaultAuthentication(ticket, updatedProfile);
            // Put updated authentication in cache
            authenticationCache.putAuthentication(auth);
            // Update current authentication object
            SecurityUtils.setCurrentAuthentication(auth);
        }
    }
}
Also used : DefaultAuthentication(org.craftercms.security.authentication.impl.DefaultAuthentication) Authentication(org.craftercms.security.authentication.Authentication) DefaultAuthentication(org.craftercms.security.authentication.impl.DefaultAuthentication) Profile(org.craftercms.profile.api.Profile) AfterReturning(org.aspectj.lang.annotation.AfterReturning)

Aggregations

DefaultAuthentication (org.craftercms.security.authentication.impl.DefaultAuthentication)14 Profile (org.craftercms.profile.api.Profile)12 Test (org.junit.Test)9 RequestContext (org.craftercms.commons.http.RequestContext)8 RequestSecurityProcessorChain (org.craftercms.security.processors.RequestSecurityProcessorChain)8 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)7 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)7 ObjectId (org.bson.types.ObjectId)5 Authentication (org.craftercms.security.authentication.Authentication)5 Date (java.util.Date)3 Cookie (javax.servlet.http.Cookie)3 Before (org.junit.Before)2 AfterReturning (org.aspectj.lang.annotation.AfterReturning)1 AccessDeniedException (org.craftercms.security.exception.AccessDeniedException)1 RequestSecurityProcessor (org.craftercms.security.processors.RequestSecurityProcessor)1 DefaultTenantsResolver (org.craftercms.security.utils.tenant.DefaultTenantsResolver)1