1 31 32 package org.apache.commons.httpclient.auth; 33 34 import org.apache.commons.httpclient.HttpConstants; 35 import org.apache.commons.httpclient.Credentials; 36 import org.apache.commons.httpclient.UsernamePasswordCredentials; 37 import org.apache.commons.httpclient.util.Base64; 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 55 56 public class BasicScheme extends RFC2617Scheme { 57 58 59 private static final Log LOG = LogFactory.getLog(BasicScheme.class); 60 61 69 public BasicScheme(final String challenge) throws MalformedChallengeException { 70 super(challenge); 71 } 72 73 74 79 public String getSchemeName() { 80 return "basic"; 81 } 82 83 95 public String authenticate(Credentials credentials, String method, String uri) 96 throws AuthenticationException { 97 98 LOG.trace("enter BasicScheme.authenticate(Credentials, String, String)"); 99 100 UsernamePasswordCredentials usernamepassword = null; 101 try { 102 usernamepassword = (UsernamePasswordCredentials) credentials; 103 } catch (ClassCastException e) { 104 throw new AuthenticationException( 105 "Credentials cannot be used for basic authentication: " 106 + credentials.getClass().getName()); 107 } 108 return BasicScheme.authenticate(usernamepassword); 109 } 110 111 119 public static String authenticate(UsernamePasswordCredentials credentials) { 120 121 LOG.trace("enter BasicScheme.authenticate(UsernamePasswordCredentials)"); 122 123 if (credentials == null) { 124 throw new IllegalArgumentException ("Credentials may not be null"); 125 } 126 StringBuffer buffer = new StringBuffer (); 127 buffer.append(credentials.getUserName()); 128 buffer.append(":"); 129 buffer.append(credentials.getPassword()); 130 131 return "Basic " + HttpConstants.getAsciiString( 132 Base64.encode(HttpConstants.getContentBytes(buffer.toString()))); 133 } 134 } 135 | Popular Tags |