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.annotation.NotThreadSafe;
35 import org.apache.http.auth.AUTH;
36 import org.apache.http.auth.AuthenticationException;
37 import org.apache.http.auth.ChallengeState;
38 import org.apache.http.auth.ContextAwareAuthScheme;
39 import org.apache.http.auth.Credentials;
40 import org.apache.http.auth.MalformedChallengeException;
41 import org.apache.http.protocol.HTTP;
42 import org.apache.http.protocol.HttpContext;
43 import org.apache.http.util.Args;
44 import org.apache.http.util.CharArrayBuffer;
45
46
47
48
49
50
51
52
53
54
55
56 @NotThreadSafe
57 public abstract class AuthSchemeBase implements ContextAwareAuthScheme {
58
59 private ChallengeState challengeState;
60
61
62
63
64
65
66
67
68
69 @Deprecated
70 public AuthSchemeBase(final ChallengeState challengeState) {
71 super();
72 this.challengeState = challengeState;
73 }
74
75 public AuthSchemeBase() {
76 super();
77 }
78
79
80
81
82
83
84
85
86
87
88
89 public void processChallenge(final Header header) throws MalformedChallengeException {
90 Args.notNull(header, "Header");
91 final String authheader = header.getName();
92 if (authheader.equalsIgnoreCase(AUTH.WWW_AUTH)) {
93 this.challengeState = ChallengeState.TARGET;
94 } else if (authheader.equalsIgnoreCase(AUTH.PROXY_AUTH)) {
95 this.challengeState = ChallengeState.PROXY;
96 } else {
97 throw new MalformedChallengeException("Unexpected header name: " + authheader);
98 }
99
100 CharArrayBuffer buffer;
101 int pos;
102 if (header instanceof FormattedHeader) {
103 buffer = ((FormattedHeader) header).getBuffer();
104 pos = ((FormattedHeader) header).getValuePos();
105 } else {
106 final String s = header.getValue();
107 if (s == null) {
108 throw new MalformedChallengeException("Header value is null");
109 }
110 buffer = new CharArrayBuffer(s.length());
111 buffer.append(s);
112 pos = 0;
113 }
114 while (pos < buffer.length() && HTTP.isWhitespace(buffer.charAt(pos))) {
115 pos++;
116 }
117 final int beginIndex = pos;
118 while (pos < buffer.length() && !HTTP.isWhitespace(buffer.charAt(pos))) {
119 pos++;
120 }
121 final int endIndex = pos;
122 final String s = buffer.substring(beginIndex, endIndex);
123 if (!s.equalsIgnoreCase(getSchemeName())) {
124 throw new MalformedChallengeException("Invalid scheme identifier: " + s);
125 }
126
127 parseChallenge(buffer, pos, buffer.length());
128 }
129
130
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 if (name != null) {
163 return name.toUpperCase(Locale.US);
164 } else {
165 return super.toString();
166 }
167 }
168
169 }