Search in sources :

Example 1 with Authenticator

use of org.springframework.extensions.webscripts.Authenticator in project alfresco-remote-api by Alfresco.

the class RemoteAuthenticatorFactoryTest method testLogInWithNonExistingPerson.

@Test
public void testLogInWithNonExistingPerson() {
    // Random non existing person
    final String username = GUID.generate();
    // Mock a request with a username in the header
    HttpServletRequest mockHttpRequest = mock(HttpServletRequest.class);
    when(mockHttpRequest.getHeader("X-Alfresco-Remote-User")).thenReturn(username);
    when(mockHttpRequest.getScheme()).thenReturn("http");
    WebScriptServletRequest mockRequest = mock(WebScriptServletRequest.class);
    when(mockRequest.getHttpServletRequest()).thenReturn(mockHttpRequest);
    HttpServletResponse mockHttpResponse = mock(HttpServletResponse.class);
    WebScriptServletResponse mockResponse = mock(WebScriptServletResponse.class);
    when(mockResponse.getHttpServletResponse()).thenReturn(mockHttpResponse);
    Authenticator authenticator = remoteUserAuthenticatorFactory.create(mockRequest, mockResponse);
    assertTrue("The non existing user should be authenticated.", authenticator.authenticate(RequiredAuthentication.user, false));
    assertTrue("The user should be auto created.", personService.personExists(username));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) WebScriptServletResponse(org.springframework.extensions.webscripts.servlet.WebScriptServletResponse) WebScriptServletRequest(org.springframework.extensions.webscripts.servlet.WebScriptServletRequest) Authenticator(org.springframework.extensions.webscripts.Authenticator) Test(org.junit.Test)

Example 2 with Authenticator

use of org.springframework.extensions.webscripts.Authenticator in project alfresco-remote-api by Alfresco.

the class RemoteAuthenticatorFactoryTest method testDisabledUser.

@Test
public void testDisabledUser() throws Exception {
    final String username = transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<String>() {

        @Override
        public String execute() throws Throwable {
            return AuthenticationUtil.runAs(new RunAsWork<String>() {

                @Override
                public String doWork() throws Exception {
                    return createPerson(false);
                }
            }, AuthenticationUtil.SYSTEM_USER_NAME);
        }
    }, false, true);
    transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<Void>() {

        @Override
        public Void execute() throws Throwable {
            return AuthenticationUtil.runAs(new RunAsWork<Void>() {

                @Override
                public Void doWork() throws Exception {
                    // Mock a request with a username in the header
                    HttpServletRequest mockHttpRequest = mock(HttpServletRequest.class);
                    when(mockHttpRequest.getHeader("X-Alfresco-Remote-User")).thenReturn(username);
                    when(mockHttpRequest.getScheme()).thenReturn("http");
                    WebScriptServletRequest mockRequest = mock(WebScriptServletRequest.class);
                    when(mockRequest.getHttpServletRequest()).thenReturn(mockHttpRequest);
                    HttpServletResponse mockHttpResponse = mock(HttpServletResponse.class);
                    WebScriptServletResponse mockResponse = mock(WebScriptServletResponse.class);
                    when(mockResponse.getHttpServletResponse()).thenReturn(mockHttpResponse);
                    Authenticator authenticator = remoteUserAuthenticatorFactory.create(mockRequest, mockResponse);
                    assertFalse(authenticator.authenticate(RequiredAuthentication.user, false));
                    return null;
                }
            }, AuthenticationUtil.SYSTEM_USER_NAME);
        }
    }, false, true);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork) HttpServletResponse(javax.servlet.http.HttpServletResponse) WebScriptServletResponse(org.springframework.extensions.webscripts.servlet.WebScriptServletResponse) WebScriptServletRequest(org.springframework.extensions.webscripts.servlet.WebScriptServletRequest) Authenticator(org.springframework.extensions.webscripts.Authenticator) Test(org.junit.Test)

Example 3 with Authenticator

use of org.springframework.extensions.webscripts.Authenticator in project alfresco-remote-api by Alfresco.

the class RemoteAuthenticatorFactoryTest method testEnabledUser.

@Test
public void testEnabledUser() throws Exception {
    final String username = transactionService.getRetryingTransactionHelper().doInTransaction(new RetryingTransactionCallback<String>() {

        @Override
        public String execute() throws Throwable {
            return AuthenticationUtil.runAs(new RunAsWork<String>() {

                @Override
                public String doWork() throws Exception {
                    return createPerson(true);
                }
            }, AuthenticationUtil.SYSTEM_USER_NAME);
        }
    }, false, true);
    // Mock a request with a username in the header
    HttpServletRequest mockHttpRequest = mock(HttpServletRequest.class);
    when(mockHttpRequest.getHeader("X-Alfresco-Remote-User")).thenReturn(username);
    when(mockHttpRequest.getScheme()).thenReturn("http");
    WebScriptServletRequest mockRequest = mock(WebScriptServletRequest.class);
    when(mockRequest.getHttpServletRequest()).thenReturn(mockHttpRequest);
    HttpServletResponse mockHttpResponse = mock(HttpServletResponse.class);
    WebScriptServletResponse mockResponse = mock(WebScriptServletResponse.class);
    when(mockResponse.getHttpServletResponse()).thenReturn(mockHttpResponse);
    Authenticator authenticator = remoteUserAuthenticatorFactory.create(mockRequest, mockResponse);
    assertTrue(authenticator.authenticate(RequiredAuthentication.user, false));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) RunAsWork(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork) HttpServletResponse(javax.servlet.http.HttpServletResponse) WebScriptServletResponse(org.springframework.extensions.webscripts.servlet.WebScriptServletResponse) WebScriptServletRequest(org.springframework.extensions.webscripts.servlet.WebScriptServletRequest) Authenticator(org.springframework.extensions.webscripts.Authenticator) Test(org.junit.Test)

Example 4 with Authenticator

use of org.springframework.extensions.webscripts.Authenticator in project alfresco-remote-api by Alfresco.

the class BlockingRemoteUserMapper method authenticateWithGuestParameters.

private boolean authenticateWithGuestParameters(RequiredAuthentication required, boolean isGuest) {
    blockingRemoteUserMapper.reset();
    WebScriptServletRequest mockRequest = prepareMockRequest(Collections.emptySet(), null);
    WebScriptServletResponse mockResponse = prepareMockResponse();
    Authenticator authenticator = remoteUserAuthenticatorFactory.create(mockRequest, mockResponse);
    return authenticator.authenticate(required, isGuest);
}
Also used : WebScriptServletResponse(org.springframework.extensions.webscripts.servlet.WebScriptServletResponse) WebScriptServletRequest(org.springframework.extensions.webscripts.servlet.WebScriptServletRequest) Authenticator(org.springframework.extensions.webscripts.Authenticator)

Example 5 with Authenticator

use of org.springframework.extensions.webscripts.Authenticator in project alfresco-remote-api by Alfresco.

the class BlockingRemoteUserMapper method checkExtAuthStillWorks.

private void checkExtAuthStillWorks(RequiredAuthentication required, Set<String> families) {
    blockingRemoteUserMapper.reset();
    DefaultRemoteUserMapper defaultRemoteUserMapper = new DefaultRemoteUserMapper();
    defaultRemoteUserMapper.setActive(true);
    defaultRemoteUserMapper.setProxyUserName(null);
    defaultRemoteUserMapper.setPersonService(personService);
    remoteUserAuthenticatorFactory.setRemoteUserMapper(defaultRemoteUserMapper);
    HttpServletRequest mockHttpRequest = mock(HttpServletRequest.class);
    when(mockHttpRequest.getScheme()).thenReturn("http");
    final String userName = "RAFACAT_usr_" + (int) (Math.random() * 1000);
    when(mockHttpRequest.getHeader(proxyHeader)).thenReturn(userName);
    WebScriptServletRequest mockRequest = mock(WebScriptServletRequest.class);
    when(mockRequest.getHttpServletRequest()).thenReturn(mockHttpRequest);
    WebScript mockWebScript = mock(WebScript.class);
    Match mockMatch = new Match("fake", Collections.EMPTY_MAP, "whatever", mockWebScript);
    when(mockRequest.getServiceMatch()).thenReturn(mockMatch);
    Description mockDescription = mock(Description.class);
    when(mockWebScript.getDescription()).thenReturn(mockDescription);
    when(mockDescription.getFamilys()).thenReturn(families);
    WebScriptServletResponse mockResponse = prepareMockResponse();
    Authenticator authenticator = remoteUserAuthenticatorFactory.create(mockRequest, mockResponse);
    final boolean authenticated = authenticator.authenticate(required, false);
    assertTrue("This should be authenticating with external auth", authenticated);
    assertFalse("We have been using the DefaultRemoteUserMapper, so our BlockingRemoteUserMapper shouldn't have been called", blockingRemoteUserMapper.isWasInterrupted());
    assertEquals("BlockingRemoteUserMapper shouldn't have been called", blockingRemoteUserMapper.getTimePassed(), 0);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Description(org.springframework.extensions.webscripts.Description) DefaultRemoteUserMapper(org.alfresco.repo.security.authentication.external.DefaultRemoteUserMapper) WebScriptServletResponse(org.springframework.extensions.webscripts.servlet.WebScriptServletResponse) WebScriptServletRequest(org.springframework.extensions.webscripts.servlet.WebScriptServletRequest) WebScript(org.springframework.extensions.webscripts.WebScript) Authenticator(org.springframework.extensions.webscripts.Authenticator) Match(org.springframework.extensions.webscripts.Match)

Aggregations

Authenticator (org.springframework.extensions.webscripts.Authenticator)6 WebScriptServletRequest (org.springframework.extensions.webscripts.servlet.WebScriptServletRequest)6 WebScriptServletResponse (org.springframework.extensions.webscripts.servlet.WebScriptServletResponse)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 Test (org.junit.Test)3 RunAsWork (org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork)2 DefaultRemoteUserMapper (org.alfresco.repo.security.authentication.external.DefaultRemoteUserMapper)1 Description (org.springframework.extensions.webscripts.Description)1 Match (org.springframework.extensions.webscripts.Match)1 WebScript (org.springframework.extensions.webscripts.WebScript)1