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