List of usage examples for javax.servlet.http HttpServletRequest BASIC_AUTH
String BASIC_AUTH
To view the source code for javax.servlet.http HttpServletRequest BASIC_AUTH.
Click Source Link
From source file:org.apache.sling.httpauth.impl.AuthorizationHeaderAuthenticationHandler.java
/** * Extract the Base64 authentication string from the request *//*w ww.j ava 2 s. c o m*/ protected AuthenticationInfo extractAuthentication(HttpServletRequest request) { // Return immediately if the header is missing String authHeader = request.getHeader(HEADER_AUTHORIZATION); if (authHeader == null || authHeader.length() == 0) { // try to fall back to cookies Cookie[] cookies = request.getCookies(); if (cookies != null) { for (int i = 0; i < cookies.length; i++) { if (HEADER_AUTHORIZATION.equalsIgnoreCase(cookies[i].getName())) { authHeader = cookies[i].getValue(); break; } } } // If still no authentication, return null if (authHeader == null || authHeader.length() == 0) { return null; } } // Get the authType (Basic, Digest) and authInfo (user/password) from // the header authHeader = authHeader.trim(); int blank = authHeader.indexOf(' '); if (blank <= 0) { return null; } String authType = authHeader.substring(0, blank); String authInfo = authHeader.substring(blank).trim(); // Check whether authorization type matches if (!authType.equalsIgnoreCase(AUTHENTICATION_SCHEME_BASIC)) { return null; } // Base64 decode and split on colon // we cannot use default base64, since we need iso encoding // (nb: ISO-8859-1 is required as per API spec to be available) String decoded; try { byte[] encoded = authInfo.getBytes("ISO-8859-1"); byte[] bytes = Base64.decodeBase64(encoded); decoded = new String(bytes, "ISO-8859-1"); } catch (UnsupportedEncodingException uee) { // unexpected log.error("extractAuthentication: Cannot en/decode authentication info", uee); return null; } SimpleCredentials creds; int colIdx = decoded.indexOf(':'); if (colIdx < 0) { creds = new SimpleCredentials(decoded, new char[0]); } else { creds = new SimpleCredentials(decoded.substring(0, colIdx), decoded.substring(colIdx + 1).toCharArray()); } if (NOT_LOGGED_IN_USER.equals(creds.getUserID())) { return null; } return new AuthenticationInfo(HttpServletRequest.BASIC_AUTH, creds); }
From source file:org.fusesource.fabric.maven.impl.MavenSecureHttpContext.java
public boolean authenticate(HttpServletRequest request, HttpServletResponse response) { // Return immediately if the header is missing String authHeader = request.getHeader(HEADER_AUTHORIZATION); if (authHeader != null && authHeader.length() > 0) { // Get the authType (Basic, Digest) and authInfo (user/password) // from the header authHeader = authHeader.trim();/*from ww w. j a v a 2s. c o m*/ int blank = authHeader.indexOf(' '); if (blank > 0) { String authType = authHeader.substring(0, blank); String authInfo = authHeader.substring(blank).trim(); // Check whether authorization type matches if (authType.equalsIgnoreCase(AUTHENTICATION_SCHEME_BASIC)) { try { String srcString = base64Decode(authInfo); int i = srcString.indexOf(':'); String username = srcString.substring(0, i); String password = srcString.substring(i + 1); // authenticate Subject subject = doAuthenticate(username, password); if (subject != null) { // as per the spec, set attributes request.setAttribute(HttpContext.AUTHENTICATION_TYPE, HttpServletRequest.BASIC_AUTH); request.setAttribute(HttpContext.REMOTE_USER, username); // succeed return true; } } catch (Exception e) { // Ignore } } } } // request authentication try { response.setHeader(HEADER_WWW_AUTHENTICATE, AUTHENTICATION_SCHEME_BASIC + " realm=\"" + this.realm + "\""); response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); response.setContentLength(0); response.flushBuffer(); } catch (IOException ioe) { // failed sending the response ... cannot do anything about it } // inform HttpService that authentication failed return false; }
From source file:org.iavante.sling.commons.services.impl.AuthToolsServiceImpl.java
@Override public AuthenticationInfo extractAuthentication(HttpServletRequest request) { // Return immediately if the header is missing String authHeader = request.getHeader(HEADER_AUTHORIZATION); log.info("Auth header" + authHeader); if (authHeader == null || authHeader.length() == 0) { // try to fall back to cookies Cookie[] cookies = request.getCookies(); if (cookies != null) { for (int i = 0; i < cookies.length; i++) { if (HEADER_AUTHORIZATION.equalsIgnoreCase(cookies[i].getName())) { authHeader = cookies[i].getValue(); break; }//from w ww .ja v a 2 s . c o m } } // If still no authentication, return null if (authHeader == null || authHeader.length() == 0) { return null; } } // Get the authType (Basic, Digest) and authInfo (user/password) from // the header authHeader = authHeader.trim(); int blank = authHeader.indexOf(' '); if (blank <= 0) { return null; } String authType = authHeader.substring(0, blank); String authInfo = authHeader.substring(blank).trim(); // Check whether authorization type matches if (!authType.equalsIgnoreCase(AUTHENTICATION_SCHEME_BASIC)) { return null; } // Base64 decode and split on colon // we cannot use default base64, since we need iso encoding // (nb: ISO-8859-1 is required as per API spec to be available) String decoded; try { byte[] encoded = authInfo.getBytes("ISO-8859-1"); byte[] bytes = Base64.decodeBase64(encoded); decoded = new String(bytes, "ISO-8859-1"); } catch (UnsupportedEncodingException uee) { // unexpected log.error("extractAuthentication: Cannot en/decode authentication info", uee); return null; } SimpleCredentials creds; int colIdx = decoded.indexOf(':'); if (colIdx < 0) { creds = new SimpleCredentials(decoded, new char[0]); } else { creds = new SimpleCredentials(decoded.substring(0, colIdx), decoded.substring(colIdx + 1).toCharArray()); } if (NOT_LOGGED_IN_USER.equals(creds.getUserID())) { return null; } return new AuthenticationInfo(HttpServletRequest.BASIC_AUTH, creds); }
From source file:org.iavante.uploader.ifaces.impl.AuthToolsServiceImpl.java
public AuthenticationInfo extractAuthentication(HttpServletRequest request) { // Return immediately if the header is missing String authHeader = request.getHeader(HEADER_AUTHORIZATION); if (authHeader == null || authHeader.length() == 0) { // try to fall back to cookies Cookie[] cookies = request.getCookies(); if (cookies != null) { for (int i = 0; i < cookies.length; i++) { if (HEADER_AUTHORIZATION.equalsIgnoreCase(cookies[i].getName())) { authHeader = cookies[i].getValue(); break; }//from w ww .j a v a 2 s .c o m } } // If still no authentication, return null if (authHeader == null || authHeader.length() == 0) { return null; } } // Get the authType (Basic, Digest) and authInfo (user/password) from // the header authHeader = authHeader.trim(); int blank = authHeader.indexOf(' '); if (blank <= 0) { return null; } String authType = authHeader.substring(0, blank); String authInfo = authHeader.substring(blank).trim(); // Check whether authorization type matches if (!authType.equalsIgnoreCase(AUTHENTICATION_SCHEME_BASIC)) { return null; } // Base64 decode and split on colon // we cannot use default base64, since we need iso encoding // (nb: ISO-8859-1 is required as per API spec to be available) String decoded; try { byte[] encoded = authInfo.getBytes("ISO-8859-1"); byte[] bytes = Base64.decodeBase64(encoded); decoded = new String(bytes, "ISO-8859-1"); } catch (UnsupportedEncodingException uee) { // unexpected log.error("extractAuthentication: Cannot en/decode authentication info", uee); return null; } SimpleCredentials creds; int colIdx = decoded.indexOf(':'); if (colIdx < 0) { creds = new SimpleCredentials(decoded, new char[0]); } else { creds = new SimpleCredentials(decoded.substring(0, colIdx), decoded.substring(colIdx + 1).toCharArray()); } if (NOT_LOGGED_IN_USER.equals(creds.getUserID())) { return null; } return new AuthenticationInfo(HttpServletRequest.BASIC_AUTH, creds); }
From source file:org.openhab.io.net.http.SecureHttpContext.java
/** * Sets the authentication header for BasicAuthentication and sends the * response back to the client (HTTP-StatusCode '401' UNAUTHORIZED). * //from www.ja v a2 s. c om * @param response to set the authentication header * @param realm the given <code>realm</code> * * @throws IOException if an error occurred while sending <code>response</code> */ private void sendAuthenticationHeader(HttpServletResponse response, final String realm) throws IOException { response.setHeader(HTTP_HEADER__AUTHENTICATE, HttpServletRequest.BASIC_AUTH + " realm=\"" + realm + "\""); response.sendError(HttpServletResponse.SC_UNAUTHORIZED); }
From source file:org.openhab.io.net.http.SecureHttpContext.java
/** * Parses the given <code>authHeader</code>, extracts username and password * and tries to authenticate with these credentials. If the login succeeded * it sets the appropriate headers to the <code>request</code> * //from w ww. ja v a2 s . c om * @param request * @param authHeader * @param realm * * @return <code>true</code> if the login succeeded and <code>false</code> * in all other cases. */ private boolean computeAuthHeader(HttpServletRequest request, final String authHeader, final String realm) { logger.trace("received authentication request '{}'", authHeader); String[] authHeaders = authHeader.trim().split(" "); if (authHeaders.length == 2) { String authType = StringUtils.trim(authHeaders[0]); String authInfo = StringUtils.trim(authHeaders[1]); if (HttpServletRequest.BASIC_AUTH.equalsIgnoreCase(authType)) { String authInfoString = new String(Base64.decodeBase64(authInfo)); String[] authInfos = authInfoString.split(":"); if (authInfos.length < 2) { logger.warn("authInfos '{}' must contain two elements separated by a colon", authInfoString); return false; } String username = authInfos[0]; String password = authInfos[1]; Subject subject = authenticate(realm, username, password); if (subject != null) { request.setAttribute(HttpContext.AUTHENTICATION_TYPE, HttpServletRequest.BASIC_AUTH); request.setAttribute(HttpContext.REMOTE_USER, username); logger.trace("authentication of user '{}' succeeded!", username); return true; } } else { logger.warn("we don't support '{}' authentication -> processing aborted", authType); } } else { logger.warn("authentication header '{}' must contain of two parts separated by a blank", authHeader); } return false; }
From source file:org.overlord.commons.auth.tomcat7.SAMLBearerTokenAuthenticator.java
/** * @see org.apache.catalina.authenticator.BasicAuthenticator#authenticate(org.apache.catalina.connector.Request, javax.servlet.http.HttpServletResponse, org.apache.catalina.deploy.LoginConfig) *//*from w w w . j av a 2 s . c o m*/ @Override public boolean authenticate(Request request, HttpServletResponse response, LoginConfig config) throws IOException { Principal principal = request.getUserPrincipal(); if (principal == null) { MessageBytes authorization = request.getCoyoteRequest().getMimeHeaders().getValue("authorization"); //$NON-NLS-1$ if (authorization != null) { authorization.toBytes(); ByteChunk authorizationBC = authorization.getByteChunk(); if (authorizationBC.startsWithIgnoreCase("basic ", 0)) { //$NON-NLS-1$ authorizationBC.setOffset(authorizationBC.getOffset() + 6); String b64Data = new String(authorizationBC.getBuffer(), authorizationBC.getOffset(), authorizationBC.getLength()); byte[] decoded = Base64.decodeBase64(b64Data); String data = new String(decoded, "UTF-8"); //$NON-NLS-1$ if (data.startsWith("SAML-BEARER-TOKEN:")) { //$NON-NLS-1$ try { String assertionData = data.substring(18); Document samlAssertion = DocumentUtil.getDocument(assertionData); SAMLAssertionParser parser = new SAMLAssertionParser(); XMLEventReader xmlEventReader = XMLInputFactory.newInstance() .createXMLEventReader(new StringReader(assertionData)); Object parsed = parser.parse(xmlEventReader); AssertionType assertion = (AssertionType) parsed; SAMLBearerTokenUtil.validateAssertion(assertion, request, allowedIssuers); if (signatureRequired) { KeyPair keyPair = getKeyPair(assertion); if (!SAMLBearerTokenUtil.isSAMLAssertionSignatureValid(samlAssertion, keyPair)) { throw new IOException( Messages.getString("SAMLBearerTokenAuthenticator.InvalidSignature")); //$NON-NLS-1$ } } principal = consumeAssertion(assertion); if (principal != null) { register(request, response, principal, HttpServletRequest.BASIC_AUTH, principal.getName(), null); return true; } } catch (Exception e) { e.printStackTrace(); return false; } } } authorizationBC.setOffset(authorizationBC.getOffset() - 6); } } return super.authenticate(request, response, config); }
From source file:org.sakaiproject.tool.podcasts.RSSPodfeedServlet.java
/** * If missing or invalid username/password given, return HTTP 401 to request * authentication./*from w w w . j av a2s .co m*/ * * @param response * The Response object so we can set headers * * @throws IOException * Throw this exception back if there was a problem setting the headers */ private void sendErrorResponse(final HttpServletResponse response) throws IOException { response.setHeader("WWW-Authenticate", HttpServletRequest.BASIC_AUTH + " realm=\"Podcaster\""); response.sendError(401); }
From source file:org.securityfilter.authenticator.BasicAuthenticator.java
/** * Returns BASIC as the authentication method. * * @return BASIC */ public String getAuthMethod() { return HttpServletRequest.BASIC_AUTH; }
From source file:org.sonatype.nexus.security.filter.authc.NexusHttpAuthenticationFilter.java
public void setFakeAuthScheme(String fakeAuthSchemeStr) { this.fakeAuthScheme = Boolean.parseBoolean(fakeAuthSchemeStr); if (fakeAuthScheme) { setAuthcScheme(FAKE_AUTH_SCHEME); setAuthzScheme(FAKE_AUTH_SCHEME); } else {/* w ww .jav a2s. c o m*/ setAuthcScheme(HttpServletRequest.BASIC_AUTH); setAuthzScheme(HttpServletRequest.BASIC_AUTH); } }