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 package org.apache.http.impl.auth;
28
29 import java.util.Locale;
30
31 import org.apache.http.FormattedHeader;
32 import org.apache.http.Header;
33 import org.apache.http.HttpRequest;
34 import org.apache.http.auth.AUTH;
35 import org.apache.http.auth.AuthenticationException;
36 import org.apache.http.auth.ChallengeState;
37 import org.apache.http.auth.ContextAwareAuthScheme;
38 import org.apache.http.auth.Credentials;
39 import org.apache.http.auth.MalformedChallengeException;
40 import org.apache.http.protocol.HTTP;
41 import org.apache.http.protocol.HttpContext;
42 import org.apache.http.util.Args;
43 import org.apache.http.util.CharArrayBuffer;
44
45
46
47
48
49
50
51
52
53
54
55 public abstract class AuthSchemeBase implements ContextAwareAuthScheme {
56
57 protected ChallengeState challengeState;
58
59
60
61
62
63
64
65
66
67 @Deprecated
68 public AuthSchemeBase(final ChallengeState challengeState) {
69 super();
70 this.challengeState = challengeState;
71 }
72
73 public AuthSchemeBase() {
74 super();
75 }
76
77
78
79
80
81
82
83
84
85
86
87 @Override
88 public void processChallenge(final Header header) throws MalformedChallengeException {
89 Args.notNull(header, "Header");
90 final String authheader = header.getName();
91 if (authheader.equalsIgnoreCase(AUTH.WWW_AUTH)) {
92 this.challengeState = ChallengeState.TARGET;
93 } else if (authheader.equalsIgnoreCase(AUTH.PROXY_AUTH)) {
94 this.challengeState = ChallengeState.PROXY;
95 } else {
96 throw new MalformedChallengeException("Unexpected header name: " + authheader);
97 }
98
99 final CharArrayBuffer buffer;
100 int pos;
101 if (header instanceof FormattedHeader) {
102 buffer = ((FormattedHeader) header).getBuffer();
103 pos = ((FormattedHeader) header).getValuePos();
104 } else {
105 final String s = header.getValue();
106 if (s == null) {
107 throw new MalformedChallengeException("Header value is null");
108 }
109 buffer = new CharArrayBuffer(s.length());
110 buffer.append(s);
111 pos = 0;
112 }
113 while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) {
114 pos++;
115 }
116 final int beginIndex = pos;
117 while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) {
118 pos++;
119 }
120 final int endIndex = pos;
121 final String s = buffer.substring(beginIndex, endIndex);
122 if (!s.equalsIgnoreCase(getSchemeName())) {
123 throw new MalformedChallengeException("Invalid scheme identifier: " + s);
124 }
125
126 parseChallenge(buffer, pos, buffer.length());
127 }
128
129
130 @Override
131 @SuppressWarnings("deprecation")
132 public Header authenticate(
133 final Credentials credentials,
134 final HttpRequest request,
135 final HttpContext context) throws AuthenticationException {
136 return authenticate(credentials, request);
137 }
138
139 protected abstract void parseChallenge(
140 CharArrayBuffer buffer, int beginIndex, int endIndex) throws MalformedChallengeException;
141
142
143
144
145
146 public boolean isProxy() {
147 return this.challengeState != null && this.challengeState == ChallengeState.PROXY;
148 }
149
150
151
152
153
154
155 public ChallengeState getChallengeState() {
156 return this.challengeState;
157 }
158
159 @Override
160 public String toString() {
161 final String name = getSchemeName();
162 return name != null ? name.toUpperCase(Locale.ROOT) : super.toString();
163 }
164
165 }