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
32 package org.apache.commons.httpclient.auth;
33
34 import java.util.Map;
35
36 /***
37 * <p>
38 * Abstract authentication scheme class that lays foundation for all
39 * RFC 2617 compliant authetication schemes and provides capabilities common
40 * to all authentication schemes defined in RFC 2617.
41 * </p>
42 *
43 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
44 */
45 public abstract class RFC2617Scheme extends AuthSchemeBase {
46
47 /***
48 * Authentication parameter map.
49 */
50 private Map params = null;
51
52 /***
53 * Default constructor for RFC2617 compliant authetication schemes.
54 *
55 * @param challenge authentication challenge
56 *
57 * @throws MalformedChallengeException is thrown if the authentication challenge
58 * is malformed
59 */
60 public RFC2617Scheme(final String challenge) throws MalformedChallengeException {
61 super(challenge);
62 String s = AuthChallengeParser.extractScheme(challenge);
63 if (!s.equalsIgnoreCase(getSchemeName())) {
64 throw new MalformedChallengeException(
65 "Invalid " + getSchemeName() + " challenge: " + challenge);
66 }
67 this.params = AuthChallengeParser.extractParams(challenge);
68 }
69
70 /***
71 * Returns authentication parameters map. Keys in the map are lower-cased.
72 *
73 * @return the map of authentication parameters
74 */
75 protected Map getParameters() {
76 return this.params;
77 }
78
79 /***
80 * Returns authentication parameter with the given name, if available.
81 *
82 * @param name The name of the parameter to be returned
83 *
84 * @return the parameter with the given name
85 */
86 public String getParameter(String name) {
87 if (name == null) {
88 throw new IllegalArgumentException("Parameter name may not be null");
89 }
90 return (String) this.params.get(name.toLowerCase());
91 }
92
93 /***
94 * Returns authentication realm. The realm may not be null.
95 *
96 * @return the authentication realm
97 */
98 public String getRealm() {
99 return getParameter("realm");
100 }
101
102 /***
103 * Returns a String identifying the authentication challenge. This is
104 * used, in combination with the host and port to determine if
105 * authorization has already been attempted or not. Schemes which
106 * require multiple requests to complete the authentication should
107 * return a different value for each stage in the request.
108 *
109 * <p>Additionally, the ID should take into account any changes to the
110 * authentication challenge and return a different value when appropriate.
111 * For example when the realm changes in basic authentication it should be
112 * considered a different authentication attempt and a different value should
113 * be returned.</p>
114 *
115 * <p>This method simply returns the realm for the challenge.</p>
116 *
117 * @return String a String identifying the authentication challenge. The
118 * returned value may be null.
119 */
120 public String getID() {
121 return getRealm();
122 }
123 }