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.hc.client5.http.auth; 28 29 import java.util.Queue; 30 31 import org.apache.hc.core5.util.Args; 32 33 /** 34 * This class represents the actual state of authentication handshake including the current {@link AuthScheme} 35 * used for request authorization as well as a collection of backup authentication options if available. 36 * 37 * @since 4.5 38 */ 39 public class AuthExchange { 40 41 public enum State { 42 43 UNCHALLENGED, CHALLENGED, HANDSHAKE, FAILURE, SUCCESS 44 45 } 46 47 private State state; 48 private AuthScheme authScheme; 49 private Queue<AuthScheme> authOptions; 50 51 public AuthExchange() { 52 super(); 53 this.state = State.UNCHALLENGED; 54 } 55 56 public void reset() { 57 this.state = State.UNCHALLENGED; 58 this.authOptions = null; 59 this.authScheme = null; 60 } 61 62 public State getState() { 63 return this.state; 64 } 65 66 public void setState(final State state) { 67 this.state = state != null ? state : State.UNCHALLENGED; 68 } 69 70 /** 71 * Returns actual {@link AuthScheme}. May be null. 72 */ 73 public AuthScheme getAuthScheme() { 74 return this.authScheme; 75 } 76 77 /** 78 * Returns {@code true} if the actual authentication scheme is connection based. 79 */ 80 public boolean isConnectionBased() { 81 return this.authScheme != null && this.authScheme.isConnectionBased(); 82 } 83 84 /** 85 * Resets the auth state with {@link AuthScheme} and clears auth options. 86 * 87 * @param authScheme auth scheme. May not be null. 88 */ 89 public void select(final AuthScheme authScheme) { 90 Args.notNull(authScheme, "Auth scheme"); 91 this.authScheme = authScheme; 92 this.authOptions = null; 93 } 94 95 /** 96 * Returns available auth options. May be null. 97 */ 98 public Queue<AuthScheme> getAuthOptions() { 99 return this.authOptions; 100 } 101 102 /** 103 * Updates the auth state with a queue of auth options. 104 * 105 * @param authOptions a queue of auth options. May not be null or empty. 106 */ 107 public void setOptions(final Queue<AuthScheme> authOptions) { 108 Args.notEmpty(authOptions, "Queue of auth options"); 109 this.authOptions = authOptions; 110 } 111 112 @Override 113 public String toString() { 114 final StringBuilder buffer = new StringBuilder(); 115 buffer.append("[").append(this.state); 116 if (this.authScheme != null) { 117 buffer.append(" ").append(this.authScheme); 118 } 119 buffer.append("]"); 120 return buffer.toString(); 121 } 122 123 }