// Path: http/src/main/java/org/kaazing/netx/http/auth/ChallengeHandler.java
// public abstract class ChallengeHandler {
//     /**
//      * Creates a new instance of the sub-class of {@link ChallengeHandler} using
//      * the {@link ServiceLoader} API with the implementation specified under
//      * META-INF/services.
//      *
//      * @param <T>      subclass of ChallengeHandler
//      * @param clazz    Class object of the sub-type
//      * @return ChallengeHandler
//      */
//     protected static <T extends ChallengeHandler> T create(Class<T> clazz) {
//         return load0(clazz, ServiceLoader.load(clazz));
//     }
// 
//     /**
//      * Creates a new instance of the sub-class of {@link ChallengeHandler} with
//      * specified {@link ClassLoader} using the {@link ServiceLoader} API with
//      * the implementation specified under META-INF/services.
//      *
//      * @param  <T>                  subclass of ChallengeHandler
//      * @param  clazz                Class object of the sub-type
//      * @param  classLoader          ClassLoader to be used to instantiate
//      * @return ChallengeHandler
//      */
//     protected static <T extends ChallengeHandler> T create(Class<T>    clazz,
//                                                            ClassLoader classLoader) {
//         return load0(clazz, ServiceLoader.load(clazz, classLoader));
//     }
// 
//     /**
//      * Can the presented challenge be potentially handled by this challenge handler?
//      *
//      * @param challengeRequest a challenge request object containing a challenge
//      * @return true iff this challenge handler could potentially respond meaningfully to the challenge.
//      */
//     public abstract boolean canHandle(ChallengeRequest challengeRequest);
// 
//     /**
//      * Handle the presented challenge by creating a challenge response or returning {@code null}.
//      * This responsibility is usually achieved
//      * by using the associated {@link LoginHandler} to obtain user credentials, and transforming those credentials
//      * into a {@link ChallengeResponse}.
//      * <p>
//      * When it is not possible to create a {@link ChallengeResponse}, this method MUST return {@code null}.
//      *
//      * @param challengeRequest a challenge object
//      * @return a challenge response object or {@code null} if no response is possible.
//      */
//     public abstract ChallengeResponse handle(ChallengeRequest challengeRequest);
// 
//     // ----------------------- Private Methods -------------------------------
// 
//     private static <T extends ChallengeHandler> T load0(Class<T>         clazz,
//                                                  ServiceLoader<T> serviceLoader) {
//         for (ChallengeHandler challengeHandler: serviceLoader) {
//             Class<?> c = challengeHandler.getClass();
//             if ((clazz != null) && clazz.isAssignableFrom(c)) {
//                 try {
//                     return clazz.cast(c.newInstance());
//                 } catch (InstantiationException e) {
//                     String s = "Failed to instantiate class " + c;
//                     throw new RuntimeException(s, e);
//                 } catch (IllegalAccessException e) {
//                     String s = "Failed to access and instantiate class " + c;
//                     throw new RuntimeException(s, e);
//                 }
//             }
//         }
//         return null;
//     }
// 
// }
// 
// Path: http/src/main/java/org/kaazing/netx/http/auth/ChallengeResponse.java
// public class ChallengeResponse {
// 
//     private char[] credentials;
//     private ChallengeHandler nextChallengeHandler;
// 
//     /**
//      * Constructor from a set of credentials to send to the server in an 'Authorization:' header
//      * and the next challenge handler responsible for handling any further challenges for the request.
//      *
//      * @param credentials a set of credentials to send to the server in an 'Authorization:' header
//      * @param nextChallengeHandler the next challenge handler responsible for handling any further challenges for the request.
//      */
//     public ChallengeResponse(char[] credentials, ChallengeHandler nextChallengeHandler) {
//         this.credentials = credentials;
//         this.nextChallengeHandler = nextChallengeHandler;
//     }
// 
//     /**
//      * Return the next challenge handler responsible for handling any further challenges for the request.
//      *
//      * @return the next challenge handler responsible for handling any further challenges for the request.
//      */
//     public ChallengeHandler getNextChallengeHandler() {
//         return nextChallengeHandler;
//     }
// 
//     /**
//      * Return a set of credentials to send to the server in an 'Authorization:' header.
//      *
//      * @return a set of credentials to send to the server in an 'Authorization:' header.
//      */
//     public char[] getCredentials() {
//         return credentials;
//     }
// 
//     /**
//      * Establish the credentials for this response.
//      *
//      * @param credentials the credentials to be used for this challenge response.
//      */
//     public void setCredentials(char[] credentials) {
//         this.credentials = credentials;
//     }
// 
//     /**
//      * Establish the next challenge handler responsible for handling any further challenges for the request.
//      *
//      * @param nextChallengeHandler the next challenge handler responsible for handling any further challenges for the request.
//      */
//     public void setNextChallengeHandler(ChallengeHandler nextChallengeHandler) {
//         this.nextChallengeHandler = nextChallengeHandler;
//     }
// 
//     /**
//      * Clear the credentials of this response.
//      * <p>
//      * Calling this method once the credentials have been communicated to the network layer
//      * protects credentials in memory.
//      */
//     public void clearCredentials() {
//         if (getCredentials() != null) {
//                 Arrays.fill(getCredentials(), (char) 0);
//         }
//     }
// }


import java.net.PasswordAuthentication;
import java.nio.charset.Charset;
import java.util.Arrays;
import org.kaazing.netx.http.auth.ChallengeHandler;
import org.kaazing.netx.http.auth.ChallengeResponse;
import org.kaazing.netx.http.internal.Base64;
/**
 * Copyright 2007-2015, Kaazing Corporation. All rights reserved.
 *
 * Licensed 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.
 */
package org.kaazing.netx.http.internal.auth;



public final class BasicChallengeResponseFactory {

    private static final Charset US_ASCII = Charset.forName("US-ASCII");

    private BasicChallengeResponseFactory() {
    }

// Path: http/src/main/java/org/kaazing/netx/http/auth/ChallengeHandler.java
// public abstract class ChallengeHandler {
//     /**
//      * Creates a new instance of the sub-class of {@link ChallengeHandler} using
//      * the {@link ServiceLoader} API with the implementation specified under
//      * META-INF/services.
//      *
//      * @param <T>      subclass of ChallengeHandler
//      * @param clazz    Class object of the sub-type
//      * @return ChallengeHandler
//      */
//     protected static <T extends ChallengeHandler> T create(Class<T> clazz) {
//         return load0(clazz, ServiceLoader.load(clazz));
//     }
// 
//     /**
//      * Creates a new instance of the sub-class of {@link ChallengeHandler} with
//      * specified {@link ClassLoader} using the {@link ServiceLoader} API with
//      * the implementation specified under META-INF/services.
//      *
//      * @param  <T>                  subclass of ChallengeHandler
//      * @param  clazz                Class object of the sub-type
//      * @param  classLoader          ClassLoader to be used to instantiate
//      * @return ChallengeHandler
//      */
//     protected static <T extends ChallengeHandler> T create(Class<T>    clazz,
//                                                            ClassLoader classLoader) {
//         return load0(clazz, ServiceLoader.load(clazz, classLoader));
//     }
// 
//     /**
//      * Can the presented challenge be potentially handled by this challenge handler?
//      *
//      * @param challengeRequest a challenge request object containing a challenge
//      * @return true iff this challenge handler could potentially respond meaningfully to the challenge.
//      */
//     public abstract boolean canHandle(ChallengeRequest challengeRequest);
// 
//     /**
//      * Handle the presented challenge by creating a challenge response or returning {@code null}.
//      * This responsibility is usually achieved
//      * by using the associated {@link LoginHandler} to obtain user credentials, and transforming those credentials
//      * into a {@link ChallengeResponse}.
//      * <p>
//      * When it is not possible to create a {@link ChallengeResponse}, this method MUST return {@code null}.
//      *
//      * @param challengeRequest a challenge object
//      * @return a challenge response object or {@code null} if no response is possible.
//      */
//     public abstract ChallengeResponse handle(ChallengeRequest challengeRequest);
// 
//     // ----------------------- Private Methods -------------------------------
// 
//     private static <T extends ChallengeHandler> T load0(Class<T>         clazz,
//                                                  ServiceLoader<T> serviceLoader) {
//         for (ChallengeHandler challengeHandler: serviceLoader) {
//             Class<?> c = challengeHandler.getClass();
//             if ((clazz != null) && clazz.isAssignableFrom(c)) {
//                 try {
//                     return clazz.cast(c.newInstance());
//                 } catch (InstantiationException e) {
//                     String s = "Failed to instantiate class " + c;
//                     throw new RuntimeException(s, e);
//                 } catch (IllegalAccessException e) {
//                     String s = "Failed to access and instantiate class " + c;
//                     throw new RuntimeException(s, e);
//                 }
//             }
//         }
//         return null;
//     }
// 
// }
// 
// Path: http/src/main/java/org/kaazing/netx/http/auth/ChallengeResponse.java
// public class ChallengeResponse {
// 
//     private char[] credentials;
//     private ChallengeHandler nextChallengeHandler;
// 
//     /**
//      * Constructor from a set of credentials to send to the server in an 'Authorization:' header
//      * and the next challenge handler responsible for handling any further challenges for the request.
//      *
//      * @param credentials a set of credentials to send to the server in an 'Authorization:' header
//      * @param nextChallengeHandler the next challenge handler responsible for handling any further challenges for the request.
//      */
//     public ChallengeResponse(char[] credentials, ChallengeHandler nextChallengeHandler) {
//         this.credentials = credentials;
//         this.nextChallengeHandler = nextChallengeHandler;
//     }
// 
//     /**
//      * Return the next challenge handler responsible for handling any further challenges for the request.
//      *
//      * @return the next challenge handler responsible for handling any further challenges for the request.
//      */
//     public ChallengeHandler getNextChallengeHandler() {
//         return nextChallengeHandler;
//     }
// 
//     /**
//      * Return a set of credentials to send to the server in an 'Authorization:' header.
//      *
//      * @return a set of credentials to send to the server in an 'Authorization:' header.
//      */
//     public char[] getCredentials() {
//         return credentials;
//     }
// 
//     /**
//      * Establish the credentials for this response.
//      *
//      * @param credentials the credentials to be used for this challenge response.
//      */
//     public void setCredentials(char[] credentials) {
//         this.credentials = credentials;
//     }
// 
//     /**
//      * Establish the next challenge handler responsible for handling any further challenges for the request.
//      *
//      * @param nextChallengeHandler the next challenge handler responsible for handling any further challenges for the request.
//      */
//     public void setNextChallengeHandler(ChallengeHandler nextChallengeHandler) {
//         this.nextChallengeHandler = nextChallengeHandler;
//     }
// 
//     /**
//      * Clear the credentials of this response.
//      * <p>
//      * Calling this method once the credentials have been communicated to the network layer
//      * protects credentials in memory.
//      */
//     public void clearCredentials() {
//         if (getCredentials() != null) {
//                 Arrays.fill(getCredentials(), (char) 0);
//         }
//     }
// }


// Path: http/src/main/java/org/kaazing/netx/http/internal/auth/BasicChallengeResponseFactory.java
import java.net.PasswordAuthentication;
import java.nio.charset.Charset;
import java.util.Arrays;
import org.kaazing.netx.http.auth.ChallengeHandler;
import org.kaazing.netx.http.auth.ChallengeResponse;
import org.kaazing.netx.http.internal.Base64;

/**
 * Copyright 2007-2015, Kaazing Corporation. All rights reserved.
 *
 * Licensed 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.
 */
package org.kaazing.netx.http.internal.auth;



public final class BasicChallengeResponseFactory {

    private static final Charset US_ASCII = Charset.forName("US-ASCII");

    private BasicChallengeResponseFactory() {
    }

    public static ChallengeResponse create(PasswordAuthentication creds, ChallengeHandler nextChallengeHandler) {