Skip to content

Commit

Permalink
Fallback til wellknown for lokal kjøring
Browse files Browse the repository at this point in the history
  • Loading branch information
jolarsen committed Aug 22, 2024
1 parent 7ab04f5 commit d872918
Showing 1 changed file with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.slf4j.Logger;
Expand Down Expand Up @@ -83,14 +84,14 @@ private static Set<OpenIDConfiguration> hentConfig() {
var azureKonfigUrl = getAzureProperty(AzureProperty.AZURE_APP_WELL_KNOWN_URL);
if (azureKonfigUrl != null) {
LOG.debug("Oppretter AzureAD konfig fra '{}'", azureKonfigUrl);
idProviderConfigs.add(createAzureAppConfiguration());
idProviderConfigs.add(createAzureAppConfiguration(azureKonfigUrl));
}

// TokenX
var tokenxKonfigUrl = getTokenXProperty(TokenXProperty.TOKEN_X_WELL_KNOWN_URL);
if (tokenxKonfigUrl != null) {
LOG.debug("Oppretter TokenX konfig fra '{}'", tokenxKonfigUrl);
idProviderConfigs.add(createTokenXConfiguration());
idProviderConfigs.add(createTokenXConfiguration(tokenxKonfigUrl));
}

var providere = idProviderConfigs.stream().map(OpenIDConfiguration::type).map(OpenIDProvider::name).collect(Collectors.joining(", "));
Expand All @@ -112,24 +113,24 @@ private static OpenIDConfiguration createStsConfiguration(String wellKnownUrl) {
}

@SuppressWarnings("unused")
private static OpenIDConfiguration createAzureAppConfiguration() {
private static OpenIDConfiguration createAzureAppConfiguration(String azureKonfigUrl) {
var proxyUrl = (ENV.isFss() && ENV.isProd()) ? ProxyProperty.getProxy() : null;
return createConfiguration(OpenIDProvider.AZUREAD,
getAzureProperty(AzureProperty.AZURE_OPENID_CONFIG_ISSUER),
getAzureProperty(AzureProperty.AZURE_OPENID_CONFIG_JWKS_URI),
getAzureProperty(AzureProperty.AZURE_OPENID_CONFIG_TOKEN_ENDPOINT),
getPropertyOrWellKnown(AzureProperty.AZURE_OPENID_CONFIG_ISSUER.name(), () -> getIssuerFra(azureKonfigUrl, proxyUrl)),
getPropertyOrWellKnown(AzureProperty.AZURE_OPENID_CONFIG_JWKS_URI.name(), () -> getJwksFra(azureKonfigUrl, proxyUrl)),
getPropertyOrWellKnown(AzureProperty.AZURE_OPENID_CONFIG_TOKEN_ENDPOINT.name(), () -> getTokenEndpointFra(azureKonfigUrl, proxyUrl)),
(ENV.isFss() && ENV.isProd()),
proxyUrl,
getAzureProperty(AzureProperty.AZURE_APP_CLIENT_ID),
getAzureProperty(AzureProperty.AZURE_APP_CLIENT_SECRET),
ENV.isLocal());
}

private static OpenIDConfiguration createTokenXConfiguration() {
private static OpenIDConfiguration createTokenXConfiguration(String tokenxKonfigUrl) {
return createConfiguration(OpenIDProvider.TOKENX,
getTokenXProperty(TokenXProperty.TOKEN_X_ISSUER),
getTokenXProperty(TokenXProperty.TOKEN_X_JWKS_URI),
getTokenXProperty(TokenXProperty.TOKEN_X_TOKEN_ENDPOINT),
getPropertyOrWellKnown(TokenXProperty.TOKEN_X_ISSUER.name(), () -> getIssuerFra(tokenxKonfigUrl)),
getPropertyOrWellKnown(TokenXProperty.TOKEN_X_JWKS_URI.name(), () -> getJwksFra(tokenxKonfigUrl)),
getPropertyOrWellKnown(TokenXProperty.TOKEN_X_TOKEN_ENDPOINT.name(), () -> getTokenEndpointFra(tokenxKonfigUrl)),
false,
null,
getTokenXProperty(TokenXProperty.TOKEN_X_CLIENT_ID),
Expand All @@ -138,14 +139,23 @@ private static OpenIDConfiguration createTokenXConfiguration() {
false);
}

private static String getPropertyOrWellKnown(String propertyname, Supplier<Optional<String>> wellknownSupplier) {
return getProperty(propertyname)
.or(wellknownSupplier)
.orElse(null);
}

private static Optional<String> getProperty(String propertyname) {
return Optional.ofNullable(ENV.getProperty(propertyname))
.or(() -> Optional.ofNullable(ENV.getProperty(propertyname.toLowerCase().replace('_', '.'))));
}

private static String getAzureProperty(AzureProperty property) {
return Optional.ofNullable(ENV.getProperty(property.name()))
.orElseGet(() -> ENV.getProperty(property.name().toLowerCase().replace('_', '.')));
return getProperty(property.name()).orElse(null);
}

private static String getTokenXProperty(TokenXProperty property) {
return Optional.ofNullable(ENV.getProperty(property.name()))
.orElseGet(() -> ENV.getProperty(property.name().toLowerCase().replace('_', '.')));
return getProperty(property.name()).orElse(null);
}

private static OpenIDConfiguration createConfiguration(OpenIDProvider type,
Expand Down

0 comments on commit d872918

Please sign in to comment.