Search in sources :

Example 51 with Profile

use of org.craftercms.profile.api.Profile in project engine by craftercms.

the class ConfigAwareLoginSuccessHandlerTest method testProcessRequest.

@Test
public void testProcessRequest() throws Exception {
    handler.handle(RequestContext.getCurrent(), new DefaultAuthentication(null, new Profile()));
    assertEquals(config.getString(LOGIN_DEFAULT_SUCCESS_URL_KEY), ((MockHttpServletResponse) RequestContext.getCurrent().getResponse()).getRedirectedUrl());
}
Also used : DefaultAuthentication(org.craftercms.security.authentication.impl.DefaultAuthentication) Profile(org.craftercms.profile.api.Profile) Test(org.junit.Test)

Example 52 with Profile

use of org.craftercms.profile.api.Profile 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 53 with Profile

use of org.craftercms.profile.api.Profile in project engine by craftercms.

the class PreviewCurrentAuthenticationResolvingProcessor method processRequest.

@Override
@SuppressWarnings("unchecked")
public void processRequest(RequestContext context, RequestSecurityProcessorChain processorChain) throws Exception {
    HttpServletRequest request = context.getRequest();
    Map<String, String> attributes = (Map<String, String>) request.getSession(true).getAttribute(ProfileRestController.PROFILE_SESSION_ATTRIBUTE);
    if (MapUtils.isNotEmpty(attributes)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Non-anonymous persona set: " + attributes);
        }
        Profile profile = new Profile();
        profile.setUsername("preview");
        profile.setEnabled(true);
        profile.setCreatedOn(new Date());
        profile.setLastModified(new Date());
        profile.setTenant("preview");
        String rolesStr = attributes.get("roles");
        if (rolesStr != null) {
            String[] roles = rolesStr.split(",");
            profile.getRoles().addAll(Arrays.asList(roles));
        }
        Map<String, Object> attributesNoUsernameNoRoles = new HashMap<String, Object>(attributes);
        attributesNoUsernameNoRoles.remove("username");
        attributesNoUsernameNoRoles.remove("roles");
        profile.setAttributes(attributesNoUsernameNoRoles);
        SecurityUtils.setAuthentication(request, new PersonaAuthentication(profile));
        processorChain.processRequest(context);
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("No persona set. Trying to resolve authentication normally");
        }
        super.processRequest(context, processorChain);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) Profile(org.craftercms.profile.api.Profile) Date(java.util.Date)

Example 54 with Profile

use of org.craftercms.profile.api.Profile 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 55 with Profile

use of org.craftercms.profile.api.Profile 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)

Aggregations

Profile (org.craftercms.profile.api.Profile)111 Test (org.junit.Test)54 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)19 MongoDataException (org.craftercms.commons.mongo.MongoDataException)15 I10nProfileException (org.craftercms.profile.api.exceptions.I10nProfileException)15 LinkedHashMap (java.util.LinkedHashMap)13 VerificationToken (org.craftercms.profile.api.VerificationToken)13 DefaultAuthentication (org.craftercms.security.authentication.impl.DefaultAuthentication)12 Date (java.util.Date)11 Map (java.util.Map)11 ObjectId (org.bson.types.ObjectId)10 RequestContext (org.craftercms.commons.http.RequestContext)9 Authentication (org.craftercms.security.authentication.Authentication)9 ArgumentMatcher (org.mockito.ArgumentMatcher)9 Mockito.anyString (org.mockito.Mockito.anyString)9 RequestSecurityProcessorChain (org.craftercms.security.processors.RequestSecurityProcessorChain)8 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)8 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)8 Tenant (org.craftercms.profile.api.Tenant)6 HashMap (java.util.HashMap)4