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.Map;
33
34 /***
35 * <p>
36 * Abstract authentication scheme class that lays foundation for all
37 * RFC 2617 compliant authetication schemes and provides capabilities common
38 * to all authentication schemes defined in RFC 2617.
39 * </p>
40 *
41 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
42 */
43 public abstract class RFC2617Scheme implements AuthScheme {
44
45 /***
46 * Authentication parameter map.
47 */
48 private Map params = null;
49
50 /***
51 * Default constructor for RFC2617 compliant authetication schemes.
52 *
53 * @since 3.0
54 */
55 public RFC2617Scheme() {
56 super();
57 }
58
59 /***
60 * Default constructor for RFC2617 compliant authetication schemes.
61 *
62 * @param challenge authentication challenge
63 *
64 * @throws MalformedChallengeException is thrown if the authentication challenge
65 * is malformed
66 *
67 * @deprecated Use parameterless constructor and {@link AuthScheme#processChallenge(String)}
68 * method
69 */
70 public RFC2617Scheme(final String challenge) throws MalformedChallengeException {
71 super();
72 processChallenge(challenge);
73 }
74
75 /***
76 * Processes the given challenge token. Some authentication schemes
77 * may involve multiple challenge-response exchanges. Such schemes must be able
78 * to maintain the state information when dealing with sequential challenges
79 *
80 * @param challenge the challenge string
81 *
82 * @throws MalformedChallengeException is thrown if the authentication challenge
83 * is malformed
84 *
85 * @since 3.0
86 */
87 public void processChallenge(final String challenge) throws MalformedChallengeException {
88 String s = AuthChallengeParser.extractScheme(challenge);
89 if (!s.equalsIgnoreCase(getSchemeName())) {
90 throw new MalformedChallengeException(
91 "Invalid " + getSchemeName() + " challenge: " + challenge);
92 }
93 this.params = AuthChallengeParser.extractParams(challenge);
94 }
95
96 /***
97 * Returns authentication parameters map. Keys in the map are lower-cased.
98 *
99 * @return the map of authentication parameters
100 */
101 protected Map getParameters() {
102 return this.params;
103 }
104
105 /***
106 * Returns authentication parameter with the given name, if available.
107 *
108 * @param name The name of the parameter to be returned
109 *
110 * @return the parameter with the given name
111 */
112 public String getParameter(String name) {
113 if (name == null) {
114 throw new IllegalArgumentException("Parameter name may not be null");
115 }
116 if (this.params == null) {
117 return null;
118 }
119 return (String) this.params.get(name.toLowerCase());
120 }
121
122 /***
123 * Returns authentication realm. The realm may not be null.
124 *
125 * @return the authentication realm
126 */
127 public String getRealm() {
128 return getParameter("realm");
129 }
130
131 /***
132 * Returns a String identifying the authentication challenge. This is
133 * used, in combination with the host and port to determine if
134 * authorization has already been attempted or not. Schemes which
135 * require multiple requests to complete the authentication should
136 * return a different value for each stage in the request.
137 *
138 * <p>Additionally, the ID should take into account any changes to the
139 * authentication challenge and return a different value when appropriate.
140 * For example when the realm changes in basic authentication it should be
141 * considered a different authentication attempt and a different value should
142 * be returned.</p>
143 *
144 * <p>This method simply returns the realm for the challenge.</p>
145 *
146 * @return String a String identifying the authentication challenge. The
147 * returned value may be null.
148 *
149 * @deprecated no longer used
150 */
151 public String getID() {
152 return getRealm();
153 }
154 }