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