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      * Updates the auth state with a queue of {@link AuthOption}s.
138      *
139      * @param authOptions a queue of auth options. May not be null or empty.
140      *
141      * @since 4.2
142      */
143     public void update(final Queue<AuthOption> authOptions) {
144         Args.notEmpty(authOptions, "Queue of auth options");
145         this.authOptions = authOptions;
146         this.authScheme = null;
147         this.credentials = null;
148     }
149 
150     /**
151      * Invalidates the authentication state by resetting its parameters.
152      *
153      * @deprecated (4.2)  use {@link #reset()}
154      */
155     @Deprecated
156     public void invalidate() {
157         reset();
158     }
159 
160     /**
161      * @deprecated (4.2) do not use
162      */
163     @Deprecated
164     public boolean isValid() {
165         return this.authScheme != null;
166     }
167 
168     /**
169      * Assigns the given {@link AuthScheme authentication scheme}.
170      *
171      * @param authScheme the {@link AuthScheme authentication scheme}
172      *
173      * @deprecated (4.2)  use {@link #update(AuthScheme, Credentials)}
174      */
175     @Deprecated
176     public void setAuthScheme(final AuthScheme authScheme) {
177         if (authScheme == null) {
178             reset();
179             return;
180         }
181         this.authScheme = authScheme;
182     }
183 
184     /**
185      * Sets user {@link Credentials} to be used for authentication
186      *
187      * @param credentials User credentials
188      *
189      * @deprecated (4.2)  use {@link #update(AuthScheme, Credentials)}
190      */
191     @Deprecated
192     public void setCredentials(final Credentials credentials) {
193         this.credentials = credentials;
194     }
195 
196     /**
197      * Returns actual {@link AuthScope} if available
198      *
199      * @return actual authentication scope if available, {@code null} otherwise
200      *
201      * @deprecated (4.2)  do not use.
202      */
203     @Deprecated
204     public AuthScope getAuthScope() {
205         return this.authScope;
206     }
207 
208     /**
209      * Sets actual {@link AuthScope}.
210      *
211      * @param authScope Authentication scope
212      *
213      * @deprecated (4.2)  do not use.
214      */
215     @Deprecated
216     public void setAuthScope(final AuthScope authScope) {
217         this.authScope = authScope;
218     }
219 
220     @Override
221     public String toString() {
222         final StringBuilder buffer = new StringBuilder();
223         buffer.append("state:").append(this.state).append(";");
224         if (this.authScheme != null) {
225             buffer.append("auth scheme:").append(this.authScheme.getSchemeName()).append(";");
226         }
227         if (this.credentials != null) {
228             buffer.append("credentials present");
229         }
230         return buffer.toString();
231     }
232 
233 }