package org.chenyang.http.impl.auth;

import org.chenyang.http.Consts;
import org.chenyang.http.Header;
import org.chenyang.http.HttpRequest;
import org.chenyang.http.auth.AuthenticationException;
import org.chenyang.http.auth.ChallengeState;
import org.chenyang.http.auth.Credentials;
import org.chenyang.http.auth.MalformedChallengeException;
import org.chenyang.http.message.BufferedHeader;
import org.chenyang.http.protocol.BasicHttpContext;
import org.chenyang.http.protocol.HttpContext;
import org.chenyang.http.util.Args;
import org.chenyang.http.util.CharArrayBuffer;
import org.chenyang.http.util.EncodingUtils;
import java.nio.charset.Charset;
import org.apache.commons.codec.binary.Base64;

public class BasicScheme extends RFC2617Scheme {
   private static final long serialVersionUID = -1931571557597830536L;
   private boolean complete;

   public BasicScheme(Charset credentialsCharset) {
      super(credentialsCharset);
      this.complete = false;
   }

   /** @deprecated */
   @Deprecated
   public BasicScheme(ChallengeState challengeState) {
      super(challengeState);
   }

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

   public String getSchemeName() {
      return "basic";
   }

   public void processChallenge(Header header) throws MalformedChallengeException {
      super.processChallenge(header);
      this.complete = true;
   }

   public boolean isComplete() {
      return this.complete;
   }

   public boolean isConnectionBased() {
      return false;
   }

   /** @deprecated */
   @Deprecated
   public Header authenticate(Credentials credentials, HttpRequest request) throws AuthenticationException {
      return this.authenticate(credentials, request, new BasicHttpContext());
   }

   public Header authenticate(Credentials credentials, HttpRequest request, HttpContext context) throws AuthenticationException {
      Args.notNull(credentials, "Credentials");
      Args.notNull(request, "HTTP request");
      StringBuilder tmp = new StringBuilder();
      tmp.append(credentials.getUserPrincipal().getName());
      tmp.append(":");
      tmp.append(credentials.getPassword() == null ? "null" : credentials.getPassword());
      Base64 base64codec = new Base64(0);
      byte[] base64password = base64codec.encode(EncodingUtils.getBytes(tmp.toString(), this.getCredentialsCharset(request)));
      CharArrayBuffer buffer = new CharArrayBuffer(32);
      if (this.isProxy()) {
         buffer.append("Proxy-Authorization");
      } else {
         buffer.append("Authorization");
      }

      buffer.append(": Basic ");
      buffer.append((byte[])base64password, 0, base64password.length);
      return new BufferedHeader(buffer);
   }

   /** @deprecated */
   @Deprecated
   public static Header authenticate(Credentials credentials, String charset, boolean proxy) {
      Args.notNull(credentials, "Credentials");
      Args.notNull(charset, "charset");
      StringBuilder tmp = new StringBuilder();
      tmp.append(credentials.getUserPrincipal().getName());
      tmp.append(":");
      tmp.append(credentials.getPassword() == null ? "null" : credentials.getPassword());
      byte[] base64password = Base64.encodeBase64(EncodingUtils.getBytes(tmp.toString(), charset), false);
      CharArrayBuffer buffer = new CharArrayBuffer(32);
      if (proxy) {
         buffer.append("Proxy-Authorization");
      } else {
         buffer.append("Authorization");
      }

      buffer.append(": Basic ");
      buffer.append((byte[])base64password, 0, base64password.length);
      return new BufferedHeader(buffer);
   }

   public String toString() {
      StringBuilder builder = new StringBuilder();
      builder.append("BASIC [complete=").append(this.complete).append("]");
      return builder.toString();
   }
}
