michael@0: /*
michael@0: * ====================================================================
michael@0: *
michael@0: * Licensed to the Apache Software Foundation (ASF) under one or more
michael@0: * contributor license agreements. See the NOTICE file distributed with
michael@0: * this work for additional information regarding copyright ownership.
michael@0: * The ASF licenses this file to You under the Apache License, Version 2.0
michael@0: * (the "License"); you may not use this file except in compliance with
michael@0: * the License. You may obtain a copy of the License at
michael@0: *
michael@0: * http://www.apache.org/licenses/LICENSE-2.0
michael@0: *
michael@0: * Unless required by applicable law or agreed to in writing, software
michael@0: * distributed under the License is distributed on an "AS IS" BASIS,
michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0: * See the License for the specific language governing permissions and
michael@0: * limitations under the License.
michael@0: * ====================================================================
michael@0: *
michael@0: * This software consists of voluntary contributions made by many
michael@0: * individuals on behalf of the Apache Software Foundation. For more
michael@0: * information on the Apache Software Foundation, please see
michael@0: *
michael@0: * The following parameters can be used to customize the behavior of this michael@0: * class: michael@0: *
basic
michael@0: */
michael@0: public String getSchemeName() {
michael@0: return "basic";
michael@0: }
michael@0:
michael@0: /**
michael@0: * Processes the Basic challenge.
michael@0: *
michael@0: * @param header the challenge header
michael@0: *
michael@0: * @throws MalformedChallengeException is thrown if the authentication challenge
michael@0: * is malformed
michael@0: */
michael@0: @Override
michael@0: public void processChallenge(
michael@0: final Header header) throws MalformedChallengeException {
michael@0: super.processChallenge(header);
michael@0: this.complete = true;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Tests if the Basic authentication process has been completed.
michael@0: *
michael@0: * @return true if Basic authorization has been processed,
michael@0: * false otherwise.
michael@0: */
michael@0: public boolean isComplete() {
michael@0: return this.complete;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns false. Basic authentication scheme is request based.
michael@0: *
michael@0: * @return false.
michael@0: */
michael@0: public boolean isConnectionBased() {
michael@0: return false;
michael@0: }
michael@0:
michael@0: /**
michael@0: * Produces basic authorization header for the given set of {@link Credentials}.
michael@0: *
michael@0: * @param credentials The set of credentials to be used for authentication
michael@0: * @param request The request being authenticated
michael@0: * @throws InvalidCredentialsException if authentication credentials are not
michael@0: * valid or not applicable for this authentication scheme
michael@0: * @throws AuthenticationException if authorization string cannot
michael@0: * be generated due to an authentication failure
michael@0: *
michael@0: * @return a basic authorization string
michael@0: */
michael@0: public Header authenticate(
michael@0: final Credentials credentials,
michael@0: final HttpRequest request) throws AuthenticationException {
michael@0:
michael@0: if (credentials == null) {
michael@0: throw new IllegalArgumentException("Credentials may not be null");
michael@0: }
michael@0: if (request == null) {
michael@0: throw new IllegalArgumentException("HTTP request may not be null");
michael@0: }
michael@0:
michael@0: String charset = AuthParams.getCredentialCharset(request.getParams());
michael@0: return authenticate(credentials, charset, isProxy());
michael@0: }
michael@0:
michael@0: /**
michael@0: * Returns a basic Authorization header value for the given
michael@0: * {@link Credentials} and charset.
michael@0: *
michael@0: * @param credentials The credentials to encode.
michael@0: * @param charset The charset to use for encoding the credentials
michael@0: *
michael@0: * @return a basic authorization header
michael@0: */
michael@0: public static Header authenticate(
michael@0: final Credentials credentials,
michael@0: final String charset,
michael@0: boolean proxy) {
michael@0: if (credentials == null) {
michael@0: throw new IllegalArgumentException("Credentials may not be null");
michael@0: }
michael@0: if (charset == null) {
michael@0: throw new IllegalArgumentException("charset may not be null");
michael@0: }
michael@0:
michael@0: StringBuilder tmp = new StringBuilder();
michael@0: tmp.append(credentials.getUserPrincipal().getName());
michael@0: tmp.append(":");
michael@0: tmp.append((credentials.getPassword() == null) ? "null" : credentials.getPassword());
michael@0:
michael@0: byte[] base64password = Base64.encodeBase64(
michael@0: EncodingUtils.getBytes(tmp.toString(), charset));
michael@0:
michael@0: CharArrayBuffer buffer = new CharArrayBuffer(32);
michael@0: if (proxy) {
michael@0: buffer.append(AUTH.PROXY_AUTH_RESP);
michael@0: } else {
michael@0: buffer.append(AUTH.WWW_AUTH_RESP);
michael@0: }
michael@0: buffer.append(": Basic ");
michael@0: buffer.append(base64password, 0, base64password.length);
michael@0:
michael@0: return new BufferedHeader(buffer);
michael@0: }
michael@0:
michael@0: }