Java Code Examples for org.apache.tomcat.util.buf.ByteChunk#startsWithIgnoreCase()

The following examples show how to use org.apache.tomcat.util.buf.ByteChunk#startsWithIgnoreCase() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BasicAuthAuthenticator.java    From carbon-device-mgt with Apache License 2.0 5 votes vote down vote up
@Override
public boolean canHandle(Request request) {
    /*
    This is done to avoid every endpoint being able to use basic auth. Add the following to
    the required web.xml of the web app.
    <context-param>
        <param-name>basicAuth</param-name>
        <param-value>true</param-value>
 </context-param>
     */
    if (!isAuthenticationSupported(request)) {
        return false;
    }
    if (request.getCoyoteRequest() == null || request.getCoyoteRequest().getMimeHeaders() == null) {
        return false;
    }
    MessageBytes authorization =
            request.getCoyoteRequest().getMimeHeaders().getValue(Constants.HTTPHeaders.HEADER_HTTP_AUTHORIZATION);
    if (authorization != null) {
        authorization.toBytes();
        ByteChunk authBC = authorization.getByteChunk();
        if (authBC.startsWithIgnoreCase(AUTH_HEADER, 0)) {
            return true;
        }
    }
    return false;
}
 
Example 2
Source File: BasicAuthenticator.java    From Tomcat7.0.67 with Apache License 2.0 4 votes vote down vote up
/**
 * Authenticate the user making this request, based on the specified
 * login configuration.  Return <code>true</code> if any specified
 * constraint has been satisfied, or <code>false</code> if we have
 * created a response challenge already.
 *
 * @param request Request we are processing
 * @param response Response we are creating
 * @param config    Login configuration describing how authentication
 *              should be performed
 *
 * @exception IOException if an input/output error occurs
 */
@Override
public boolean authenticate(Request request,
                            HttpServletResponse response,
                            LoginConfig config)
    throws IOException {

    if (checkForCachedAuthentication(request, response, true)) {
        return true;
    }

    // Validate any credentials already included with this request
    String username = null;
    String password = null;

    MessageBytes authorization = 
        request.getCoyoteRequest().getMimeHeaders()
        .getValue("authorization");
    
    if (authorization != null) {
        authorization.toBytes();
        ByteChunk authorizationBC = authorization.getByteChunk();
        if (authorizationBC.startsWithIgnoreCase("basic ", 0)) {
            authorizationBC.setOffset(authorizationBC.getOffset() + 6);
            
            byte[] decoded = Base64.decodeBase64(
                    authorizationBC.getBuffer(),
                    authorizationBC.getOffset(),
                    authorizationBC.getLength());
            
            // Get username and password
            int colon = -1;
            for (int i = 0; i < decoded.length; i++) {
                if (decoded[i] == ':') {
                    colon = i;
                    break;
                }
            }

            if (colon < 0) {
                username = new String(decoded, B2CConverter.ISO_8859_1);
            } else {
                username = new String(
                        decoded, 0, colon, B2CConverter.ISO_8859_1);
                password = new String(
                        decoded, colon + 1, decoded.length - colon - 1,
                        B2CConverter.ISO_8859_1);
            }
            
            authorizationBC.setOffset(authorizationBC.getOffset() - 6);
        }

        Principal principal = context.getRealm().authenticate(username, password);
        if (principal != null) {
            register(request, response, principal,
                    HttpServletRequest.BASIC_AUTH, username, password);
            return (true);
        }
    }
    
    StringBuilder value = new StringBuilder(16);
    value.append("Basic realm=\"");
    if (config.getRealmName() == null) {
        value.append(REALM_NAME);
    } else {
        value.append(config.getRealmName());
    }
    value.append('\"');        
    response.setHeader(AUTH_HEADER_NAME, value.toString());
    response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    return (false);

}
 
Example 3
Source File: BasicAuthenticator.java    From tomcatsrc with Apache License 2.0 4 votes vote down vote up
/**
 * Authenticate the user making this request, based on the specified
 * login configuration.  Return <code>true</code> if any specified
 * constraint has been satisfied, or <code>false</code> if we have
 * created a response challenge already.
 *
 * @param request Request we are processing
 * @param response Response we are creating
 * @param config    Login configuration describing how authentication
 *              should be performed
 *
 * @exception IOException if an input/output error occurs
 */
@Override
public boolean authenticate(Request request,
                            HttpServletResponse response,
                            LoginConfig config)
    throws IOException {

    if (checkForCachedAuthentication(request, response, true)) {
        return true;
    }

    // Validate any credentials already included with this request
    String username = null;
    String password = null;

    MessageBytes authorization = 
        request.getCoyoteRequest().getMimeHeaders()
        .getValue("authorization");
    
    if (authorization != null) {
        authorization.toBytes();
        ByteChunk authorizationBC = authorization.getByteChunk();
        if (authorizationBC.startsWithIgnoreCase("basic ", 0)) {
            authorizationBC.setOffset(authorizationBC.getOffset() + 6);
            
            byte[] decoded = Base64.decodeBase64(
                    authorizationBC.getBuffer(),
                    authorizationBC.getOffset(),
                    authorizationBC.getLength());
            
            // Get username and password
            int colon = -1;
            for (int i = 0; i < decoded.length; i++) {
                if (decoded[i] == ':') {
                    colon = i;
                    break;
                }
            }

            if (colon < 0) {
                username = new String(decoded, B2CConverter.ISO_8859_1);
            } else {
                username = new String(
                        decoded, 0, colon, B2CConverter.ISO_8859_1);
                password = new String(
                        decoded, colon + 1, decoded.length - colon - 1,
                        B2CConverter.ISO_8859_1);
            }
            
            authorizationBC.setOffset(authorizationBC.getOffset() - 6);
        }

        Principal principal = context.getRealm().authenticate(username, password);
        if (principal != null) {
            register(request, response, principal,
                    HttpServletRequest.BASIC_AUTH, username, password);
            return (true);
        }
    }
    
    StringBuilder value = new StringBuilder(16);
    value.append("Basic realm=\"");
    if (config.getRealmName() == null) {
        value.append(REALM_NAME);
    } else {
        value.append(config.getRealmName());
    }
    value.append('\"');        
    response.setHeader(AUTH_HEADER_NAME, value.toString());
    response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    return (false);

}