-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTokenAuthScheme.java
More file actions
66 lines (56 loc) · 2.1 KB
/
TokenAuthScheme.java
File metadata and controls
66 lines (56 loc) · 2.1 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package biz.neustar.pagerduty.util;
import org.apache.http.Header;
import org.apache.http.HttpRequest;
import org.apache.http.auth.AUTH;
import org.apache.http.auth.AuthenticationException;
import org.apache.http.auth.Credentials;
import org.apache.http.impl.auth.RFC2617Scheme;
import org.apache.http.message.BufferedHeader;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.CharArrayBuffer;
public class TokenAuthScheme extends RFC2617Scheme {
public static final String TOKEN_HEADER_PREAMBLE = ": Token token=";
private boolean complete;
public TokenAuthScheme() {
this.complete = false;
}
@Override
public Header authenticate(Credentials creds, HttpRequest request)
throws AuthenticationException {
return authenticate(creds, request, new BasicHttpContext());
}
@Override
public String getSchemeName() {
return "token";
}
@Override
public boolean isComplete() {
return this.complete;
}
@Override
public boolean isConnectionBased() {
return false;
}
/* (non-Javadoc)
* @see org.apache.http.impl.auth.AuthSchemeBase#authenticate(org.apache.http.auth.Credentials, org.apache.http.HttpRequest, org.apache.http.protocol.HttpContext)
*/
@Override
public Header authenticate(Credentials credentials, HttpRequest request,
HttpContext context) throws AuthenticationException {
if (credentials instanceof TokenAuthCredentials) {
TokenAuthCredentials tokenCredentials = (TokenAuthCredentials) credentials;
CharArrayBuffer buffer = new CharArrayBuffer(32);
if (isProxy()) {
buffer.append(AUTH.PROXY_AUTH_RESP);
} else {
buffer.append(AUTH.WWW_AUTH_RESP);
}
buffer.append(TOKEN_HEADER_PREAMBLE);
buffer.append(tokenCredentials.getPassword());
return new BufferedHeader(buffer);
} else {
return super.authenticate(credentials, request, context);
}
}
}