Search in sources :

Example 6 with Ticket

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

the class AuthenticationManagerImpl method authenticateUser.

@Override
public Authentication authenticateUser(String tenant, String username, String password) {
    try {
        Ticket ticket = authenticationService.authenticate(tenant, username, password);
        Profile profile = profileService.getProfile(ticket.getProfileId());
        if (profile == null) {
            throw new AuthenticationSystemException("No profile found for ID '" + ticket.getProfileId() + "'");
        }
        String ticketId = ticket.getId();
        DefaultAuthentication auth = new DefaultAuthentication(ticketId, profile);
        authenticationCache.putAuthentication(auth);
        logger.debug("Authentication successful for user '{}' (ticket ID = '{}')", ticket.getProfileId(), ticketId);
        return auth;
    } catch (ProfileRestServiceException e) {
        switch(e.getErrorCode()) {
            case DISABLED_PROFILE:
                throw new DisabledUserException("User is disabled", e);
            case BAD_CREDENTIALS:
                throw new BadCredentialsException("Invalid username and/or password", e);
            default:
                throw new AuthenticationSystemException("An unexpected error occurred while authenticating", e);
        }
    } catch (ProfileException e) {
        throw new AuthenticationSystemException("An unexpected error occurred while authenticating", e);
    }
}
Also used : Ticket(org.craftercms.profile.api.Ticket) ProfileRestServiceException(org.craftercms.profile.exceptions.ProfileRestServiceException) AuthenticationSystemException(org.craftercms.security.exception.AuthenticationSystemException) ProfileException(org.craftercms.profile.api.exceptions.ProfileException) BadCredentialsException(org.craftercms.security.exception.BadCredentialsException) Profile(org.craftercms.profile.api.Profile) DisabledUserException(org.craftercms.security.exception.DisabledUserException)

Example 7 with Ticket

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

the class AuthenticationServiceIT method testCreateTicket.

@Test
public void testCreateTicket() throws Exception {
    String profileId = profileService.getProfileByUsername(DEFAULT_TENANT_NAME, ADMIN_USERNAME).getId().toString();
    Ticket ticket = authenticationService.createTicket(profileId);
    assertNotNull(ticket);
    assertNotNull(ticket.getId());
    assertEquals(profileId, ticket.getProfileId());
    assertEquals(DEFAULT_TENANT_NAME, ticket.getTenant());
    assertNotNull(ticket.getLastRequestTime());
    authenticationService.invalidateTicket(ticket.getId());
}
Also used : Ticket(org.craftercms.profile.api.Ticket) Test(org.junit.Test)

Example 8 with Ticket

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

the class AuthenticationServiceIT method testAuthenticate.

@Test
public void testAuthenticate() throws Exception {
    Ticket ticket = authenticationService.authenticate(DEFAULT_TENANT_NAME, ADMIN_USERNAME, ADMIN_PASSWORD);
    assertNotNull(ticket);
    assertNotNull(ticket.getId());
    assertNotNull(ticket.getProfileId());
    assertEquals(DEFAULT_TENANT_NAME, ticket.getTenant());
    assertNotNull(ticket.getLastRequestTime());
    authenticationService.invalidateTicket(ticket.getId());
}
Also used : Ticket(org.craftercms.profile.api.Ticket) Test(org.junit.Test)

Example 9 with Ticket

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

the class AuthenticationServiceIT method testGetExpiredTicket.

@Test
public void testGetExpiredTicket() throws Exception {
    Ticket ticket = authenticationService.authenticate(DEFAULT_TENANT_NAME, ADMIN_USERNAME, ADMIN_PASSWORD);
    assertNotNull(ticket);
    Thread.sleep(TimeUnit.SECONDS.toMillis(4));
    ticket = authenticationService.getTicket(ticket.getId());
    assertNull(ticket);
}
Also used : Ticket(org.craftercms.profile.api.Ticket) Test(org.junit.Test)

Example 10 with Ticket

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

the class AuthenticationServiceImplTest method testGetTicket.

@Test
public void testGetTicket() throws Exception {
    Ticket ticket = authenticationService.getTicket(TICKET_ID);
    assertNotNull(ticket);
    assertEquals(TICKET_ID, ticket.getId());
    assertEquals(PROFILE1_ID.toString(), ticket.getProfileId());
    assertNotNull(ticket.getLastRequestTime());
    verify(ticketRepository).findByStringId(TICKET_ID);
    verify(ticketRepository).save(ticket);
}
Also used : Ticket(org.craftercms.profile.api.Ticket) Test(org.junit.Test)

Aggregations

Ticket (org.craftercms.profile.api.Ticket)19 Test (org.junit.Test)9 Date (java.util.Date)8 MongoDataException (org.craftercms.commons.mongo.MongoDataException)4 Profile (org.craftercms.profile.api.Profile)4 I10nProfileException (org.craftercms.profile.api.exceptions.I10nProfileException)4 ProfileException (org.craftercms.profile.api.exceptions.ProfileException)2 DisabledProfileException (org.craftercms.profile.exceptions.DisabledProfileException)2 ProfileRestServiceException (org.craftercms.profile.exceptions.ProfileRestServiceException)2 AuthenticationSystemException (org.craftercms.security.exception.AuthenticationSystemException)2 DisabledUserException (org.craftercms.security.exception.DisabledUserException)2 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)1 BadCredentialsException (org.craftercms.profile.exceptions.BadCredentialsException)1 ProfileLockedException (org.craftercms.profile.exceptions.ProfileLockedException)1 BadCredentialsException (org.craftercms.security.exception.BadCredentialsException)1