Example usage for javax.servlet.http HttpServletRequest BASIC_AUTH

List of usage examples for javax.servlet.http HttpServletRequest BASIC_AUTH

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletRequest BASIC_AUTH.

Prototype

String BASIC_AUTH

To view the source code for javax.servlet.http HttpServletRequest BASIC_AUTH.

Click Source Link

Document

String identifier for Basic authentication.

Usage

From source file:org.sventon.web.HttpBasicAuthenticationHandlerTest.java

@Test
public void testGetAuthScheme() throws Exception {
    final HttpBasicAuthenticationHandler handler = new HttpBasicAuthenticationHandler();
    assertEquals(HttpServletRequest.BASIC_AUTH, handler.getAuthScheme());
}

From source file:com.enonic.cms.web.webdav.DavSessionProviderImpl.java

private String[] getCredentials(WebdavRequest request) throws DavException {
    final String authHeader = request.getHeader(DavConstants.HEADER_AUTHORIZATION);
    if (authHeader == null) {
        return null;
    }//from w ww  .  j  a  v a  2 s .  c  om

    final String[] authStr = authHeader.split(" ");
    if (authStr.length < 2) {
        return null;
    }

    if (!authStr[0].equalsIgnoreCase(HttpServletRequest.BASIC_AUTH)) {
        return null;
    }

    final String decAuthStr = new String(Base64.decodeBase64(authStr[1].getBytes()), Charsets.ISO_8859_1);
    final int pos = decAuthStr.indexOf(':');
    final String userName = decAuthStr.substring(0, pos);
    final String password = decAuthStr.substring(pos + 1);

    return new String[] { userName, password };
}

From source file:com.enonic.cms.server.service.webdav.DavSessionProviderImpl.java

/**
 * Return the credentials./*from  w  ww. jav  a  2s .c  o  m*/
 */
private String[] getCredentials(WebdavRequest request) throws DavException {
    try {
        String authHeader = request.getHeader(DavConstants.HEADER_AUTHORIZATION);
        if (authHeader != null) {
            String[] authStr = authHeader.split(" ");
            if (authStr.length >= 2 && authStr[0].equalsIgnoreCase(HttpServletRequest.BASIC_AUTH)) {
                String decAuthStr = new String(Base64.decodeBase64(authStr[1].getBytes()), "ISO-8859-1");
                int pos = decAuthStr.indexOf(':');
                String userid = decAuthStr.substring(0, pos);
                String passwd = decAuthStr.substring(pos + 1);
                return new String[] { userid, passwd };
            }
        }

        return null;
    } catch (Exception e) {
        throw new DavException(DavServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
    }
}

From source file:org.switchyard.component.http.endpoint.StandaloneEndpointPublisher.java

/**
 * Method for get request information from a http exchange.
 *
 * @param request HttpExchange/*ww w. j  a va2s .  c  om*/
 * @param type ContentType
 * @return Request information from a http exchange
 * @throws IOException when the request information could not be read
 */
public static HttpRequestInfo getRequestInfo(HttpExchange request, ContentType type) throws IOException {
    HttpRequestInfo requestInfo = new HttpRequestInfo();

    if (request.getHttpContext().getAuthenticator() instanceof BasicAuthenticator) {
        requestInfo.setAuthType(HttpServletRequest.BASIC_AUTH);
    }
    URI u = request.getRequestURI();
    URI requestURI = null;
    try {
        requestURI = new URI(u.getScheme(), u.getUserInfo(), u.getHost(), u.getPort(), u.getPath(), null, null);
    } catch (URISyntaxException e) {
        // Strange that this could happen when copying from another URI.
        LOGGER.debug(e);
    }
    requestInfo.setCharacterEncoding(type.getCharset());
    requestInfo.setContentType(type.toString());
    requestInfo.setContextPath(request.getHttpContext().getPath());
    requestInfo.setLocalAddr(request.getLocalAddress().getAddress().getHostAddress());
    requestInfo.setLocalName(request.getLocalAddress().getAddress().getHostName());
    requestInfo.setMethod(request.getRequestMethod());
    requestInfo.setProtocol(request.getProtocol());
    requestInfo.setQueryString(u.getQuery());
    requestInfo.setRemoteAddr(request.getRemoteAddress().getAddress().getHostAddress());
    requestInfo.setRemoteHost(request.getRemoteAddress().getAddress().getHostName());
    if (request.getHttpContext().getAuthenticator() instanceof BasicAuthenticator) {
        requestInfo.setRemoteUser(request.getPrincipal().getUsername());
    }
    requestInfo.setContentLength(request.getRequestBody().available());
    // requestInfo.setRequestSessionId(request.getRequestedSessionId());
    if (requestURI != null) {
        requestInfo.setRequestURI(requestURI.toString());
    }
    requestInfo.setScheme(u.getScheme());
    requestInfo.setServerName(u.getHost());
    requestInfo.setRequestPath(u.getPath());

    // Http Query params...
    if (requestInfo.getQueryString() != null) {
        Charset charset = null;
        if (type.getCharset() != null) {
            try {
                charset = Charset.forName(type.getCharset());
            } catch (Exception exception) {
                LOGGER.debug(exception);
            }
        }
        for (NameValuePair nameValuePair : URLEncodedUtils.parse(requestInfo.getQueryString(), charset)) {
            requestInfo.addQueryParam(nameValuePair.getName(), nameValuePair.getValue());
        }
    }

    // Credentials...
    requestInfo.getCredentials().addAll(new HttpExchangeCredentialExtractor().extract(request));

    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace(requestInfo);
    }

    return requestInfo;
}

From source file:fr.paris.lutece.plugins.mylutece.modules.oauth.authentication.OAuthAuthentication.java

/**
 *
 *{@inheritDoc}
 */
public String getAuthType(HttpServletRequest request) {
    return HttpServletRequest.BASIC_AUTH;
}

From source file:io.fabric8.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();//  w  w w  .  j  a  v  a  2  s. 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 + "\"");
        // must response with status and flush as Jetty may report org.eclipse.jetty.server.Response Committed before 401 null
        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:fr.paris.lutece.plugins.mylutece.modules.database.authentication.BaseAuthentication.java

/**
 * Gets the Authentification type//from  ww  w .ja v a2s  . c  o m
 * @param request The HTTP request
 * @return The type of authentication
 */
@Override
public String getAuthType(HttpServletRequest request) {
    return HttpServletRequest.BASIC_AUTH;
}

From source file:net.lightbody.bmp.proxy.jetty.jetty.servlet.ServletHttpRequest.java

public String getAuthType() {
    String at = _httpRequest.getAuthType();
    if (at == SecurityConstraint.__BASIC_AUTH)
        return HttpServletRequest.BASIC_AUTH;
    if (at == SecurityConstraint.__FORM_AUTH)
        return HttpServletRequest.FORM_AUTH;
    if (at == SecurityConstraint.__DIGEST_AUTH)
        return HttpServletRequest.DIGEST_AUTH;
    if (at == SecurityConstraint.__CERT_AUTH)
        return HttpServletRequest.CLIENT_CERT_AUTH;
    if (at == SecurityConstraint.__CERT_AUTH2)
        return HttpServletRequest.CLIENT_CERT_AUTH;
    return at;//  w w w.j  a v a  2  s.c  o  m
}

From source file:org.apache.jackrabbit.server.JahiaBasicCredentialsProvider.java

@Override
public Credentials getCredentials(HttpServletRequest request) throws LoginException, ServletException {
    String authHeader = request.getHeader(DavConstants.HEADER_AUTHORIZATION);
    if (authHeader != null) {
        try {//w w  w. java 2s  .  com
            String[] authStr = authHeader.split(" ");
            if (authStr.length >= 2 && authStr[0].equalsIgnoreCase(HttpServletRequest.BASIC_AUTH)) {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                Base64.decode(authStr[1].toCharArray(), out);
                String decAuthStr = out.toString("ISO-8859-1");
                int pos = decAuthStr.indexOf(':');
                String userid = decAuthStr.substring(0, pos);
                String passwd = decAuthStr.substring(pos + 1);
                return createCredentials(userid, passwd.toCharArray());
            }
            throw new ServletException("Unable to decode authorization.");
        } catch (IOException e) {
            throw new ServletException("Unable to decode authorization: " + e.toString());
        }
    }

    return super.getCredentials(request);
}

From source file:org.apache.sling.auth.core.impl.HttpBasicAuthenticationHandler.java

/**
 * Extract the Base64 authentication string from the request
 *//*from w  w w . j a  v a2 s.  c o m*/
protected AuthenticationInfo extractCredentials(HttpServletRequest request) {

    // Return immediately if the header is missing
    String authHeader = request.getHeader(HEADER_AUTHORIZATION);
    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;
    }

    final int colIdx = decoded.indexOf(':');
    final String userId;
    final char[] password;
    if (colIdx < 0) {
        userId = decoded;
        password = new char[0];
    } else {
        userId = decoded.substring(0, colIdx);
        password = decoded.substring(colIdx + 1).toCharArray();
    }

    return new AuthenticationInfo(HttpServletRequest.BASIC_AUTH, userId, password);
}