List of usage examples for org.springframework.web.util UriComponents getUserInfo
@Nullable public abstract String getUserInfo();
From source file:org.mitre.discovery.util.WebfingerURLNormalizer.java
/** * Normalize the resource string as per OIDC Discovery. * @param identifier//from w w w . j av a 2s.c o m * @return the normalized string, or null if the string can't be normalized */ public static UriComponents normalizeResource(String identifier) { // try to parse the URI // NOTE: we can't use the Java built-in URI class because it doesn't split the parts appropriately if (Strings.isNullOrEmpty(identifier)) { logger.warn("Can't normalize null or empty URI: " + identifier); return null; // nothing we can do } else { //UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(identifier); UriComponentsBuilder builder = UriComponentsBuilder.newInstance(); Matcher m = pattern.matcher(identifier); if (m.matches()) { builder.scheme(m.group(2)); builder.userInfo(m.group(6)); builder.host(m.group(8)); String port = m.group(10); if (!Strings.isNullOrEmpty(port)) { builder.port(Integer.parseInt(port)); } builder.path(m.group(11)); builder.query(m.group(13)); builder.fragment(m.group(15)); // we throw away the hash, but this is the group it would be if we kept it } else { // doesn't match the pattern, throw it out logger.warn("Parser couldn't match input: " + identifier); return null; } UriComponents n = builder.build(); if (Strings.isNullOrEmpty(n.getScheme())) { if (!Strings.isNullOrEmpty(n.getUserInfo()) && Strings.isNullOrEmpty(n.getPath()) && Strings.isNullOrEmpty(n.getQuery()) && n.getPort() < 0) { // scheme empty, userinfo is not empty, path/query/port are empty // set to "acct" (rule 2) builder.scheme("acct"); } else { // scheme is empty, but rule 2 doesn't apply // set scheme to "https" (rule 3) builder.scheme("https"); } } // fragment must be stripped (rule 4) builder.fragment(null); return builder.build(); } }
From source file:org.mitre.discovery.util.WebfingerURLNormalizer.java
public static String serializeURL(UriComponents uri) { if (uri.getScheme() != null && (uri.getScheme().equals("acct") || uri.getScheme().equals("mailto") || uri.getScheme().equals("tel") || uri.getScheme().equals("device"))) { // serializer copied from HierarchicalUriComponents but with "//" removed StringBuilder uriBuilder = new StringBuilder(); if (uri.getScheme() != null) { uriBuilder.append(uri.getScheme()); uriBuilder.append(':'); }/* w ww . ja v a2 s . co m*/ if (uri.getUserInfo() != null || uri.getHost() != null) { if (uri.getUserInfo() != null) { uriBuilder.append(uri.getUserInfo()); uriBuilder.append('@'); } if (uri.getHost() != null) { uriBuilder.append(uri.getHost()); } if (uri.getPort() != -1) { uriBuilder.append(':'); uriBuilder.append(uri.getPort()); } } String path = uri.getPath(); if (StringUtils.hasLength(path)) { if (uriBuilder.length() != 0 && path.charAt(0) != '/') { uriBuilder.append('/'); } uriBuilder.append(path); } String query = uri.getQuery(); if (query != null) { uriBuilder.append('?'); uriBuilder.append(query); } if (uri.getFragment() != null) { uriBuilder.append('#'); uriBuilder.append(uri.getFragment()); } return uriBuilder.toString(); } else { return uri.toUriString(); } }
From source file:io.spring.initializr.web.autoconfigure.CloudfoundryEnvironmentPostProcessor.java
@Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication springApplication) { Map<String, Object> map = new LinkedHashMap<>(); String uri = environment.getProperty("vcap.services.stats-index.credentials.uri"); if (StringUtils.hasText(uri)) { UriComponents uriComponents = UriComponentsBuilder.fromUriString(uri).build(); String userInfo = uriComponents.getUserInfo(); if (StringUtils.hasText(userInfo)) { String[] credentials = userInfo.split(":"); map.put("initializr.stats.elastic.username", credentials[0]); map.put("initializr.stats.elastic.password", credentials[1]); }/*from w ww.ja va2 s . c o m*/ map.put("initializr.stats.elastic.uri", UriComponentsBuilder.fromUriString(uri).userInfo(null).build().toString()); addOrReplace(environment.getPropertySources(), map); } }
From source file:org.mitre.discovery.web.DiscoveryEndpoint.java
@RequestMapping(value = { "/" + WEBFINGER_URL }, produces = MediaType.APPLICATION_JSON_VALUE)
public String webfinger(@RequestParam("resource") String resource,
@RequestParam(value = "rel", required = false) String rel, Model model) {
if (!Strings.isNullOrEmpty(rel) && !rel.equals("http://openid.net/specs/connect/1.0/issuer")) {
logger.warn("Responding to webfinger request for non-OIDC relation: " + rel);
}//from w ww . j ava2 s . c o m
if (!resource.equals(config.getIssuer())) {
// it's not the issuer directly, need to check other methods
UriComponents resourceUri = WebfingerURLNormalizer.normalizeResource(resource);
if (resourceUri != null && resourceUri.getScheme() != null && resourceUri.getScheme().equals("acct")) {
// acct: URI (email address format)
// check on email addresses first
UserInfo user = userService
.getByEmailAddress(resourceUri.getUserInfo() + "@" + resourceUri.getHost());
if (user == null) {
// user wasn't found, see if the local part of the username matches, plus our issuer host
user = userService.getByUsername(resourceUri.getUserInfo()); // first part is the username
if (user != null) {
// username matched, check the host component
UriComponents issuerComponents = UriComponentsBuilder.fromHttpUrl(config.getIssuer())
.build();
if (!Strings.nullToEmpty(issuerComponents.getHost())
.equals(Strings.nullToEmpty(resourceUri.getHost()))) {
logger.info("Host mismatch, expected " + issuerComponents.getHost() + " got "
+ resourceUri.getHost());
model.addAttribute(HttpCodeView.CODE, HttpStatus.NOT_FOUND);
return HttpCodeView.VIEWNAME;
}
} else {
// if the user's still null, punt and say we didn't find them
logger.info("User not found: " + resource);
model.addAttribute(HttpCodeView.CODE, HttpStatus.NOT_FOUND);
return HttpCodeView.VIEWNAME;
}
}
} else {
logger.info("Unknown URI format: " + resource);
model.addAttribute(HttpCodeView.CODE, HttpStatus.NOT_FOUND);
return HttpCodeView.VIEWNAME;
}
}
// if we got here, then we're good, return ourselves
model.addAttribute("resource", resource);
model.addAttribute("issuer", config.getIssuer());
return "webfingerView";
}