All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.logspace.agent.shaded.apache.http.impl.auth.BasicScheme Maven / Gradle / Ivy

There is a newer version: 0.3.2
Show newest version
/*
 * ====================================================================
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * .
 *
 */
package io.logspace.agent.shaded.apache.http.impl.auth;

import java.nio.charset.Charset;

import io.logspace.agent.shaded.apache.commons.codec.binary.Base64;
import io.logspace.agent.shaded.apache.http.Consts;
import io.logspace.agent.shaded.apache.http.Header;
import io.logspace.agent.shaded.apache.http.HttpRequest;
import io.logspace.agent.shaded.apache.http.annotation.NotThreadSafe;
import io.logspace.agent.shaded.apache.http.auth.AUTH;
import io.logspace.agent.shaded.apache.http.auth.AuthenticationException;
import io.logspace.agent.shaded.apache.http.auth.ChallengeState;
import io.logspace.agent.shaded.apache.http.auth.Credentials;
import io.logspace.agent.shaded.apache.http.auth.MalformedChallengeException;
import io.logspace.agent.shaded.apache.http.message.BufferedHeader;
import io.logspace.agent.shaded.apache.http.protocol.BasicHttpContext;
import io.logspace.agent.shaded.apache.http.protocol.HttpContext;
import io.logspace.agent.shaded.apache.http.util.Args;
import io.logspace.agent.shaded.apache.http.util.CharArrayBuffer;
import io.logspace.agent.shaded.apache.http.util.EncodingUtils;

/**
 * Basic authentication scheme as defined in RFC 2617.
 *
 * @since 4.0
 */
@NotThreadSafe
public class BasicScheme extends RFC2617Scheme {

    private final Base64 base64codec;
    /** Whether the basic authentication process is complete */
    private boolean complete;

    /**
     * @since 4.3
     */
    public BasicScheme(final Charset credentialsCharset) {
        super(credentialsCharset);
        this.base64codec = new Base64(0);
        this.complete = false;
    }

    /**
     * Creates an instance of BasicScheme with the given challenge
     * state.
     *
     * @since 4.2
     *
     * @deprecated (4.3) do not use.
     */
    @Deprecated
    public BasicScheme(final ChallengeState challengeState) {
        super(challengeState);
        this.base64codec = new Base64(0);
    }

    public BasicScheme() {
        this(Consts.ASCII);
    }

    /**
     * Returns textual designation of the basic authentication scheme.
     *
     * @return basic
     */
    public String getSchemeName() {
        return "basic";
    }

    /**
     * Processes the Basic challenge.
     *
     * @param header the challenge header
     *
     * @throws MalformedChallengeException is thrown if the authentication challenge
     * is malformed
     */
    @Override
    public void processChallenge(
            final Header header) throws MalformedChallengeException {
        super.processChallenge(header);
        this.complete = true;
    }

    /**
     * Tests if the Basic authentication process has been completed.
     *
     * @return true if Basic authorization has been processed,
     *   false otherwise.
     */
    public boolean isComplete() {
        return this.complete;
    }

    /**
     * Returns false. Basic authentication scheme is request based.
     *
     * @return false.
     */
    public boolean isConnectionBased() {
        return false;
    }

    /**
     * @deprecated (4.2) Use {@link io.logspace.agent.shaded.apache.http.auth.ContextAwareAuthScheme#authenticate(
     *   Credentials, HttpRequest, io.logspace.agent.shaded.apache.http.protocol.HttpContext)}
     */
    @Deprecated
    public Header authenticate(
            final Credentials credentials, final HttpRequest request) throws AuthenticationException {
        return authenticate(credentials, request, new BasicHttpContext());
    }

    /**
     * Produces basic authorization header for the given set of {@link Credentials}.
     *
     * @param credentials The set of credentials to be used for authentication
     * @param request The request being authenticated
     * @throws io.logspace.agent.shaded.apache.http.auth.InvalidCredentialsException if authentication
     *   credentials are not valid or not applicable for this authentication scheme
     * @throws AuthenticationException if authorization string cannot
     *   be generated due to an authentication failure
     *
     * @return a basic authorization string
     */
    @Override
    public Header authenticate(
            final Credentials credentials,
            final HttpRequest request,
            final HttpContext context) throws AuthenticationException {

        Args.notNull(credentials, "Credentials");
        Args.notNull(request, "HTTP request");
        final StringBuilder tmp = new StringBuilder();
        tmp.append(credentials.getUserPrincipal().getName());
        tmp.append(":");
        tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword());

        final byte[] base64password = base64codec.encode(
                EncodingUtils.getBytes(tmp.toString(), getCredentialsCharset(request)));

        final CharArrayBuffer buffer = new CharArrayBuffer(32);
        if (isProxy()) {
            buffer.append(AUTH.PROXY_AUTH_RESP);
        } else {
            buffer.append(AUTH.WWW_AUTH_RESP);
        }
        buffer.append(": Basic ");
        buffer.append(base64password, 0, base64password.length);

        return new BufferedHeader(buffer);
    }

    /**
     * Returns a basic Authorization header value for the given
     * {@link Credentials} and charset.
     *
     * @param credentials The credentials to encode.
     * @param charset The charset to use for encoding the credentials
     *
     * @return a basic authorization header
     *
     * @deprecated (4.3) use {@link #authenticate(Credentials, HttpRequest, HttpContext)}.
     */
    @Deprecated
    public static Header authenticate(
            final Credentials credentials,
            final String charset,
            final boolean proxy) {
        Args.notNull(credentials, "Credentials");
        Args.notNull(charset, "charset");

        final StringBuilder tmp = new StringBuilder();
        tmp.append(credentials.getUserPrincipal().getName());
        tmp.append(":");
        tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword());

        final byte[] base64password = Base64.encodeBase64(
                EncodingUtils.getBytes(tmp.toString(), charset), false);

        final CharArrayBuffer buffer = new CharArrayBuffer(32);
        if (proxy) {
            buffer.append(AUTH.PROXY_AUTH_RESP);
        } else {
            buffer.append(AUTH.WWW_AUTH_RESP);
        }
        buffer.append(": Basic ");
        buffer.append(base64password, 0, base64password.length);

        return new BufferedHeader(buffer);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy