1 /******************************************************************************* 2 * Copyright 2012 Internet2 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 /* 17 * $Header: /home/hagleyj/i2mi/grouper-misc/grouperClient/src/ext/edu/internet2/middleware/grouperClientExt/org/apache/commons/httpclient/auth/BasicScheme.java,v 1.1 2008-11-30 10:57:20 mchyzer Exp $ 18 * $Revision: 1.1 $ 19 * $Date: 2008-11-30 10:57:20 $ 20 * 21 * ==================================================================== 22 * 23 * Licensed to the Apache Software Foundation (ASF) under one or more 24 * contributor license agreements. See the NOTICE file distributed with 25 * this work for additional information regarding copyright ownership. 26 * The ASF licenses this file to You under the Apache License, Version 2.0 27 * (the "License"); you may not use this file except in compliance with 28 * the License. You may obtain a copy of the License at 29 * 30 * http://www.apache.org/licenses/LICENSE-2.0 31 * 32 * Unless required by applicable law or agreed to in writing, software 33 * distributed under the License is distributed on an "AS IS" BASIS, 34 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 35 * See the License for the specific language governing permissions and 36 * limitations under the License. 37 * ==================================================================== 38 * 39 * This software consists of voluntary contributions made by many 40 * individuals on behalf of the Apache Software Foundation. For more 41 * information on the Apache Software Foundation, please see 42 * <http://www.apache.org/>. 43 * 44 */ 45 46 package edu.internet2.middleware.grouperInstallerExt.org.apache.commons.httpclient.auth; 47 48 import edu.internet2.middleware.grouperInstallerExt.org.apache.commons.codec.binary.Base64; 49 import edu.internet2.middleware.grouperInstallerExt.org.apache.commons.httpclient.Credentials; 50 import edu.internet2.middleware.grouperInstallerExt.org.apache.commons.httpclient.HttpMethod; 51 import edu.internet2.middleware.grouperInstallerExt.org.apache.commons.httpclient.UsernamePasswordCredentials; 52 import edu.internet2.middleware.grouperInstallerExt.org.apache.commons.httpclient.util.EncodingUtil; 53 import edu.internet2.middleware.grouperInstallerExt.org.apache.commons.logging.Log; 54 import edu.internet2.middleware.grouperInstallerExt.org.apache.commons.logging.LogFactory; 55 56 /** 57 * <p> 58 * Basic authentication scheme as defined in RFC 2617. 59 * </p> 60 * 61 * @author <a href="mailto:remm@apache.org">Remy Maucherat</a> 62 * @author Rodney Waldhoff 63 * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a> 64 * @author Ortwin Gl?ck 65 * @author Sean C. Sullivan 66 * @author <a href="mailto:adrian@ephox.com">Adrian Sutton</a> 67 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a> 68 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a> 69 */ 70 71 public class BasicScheme extends RFC2617Scheme { 72 73 /** Log object for this class. */ 74 private static final Log LOG = LogFactory.getLog(BasicScheme.class); 75 76 /** Whether the basic authentication process is complete */ 77 private boolean complete; 78 79 /** 80 * Default constructor for the basic authetication scheme. 81 * 82 * @since 3.0 83 */ 84 public BasicScheme() { 85 super(); 86 this.complete = false; 87 } 88 89 /** 90 * Constructor for the basic authetication scheme. 91 * 92 * @param challenge authentication challenge 93 * 94 * @throws MalformedChallengeException is thrown if the authentication challenge 95 * is malformed 96 * 97 * @deprecated Use parameterless constructor and {@link AuthScheme#processChallenge(String)} 98 * method 99 */ 100 public BasicScheme(final String challenge) throws MalformedChallengeException { 101 super(challenge); 102 this.complete = true; 103 } 104 105 /** 106 * Returns textual designation of the basic authentication scheme. 107 * 108 * @return <code>basic</code> 109 */ 110 public String getSchemeName() { 111 return "basic"; 112 } 113 114 /** 115 * Processes the Basic challenge. 116 * 117 * @param challenge the challenge string 118 * 119 * @throws MalformedChallengeException is thrown if the authentication challenge 120 * is malformed 121 * 122 * @since 3.0 123 */ 124 public void processChallenge(String challenge) 125 throws MalformedChallengeException 126 { 127 super.processChallenge(challenge); 128 this.complete = true; 129 } 130 131 /** 132 * Tests if the Basic authentication process has been completed. 133 * 134 * @return <tt>true</tt> if Basic authorization has been processed, 135 * <tt>false</tt> otherwise. 136 * 137 * @since 3.0 138 */ 139 public boolean isComplete() { 140 return this.complete; 141 } 142 143 /** 144 * Produces basic authorization string for the given set of 145 * {@link Credentials}. 146 * 147 * @param credentials The set of credentials to be used for athentication 148 * @param method Method name is ignored by the basic authentication scheme 149 * @param uri URI is ignored by the basic authentication scheme 150 * @throws InvalidCredentialsException if authentication credentials 151 * are not valid or not applicable for this authentication scheme 152 * @throws AuthenticationException if authorization string cannot 153 * be generated due to an authentication failure 154 * 155 * @return a basic authorization string 156 * 157 * @deprecated Use {@link #authenticate(Credentials, HttpMethod)} 158 */ 159 public String authenticate(Credentials credentials, String method, String uri) 160 throws AuthenticationException { 161 162 LOG.trace("enter BasicScheme.authenticate(Credentials, String, String)"); 163 164 UsernamePasswordCredentials usernamepassword = null; 165 try { 166 usernamepassword = (UsernamePasswordCredentials) credentials; 167 } catch (ClassCastException e) { 168 throw new InvalidCredentialsException( 169 "Credentials cannot be used for basic authentication: " 170 + credentials.getClass().getName()); 171 } 172 return BasicScheme.authenticate(usernamepassword); 173 } 174 175 /** 176 * Returns <tt>false</tt>. Basic authentication scheme is request based. 177 * 178 * @return <tt>false</tt>. 179 * 180 * @since 3.0 181 */ 182 public boolean isConnectionBased() { 183 return false; 184 } 185 186 /** 187 * Produces basic authorization string for the given set of {@link Credentials}. 188 * 189 * @param credentials The set of credentials to be used for athentication 190 * @param method The method being authenticated 191 * @throws InvalidCredentialsException if authentication credentials 192 * are not valid or not applicable for this authentication scheme 193 * @throws AuthenticationException if authorization string cannot 194 * be generated due to an authentication failure 195 * 196 * @return a basic authorization string 197 * 198 * @since 3.0 199 */ 200 public String authenticate(Credentials credentials, HttpMethod method) throws AuthenticationException { 201 202 LOG.trace("enter BasicScheme.authenticate(Credentials, HttpMethod)"); 203 204 if (method == null) { 205 throw new IllegalArgumentException("Method may not be null"); 206 } 207 UsernamePasswordCredentials usernamepassword = null; 208 try { 209 usernamepassword = (UsernamePasswordCredentials) credentials; 210 } catch (ClassCastException e) { 211 throw new InvalidCredentialsException( 212 "Credentials cannot be used for basic authentication: " 213 + credentials.getClass().getName()); 214 } 215 return BasicScheme.authenticate( 216 usernamepassword, 217 method.getParams().getCredentialCharset()); 218 } 219 220 /** 221 * @deprecated Use {@link #authenticate(UsernamePasswordCredentials, String)} 222 * 223 * Returns a basic <tt>Authorization</tt> header value for the given 224 * {@link UsernamePasswordCredentials}. 225 * 226 * @param credentials The credentials to encode. 227 * 228 * @return a basic authorization string 229 */ 230 public static String authenticate(UsernamePasswordCredentials credentials) { 231 return authenticate(credentials, "ISO-8859-1"); 232 } 233 234 /** 235 * Returns a basic <tt>Authorization</tt> header value for the given 236 * {@link UsernamePasswordCredentials} and charset. 237 * 238 * @param credentials The credentials to encode. 239 * @param charset The charset to use for encoding the credentials 240 * 241 * @return a basic authorization string 242 * 243 * @since 3.0 244 */ 245 public static String authenticate(UsernamePasswordCredentials credentials, String charset) { 246 247 LOG.trace("enter BasicScheme.authenticate(UsernamePasswordCredentials, String)"); 248 249 if (credentials == null) { 250 throw new IllegalArgumentException("Credentials may not be null"); 251 } 252 if (charset == null || charset.length() == 0) { 253 throw new IllegalArgumentException("charset may not be null or empty"); 254 } 255 StringBuffer buffer = new StringBuffer(); 256 buffer.append(credentials.getUserName()); 257 buffer.append(":"); 258 buffer.append(credentials.getPassword()); 259 260 return "Basic " + EncodingUtil.getAsciiString( 261 Base64.encodeBase64(EncodingUtil.getBytes(buffer.toString(), charset))); 262 } 263 264 }