Search in sources :

Example 1 with NoCredentialsFoundException

use of org.alfresco.service.cmr.remoteticket.NoCredentialsFoundException in project alfresco-remote-api by Alfresco.

the class RemoteAlfrescoTicketServiceTest method testGetTicket.

/**
 * Getting cached and non-cached credentials
 */
public void testGetTicket() throws Exception {
    // Run this test initially as the first user
    AuthenticationUtil.setFullyAuthenticatedUser(USER_ONE);
    // First, try an invalid system
    try {
        remoteAlfrescoTicketService.getAlfrescoTicket(INVALID_REMOTE_SYSTEM_ID);
        fail("Shouldn't work for an invalid system");
    } catch (NoSuchSystemException e) {
    }
    try {
        remoteAlfrescoTicketService.refetchAlfrescoTicket(INVALID_REMOTE_SYSTEM_ID);
        fail("Shouldn't work for an invalid system");
    } catch (NoSuchSystemException e) {
    }
    // Can't get or refresh if no credentials exist
    try {
        remoteAlfrescoTicketService.getAlfrescoTicket(TEST_REMOTE_SYSTEM_ID);
        fail("Shouldn't work when no credentials");
    } catch (NoCredentialsFoundException e) {
    }
    try {
        remoteAlfrescoTicketService.refetchAlfrescoTicket(TEST_REMOTE_SYSTEM_ID);
        fail("Shouldn't work when no credentials");
    } catch (NoCredentialsFoundException e) {
    }
    // Have some stored
    remoteAlfrescoTicketService.storeRemoteCredentials(TEST_REMOTE_SYSTEM_ID, USER_ONE, PASSWORD);
    // A ticket will now exist
    RemoteAlfrescoTicketInfo ticket = remoteAlfrescoTicketService.getAlfrescoTicket(TEST_REMOTE_SYSTEM_ID);
    assertNotNull(ticket);
    assertNotNull(ticket.getAsUrlParameters());
    // Ask again, will get the same one
    RemoteAlfrescoTicketInfo ticket2 = remoteAlfrescoTicketService.getAlfrescoTicket(TEST_REMOTE_SYSTEM_ID);
    assertNotNull(ticket2);
    assertEquals(ticket.getAsUrlParameters(), ticket2.getAsUrlParameters());
    // Force a re-fetch, will get another
    RemoteAlfrescoTicketInfo ticket3 = remoteAlfrescoTicketService.refetchAlfrescoTicket(TEST_REMOTE_SYSTEM_ID);
    assertNotNull(ticket3);
    assertNotSame(ticket.getAsUrlParameters(), ticket3.getAsUrlParameters());
    // Ask for the ticket again, get the 2nd one again
    RemoteAlfrescoTicketInfo ticket4 = remoteAlfrescoTicketService.getAlfrescoTicket(TEST_REMOTE_SYSTEM_ID);
    assertNotNull(ticket4);
    assertEquals(ticket3.getAsUrlParameters(), ticket4.getAsUrlParameters());
    // Zap from the cache, will trigger another to be fetched
    ticketsCache.clear();
    RemoteAlfrescoTicketInfo ticket5 = remoteAlfrescoTicketService.getAlfrescoTicket(TEST_REMOTE_SYSTEM_ID);
    assertNotNull(ticket5);
    assertNotSame(ticket.getAsUrlParameters(), ticket5.getAsUrlParameters());
    assertNotSame(ticket3.getAsUrlParameters(), ticket5.getAsUrlParameters());
    // Change the password so it's no longer valid
    PasswordCredentialsInfoImpl creds = (PasswordCredentialsInfoImpl) remoteCredentialsService.getPersonCredentials(TEST_REMOTE_SYSTEM_ID);
    assertNotNull(creds);
    creds.setRemotePassword("INVALID");
    remoteCredentialsService.updateCredentials(creds);
    // Currently will be marked as still working
    assertEquals(true, creds.getLastAuthenticationSucceeded());
    // Get will work, as ticket was previously cached
    RemoteAlfrescoTicketInfo ticket6 = remoteAlfrescoTicketService.getAlfrescoTicket(TEST_REMOTE_SYSTEM_ID);
    assertNotNull(ticket6);
    assertEquals(ticket5.getAsUrlParameters(), ticket6.getAsUrlParameters());
    // Re-fetch will fail with authentication error
    try {
        remoteAlfrescoTicketService.refetchAlfrescoTicket(TEST_REMOTE_SYSTEM_ID);
        fail("Shouldn't be able to refetch with wrong details");
    } catch (AuthenticationException e) {
    }
    // Now a get will fail too, as the cache will be invalidated
    try {
        remoteAlfrescoTicketService.getAlfrescoTicket(TEST_REMOTE_SYSTEM_ID);
        fail("Shouldn't be able to get after refresh with wrong details");
    } catch (AuthenticationException e) {
    }
    // If we check the credentials, will now be marked as failing
    creds = (PasswordCredentialsInfoImpl) remoteCredentialsService.getPersonCredentials(TEST_REMOTE_SYSTEM_ID);
    assertEquals(false, creds.getLastAuthenticationSucceeded());
    // Change the password back to what it should be, and re-get
    creds.setRemotePassword(PASSWORD);
    remoteCredentialsService.updateCredentials(creds);
    RemoteAlfrescoTicketInfo ticket7 = remoteAlfrescoTicketService.getAlfrescoTicket(TEST_REMOTE_SYSTEM_ID);
    assertNotNull(ticket7);
    assertNotSame(ticket.getAsUrlParameters(), ticket7.getAsUrlParameters());
    assertNotSame(ticket3.getAsUrlParameters(), ticket7.getAsUrlParameters());
    assertNotSame(ticket5.getAsUrlParameters(), ticket7.getAsUrlParameters());
    // Should now be marked as working again
    creds = (PasswordCredentialsInfoImpl) remoteCredentialsService.getPersonCredentials(TEST_REMOTE_SYSTEM_ID);
    assertEquals(true, creds.getLastAuthenticationSucceeded());
    // Check that failure can be marked in a read only transaction
    creds.setRemotePassword("INVALID");
    remoteCredentialsService.updateCredentials(creds);
    retryingTransactionHelper.doInTransaction(new RetryingTransactionHelper.RetryingTransactionCallback<Void>() {

        public Void execute() {
            try {
                remoteAlfrescoTicketService.refetchAlfrescoTicket(TEST_REMOTE_SYSTEM_ID);
                fail("Shouldn't be able to refetch with wrong details");
            } catch (AuthenticationException e) {
            }
            return null;
        }
    }, false, // after MNT-13871, POST api/login webscript now requires read-write transaction
    true);
    // Check it was still marked as invalid, despite a read only transaction
    creds = (PasswordCredentialsInfoImpl) remoteCredentialsService.getPersonCredentials(TEST_REMOTE_SYSTEM_ID);
    assertEquals(false, creds.getLastAuthenticationSucceeded());
}
Also used : NoSuchSystemException(org.alfresco.service.cmr.remoteticket.NoSuchSystemException) PasswordCredentialsInfoImpl(org.alfresco.repo.remotecredentials.PasswordCredentialsInfoImpl) AuthenticationException(org.alfresco.repo.security.authentication.AuthenticationException) RetryingTransactionHelper(org.alfresco.repo.transaction.RetryingTransactionHelper) RemoteAlfrescoTicketInfo(org.alfresco.service.cmr.remoteticket.RemoteAlfrescoTicketInfo) NoCredentialsFoundException(org.alfresco.service.cmr.remoteticket.NoCredentialsFoundException)

Aggregations

PasswordCredentialsInfoImpl (org.alfresco.repo.remotecredentials.PasswordCredentialsInfoImpl)1 AuthenticationException (org.alfresco.repo.security.authentication.AuthenticationException)1 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)1 NoCredentialsFoundException (org.alfresco.service.cmr.remoteticket.NoCredentialsFoundException)1 NoSuchSystemException (org.alfresco.service.cmr.remoteticket.NoSuchSystemException)1 RemoteAlfrescoTicketInfo (org.alfresco.service.cmr.remoteticket.RemoteAlfrescoTicketInfo)1