Search in sources :

Example 11 with DefaultAuthentication

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

the class ProfileControllerTest method setCurrentUser.

private void setCurrentUser(Profile profile) {
    DefaultAuthentication auth = new DefaultAuthentication(null, profile);
    SecurityUtils.setCurrentAuthentication(auth);
}
Also used : DefaultAuthentication(org.craftercms.security.authentication.impl.DefaultAuthentication)

Example 12 with DefaultAuthentication

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

the class AddSecurityCookiesProcessorTest method testAddCookiesLoggedIn.

@Test
public void testAddCookiesLoggedIn() throws Exception {
    String ticket = new ObjectId().toString();
    Date lastModified = new Date();
    Profile profile = new Profile();
    profile.setLastModified(lastModified);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    RequestSecurityProcessor flushResponseProcessor = new RequestSecurityProcessor() {

        @Override
        public void processRequest(RequestContext context, RequestSecurityProcessorChain processorChain) throws Exception {
            context.getResponse().getOutputStream().flush();
        }
    };
    RequestSecurityProcessorChain chain = new RequestSecurityProcessorChainImpl(Arrays.asList(processor, flushResponseProcessor).iterator());
    Authentication auth = new DefaultAuthentication(ticket, profile);
    SecurityUtils.setAuthentication(request, auth);
    processor.processRequest(context, chain);
    Cookie ticketCookie = response.getCookie(SecurityUtils.TICKET_COOKIE_NAME);
    assertNotNull(ticketCookie);
    assertEquals(ticket, ticketCookie.getValue());
    Cookie profileLastModifiedCookie = response.getCookie(SecurityUtils.PROFILE_LAST_MODIFIED_COOKIE_NAME);
    assertNotNull(profileLastModifiedCookie);
    assertEquals(profile.getLastModified().getTime(), Long.parseLong(profileLastModifiedCookie.getValue()));
}
Also used : Cookie(javax.servlet.http.Cookie) RequestSecurityProcessorChain(org.craftercms.security.processors.RequestSecurityProcessorChain) ObjectId(org.bson.types.ObjectId) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Date(java.util.Date) Profile(org.craftercms.profile.api.Profile) DefaultAuthentication(org.craftercms.security.authentication.impl.DefaultAuthentication) Authentication(org.craftercms.security.authentication.Authentication) DefaultAuthentication(org.craftercms.security.authentication.impl.DefaultAuthentication) RequestSecurityProcessor(org.craftercms.security.processors.RequestSecurityProcessor) RequestContext(org.craftercms.commons.http.RequestContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 13 with DefaultAuthentication

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

the class LoginProcessorTest method setUp.

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    DefaultTenantsResolver resolver = new DefaultTenantsResolver();
    resolver.setDefaultTenantNames(TENANTS);
    processor = new LoginProcessor();
    processor.setTenantsResolver(resolver);
    processor.setAuthenticationManager(authenticationManager);
    processor.setLoginSuccessHandler(loginSuccessHandler);
    processor.setLoginFailureHandler(loginFailureHandler);
    processor.setRememberMeManager(rememberMeManager);
    Profile profile = new Profile();
    profile.setUsername(USERNAME);
    when(authenticationManager.authenticateUser(TENANTS, USERNAME, VALID_PASSWORD)).thenReturn(new DefaultAuthentication(TICKET, profile));
    doThrow(BadCredentialsException.class).when(authenticationManager).authenticateUser(TENANTS, USERNAME, INVALID_PASSWORD);
}
Also used : DefaultTenantsResolver(org.craftercms.security.utils.tenant.DefaultTenantsResolver) DefaultAuthentication(org.craftercms.security.authentication.impl.DefaultAuthentication) Profile(org.craftercms.profile.api.Profile) Before(org.junit.Before)

Example 14 with DefaultAuthentication

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

the class SecurityExceptionProcessorTest method testAccessDeniedWithAuthentication.

@Test
public void testAccessDeniedWithAuthentication() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    RequestSecurityProcessorChain chain = mock(RequestSecurityProcessorChain.class);
    doThrow(AccessDeniedException.class).when(chain).processRequest(context);
    SecurityUtils.setAuthentication(request, new DefaultAuthentication(new ObjectId().toString(), new Profile()));
    processor.processRequest(context, chain);
    verify(chain).processRequest(context);
    verify(accessDeniedHandler).handle(eq(context), any(AccessDeniedException.class));
}
Also used : DefaultAuthentication(org.craftercms.security.authentication.impl.DefaultAuthentication) AccessDeniedException(org.craftercms.security.exception.AccessDeniedException) 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)

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