Search in sources :

Example 36 with Profile

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

the class AuthenticationServiceImpl method authenticate.

@Override
public Ticket authenticate(String tenantName, String username, String password) throws ProfileException {
    checkIfManageTicketsIsAllowed(tenantName);
    Profile profile = profileService.getProfileByUsername(tenantName, username, ProfileConstants.NO_ATTRIBUTE);
    if (profile == null) {
        // Invalid username
        throw new BadCredentialsException();
    }
    if (!profile.isEnabled()) {
        throw new DisabledProfileException(profile.getId().toString(), tenantName);
    }
    if (isProfileInTimeOut(profile)) {
        throw new ProfileLockedException();
    }
    try {
        if (!CryptoUtils.matchPassword(profile.getPassword(), password)) {
            // Invalid password
            countAsFail(profile);
            throw new BadCredentialsException();
        }
        clearAllLoginAttempts(profile);
        Ticket ticket = new Ticket();
        ticket.setId(UUID.randomUUID().toString());
        ticket.setTenant(tenantName);
        ticket.setProfileId(profile.getId().toString());
        ticket.setLastRequestTime(new Date());
        ticketRepository.insert(ticket);
        logger.debug(LOG_KEY_AUTHENTICATION_SUCCESSFUL, profile.getId(), ticket);
        return ticket;
    } catch (MongoDataException e) {
        throw new I10nProfileException(ERROR_KEY_CREATE_TICKET_ERROR, profile.getId());
    }
}
Also used : Ticket(org.craftercms.profile.api.Ticket) ProfileLockedException(org.craftercms.profile.exceptions.ProfileLockedException) I10nProfileException(org.craftercms.profile.api.exceptions.I10nProfileException) DisabledProfileException(org.craftercms.profile.exceptions.DisabledProfileException) MongoDataException(org.craftercms.commons.mongo.MongoDataException) BadCredentialsException(org.craftercms.profile.exceptions.BadCredentialsException) Profile(org.craftercms.profile.api.Profile) Date(java.util.Date)

Example 37 with Profile

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

the class AuthenticationServiceImpl method createPersistentLogin.

@Override
public PersistentLogin createPersistentLogin(String profileId) throws ProfileException {
    Profile profile = profileService.getProfile(profileId, ProfileConstants.NO_ATTRIBUTE);
    if (profile != null) {
        String tenantName = profile.getTenant();
        checkIfManageTicketsIsAllowed(tenantName);
        if (!profile.isEnabled()) {
            throw new DisabledProfileException(profile.getId().toString(), tenantName);
        }
        try {
            PersistentLogin login = new PersistentLogin();
            login.setId(UUID.randomUUID().toString());
            login.setTenant(tenantName);
            login.setProfileId(profileId);
            login.setToken(UUID.randomUUID().toString());
            login.setTimestamp(new Date());
            persistentLoginRepository.insert(login);
            logger.debug(LOG_KEY_PERSISTENT_LOGIN_CREATED, profile.getId(), login);
            return login;
        } catch (MongoDataException e) {
            throw new I10nProfileException(ERROR_KEY_CREATE_PERSISTENT_LOGIN_ERROR, profile.getId());
        }
    } else {
        throw new NoSuchProfileException.ById(profileId);
    }
}
Also used : I10nProfileException(org.craftercms.profile.api.exceptions.I10nProfileException) DisabledProfileException(org.craftercms.profile.exceptions.DisabledProfileException) MongoDataException(org.craftercms.commons.mongo.MongoDataException) PersistentLogin(org.craftercms.profile.api.PersistentLogin) Profile(org.craftercms.profile.api.Profile) Date(java.util.Date)

Example 38 with Profile

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

the class AuthenticationServiceImpl method createTicket.

@Override
public Ticket createTicket(String profileId) throws ProfileException {
    Profile profile = profileService.getProfile(profileId, ProfileConstants.NO_ATTRIBUTE);
    if (profile != null) {
        String tenantName = profile.getTenant();
        checkIfManageTicketsIsAllowed(tenantName);
        if (!profile.isEnabled()) {
            throw new DisabledProfileException(profile.getId().toString(), tenantName);
        }
        try {
            Ticket ticket = new Ticket();
            ticket.setId(UUID.randomUUID().toString());
            ticket.setTenant(tenantName);
            ticket.setProfileId(profile.getId().toString());
            ticket.setLastRequestTime(new Date());
            ticketRepository.insert(ticket);
            logger.debug(LOG_KEY_TICKET_CREATED, profile.getId(), ticket);
            return ticket;
        } catch (MongoDataException e) {
            throw new I10nProfileException(ERROR_KEY_CREATE_TICKET_ERROR, profile.getId());
        }
    } else {
        throw new NoSuchProfileException.ById(profileId);
    }
}
Also used : Ticket(org.craftercms.profile.api.Ticket) I10nProfileException(org.craftercms.profile.api.exceptions.I10nProfileException) DisabledProfileException(org.craftercms.profile.exceptions.DisabledProfileException) MongoDataException(org.craftercms.commons.mongo.MongoDataException) Profile(org.craftercms.profile.api.Profile) Date(java.util.Date)

Example 39 with Profile

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

the class RememberMeManagerImplTest method getProfile.

protected Profile getProfile() {
    Profile profile = new Profile();
    profile.setId(PROFILE_ID);
    return profile;
}
Also used : Profile(org.craftercms.profile.api.Profile)

Example 40 with Profile

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

the class ProfileServiceIT method testCreateProfile.

@Test
public void testCreateProfile() throws Exception {
    Map<String, Object> attributes = new LinkedHashMap<>(2);
    attributes.put("firstName", AVASQUEZ_FIRST_NAME);
    attributes.put("lastName", AVASQUEZ_LAST_NAME);
    Profile profile = profileService.createProfile(DEFAULT_TENANT, AVASQUEZ_USERNAME, AVASQUEZ_PASSWORD1, AVASQUEZ_EMAIL1, true, AVASQUEZ_ROLES1, attributes, VERIFICATION_URL);
    try {
        assertNotNull(profile);
        assertNotNull(profile.getId());
        assertEquals(AVASQUEZ_USERNAME, profile.getUsername());
        assertNull(profile.getPassword());
        assertEquals(AVASQUEZ_EMAIL1, profile.getEmail());
        assertFalse(profile.isVerified());
        assertTrue(profile.isEnabled());
        assertNotNull(profile.getCreatedOn());
        assertNotNull(profile.getLastModified());
        assertEquals(DEFAULT_TENANT, profile.getTenant());
        assertEquals(AVASQUEZ_ROLES1, profile.getRoles());
        assertNotNull(profile.getAttributes());
        assertEquals(attributes, profile.getAttributes());
    } finally {
        profileService.deleteProfile(profile.getId().toString());
    }
}
Also used : Profile(org.craftercms.profile.api.Profile) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Aggregations

Profile (org.craftercms.profile.api.Profile)110 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 Authentication (org.craftercms.security.authentication.Authentication)11 Date (java.util.Date)10 Map (java.util.Map)10 ObjectId (org.bson.types.ObjectId)9 RequestContext (org.craftercms.commons.http.RequestContext)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)5 Ticket (org.craftercms.profile.api.Ticket)4