Search in sources :

Example 1 with BasicUserPrincipal

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

the class BasicAuthPlugin method doAuthenticate.

@Override
public boolean doAuthenticate(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws Exception {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;
    String authHeader = request.getHeader("Authorization");
    if (authHeader != null) {
        BasicAuthPlugin.authHeader.set(new BasicHeader("Authorization", authHeader));
        StringTokenizer st = new StringTokenizer(authHeader);
        if (st.hasMoreTokens()) {
            String basic = st.nextToken();
            if (basic.equalsIgnoreCase("Basic")) {
                try {
                    String credentials = new String(Base64.decodeBase64(st.nextToken()), "UTF-8");
                    int p = credentials.indexOf(":");
                    if (p != -1) {
                        final String username = credentials.substring(0, p).trim();
                        String pwd = credentials.substring(p + 1).trim();
                        if (!authenticate(username, pwd)) {
                            log.debug("Bad auth credentials supplied in Authorization header");
                            authenticationFailure(response, "Bad credentials");
                        } else {
                            HttpServletRequestWrapper wrapper = new HttpServletRequestWrapper(request) {

                                @Override
                                public Principal getUserPrincipal() {
                                    return new BasicUserPrincipal(username);
                                }
                            };
                            filterChain.doFilter(wrapper, response);
                            return true;
                        }
                    } else {
                        authenticationFailure(response, "Invalid authentication token");
                    }
                } catch (UnsupportedEncodingException e) {
                    throw new Error("Couldn't retrieve authentication", e);
                }
            }
        }
    } else {
        if (blockUnknown) {
            authenticationFailure(response, "require authentication");
        } else {
            request.setAttribute(AuthenticationPlugin.class.getName(), authenticationProvider.getPromptHeaders());
            filterChain.doFilter(request, response);
            return true;
        }
    }
    return false;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) StringTokenizer(java.util.StringTokenizer) BasicUserPrincipal(org.apache.http.auth.BasicUserPrincipal) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) UnsupportedEncodingException(java.io.UnsupportedEncodingException) BasicHeader(org.apache.http.message.BasicHeader)

Example 2 with BasicUserPrincipal

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

the class PKIAuthenticationPlugin method doAuthenticate.

@SuppressForbidden(reason = "Needs currentTimeMillis to compare against time in header")
@Override
public boolean doAuthenticate(ServletRequest request, ServletResponse response, FilterChain filterChain) throws Exception {
    String requestURI = ((HttpServletRequest) request).getRequestURI();
    if (requestURI.endsWith(PATH)) {
        filterChain.doFilter(request, response);
        return true;
    }
    long receivedTime = System.currentTimeMillis();
    String header = ((HttpServletRequest) request).getHeader(HEADER);
    if (header == null) {
        //this must not happen
        log.error("No SolrAuth header present");
        filterChain.doFilter(request, response);
        return true;
    }
    List<String> authInfo = StrUtils.splitWS(header, false);
    if (authInfo.size() < 2) {
        log.error("Invalid SolrAuth Header {}", header);
        filterChain.doFilter(request, response);
        return true;
    }
    String nodeName = authInfo.get(0);
    String cipher = authInfo.get(1);
    PKIHeaderData decipher = decipherHeader(nodeName, cipher);
    if (decipher == null) {
        log.error("Could not decipher a header {} . No principal set", header);
        filterChain.doFilter(request, response);
        return true;
    }
    if ((receivedTime - decipher.timestamp) > MAX_VALIDITY) {
        log.error("Invalid key request timestamp: {} , received timestamp: {} , TTL: {}", decipher.timestamp, receivedTime, MAX_VALIDITY);
        filterChain.doFilter(request, response);
        return true;
    }
    final Principal principal = "$".equals(decipher.userName) ? SU : new BasicUserPrincipal(decipher.userName);
    filterChain.doFilter(getWrapper((HttpServletRequest) request, principal), response);
    return true;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BasicUserPrincipal(org.apache.http.auth.BasicUserPrincipal) BasicUserPrincipal(org.apache.http.auth.BasicUserPrincipal) Principal(java.security.Principal) SuppressForbidden(org.apache.solr.common.util.SuppressForbidden)

Example 3 with BasicUserPrincipal

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

the class MockAuthenticationPlugin method forward.

protected void forward(String user, ServletRequest req, ServletResponse rsp, FilterChain chain) throws IOException, ServletException {
    if (user != null) {
        final Principal p = new BasicUserPrincipal(user);
        req = new HttpServletRequestWrapper((HttpServletRequest) req) {

            @Override
            public Principal getUserPrincipal() {
                return p;
            }
        };
    }
    chain.doFilter(req, rsp);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) BasicUserPrincipal(org.apache.http.auth.BasicUserPrincipal) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) Principal(java.security.Principal) BasicUserPrincipal(org.apache.http.auth.BasicUserPrincipal)

Example 4 with BasicUserPrincipal

use of org.apache.http.auth.BasicUserPrincipal in project cas by apereo.

the class MockLoginModule method login.

@Override
public boolean login() throws LoginException {
    val callbacks = new Callback[] { new NameCallback("f"), new PasswordCallback("f", false) };
    try {
        this.callbackHandler.handle(callbacks);
    } catch (final Exception e) {
        throw new LoginException();
    }
    val userName = ((NameCallback) callbacks[0]).getName();
    val password = new String(((PasswordCallback) callbacks[1]).getPassword());
    if ("test".equals(userName) && "test".equals(password)) {
        this.subject.getPrincipals().add(new BasicUserPrincipal(userName));
        return true;
    }
    throw new LoginException();
}
Also used : lombok.val(lombok.val) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) BasicUserPrincipal(org.apache.http.auth.BasicUserPrincipal) PasswordCallback(javax.security.auth.callback.PasswordCallback) LoginException(javax.security.auth.login.LoginException) LoginException(javax.security.auth.login.LoginException)

Example 5 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)

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