1 /** 2 * Copyright 2014 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/AuthChallengeParser.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.grouperClientExt.org.apache.commons.httpclient.auth; 47 48 import java.util.HashMap; 49 import java.util.List; 50 import java.util.Map; 51 52 import edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.Header; 53 import edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.NameValuePair; 54 import edu.internet2.middleware.grouperClientExt.org.apache.commons.httpclient.util.ParameterParser; 55 56 /** 57 * This class provides utility methods for parsing HTTP www and proxy authentication 58 * challenges. 59 * 60 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a> 61 * 62 * @since 2.0beta1 63 */ 64 public final class AuthChallengeParser { 65 /** 66 * Extracts authentication scheme from the given authentication 67 * challenge. 68 * 69 * @param challengeStr the authentication challenge string 70 * @return authentication scheme 71 * 72 * @throws MalformedChallengeException when the authentication challenge string 73 * is malformed 74 * 75 * @since 2.0beta1 76 */ 77 public static String extractScheme(final String challengeStr) 78 throws MalformedChallengeException { 79 if (challengeStr == null) { 80 throw new IllegalArgumentException("Challenge may not be null"); 81 } 82 int idx = challengeStr.indexOf(' '); 83 String s = null; 84 if (idx == -1) { 85 s = challengeStr; 86 } else { 87 s = challengeStr.substring(0, idx); 88 } 89 if (s.equals("")) { 90 throw new MalformedChallengeException("Invalid challenge: " + challengeStr); 91 } 92 return s.toLowerCase(); 93 } 94 95 /** 96 * Extracts a map of challenge parameters from an authentication challenge. 97 * Keys in the map are lower-cased 98 * 99 * @param challengeStr the authentication challenge string 100 * @return a map of authentication challenge parameters 101 * @throws MalformedChallengeException when the authentication challenge string 102 * is malformed 103 * 104 * @since 2.0beta1 105 */ 106 public static Map extractParams(final String challengeStr) 107 throws MalformedChallengeException { 108 if (challengeStr == null) { 109 throw new IllegalArgumentException("Challenge may not be null"); 110 } 111 int idx = challengeStr.indexOf(' '); 112 if (idx == -1) { 113 throw new MalformedChallengeException("Invalid challenge: " + challengeStr); 114 } 115 Map map = new HashMap(); 116 ParameterParserre/grouperClientExt/org/apache/commons/httpclient/util/ParameterParser.html#ParameterParser">ParameterParser parser = new ParameterParser(); 117 List params = parser.parse( 118 challengeStr.substring(idx + 1, challengeStr.length()), ','); 119 for (int i = 0; i < params.size(); i++) { 120 NameValuePair../../../../../../../edu/internet2/middleware/grouperClientExt/org/apache/commons/httpclient/NameValuePair.html#NameValuePair">NameValuePair param = (NameValuePair) params.get(i); 121 map.put(param.getName().toLowerCase(), param.getValue()); 122 } 123 return map; 124 } 125 126 /** 127 * Extracts a map of challenges ordered by authentication scheme name 128 * 129 * @param headers the array of authorization challenges 130 * @return a map of authorization challenges 131 * 132 * @throws MalformedChallengeException if any of challenge strings 133 * is malformed 134 * 135 * @since 3.0 136 */ 137 public static Map parseChallenges(final Header[] headers) 138 throws MalformedChallengeException { 139 if (headers == null) { 140 throw new IllegalArgumentException("Array of challenges may not be null"); 141 } 142 String challenge = null; 143 Map challengemap = new HashMap(headers.length); 144 for (int i = 0; i < headers.length; i++) { 145 challenge = headers[i].getValue(); 146 String s = AuthChallengeParser.extractScheme(challenge); 147 challengemap.put(s, challenge); 148 } 149 return challengemap; 150 } 151 }