/** * Replace URI template variables in the target URL with encoded model * attributes or URI variables from the current request. Model attributes * referenced in the URL are removed from the model. * @param targetUrl the redirect URL * @param model a Map that contains model attributes * @param currentUriVariables current request URI variables to use * @param encodingScheme the encoding scheme to use * @throws UnsupportedEncodingException if string encoding failed */ protected StringBuilder replaceUriTemplateVariables( String targetUrl, Map<String, Object> model, Map<String, String> currentUriVariables, String encodingScheme) throws UnsupportedEncodingException { StringBuilder result = new StringBuilder(); Matcher matcher = URI_TEMPLATE_VARIABLE_PATTERN.matcher(targetUrl); int endLastMatch = 0; while (matcher.find()) { String name = matcher.group(1); Object value = (model.containsKey(name) ? model.remove(name) : currentUriVariables.get(name)); if (value == null) { throw new IllegalArgumentException("Model has no value for key '" + name + "'"); } result.append(targetUrl.substring(endLastMatch, matcher.start())); result.append(UriUtils.encodePathSegment(value.toString(), encodingScheme)); endLastMatch = matcher.end(); } result.append(targetUrl.substring(endLastMatch, targetUrl.length())); return result; }
/** * Replace URI template variables in the target URL with encoded model * attributes or URI variables from the current request. Model attributes * referenced in the URL are removed from the model. * @param targetUrl the redirect URL * @param model a Map that contains model attributes * @param currentUriVariables current request URI variables to use * @param encodingScheme the encoding scheme to use * @throws UnsupportedEncodingException if string encoding failed */ protected StringBuilder replaceUriTemplateVariables( String targetUrl, Map<String, Object> model, Map<String, String> currentUriVariables, String encodingScheme) throws UnsupportedEncodingException { StringBuilder result = new StringBuilder(); Matcher matcher = URI_TEMPLATE_VARIABLE_PATTERN.matcher(targetUrl); int endLastMatch = 0; while (matcher.find()) { String name = matcher.group(1); Object value = (model.containsKey(name) ? model.remove(name) : currentUriVariables.get(name)); if (value == null) { throw new IllegalArgumentException("Model has no value for key '" + name + "'"); } result.append(targetUrl.substring(endLastMatch, matcher.start())); result.append(UriUtils.encodePathSegment(value.toString(), encodingScheme)); endLastMatch = matcher.end(); } result.append(targetUrl.substring(endLastMatch, targetUrl.length())); return result; }
@Test public void encodePathSegment() { assertEquals("Invalid encoded result", "foobar", UriUtils.encodePathSegment("foobar", CHARSET)); assertEquals("Invalid encoded result", "%2Ffoo%2Fbar", UriUtils.encodePathSegment("/foo/bar", CHARSET)); }
@Override protected ClientHttpRequest createRequest(URI url, HttpMethod method) throws IOException { ClientHttpRequest httpRequest = super.createRequest(url, method); httpRequest.getHeaders().add("Authorization", String.format("Basic %s", Base64.encode(String.format("%s:%s", UriUtils.encodePathSegment(clientConfig.getClientId(), "UTF-8"), UriUtils.encodePathSegment(clientConfig.getClientSecret(), "UTF-8"))))); return httpRequest; } };
try { uri = StringUtils.replace(uri, template, (value != null ? UriUtils.encodePathSegment(value, encoding) : ""));
@Override public UserDetails loadUserByUsername(String clientId) throws UsernameNotFoundException { try { String decodedClientId = UriUtils.decode(clientId, "UTF-8"); ClientDetailsEntity client = clientDetailsService.loadClientByClientId(decodedClientId); if (client != null) { String encodedPassword = UriUtils.encodePathSegment(Strings.nullToEmpty(client.getClientSecret()), "UTF-8"); if (config.isHeartMode() || // if we're running HEART mode turn off all client secrets (client.getTokenEndpointAuthMethod() != null && (client.getTokenEndpointAuthMethod().equals(AuthMethod.PRIVATE_KEY) || client.getTokenEndpointAuthMethod().equals(AuthMethod.SECRET_JWT)))) { // Issue a random password each time to prevent password auth from being used (or skipped) // for private key or shared key clients, see #715 encodedPassword = new BigInteger(512, new SecureRandom()).toString(16); } boolean enabled = true; boolean accountNonExpired = true; boolean credentialsNonExpired = true; boolean accountNonLocked = true; Collection<GrantedAuthority> authorities = new HashSet<>(client.getAuthorities()); authorities.add(ROLE_CLIENT); return new User(decodedClientId, encodedPassword, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities); } else { throw new UsernameNotFoundException("Client not found: " + clientId); } } catch (UnsupportedEncodingException | InvalidClientException e) { throw new UsernameNotFoundException("Client not found: " + clientId); } }
RegisteredClient registered = new RegisteredClient(client, token.getValue(), config.getIssuer() + "register/" + UriUtils.encodePathSegment(client.getClientId(), "UTF-8"));
RegisteredClient registered = new RegisteredClient(client, token.getValue(), config.getIssuer() + "resource/" + UriUtils.encodePathSegment(client.getClientId(), "UTF-8"));
try { uri = StringUtils.replace(uri, template, (value != null ? UriUtils.encodePathSegment(value, encoding) : ""));
RegisteredClient registered = new RegisteredClient(savedClient, token.getValue(), config.getIssuer() + "register/" + UriUtils.encodePathSegment(savedClient.getClientId(), "UTF-8")); m.addAttribute("client", registered);
RegisteredClient registered = new RegisteredClient(savedClient, token.getValue(), config.getIssuer() + "resource/" + UriUtils.encodePathSegment(savedClient.getClientId(), "UTF-8")); m.addAttribute("client", registered);
RegisteredClient registered = new RegisteredClient(savedClient, token.getValue(), config.getIssuer() + "register/" + UriUtils.encodePathSegment(savedClient.getClientId(), "UTF-8"));
RegisteredClient registered = new RegisteredClient(savedClient, token.getValue(), config.getIssuer() + "resource/" + UriUtils.encodePathSegment(savedClient.getClientId(), "UTF-8"));
private String encodePathSegment(final String pathSegment) { return UriUtils.encodePathSegment(pathSegment, "UTF-8"); }
private String encodePathSegment(final String pathSegment) { return UriUtils.encodePathSegment(pathSegment, "UTF-8"); }
private static String encodePathSegment(String pathSegment) { return UriUtils.encodePathSegment(pathSegment, UTF_8.name()); } }
static private String getURLForCreation(String tenant, ComponentDescriptor<?> descriptor) { try { return String.format("/repository/%s/components/%s", UriUtils.encodePathSegment(tenant, "UTF8"), UriUtils.encodePathSegment( ComponentsRestClientUtils.escapeComponentName(descriptor.getDisplayName()), "UTF8")); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } }
/** * Replace URI template variables in the target URL with encoded model * attributes or URI variables from the current request. Model attributes * referenced in the URL are removed from the model. * @param targetUrl the redirect URL * @param model Map that contains model attributes * @param currentUriVariables current request URI variables to use * @param encodingScheme the encoding scheme to use * @throws UnsupportedEncodingException if string encoding failed */ protected StringBuilder replaceUriTemplateVariables( String targetUrl, Map<String, Object> model, Map<String, String> currentUriVariables, String encodingScheme) throws UnsupportedEncodingException { StringBuilder result = new StringBuilder(); Matcher matcher = URI_TEMPLATE_VARIABLE_PATTERN.matcher(targetUrl); int endLastMatch = 0; while (matcher.find()) { String name = matcher.group(1); Object value = (model.containsKey(name) ? model.remove(name) : currentUriVariables.get(name)); if (value == null) { throw new IllegalArgumentException("Model has no value for key '" + name + "'"); } result.append(targetUrl.substring(endLastMatch, matcher.start())); result.append(UriUtils.encodePathSegment(value.toString(), encodingScheme)); endLastMatch = matcher.end(); } result.append(targetUrl.substring(endLastMatch, targetUrl.length())); return result; }
@Override protected ClientHttpRequest createRequest(URI url, HttpMethod method) throws IOException { ClientHttpRequest httpRequest = super.createRequest(url, method); httpRequest.getHeaders().add("Authorization", String.format("Basic %s", Base64.encode(String.format("%s:%s", UriUtils.encodePathSegment(clientConfig.getClientId(), "UTF-8"), UriUtils.encodePathSegment(clientConfig.getClientSecret(), "UTF-8"))))); return httpRequest; } };
@Override protected ClientHttpRequest createRequest(URI url, HttpMethod method) throws IOException { ClientHttpRequest httpRequest = super.createRequest(url, method); httpRequest.getHeaders().add("Authorization", String.format("Basic %s", Base64.encode(String.format("%s:%s", UriUtils.encodePathSegment(clientConfig.getClientId(), "UTF-8"), UriUtils.encodePathSegment(clientConfig.getClientSecret(), "UTF-8"))))); return httpRequest; } };