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 /***
34 * This class provides detailed information about the state of the
35 * authentication process.
36 *
37 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
38 * @since 3.0
39 */
40 public class AuthState {
41
42 public static final String PREEMPTIVE_AUTH_SCHEME = "basic";
43
44 /*** Actual authentication scheme */
45 private AuthScheme authScheme = null;
46
47 /*** Whether an authetication challenged has been received */
48 private boolean authRequested = false;
49
50 /*** Whether the authetication challenge has been responsed to */
51 private boolean authAttempted = false;
52
53 /*** Whether preemtive authentication is attempted */
54 private boolean preemptive = false;
55
56 /***
57 * Default constructor.
58 *
59 */
60 public AuthState() {
61 super();
62 }
63
64 /***
65 * Invalidates the authentication state by resetting its parameters.
66 */
67 public void invalidate() {
68 this.authScheme = null;
69 this.authRequested = false;
70 this.authAttempted = false;
71 this.preemptive = false;
72 }
73
74 /***
75 * Tests whether authenication challenge has been received
76 *
77 * @return <tt>true</tt> if authenication challenge has been received,
78 * <tt>false</tt> otherwise
79 */
80 public boolean isAuthRequested() {
81 return this.authRequested;
82 }
83
84 /***
85 * Sets authentication request status
86 *
87 * @param challengeReceived <tt>true</tt> if authenication has been requested,
88 * <tt>false</tt> otherwise
89 */
90 public void setAuthRequested(boolean challengeReceived) {
91 this.authRequested = challengeReceived;
92 }
93
94 /***
95 * Tests whether authenication challenge has been responsed to
96 *
97 * @return <tt>true</tt> if authenication challenge has been responsed to,
98 * <tt>false</tt> otherwise
99 */
100 public boolean isAuthAttempted() {
101 return this.authAttempted;
102 }
103
104 /***
105 * Sets authentication attempt status
106 *
107 * @param challengeResponded <tt>true</tt> if authenication has been attempted,
108 * <tt>false</tt> otherwise
109 */
110 public void setAuthAttempted(boolean challengeResponded) {
111 this.authAttempted = challengeResponded;
112 }
113
114 /***
115 * Preemptively assigns Basic authentication scheme.
116 */
117 public void setPreemptive() {
118 if (!this.preemptive) {
119 if (this.authScheme != null) {
120 throw new IllegalStateException("Authentication state already initialized");
121 }
122 this.authScheme = AuthPolicy.getAuthScheme(PREEMPTIVE_AUTH_SCHEME);
123 this.preemptive = true;
124 }
125 }
126
127 /***
128 * Tests if preemptive authentication is used.
129 *
130 * @return <tt>true</tt> if using the default Basic {@link AuthScheme
131 * authentication scheme}, <tt>false</tt> otherwise.
132 */
133 public boolean isPreemptive() {
134 return this.preemptive;
135 }
136
137 /***
138 * Assigns the given {@link AuthScheme authentication scheme}.
139 *
140 * @param authScheme the {@link AuthScheme authentication scheme}
141 */
142 public void setAuthScheme(final AuthScheme authScheme) {
143 if (authScheme == null) {
144 invalidate();
145 return;
146 }
147 if (this.preemptive && !(this.authScheme.getClass().isInstance(authScheme))) {
148 this.preemptive = false;
149 this.authAttempted = false;
150 }
151 this.authScheme = authScheme;
152 }
153
154 /***
155 * Returns the {@link AuthScheme authentication scheme}.
156 *
157 * @return {@link AuthScheme authentication scheme}
158 */
159 public AuthScheme getAuthScheme() {
160 return authScheme;
161 }
162
163 /***
164 * Returns the authentication realm.
165 *
166 * @return the name of the authentication realm
167 */
168 public String getRealm() {
169 if (this.authScheme != null) {
170 return this.authScheme.getRealm();
171 } else {
172 return null;
173 }
174 }
175
176 public String toString() {
177 StringBuffer buffer = new StringBuffer();
178 buffer.append("Auth state: auth requested [");
179 buffer.append(this.authRequested);
180 buffer.append("]; auth attempted [");
181 buffer.append(this.authAttempted);
182 if (this.authScheme != null) {
183 buffer.append("]; auth scheme [");
184 buffer.append(this.authScheme.getSchemeName());
185 buffer.append("]; realm [");
186 buffer.append(this.authScheme.getRealm());
187 }
188 buffer.append("] preemptive [");
189 buffer.append(this.preemptive);
190 buffer.append("]");
191 return buffer.toString();
192 }
193 }