1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package org.apache.commons.httpclient.auth;
31
32 import java.util.HashMap;
33 import java.util.List;
34 import java.util.Map;
35
36 import org.apache.commons.httpclient.Header;
37 import org.apache.commons.httpclient.NameValuePair;
38 import org.apache.commons.httpclient.util.ParameterParser;
39
40 /***
41 * This class provides utility methods for parsing HTTP www and proxy authentication
42 * challenges.
43 *
44 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
45 *
46 * @since 2.0beta1
47 */
48 public final class AuthChallengeParser {
49 /***
50 * Extracts authentication scheme from the given authentication
51 * challenge.
52 *
53 * @param challengeStr the authentication challenge string
54 * @return authentication scheme
55 *
56 * @throws MalformedChallengeException when the authentication challenge string
57 * is malformed
58 *
59 * @since 2.0beta1
60 */
61 public static String extractScheme(final String challengeStr)
62 throws MalformedChallengeException {
63 if (challengeStr == null) {
64 throw new IllegalArgumentException("Challenge may not be null");
65 }
66 int idx = challengeStr.indexOf(' ');
67 String s = null;
68 if (idx == -1) {
69 s = challengeStr;
70 } else {
71 s = challengeStr.substring(0, idx);
72 }
73 if (s.equals("")) {
74 throw new MalformedChallengeException("Invalid challenge: " + challengeStr);
75 }
76 return s.toLowerCase();
77 }
78
79 /***
80 * Extracts a map of challenge parameters from an authentication challenge.
81 * Keys in the map are lower-cased
82 *
83 * @param challengeStr the authentication challenge string
84 * @return a map of authentication challenge parameters
85 * @throws MalformedChallengeException when the authentication challenge string
86 * is malformed
87 *
88 * @since 2.0beta1
89 */
90 public static Map extractParams(final String challengeStr)
91 throws MalformedChallengeException {
92 if (challengeStr == null) {
93 throw new IllegalArgumentException("Challenge may not be null");
94 }
95 int idx = challengeStr.indexOf(' ');
96 if (idx == -1) {
97 throw new MalformedChallengeException("Invalid challenge: " + challengeStr);
98 }
99 Map map = new HashMap();
100 ParameterParser parser = new ParameterParser();
101 List params = parser.parse(
102 challengeStr.substring(idx + 1, challengeStr.length()), ',');
103 for (int i = 0; i < params.size(); i++) {
104 NameValuePair param = (NameValuePair) params.get(i);
105 map.put(param.getName().toLowerCase(), param.getValue());
106 }
107 return map;
108 }
109
110 /***
111 * Extracts a map of challenges ordered by authentication scheme name
112 *
113 * @param headers the array of authorization challenges
114 * @return a map of authorization challenges
115 *
116 * @throws MalformedChallengeException if any of challenge strings
117 * is malformed
118 *
119 * @since 3.0
120 */
121 public static Map parseChallenges(final Header[] headers)
122 throws MalformedChallengeException {
123 if (headers == null) {
124 throw new IllegalArgumentException("Array of challenges may not be null");
125 }
126 String challenge = null;
127 Map challengemap = new HashMap(headers.length);
128 for (int i = 0; i < headers.length; i++) {
129 challenge = headers[i].getValue();
130 String s = AuthChallengeParser.extractScheme(challenge);
131 challengemap.put(s, challenge);
132 }
133 return challengemap;
134 }
135 }