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.authScheme != null) {
116 throw new IllegalStateException("Authentication state already initialized");
117 }
118 this.authScheme = AuthPolicy.getAuthScheme("basic");
119 this.preemptive = true;
120 }
121
122 /***
123 * Tests if preemptive authentication is used.
124 *
125 * @return <tt>true</tt> if using the default Basic {@link AuthScheme
126 * authentication scheme}, <tt>false</tt> otherwise.
127 */
128 public boolean isPreemptive() {
129 return this.preemptive;
130 }
131
132 /***
133 * Assigns the given {@link AuthScheme authentication scheme}.
134 *
135 * @param authScheme the {@link AuthScheme authentication scheme}
136 */
137 public void setAuthScheme(final AuthScheme authScheme) {
138 this.authScheme = authScheme;
139 this.preemptive = false;
140 }
141
142 /***
143 * Returns the {@link AuthScheme authentication scheme}.
144 *
145 * @return {@link AuthScheme authentication scheme}
146 */
147 public AuthScheme getAuthScheme() {
148 return authScheme;
149 }
150
151 /***
152 * Returns the authentication realm.
153 *
154 * @return the name of the authentication realm
155 */
156 public String getRealm() {
157 if (this.authScheme != null) {
158 return this.authScheme.getRealm();
159 } else {
160 return null;
161 }
162 }
163
164 public String toString() {
165 StringBuffer buffer = new StringBuffer();
166 buffer.append("Auth state: auth requested [");
167 buffer.append(this.authRequested);
168 buffer.append("]; auth attempted [");
169 buffer.append(this.authAttempted);
170 if (this.authScheme != null) {
171 buffer.append("]; auth scheme [");
172 buffer.append(this.authScheme.getSchemeName());
173 buffer.append("]; realm [");
174 buffer.append(this.authScheme.getRealm());
175 }
176 buffer.append("] preemptive [");
177 buffer.append(this.preemptive);
178 buffer.append("]");
179 return buffer.toString();
180 }
181 }