Below are some updates to the httpclient4 OAuthScheme which handle
some issues with certain types of URLS which are either wrapped or
have existing query string parameters.
@Override
public void processChallenge(final Header header)
throws MalformedChallengeException {
super.processChallenge(header);
this.complete = true;
}
public Header authenticate(Credentials credentials, HttpRequest
request)
throws AuthenticationException {
try {
// The following is more complex because some Requests don't
include the
// server name in the URL instead it just has the path and query
string
String uri;
String method;
HttpUriRequest uriRequest = getHttpUriRequest(request);
if (uriRequest != null) {
uri = uriRequest.getURI().toString();
method = uriRequest.getMethod();
} else {
RequestLine requestLine = request.getRequestLine();
uri = requestLine.getUri();
method = requestLine.getMethod();
}
String url = getUriWithoutQuery(uri);
Collection<Entry<String, String>> parameters = getParameters
(uri);
OAuthMessage message = new OAuthMessage(method, url,
parameters);
OAuthAccessor accessor = getOAuthAccessor(credentials);
message.addRequiredParameters(accessor);
String realm = getParameter("realm");
String authorization = message.getAuthorizationHeader(realm);
return new BasicHeader("Authorization", authorization);
} catch (Throwable t) {
throw new AuthenticationException("Unable to create OAuth
header", t);
}
}
private OAuthAccessor getOAuthAccessor(Credentials credentials) {
String consumerKey;
String consumerSecret;
String accessToken = null;
String tokenSecret = null;
if (credentials instanceof OAuthCredentials) {
OAuthCredentials oauthCredentials = (OAuthCredentials)
credentials;
consumerKey = oauthCredentials.getConsumerKey();
consumerSecret = oauthCredentials.getConsumerSecret();
accessToken = oauthCredentials.getAccessToken();
tokenSecret = oauthCredentials.getTokenSecret();
} else {
consumerKey = credentials.getUserPrincipal().getName();
consumerSecret = credentials.getPassword();
}
OAuthAccessor accessor = new OAuthAccessor(new OAuthConsumer("",
consumerKey, consumerSecret, null));
accessor.accessToken = accessToken;
accessor.tokenSecret = tokenSecret;
return accessor;
}
private HttpUriRequest getHttpUriRequest(final HttpRequest request)
{
if (request instanceof RequestWrapper) {
RequestWrapper wrappedRequest = (RequestWrapper)request;
return getHttpUriRequest(wrappedRequest.getOriginal());
} else if (request instanceof HttpUriRequest) {
return (HttpUriRequest)request;
} else {
return null;
}
}
private String getUriWithoutQuery(String uri) {
String url = uri;
int queryIndex = url.indexOf('?');
if (queryIndex != -1) {
url = url.substring(0, queryIndex);
}
return url;
}
private Collection<Entry<String, String>> getParameters(String uri)
throws URISyntaxException {
List<NameValuePair> parameters = URLEncodedUtils.parse(new URI
(uri), null);
List<Entry<String,String>> parameterEntries = new
ArrayList<Entry<String,String>>();
for (NameValuePair parameter : parameters) {
String name = parameter.getName();
String value = parameter.getValue();
parameterEntries.add(new OAuth.Parameter(name, value));
}
return parameterEntries;
}
public String getSchemeName() {
return "oauth";
}
public boolean isComplete() {
return complete;
}
public boolean isConnectionBased() {
return false;
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"OAuth" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [email protected]
For more options, visit this group at http://groups.google.com/group/oauth?hl=en
-~----------~----~----~----~------~----~------~--~---