
com.sap.cloud.sdk.cloudplatform.connectivity.DefaultSubdomainReplacer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of connectivity-scp-cf Show documentation
Show all versions of connectivity-scp-cf Show documentation
Implementation of the Cloud platform abstraction for general-purpose connectivity
on the SAP Cloud Platform (Cloud Foundry).
The newest version!
/*
* Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved.
*/
package com.sap.cloud.sdk.cloudplatform.connectivity;
import java.net.URI;
import javax.annotation.Nonnull;
import com.sap.cloud.sdk.cloudplatform.security.exception.TokenRequestFailedException;
import io.vavr.control.Try;
/**
* Default strategy for replacing the subdomain of the XSUAA URL used to get access tokens for destinations. In this
* strategy, the tenant specific subdomain (i.e. identity zone) of the XSUAA URL is replaced with the tenant specific
* subdomain of the service that issued the user's JWT.
*/
public class DefaultSubdomainReplacer implements SubdomainReplacer
{
/**
* This function takes the issuer URL from the user's JWT and the XSUAA URL and returns a new XSUAA URL. In this
* implementation, the XSUAA's tenant specific subdomain is replaced by the tenant specific subdomain of the service
* that issued the user's JWT.
*
* @param issuerUrl
* The URL of the service that issued the user's JWT.
* @param xsuaaUrl
* The URL of XSUAA instance that should be used to retrieve access tokens.
* @return A new XSUAA URL.
*/
@Override
@Nonnull
public URI replaceSubdomain( @Nonnull final String issuerUrl, @Nonnull final URI xsuaaUrl )
{
final String issuerSubdomain =
Try.of(() -> new URI(issuerUrl)).map(this::parseSubdomain).getOrElseThrow(
ex -> new TokenRequestFailedException("Unable to parse JWT issuer URL", ex));
final String xsuaaSubdomain = parseSubdomain(xsuaaUrl);
if( xsuaaSubdomain.equals(issuerSubdomain) ) {
return xsuaaUrl;
} else {
return replace(xsuaaUrl, issuerSubdomain);
}
}
private URI replace( @Nonnull final URI uri, @Nonnull final String subdomain )
throws TokenRequestFailedException
{
return Try
.of(
() -> new URI(
uri.getScheme(),
null,
subdomain + uri.getHost().substring(uri.getHost().indexOf(".")),
uri.getPort(),
uri.getPath(),
null,
null))
.getOrElseThrow(TokenRequestFailedException::new);
}
private String parseSubdomain( final URI uri )
throws TokenRequestFailedException
{
final String host = uri.getHost();
if( host == null || !host.contains(".") ) {
throw new TokenRequestFailedException("Failed to determine subdomain: invalid host in URI '" + uri + "'.");
}
return host.split("\\.")[0];
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy