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