Search in sources :

Example 1 with Configured

use of io.helidon.config.metadata.Configured in project helidon by oracle.

the class HttpBasicAuthProvider method syncOutbound.

@Override
protected OutboundSecurityResponse syncOutbound(ProviderRequest providerRequest, SecurityEnvironment outboundEnv, EndpointConfig outboundEp) {
    // explicit username in request properties
    Optional<Object> maybeUsername = outboundEp.abacAttribute(EP_PROPERTY_OUTBOUND_USER);
    if (maybeUsername.isPresent()) {
        String username = maybeUsername.get().toString();
        char[] password = passwordFromEndpoint(outboundEp);
        return toBasicAuthOutbound(outboundEnv, HttpBasicOutboundConfig.DEFAULT_TOKEN_HANDLER, username, password);
    }
    var target = outboundConfig.findTargetCustomObject(outboundEnv, HttpBasicOutboundConfig.class, HttpBasicOutboundConfig::create, HttpBasicOutboundConfig::create);
    if (target.isEmpty()) {
        return OutboundSecurityResponse.abstain();
    }
    HttpBasicOutboundConfig outboundConfig = target.get();
    if (outboundConfig.hasExplicitUser()) {
        // use configured user
        return toBasicAuthOutbound(outboundEnv, outboundConfig.tokenHandler(), outboundConfig.explicitUser(), outboundConfig.explicitPassword());
    } else {
        // propagate current user (if possible)
        SecurityContext secContext = providerRequest.securityContext();
        // first try user
        Optional<BasicPrivateCredentials> creds = secContext.user().flatMap(this::credentialsFromSubject);
        if (creds.isEmpty()) {
            // if not present, try service
            creds = secContext.service().flatMap(this::credentialsFromSubject);
        }
        Optional<char[]> overridePassword = outboundEp.abacAttribute(EP_PROPERTY_OUTBOUND_PASSWORD).map(String::valueOf).map(String::toCharArray);
        return creds.map(credentials -> {
            char[] password = overridePassword.orElse(credentials.password);
            return toBasicAuthOutbound(outboundEnv, outboundConfig.tokenHandler(), credentials.username, password);
        }).orElseGet(OutboundSecurityResponse::abstain);
    }
}
Also used : OutboundSecurityResponse(io.helidon.security.OutboundSecurityResponse) ProviderRequest(io.helidon.security.ProviderRequest) HashMap(java.util.HashMap) UserStoreService(io.helidon.security.providers.httpauth.spi.UserStoreService) AuthenticationProvider(io.helidon.security.spi.AuthenticationProvider) Matcher(java.util.regex.Matcher) Map(java.util.Map) Subject(io.helidon.security.Subject) LinkedList(java.util.LinkedList) ConfiguredOption(io.helidon.config.metadata.ConfiguredOption) Config(io.helidon.config.Config) SubjectType(io.helidon.security.SubjectType) OutboundSecurityProvider(io.helidon.security.spi.OutboundSecurityProvider) Configured(io.helidon.config.metadata.Configured) SecurityProvider(io.helidon.security.spi.SecurityProvider) SynchronousProvider(io.helidon.security.spi.SynchronousProvider) ServiceLoader(java.util.ServiceLoader) SecurityContext(io.helidon.security.SecurityContext) HelidonServiceLoader(io.helidon.common.serviceloader.HelidonServiceLoader) TokenHandler(io.helidon.security.util.TokenHandler) Logger(java.util.logging.Logger) AuthenticationResponse(io.helidon.security.AuthenticationResponse) OutboundConfig(io.helidon.security.providers.common.OutboundConfig) Principal(io.helidon.security.Principal) StandardCharsets(java.nio.charset.StandardCharsets) SecurityResponse(io.helidon.security.SecurityResponse) Base64(java.util.Base64) List(java.util.List) EndpointConfig(io.helidon.security.EndpointConfig) SecurityEnvironment(io.helidon.security.SecurityEnvironment) OutboundTarget(io.helidon.security.providers.common.OutboundTarget) Role(io.helidon.security.Role) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) SecurityContext(io.helidon.security.SecurityContext) OutboundSecurityResponse(io.helidon.security.OutboundSecurityResponse)

Example 2 with Configured

use of io.helidon.config.metadata.Configured in project helidon by oracle.

the class OutboundConfig method createFromConfig.

static OutboundConfig createFromConfig(Config providerConfig, OutboundTarget[] defaults) {
    Config config = providerConfig.get(CONFIG_OUTBOUND);
    List<OutboundTarget> configuredTargets = config.asList(OutboundTarget::create).orElse(List.of());
    boolean useDefaults = configuredTargets.stream().noneMatch(targetConfig -> "default".equals(targetConfig.name())) && (null != defaults);
    Builder builder = OutboundConfig.builder();
    if (useDefaults) {
        // first add default values
        Arrays.stream(defaults).forEach(builder::addTarget);
    }
    // then add configured values
    configuredTargets.forEach(builder::addTarget);
    return builder.build();
}
Also used : Arrays(java.util.Arrays) IdentityHashMap(java.util.IdentityHashMap) ProviderRequest(io.helidon.security.ProviderRequest) Config(io.helidon.config.Config) OutboundSecurityProvider(io.helidon.security.spi.OutboundSecurityProvider) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Configured(io.helidon.config.metadata.Configured) Set(java.util.Set) Function(java.util.function.Function) Supplier(java.util.function.Supplier) HashSet(java.util.HashSet) List(java.util.List) EndpointConfig(io.helidon.security.EndpointConfig) SecurityEnvironment(io.helidon.security.SecurityEnvironment) Map(java.util.Map) Optional(java.util.Optional) LinkedList(java.util.LinkedList) ConfiguredOption(io.helidon.config.metadata.ConfiguredOption) Collections(java.util.Collections) Config(io.helidon.config.Config) EndpointConfig(io.helidon.security.EndpointConfig)

Aggregations

Config (io.helidon.config.Config)2 Configured (io.helidon.config.metadata.Configured)2 ConfiguredOption (io.helidon.config.metadata.ConfiguredOption)2 EndpointConfig (io.helidon.security.EndpointConfig)2 ProviderRequest (io.helidon.security.ProviderRequest)2 SecurityEnvironment (io.helidon.security.SecurityEnvironment)2 OutboundSecurityProvider (io.helidon.security.spi.OutboundSecurityProvider)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 HelidonServiceLoader (io.helidon.common.serviceloader.HelidonServiceLoader)1 AuthenticationResponse (io.helidon.security.AuthenticationResponse)1 OutboundSecurityResponse (io.helidon.security.OutboundSecurityResponse)1 Principal (io.helidon.security.Principal)1 Role (io.helidon.security.Role)1 SecurityContext (io.helidon.security.SecurityContext)1 SecurityResponse (io.helidon.security.SecurityResponse)1 Subject (io.helidon.security.Subject)1 SubjectType (io.helidon.security.SubjectType)1