Search in sources :

Example 46 with Profile

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

the class UrlAccessRestrictionCheckingProcessorTest method testUnAllowedAccess.

@Test(expected = AccessDeniedException.class)
public void testUnAllowedAccess() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", URL);
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response, null);
    RequestSecurityProcessorChain chain = mock(RequestSecurityProcessorChain.class);
    SecurityUtils.setAuthentication(request, new DefaultAuthentication(new ObjectId().toString(), new Profile()));
    processor.processRequest(context, chain);
}
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 47 with Profile

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

the class AuthenticationServiceImplTest method getProfile2.

private Profile getProfile2() {
    Profile profile = new Profile();
    profile.setId(PROFILE2_ID);
    profile.setUsername(USERNAME2);
    profile.setPassword(CryptoUtils.hashPassword(PASSWORD));
    profile.setEnabled(false);
    profile.setTenant(TENANT_NAME);
    return profile;
}
Also used : Profile(org.craftercms.profile.api.Profile)

Example 48 with Profile

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

the class AuthenticationServiceImplTest method getProfile1.

private Profile getProfile1() {
    Profile profile = new Profile();
    profile.setId(PROFILE1_ID);
    profile.setUsername(USERNAME1);
    profile.setPassword(CryptoUtils.hashPassword(PASSWORD));
    profile.setEnabled(true);
    profile.setTenant(TENANT_NAME);
    return profile;
}
Also used : Profile(org.craftercms.profile.api.Profile)

Example 49 with Profile

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

the class ProfileHeadersAuthenticationFilter method doGetPreAuthenticatedPrincipal.

@Override
protected Object doGetPreAuthenticatedPrincipal(final HttpServletRequest request) {
    String username = request.getHeader(getUsernameHeaderName());
    String email = request.getHeader(getEmailHeaderName());
    if (isNoneEmpty(username, email)) {
        try {
            String[] tenantNames = tenantsResolver.getTenants();
            Tenant tenant = getSsoEnabledTenant(tenantNames);
            if (tenant != null) {
                Profile profile = profileService.getProfileByUsername(tenant.getName(), username);
                if (profile == null) {
                    profile = createProfileWithSsoInfo(username, tenant, request);
                }
                return new ProfileUser(profile);
            } else {
                logger.warn("A SSO login was attempted, but none of the tenants [{}] is enabled for SSO", (Object) tenantNames);
            }
        } catch (ProfileException e) {
            logger.error("Error processing headers authentication for '{}'", username, e);
        }
    }
    return null;
}
Also used : Tenant(org.craftercms.profile.api.Tenant) ProfileException(org.craftercms.profile.api.exceptions.ProfileException) Profile(org.craftercms.profile.api.Profile)

Example 50 with Profile

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

the class TargetingPreAuthenticatedFilter method getPreAuthenticatedPrincipal.

@Override
@SuppressWarnings("unchecked")
protected Object getPreAuthenticatedPrincipal(final HttpServletRequest request) {
    HttpSession session = request.getSession();
    if (session != null) {
        Map<String, String> attributes = (Map<String, String>) session.getAttribute(ProfileRestController.PROFILE_SESSION_ATTRIBUTE);
        if (isNotEmpty(attributes)) {
            if (logger.isDebugEnabled()) {
                logger.debug("Non-anonymous persona set: " + attributes);
            }
            Profile profile = new Profile();
            profile.setId(new ObjectId(attributes.get("id")));
            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> customAttributes = new HashMap<>(attributes);
            customAttributes.remove("id");
            customAttributes.remove("username");
            customAttributes.remove("roles");
            profile.setAttributes(customAttributes);
            return new TargetingUser(new TargetingAuthentication(profile));
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("No persona set. Trying to resolve authentication normally");
    }
    return null;
}
Also used : ObjectId(org.bson.types.ObjectId) HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) Profile(org.craftercms.profile.api.Profile) Date(java.util.Date) Map(java.util.Map) HashMap(java.util.HashMap)

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