Search in sources :

Example 11 with BasicUserPrincipal

use of org.apache.http.auth.BasicUserPrincipal in project jeeshop by remibantos.

the class UsersCT method resetPassword_shouldUpdateUserPasswordForAuthenticatedUser.

@Test
public void resetPassword_shouldUpdateUserPasswordForAuthenticatedUser() throws Exception {
    User user = notActivatedTestUser();
    when(sessionContextMock.isUserInRole(JeeshopRoles.USER)).thenReturn(true);
    when(sessionContextMock.getUserPrincipal()).thenReturn(new BasicUserPrincipal(user.getLogin()));
    service.resetPassword(sessionContextMock, user.getLogin(), null, "newPassword");
    final User updatedUser = entityManager.find(User.class, user.getId());
    assertThat(updatedUser).isNotNull();
    assertThat(updatedUser.getPassword()).isEqualTo(hashSha256Base64("newPassword"));
    verify(mailerMock).sendMail(testMailTemplate.changePasswordMailTpl().getSubject(), user.getLogin(), testMailTemplate.changePasswordMailTpl().getContent());
    removeTestUser(user);
}
Also used : TestUser(org.rembx.jeeshop.user.test.TestUser) User(org.rembx.jeeshop.user.model.User) BasicUserPrincipal(org.apache.http.auth.BasicUserPrincipal) Test(org.junit.jupiter.api.Test)

Example 12 with BasicUserPrincipal

use of org.apache.http.auth.BasicUserPrincipal in project jeeshop by remibantos.

the class UsersCT method modify_ShouldThrowUnauthorizedError_WhenAuthenticatedUserDoesNotMatchLogin.

@Test
public void modify_ShouldThrowUnauthorizedError_WhenAuthenticatedUserDoesNotMatchLogin() throws Exception {
    User detachedUserToModify = new User("test2@test.com", "test", "John", "Doe", "+33616161616", null, new Date(), "fr_FR", null);
    try {
        when(sessionContextMock.isUserInRole(JeeshopRoles.USER)).thenReturn(true);
        when(sessionContextMock.getUserPrincipal()).thenReturn(new BasicUserPrincipal(testUser.firstUser().getLogin()));
        service.modify(sessionContextMock, detachedUserToModify);
        fail("should have thrown ex");
    } catch (WebApplicationException e) {
        assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.UNAUTHORIZED);
    }
}
Also used : TestUser(org.rembx.jeeshop.user.test.TestUser) User(org.rembx.jeeshop.user.model.User) BasicUserPrincipal(org.apache.http.auth.BasicUserPrincipal) WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 13 with BasicUserPrincipal

use of org.apache.http.auth.BasicUserPrincipal in project lucene-solr by apache.

the class TestPKIAuthenticationPlugin method test.

public void test() throws Exception {
    AtomicReference<Principal> principal = new AtomicReference<>();
    String nodeName = "node_x_233";
    final MockPKIAuthenticationPlugin mock = new MockPKIAuthenticationPlugin(null, nodeName);
    LocalSolrQueryRequest localSolrQueryRequest = new LocalSolrQueryRequest(null, new ModifiableSolrParams()) {

        @Override
        public Principal getUserPrincipal() {
            return principal.get();
        }
    };
    PublicKey correctKey = CryptoKeys.deserializeX509PublicKey(mock.getPublicKey());
    mock.remoteKeys.put(nodeName, correctKey);
    principal.set(new BasicUserPrincipal("solr"));
    mock.solrRequestInfo = new SolrRequestInfo(localSolrQueryRequest, new SolrQueryResponse());
    BasicHttpRequest request = new BasicHttpRequest("GET", "http://localhost:56565");
    mock.setHeader(request);
    final AtomicReference<Header> header = new AtomicReference<>();
    header.set(request.getFirstHeader(PKIAuthenticationPlugin.HEADER));
    assertNotNull(header.get());
    assertTrue(header.get().getValue().startsWith(nodeName));
    final AtomicReference<ServletRequest> wrappedRequestByFilter = new AtomicReference<>();
    HttpServletRequest mockReq = createMockRequest(header);
    FilterChain filterChain = (servletRequest, servletResponse) -> wrappedRequestByFilter.set(servletRequest);
    mock.doAuthenticate(mockReq, null, filterChain);
    assertNotNull(wrappedRequestByFilter.get());
    assertEquals("solr", ((HttpServletRequest) wrappedRequestByFilter.get()).getUserPrincipal().getName());
    //test 2
    // no user
    principal.set(null);
    header.set(null);
    //
    wrappedRequestByFilter.set(null);
    request = new BasicHttpRequest("GET", "http://localhost:56565");
    mock.setHeader(request);
    assertNull(request.getFirstHeader(PKIAuthenticationPlugin.HEADER));
    mock.doAuthenticate(mockReq, null, filterChain);
    assertNotNull(wrappedRequestByFilter.get());
    assertNull(((HttpServletRequest) wrappedRequestByFilter.get()).getUserPrincipal());
    //test 3 . No user request . Request originated from Solr
    //create pub key in advance because it can take time and it should be
    //created before the header is set
    PublicKey key = new CryptoKeys.RSAKeyPair().getPublicKey();
    mock.solrRequestInfo = null;
    header.set(null);
    wrappedRequestByFilter.set(null);
    request = new BasicHttpRequest("GET", "http://localhost:56565");
    mock.setHeader(request);
    header.set(request.getFirstHeader(PKIAuthenticationPlugin.HEADER));
    assertNotNull(header.get());
    assertTrue(header.get().getValue().startsWith(nodeName));
    mock.doAuthenticate(mockReq, null, filterChain);
    assertNotNull(wrappedRequestByFilter.get());
    assertEquals("$", ((HttpServletRequest) wrappedRequestByFilter.get()).getUserPrincipal().getName());
    /*test4 mock the restart of a node*/
    MockPKIAuthenticationPlugin mock1 = new MockPKIAuthenticationPlugin(null, nodeName) {

        int called = 0;

        @Override
        PublicKey getRemotePublicKey(String nodename) {
            try {
                return called == 0 ? key : correctKey;
            } finally {
                called++;
            }
        }
    };
    mock1.doAuthenticate(mockReq, null, filterChain);
    assertNotNull(wrappedRequestByFilter.get());
    assertEquals("$", ((HttpServletRequest) wrappedRequestByFilter.get()).getUserPrincipal().getName());
}
Also used : FilterChain(javax.servlet.FilterChain) ServletRequest(javax.servlet.ServletRequest) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) PublicKey(java.security.PublicKey) HashMap(java.util.HashMap) BasicUserPrincipal(org.apache.http.auth.BasicUserPrincipal) CoreContainer(org.apache.solr.core.CoreContainer) AtomicReference(java.util.concurrent.atomic.AtomicReference) Header(org.apache.http.Header) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrTestCaseJ4(org.apache.solr.SolrTestCaseJ4) BasicHttpRequest(org.apache.http.message.BasicHttpRequest) Mockito(org.mockito.Mockito) HttpServletRequest(javax.servlet.http.HttpServletRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) Principal(java.security.Principal) Map(java.util.Map) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) CryptoKeys(org.apache.solr.util.CryptoKeys) ServletRequest(javax.servlet.ServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) BasicUserPrincipal(org.apache.http.auth.BasicUserPrincipal) PublicKey(java.security.PublicKey) FilterChain(javax.servlet.FilterChain) AtomicReference(java.util.concurrent.atomic.AtomicReference) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) BasicHttpRequest(org.apache.http.message.BasicHttpRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) Header(org.apache.http.Header) CryptoKeys(org.apache.solr.util.CryptoKeys) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) BasicUserPrincipal(org.apache.http.auth.BasicUserPrincipal) Principal(java.security.Principal)

Example 14 with BasicUserPrincipal

use of org.apache.http.auth.BasicUserPrincipal in project jeeshop by remibantos.

the class OrdersCT method findAll_whenClientHasUserRoleOnlyAndwithPagination_shouldReturnNoneEmptyListPaginated.

@Test
public void findAll_whenClientHasUserRoleOnlyAndwithPagination_shouldReturnNoneEmptyListPaginated() {
    when(sessionContextMock.isUserInRole(JeeshopRoles.USER)).thenReturn(true);
    when(sessionContextMock.isUserInRole(JeeshopRoles.ADMIN)).thenReturn(false);
    when(sessionContextMock.getUserPrincipal()).thenReturn(new BasicUserPrincipal(testOrder.firstOrdersUser().getLogin()));
    List<Order> orders = service.findAll(sessionContextMock, null, 0, 1, null, null, null, null, null);
    assertThat(orders).isNotEmpty();
    assertThat(orders).containsExactly(testOrder.firstOrder());
}
Also used : Order(org.rembx.jeeshop.order.model.Order) TestOrder(org.rembx.jeeshop.order.test.TestOrder) BasicUserPrincipal(org.apache.http.auth.BasicUserPrincipal) Test(org.junit.jupiter.api.Test)

Example 15 with BasicUserPrincipal

use of org.apache.http.auth.BasicUserPrincipal in project jeeshop by remibantos.

the class OrdersCT method find_whenClientHasUserRoleAndOrderBelongsToAnotherUser_ShouldThrowException.

@Test
public void find_whenClientHasUserRoleAndOrderBelongsToAnotherUser_ShouldThrowException() throws Exception {
    entityManager.getTransaction().begin();
    User user = new User("777@test.com", "test", "M.", "John", "Doe", "+33616161616", null, null, "fr_FR", null);
    entityManager.persist(user);
    entityManager.getTransaction().commit();
    when(sessionContextMock.isUserInRole(JeeshopRoles.USER)).thenReturn(true);
    when(sessionContextMock.isUserInRole(JeeshopRoles.ADMIN)).thenReturn(false);
    when(sessionContextMock.getUserPrincipal()).thenReturn(new BasicUserPrincipal("777@test.com"));
    try {
        service.find(sessionContextMock, 1L, null);
        fail("should have thrown ex");
    } catch (WebApplicationException e) {
        assertThat(e.getResponse().getStatusInfo()).isEqualTo(Response.Status.UNAUTHORIZED);
    } finally {
        entityManager.getTransaction().begin();
        entityManager.remove(user);
        entityManager.persist(user);
    }
}
Also used : TestUser(org.rembx.jeeshop.user.test.TestUser) User(org.rembx.jeeshop.user.model.User) BasicUserPrincipal(org.apache.http.auth.BasicUserPrincipal) WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) Test(org.junit.jupiter.api.Test)

Aggregations

BasicUserPrincipal (org.apache.http.auth.BasicUserPrincipal)21 Test (org.junit.jupiter.api.Test)12 Order (org.rembx.jeeshop.order.model.Order)6 TestOrder (org.rembx.jeeshop.order.test.TestOrder)6 Principal (java.security.Principal)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 WebApplicationException (org.rembx.jeeshop.rest.WebApplicationException)3 User (org.rembx.jeeshop.user.model.User)3 TestUser (org.rembx.jeeshop.user.test.TestUser)3 HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)2 Address (org.rembx.jeeshop.user.model.Address)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 PublicKey (java.security.PublicKey)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 StringTokenizer (java.util.StringTokenizer)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Callback (javax.security.auth.callback.Callback)1