Search in sources :

Example 26 with AuthenticationException

use of org.alfresco.repo.security.authentication.AuthenticationException in project alfresco-remote-api by Alfresco.

the class AuthenticationsImpl method validateTicket.

@Override
public LoginTicketResponse validateTicket(String me, Parameters parameters, WithResponse withResponse) {
    if (!People.DEFAULT_USER.equals(me)) {
        throw new InvalidArgumentException("Invalid parameter: " + me);
    }
    final String ticket = getTicket(parameters);
    try {
        final String ticketUser = ticketComponent.validateTicket(ticket);
        final String currentUser = AuthenticationUtil.getFullyAuthenticatedUser();
        // or the user is not fully authenticated
        if (currentUser == null || !currentUser.equals(ticketUser)) {
            throw new NotFoundException(NotFoundException.DEFAULT_MESSAGE_ID, new String[] { ticket });
        }
    } catch (AuthenticationException e) {
        throw new NotFoundException(NotFoundException.DEFAULT_MESSAGE_ID, new String[] { ticket });
    }
    LoginTicketResponse response = new LoginTicketResponse();
    response.setId(ticket);
    return response;
}
Also used : LoginTicketResponse(org.alfresco.rest.api.model.LoginTicketResponse) InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) AuthenticationException(org.alfresco.repo.security.authentication.AuthenticationException) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException)

Example 27 with AuthenticationException

use of org.alfresco.repo.security.authentication.AuthenticationException in project alfresco-remote-api by Alfresco.

the class AuthenticationsImpl method deleteTicket.

@Override
public void deleteTicket(String me, Parameters parameters, WithResponse withResponse) {
    if (!People.DEFAULT_USER.equals(me)) {
        throw new InvalidArgumentException("Invalid parameter: " + me);
    }
    final String ticket = getTicket(parameters);
    try {
        final String ticketUser = ticketComponent.validateTicket(ticket);
        final String currentUser = AuthenticationUtil.getFullyAuthenticatedUser();
        // or the user is not fully authenticated
        if (currentUser == null || !currentUser.equals(ticketUser)) {
            throw new NotFoundException(NotFoundException.DEFAULT_MESSAGE_ID, new String[] { ticket });
        } else {
            // delete the ticket
            authenticationService.invalidateTicket(ticket);
        }
    } catch (AuthenticationException e) {
        throw new NotFoundException(NotFoundException.DEFAULT_MESSAGE_ID, new String[] { ticket });
    }
}
Also used : InvalidArgumentException(org.alfresco.rest.framework.core.exceptions.InvalidArgumentException) AuthenticationException(org.alfresco.repo.security.authentication.AuthenticationException) NotFoundException(org.alfresco.rest.framework.core.exceptions.NotFoundException)

Example 28 with AuthenticationException

use of org.alfresco.repo.security.authentication.AuthenticationException in project alfresco-remote-api by Alfresco.

the class AbstractLoginBean method login.

protected Map<String, Object> login(final String username, String password) {
    try {
        // get ticket
        authenticationService.authenticate(username, password.toCharArray());
        eventPublisher.publishEvent(new EventPreparator() {

            @Override
            public Event prepareEvent(String user, String networkId, String transactionId) {
                // TODO need to fix up to pass correct seqNo and alfrescoClientId
                return new RepositoryEventImpl(-1l, "login", transactionId, networkId, new Date().getTime(), username, null);
            }
        });
        // add ticket to model for javascript and template access
        Map<String, Object> model = new HashMap<String, Object>(7, 1.0f);
        model.put("username", username);
        model.put("ticket", authenticationService.getCurrentTicket());
        return model;
    } catch (AuthenticationException e) {
        throw new WebScriptException(HttpServletResponse.SC_FORBIDDEN, "Login failed");
    } finally {
        AuthenticationUtil.clearCurrentSecurityContext();
    }
}
Also used : EventPreparator(org.alfresco.repo.events.EventPreparator) WebScriptException(org.springframework.extensions.webscripts.WebScriptException) HashMap(java.util.HashMap) AuthenticationException(org.alfresco.repo.security.authentication.AuthenticationException) Event(org.alfresco.events.types.Event) RepositoryEventImpl(org.alfresco.events.types.RepositoryEventImpl) Date(java.util.Date)

Example 29 with AuthenticationException

use of org.alfresco.repo.security.authentication.AuthenticationException in project alfresco-remote-api by Alfresco.

the class RemoteAlfrescoTicketServiceTest method testGetStoreGetCredentials.

/**
 * Getting, storing and fetching credentials
 */
public void testGetStoreGetCredentials() throws Exception {
    // Run this test initially as the first user
    AuthenticationUtil.setFullyAuthenticatedUser(USER_ONE);
    // First, try an invalid system
    try {
        remoteAlfrescoTicketService.getRemoteCredentials(INVALID_REMOTE_SYSTEM_ID);
        fail("Shouldn't work for an invalid system");
    } catch (NoSuchSystemException e) {
    }
    try {
        remoteAlfrescoTicketService.storeRemoteCredentials(INVALID_REMOTE_SYSTEM_ID, null, null);
        fail("Shouldn't work for an invalid system");
    } catch (NoSuchSystemException e) {
    }
    // Our user starts out without credentials
    BaseCredentialsInfo credentials = remoteAlfrescoTicketService.getRemoteCredentials(TEST_REMOTE_SYSTEM_ID);
    assertEquals(null, credentials);
    // Try to store some invalid credentials (real user, but password wrong)
    try {
        remoteAlfrescoTicketService.storeRemoteCredentials(TEST_REMOTE_SYSTEM_ID, USER_ONE, "invalid");
        fail("Credentials invalid, shouldn't be allowed");
    } catch (AuthenticationException e) {
    }
    // And an invalid user
    try {
        remoteAlfrescoTicketService.storeRemoteCredentials(TEST_REMOTE_SYSTEM_ID, "thisUSERdoesNOTexist", "invalid");
        fail("Credentials invalid, shouldn't be allowed");
    } catch (AuthenticationException e) {
    }
    // Still none there
    credentials = remoteAlfrescoTicketService.getRemoteCredentials(TEST_REMOTE_SYSTEM_ID);
    assertEquals(null, credentials);
    // Store some valid ones
    credentials = remoteAlfrescoTicketService.storeRemoteCredentials(TEST_REMOTE_SYSTEM_ID, USER_ONE, PASSWORD);
    assertNotNull(credentials);
    assertEquals(TEST_REMOTE_SYSTEM_ID, credentials.getRemoteSystemName());
    assertEquals(USER_ONE, credentials.getRemoteUsername());
    // Check we can find them
    credentials = remoteAlfrescoTicketService.getRemoteCredentials(TEST_REMOTE_SYSTEM_ID);
    assertNotNull(credentials);
    assertEquals(TEST_REMOTE_SYSTEM_ID, credentials.getRemoteSystemName());
    assertEquals(USER_ONE, credentials.getRemoteUsername());
    // Store some different, valid credentials for the user
    credentials = remoteAlfrescoTicketService.storeRemoteCredentials(TEST_REMOTE_SYSTEM_ID, USER_TWO, PASSWORD);
    assertNotNull(credentials);
    assertEquals(TEST_REMOTE_SYSTEM_ID, credentials.getRemoteSystemName());
    assertEquals(USER_TWO, credentials.getRemoteUsername());
    // Check we see the change
    credentials = remoteAlfrescoTicketService.getRemoteCredentials(TEST_REMOTE_SYSTEM_ID);
    assertNotNull(credentials);
    assertEquals(TEST_REMOTE_SYSTEM_ID, credentials.getRemoteSystemName());
    assertEquals(USER_TWO, credentials.getRemoteUsername());
    // Switch to the other user, no credentials there
    AuthenticationUtil.setFullyAuthenticatedUser(USER_TWO);
    credentials = remoteAlfrescoTicketService.getRemoteCredentials(TEST_REMOTE_SYSTEM_ID);
    assertEquals(null, credentials);
    // Switch back, and delete
    AuthenticationUtil.setFullyAuthenticatedUser(USER_ONE);
    credentials = remoteAlfrescoTicketService.getRemoteCredentials(TEST_REMOTE_SYSTEM_ID);
    assertNotNull(credentials);
    boolean deleted = remoteAlfrescoTicketService.deleteRemoteCredentials(TEST_REMOTE_SYSTEM_ID);
    assertEquals(true, deleted);
    // Will have gone
    credentials = remoteAlfrescoTicketService.getRemoteCredentials(TEST_REMOTE_SYSTEM_ID);
    assertEquals(null, credentials);
    // Double delete is reported
    deleted = remoteAlfrescoTicketService.deleteRemoteCredentials(TEST_REMOTE_SYSTEM_ID);
    assertEquals(false, deleted);
}
Also used : NoSuchSystemException(org.alfresco.service.cmr.remoteticket.NoSuchSystemException) BaseCredentialsInfo(org.alfresco.service.cmr.remotecredentials.BaseCredentialsInfo) AuthenticationException(org.alfresco.repo.security.authentication.AuthenticationException)

Aggregations

AuthenticationException (org.alfresco.repo.security.authentication.AuthenticationException)29 SessionUser (org.alfresco.repo.SessionUser)15 HttpSession (javax.servlet.http.HttpSession)9 IOException (java.io.IOException)8 User (org.alfresco.web.bean.repository.User)8 HashMap (java.util.HashMap)5 AuthenticationService (org.alfresco.service.cmr.security.AuthenticationService)5 WebApplicationContext (org.springframework.web.context.WebApplicationContext)5 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)4 NodeRef (org.alfresco.service.cmr.repository.NodeRef)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 RetryingTransactionHelper (org.alfresco.repo.transaction.RetryingTransactionHelper)3 TicketCredentials (org.alfresco.repo.web.auth.TicketCredentials)3 Serializable (java.io.Serializable)2 UnknownHostException (java.net.UnknownHostException)2 Matcher (java.util.regex.Matcher)2 FacesContext (javax.faces.context.FacesContext)2 PortletException (javax.portlet.PortletException)2 PortletSession (javax.portlet.PortletSession)2