Example usage for org.apache.http.auth BasicUserPrincipal BasicUserPrincipal

List of usage examples for org.apache.http.auth BasicUserPrincipal BasicUserPrincipal

Introduction

In this page you can find the example usage for org.apache.http.auth BasicUserPrincipal BasicUserPrincipal.

Prototype

public BasicUserPrincipal(final String username) 

Source Link

Usage

From source file:org.hawk.service.api.dt.http.LazyCredentials.java

@Override
public Principal getUserPrincipal() {
    if (principal == null) {
        getCredentials();/*w w  w .j  ava2s.co m*/
        if (principal == null) {
            return new BasicUserPrincipal("");
        }
    }
    return principal;
}

From source file:org.fcrepo.auth.integration.ModeShapeHonorsPEPResponseIT.java

@Test
public void testPermissivePEP() throws RepositoryException {
    when(request.getRemoteUser()).thenReturn("fred");
    when(request.getUserPrincipal()).thenReturn(new BasicUserPrincipal("fred"));
    when(request.isUserInRole(Mockito.eq(ServletContainerAuthenticationProvider.FEDORA_USER_ROLE)))
            .thenReturn(true);/* w w w  .j a va  2 s .  co  m*/
    Mockito.reset(pep);
    when(pep.hasModeShapePermission(any(Path.class), any(String[].class), Matchers.<Set<Principal>>any(),
            any(Principal.class))).thenReturn(true);

    final ServletCredentials credentials = new ServletCredentials(request);
    final Session session = repo.login(credentials);
    final Privilege[] rootPrivs = session.getAccessControlManager().getPrivileges("/");
    for (final Privilege p : rootPrivs) {
        logger.debug("got priv: " + p.getName());
    }
    final ObjectService os = new ObjectService();
    final FedoraObject fo = os.createObject(session, "/myobject");
    verify(pep, times(5)).hasModeShapePermission(any(Path.class), any(String[].class),
            Matchers.<Set<Principal>>any(), any(Principal.class));
}

From source file:org.fcrepo.auth.integration.DelegatedUserIT.java

@Test
public void testDelegatedUserAccess() throws RepositoryException {

    // mock request by an admin user, on behalf of a regular user
    when(request.getRemoteUser()).thenReturn("admin1");
    when(request.getUserPrincipal()).thenReturn(new BasicUserPrincipal("admin1"));
    when(request.isUserInRole(eq(ServletContainerAuthenticationProvider.FEDORA_ADMIN_ROLE))).thenReturn(true);
    when(request.getHeader("On-Behalf-Of")).thenReturn("user1");

    Mockito.reset(fad);/*from   www.  ja v  a 2s  . co m*/
    // set up a restrictive mock FAD, which should deny non-admin users
    when(fad.hasPermission(any(Session.class), any(Path.class), any(String[].class))).thenReturn(false);

    final ServletCredentials credentials = new ServletCredentials(request);
    final FedoraSession session = repo.login(credentials);
    final Session jcrSession = getJcrSession(session);
    assertEquals("Session user principal is user1", "user1",
            ((Principal) jcrSession.getAttribute(FedoraAuthorizationDelegate.FEDORA_USER_PRINCIPAL)).getName());

    // try to create an object, this should fail because it is being executed as a non-admin user
    final ContainerService os = new ContainerServiceImpl();
    try {
        os.findOrCreate(session, "/myobject");
    } catch (final RepositoryRuntimeException e) {
        final Throwable cause = e.getCause();
        if (cause != null && cause instanceof AccessDeniedException) {
            logger.debug("caught expected access denied exception");
        } else {
            throw e;
        }
    }
    verify(fad, atLeastOnce()).hasPermission(any(Session.class), any(Path.class), any(String[].class));
}

From source file:org.fcrepo.integration.kernel.modeshape.FedoraSessionImplIT.java

@Test
public void testGetIdWithUserIdNonURI() throws RepositoryException {
    // Set basic URI for user agent with environment variable: fcrepo.auth.webac.userAgent.baseUri
    System.setProperty(FedoraSessionUserUtil.USER_AGENT_BASE_URI_PROPERTY, TEST_USER_AGENT_BASE_URI);

    when(request.getRemoteUser()).thenReturn(FEDORA_USER);
    when(request.getUserPrincipal()).thenReturn(new BasicUserPrincipal(FEDORA_USER));
    when(request.isUserInRole(eq("admin"))).thenReturn(true);

    final ServletCredentials credentials = new ServletCredentials(request);
    final FedoraSession session = repo.login(credentials);

    assertEquals("User agent URI invalid.", URI.create(TEST_USER_AGENT_BASE_URI + FEDORA_USER),
            session.getUserURI());/*from  w ww  . java2  s . c o  m*/
}

From source file:org.fcrepo.auth.integration.ModeShapeHonorsFADResponseIT.java

@Test
public void testPermissiveFAD() throws RepositoryException {
    when(request.getRemoteUser()).thenReturn("fred");
    when(request.getUserPrincipal()).thenReturn(new BasicUserPrincipal("fred"));
    when(request.isUserInRole(Mockito.eq(ServletContainerAuthenticationProvider.FEDORA_USER_ROLE)))
            .thenReturn(true);/*w w  w .  j  a  va  2 s .c o  m*/
    Mockito.reset(fad);
    when(fad.hasPermission(any(Session.class), any(Path.class), any(String[].class))).thenReturn(true);

    final ServletCredentials credentials = new ServletCredentials(request);
    final FedoraSession session = repo.login(credentials);
    final Session jcrSession = getJcrSession(session);
    final Privilege[] rootPrivs = jcrSession.getAccessControlManager().getPrivileges("/");
    for (final Privilege p : rootPrivs) {
        logger.debug("got priv: " + p.getName());
    }
    final ContainerService os = new ContainerServiceImpl();
    os.findOrCreate(session, "/myobject");
    verify(fad, atLeastOnce()).hasPermission(any(Session.class), any(Path.class), any(String[].class));
}

From source file:org.restheart.test.integration.SecurityAuthTokenIT.java

@Test
public void testAuthToken() throws Exception {
    Response resp = adminExecutor.execute(Request.Get(rootUri));

    HttpResponse httpResp = resp.returnResponse();
    assertNotNull(httpResp);//  www .  j ava  2s  . c o m

    StatusLine statusLine = httpResp.getStatusLine();
    assertNotNull(statusLine);

    assertEquals("check authorized", HttpStatus.SC_OK, statusLine.getStatusCode());

    Header[] _authToken = httpResp.getHeaders(AUTH_TOKEN_HEADER.toString());
    Header[] _authTokenValid = httpResp.getHeaders(AUTH_TOKEN_VALID_HEADER.toString());
    Header[] _authTokenLocation = httpResp.getHeaders(AUTH_TOKEN_LOCATION_HEADER.toString());

    assertNotNull("check not null auth token header", _authToken);
    assertNotNull("check not null auth token valid header", _authTokenValid);
    assertNotNull("check not null auth token location header", _authTokenLocation);

    assertTrue("check not empty array auth token header array ", _authToken.length == 1);
    assertTrue("check not empty array auth token valid header array", _authTokenValid.length == 1);
    assertTrue("check not empty array auth token location header array", _authTokenLocation.length == 1);

    assertTrue("check not empty array auth token header value not null or empty",
            _authToken[0] != null && _authToken[0].getValue() != null && !_authToken[0].getValue().isEmpty());
    assertTrue("check not empty array auth token valid value not null or empty", _authTokenValid[0] != null
            && _authTokenValid[0].getValue() != null && !_authTokenValid[0].getValue().isEmpty());
    assertTrue("check not empty array auth token location  not null or empty", _authTokenLocation[0] != null
            && _authTokenLocation[0].getValue() != null && !_authTokenLocation[0].getValue().isEmpty());

    Response resp2 = unauthExecutor.auth(new Credentials() {
        @Override
        public Principal getUserPrincipal() {
            return new BasicUserPrincipal("admin");
        }

        @Override
        public String getPassword() {
            return _authToken[0].getValue();
        }
    }).execute(Request.Get(rootUri));

    HttpResponse httpResp2 = resp2.returnResponse();
    assertNotNull(httpResp2);

    StatusLine statusLine2 = httpResp2.getStatusLine();
    assertNotNull(statusLine2);

    assertEquals("check authorized via auth token", HttpStatus.SC_OK, statusLine2.getStatusCode());
}

From source file:org.fcrepo.integration.kernel.modeshape.FedoraSessionImplIT.java

@Test
public void testGetIdWithUserIdURI() throws RepositoryException {

    // test with an absolute user uri
    final String userUri = TEST_USER_AGENT_BASE_URI + FEDORA_USER;
    when(request.getRemoteUser()).thenReturn(userUri);
    when(request.getUserPrincipal()).thenReturn(new BasicUserPrincipal(userUri));
    when(request.isUserInRole(eq("admin"))).thenReturn(true);

    ServletCredentials credentials = new ServletCredentials(request);
    FedoraSession session = repo.login(credentials);

    assertEquals("User agent URI invalid.", URI.create(userUri), session.getUserURI());

    // test with an Opaque user uri
    final String opaqueUserUri = "user:info:" + FEDORA_USER;
    when(request.getRemoteUser()).thenReturn(opaqueUserUri);
    when(request.getUserPrincipal()).thenReturn(new BasicUserPrincipal(opaqueUserUri));
    when(request.isUserInRole(eq("admin"))).thenReturn(true);

    credentials = new ServletCredentials(request);
    session = repo.login(credentials);/*  ww  w . ja  v a2s . co m*/

    assertEquals("User agent URI invalid.", URI.create(opaqueUserUri), session.getUserURI());
}

From source file:org.apache.jena.security.example.ExampleEvaluator.java

public void setPrincipal(String userName) {
    if (userName == null) {
        principal = null;
    }
    principal = new BasicUserPrincipal(userName);
}

From source file:org.fcrepo.auth.integration.ModeShapeHonorsPEPResponseIT.java

@Test(expected = AccessControlException.class)
public void testRestrictivePEP() throws RepositoryException {
    when(request.getRemoteUser()).thenReturn("fred");
    when(request.getUserPrincipal()).thenReturn(new BasicUserPrincipal("fred"));
    when(request.isUserInRole(Mockito.eq(ServletContainerAuthenticationProvider.FEDORA_USER_ROLE)))
            .thenReturn(true);//from www.j a v  a  2 s.c o  m

    // first permission check is for login
    Mockito.reset(pep);
    when(pep.hasModeShapePermission(any(Path.class), any(String[].class), Matchers.<Set<Principal>>any(),
            any(Principal.class))).thenReturn(true, false);

    final ServletCredentials credentials = new ServletCredentials(request);
    final Session session = repo.login(credentials);
    final ObjectService os = new ObjectService();
    final FedoraObject fo = os.createObject(session, "/myobject");
    verify(pep, times(5)).hasModeShapePermission(any(Path.class), any(String[].class),
            Matchers.<Set<Principal>>any(), any(Principal.class));
}

From source file:org.fcrepo.auth.integration.ModeShapeHonorsFADResponseIT.java

@Test(expected = AccessDeniedException.class)
public void testRestrictiveFAD() throws Throwable {
    when(request.getRemoteUser()).thenReturn("fred");
    when(request.getUserPrincipal()).thenReturn(new BasicUserPrincipal("fred"));
    when(request.isUserInRole(Mockito.eq(ServletContainerAuthenticationProvider.FEDORA_USER_ROLE)))
            .thenReturn(true);/*from ww  w  .  j  a  v  a2 s .  c  o m*/

    // first permission check is for login
    Mockito.reset(fad);
    when(fad.hasPermission(any(Session.class), any(Path.class), any(String[].class))).thenReturn(true, false);

    final ServletCredentials credentials = new ServletCredentials(request);
    final FedoraSession session = repo.login(credentials);
    final ContainerService os = new ContainerServiceImpl();
    try {
        os.findOrCreate(session, "/myobject");
    } catch (final RepositoryRuntimeException e) {
        throw e.getCause();
    }
    verify(fad, times(5)).hasPermission(any(Session.class), any(Path.class), any(String[].class));
}