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;
}
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;
}
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);
}
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();
}
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);
}
Aggregations