1 /*
2 * ====================================================================
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ====================================================================
19 *
20 * This software consists of voluntary contributions made by many
21 * individuals on behalf of the Apache Software Foundation. For more
22 * information on the Apache Software Foundation, please see
23 * <http://www.apache.org/>.
24 *
25 */
26
27 package org.apache.http.auth;
28
29 import java.util.Queue;
30
31 import org.apache.http.annotation.NotThreadSafe;
32
33 /**
34 * This class provides detailed information about the state of the authentication process.
35 *
36 * @since 4.0
37 */
38 @NotThreadSafe
39 public class AuthState {
40
41 /** Actual state of authentication protocol */
42 private AuthProtocolState state;
43
44 /** Actual authentication scheme */
45 private AuthScheme authScheme;
46
47 /** Actual authentication scope */
48 private AuthScope authScope;
49
50 /** Credentials selected for authentication */
51 private Credentials credentials;
52
53 /** Available auth options */
54 private Queue<AuthOption> authOptions;
55
56 public AuthState() {
57 super();
58 this.state = AuthProtocolState.UNCHALLENGED;
59 }
60
61 /**
62 * Resets the auth state.
63 *
64 * @since 4.2
65 */
66 public void reset() {
67 this.state = AuthProtocolState.UNCHALLENGED;
68 this.authOptions = null;
69 this.authScheme = null;
70 this.authScope = null;
71 this.credentials = null;
72 }
73
74 /**
75 * @since 4.2
76 */
77 public AuthProtocolState getState() {
78 return this.state;
79 }
80
81 /**
82 * @since 4.2
83 */
84 public void setState(final AuthProtocolState state) {
85 this.state = state != null ? state : AuthProtocolState.UNCHALLENGED;
86 }
87
88 /**
89 * Returns actual {@link AuthScheme}. May be null.
90 */
91 public AuthScheme getAuthScheme() {
92 return this.authScheme;
93 }
94
95 /**
96 * Returns actual {@link Credentials}. May be null.
97 */
98 public Credentials getCredentials() {
99 return this.credentials;
100 }
101
102 /**
103 * Updates the auth state with {@link AuthScheme} and {@link Credentials}.
104 *
105 * @param authScheme auth scheme. May not be null.
106 * @param credentials user crednetials. May not be null.
107 *
108 * @since 4.2
109 */
110 public void update(final AuthScheme authScheme, final Credentials credentials) {
111 if (authScheme == null) {
112 throw new IllegalArgumentException("Auth scheme may not be null or empty");
113 }
114 if (credentials == null) {
115 throw new IllegalArgumentException("Credentials may not be null or empty");
116 }
117 this.authScheme = authScheme;
118 this.credentials = credentials;
119 this.authOptions = null;
120 }
121
122 /**
123 * Returns available {@link AuthOption}s. May be null.
124 *
125 * @since 4.2
126 */
127 public Queue<AuthOption> getAuthOptions() {
128 return this.authOptions;
129 }
130
131 /**
132 * Returns <code>true</code> if {@link AuthOption}s are available, <code>false</code>
133 * otherwise.
134 *
135 * @since 4.2
136 */
137 public boolean hasAuthOptions() {
138 return this.authOptions != null && !this.authOptions.isEmpty();
139 }
140
141 /**
142 * Updates the auth state with a queue of {@link AuthOption}s.
143 *
144 * @param authOptions a queue of auth options. May not be null or empty.
145 *
146 * @since 4.2
147 */
148 public void update(final Queue<AuthOption> authOptions) {
149 if (authOptions == null || authOptions.isEmpty()) {
150 throw new IllegalArgumentException("Queue of auth options may not be null or empty");
151 }
152 this.authOptions = authOptions;
153 this.authScheme = null;
154 this.credentials = null;
155 }
156
157 /**
158 * Invalidates the authentication state by resetting its parameters.
159 *
160 * @deprecated (4.2) use {@link #reset()}
161 */
162 @Deprecated
163 public void invalidate() {
164 reset();
165 }
166
167 /**
168 * @deprecated (4.2) do not use
169 */
170 @Deprecated
171 public boolean isValid() {
172 return this.authScheme != null;
173 }
174
175 /**
176 * Assigns the given {@link AuthScheme authentication scheme}.
177 *
178 * @param authScheme the {@link AuthScheme authentication scheme}
179 *
180 * @deprecated (4.2) use {@link #update(AuthScheme, Credentials)}
181 */
182 @Deprecated
183 public void setAuthScheme(final AuthScheme authScheme) {
184 if (authScheme == null) {
185 reset();
186 return;
187 }
188 this.authScheme = authScheme;
189 }
190
191 /**
192 * Sets user {@link Credentials} to be used for authentication
193 *
194 * @param credentials User credentials
195 *
196 * @deprecated (4.2) use {@link #update(AuthScheme, Credentials)}
197 */
198 @Deprecated
199 public void setCredentials(final Credentials credentials) {
200 this.credentials = credentials;
201 }
202
203 /**
204 * Returns actual {@link AuthScope} if available
205 *
206 * @return actual authentication scope if available, <code>null</code otherwise
207 *
208 * @deprecated (4.2) do not use.
209 */
210 @Deprecated
211 public AuthScope getAuthScope() {
212 return this.authScope;
213 }
214
215 /**
216 * Sets actual {@link AuthScope}.
217 *
218 * @param authScope Authentication scope
219 *
220 * @deprecated (4.2) do not use.
221 */
222 @Deprecated
223 public void setAuthScope(final AuthScope authScope) {
224 this.authScope = authScope;
225 }
226
227 @Override
228 public String toString() {
229 StringBuilder buffer = new StringBuilder();
230 buffer.append("state:").append(this.state).append(";");
231 if (this.authScheme != null) {
232 buffer.append("auth scheme:").append(this.authScheme.getSchemeName()).append(";");
233 }
234 if (this.credentials != null) {
235 buffer.append("credentials present");
236 }
237 return buffer.toString();
238 }
239
240 }