uri-based parsing of user inputs

pull/324/merge
Justin Richer 2013-04-18 15:22:49 -04:00
parent 51dfd67d0c
commit 70cac0c690
1 changed files with 51 additions and 7 deletions

View File

@ -3,6 +3,8 @@
*/
package org.mitre.openid.connect.client.service.impl;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.ExecutionException;
import javax.servlet.http.HttpServletRequest;
@ -32,9 +34,9 @@ public class WebfingerIssuerService implements IssuerService {
private static Logger logger = LoggerFactory.getLogger(WebfingerIssuerService.class);
// map of user input -> issuer, loaded dynamically from webfinger discover
private LoadingCache<String, String> issuers;
private LoadingCache<URI, String> issuers;
private String parameterName = "login";
private String parameterName = "identifier";
public WebfingerIssuerService() {
issuers = CacheBuilder.newBuilder().build(new WebfingerIssuerFetcher());
@ -67,9 +69,51 @@ public class WebfingerIssuerService implements IssuerService {
* @param resource
* @return
*/
private String normalizeResource(String resource) {
// TODO
return null;
public static URI normalizeResource(String resource) {
// try to parse the URI
try {
URI uri = new URI(resource);
if (!Strings.isNullOrEmpty(uri.getAuthority())) {
if (!Strings.isNullOrEmpty(uri.getScheme())) {
// there's already a scheme, we'll keep it
// strip the fragment
URI noFragment = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), uri.getQuery(), null);
return noFragment;
} else {
// no scheme
if (Strings.isNullOrEmpty(uri.getUserInfo())) {
// no "user" portion, treat as https
URI https = new URI("https", uri.getAuthority(), uri.getPath(), uri.getQuery(), null);
return https;
} else {
// it's a user@host format, prepend the "acct:" scheme
URI acct = new URI("acct", uri.getAuthority(), uri.getPath(), uri.getQuery(), null);
return acct;
}
}
} else {
// no authority section, this is an error
logger.warn("No authority section: " + resource);
return null;
}
} catch (URISyntaxException e) {
logger.warn("Failed parsing input URI: " + resource, e);
return null;
}
}
@ -92,7 +136,7 @@ public class WebfingerIssuerService implements IssuerService {
* @author jricher
*
*/
private class WebfingerIssuerFetcher extends CacheLoader<String, String> {
private class WebfingerIssuerFetcher extends CacheLoader<URI, String> {
private HttpClient httpClient = new DefaultHttpClient();
private HttpComponentsClientHttpRequestFactory httpFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
private RestTemplate restTemplate = new RestTemplate(httpFactory);
@ -100,7 +144,7 @@ public class WebfingerIssuerService implements IssuerService {
* @see com.google.common.cache.CacheLoader#load(java.lang.Object)
*/
@Override
public String load(String key) throws Exception {
public String load(URI key) throws Exception {
// TODO Auto-generated method stub
return null;
}