Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (c) 2018, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.helidon.security.providers.httpauth;
import java.lang.System.Logger.Level;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import io.helidon.common.HelidonServiceLoader;
import io.helidon.common.config.Config;
import io.helidon.config.metadata.Configured;
import io.helidon.config.metadata.ConfiguredOption;
import io.helidon.security.AuthenticationResponse;
import io.helidon.security.EndpointConfig;
import io.helidon.security.OutboundSecurityResponse;
import io.helidon.security.Principal;
import io.helidon.security.ProviderRequest;
import io.helidon.security.Role;
import io.helidon.security.SecurityContext;
import io.helidon.security.SecurityEnvironment;
import io.helidon.security.SecurityResponse;
import io.helidon.security.Subject;
import io.helidon.security.SubjectType;
import io.helidon.security.providers.common.OutboundConfig;
import io.helidon.security.providers.common.OutboundTarget;
import io.helidon.security.providers.httpauth.spi.UserStoreService;
import io.helidon.security.spi.AuthenticationProvider;
import io.helidon.security.spi.OutboundSecurityProvider;
import io.helidon.security.spi.SecurityProvider;
import io.helidon.security.util.TokenHandler;
/**
* Http authentication security provider.
* Provides support for username and password authentication, with support for roles list.
*/
public class HttpBasicAuthProvider implements AuthenticationProvider, OutboundSecurityProvider {
static final String HEADER_AUTHENTICATION_REQUIRED = "WWW-Authenticate";
static final String HEADER_AUTHENTICATION = "authorization";
static final String BASIC_PREFIX = "basic ";
private static final System.Logger LOGGER = System.getLogger(HttpBasicAuthProvider.class.getName());
static final Pattern CREDENTIAL_PATTERN = Pattern.compile("(.*?):(.*)");
private final List userStores;
private final boolean optional;
private final String realm;
private final SubjectType subjectType;
private final OutboundConfig outboundConfig;
private final boolean outboundTargetsExist;
HttpBasicAuthProvider(Builder builder) {
this.userStores = new LinkedList<>(builder.userStores);
this.optional = builder.optional;
this.realm = builder.realm;
this.subjectType = builder.subjectType;
this.outboundConfig = builder.outboundBuilder.build();
this.outboundTargetsExist = outboundConfig.targets().size() > 0;
}
/**
* Get a builder instance to construct a new security provider.
* Alternative approach is {@link #create(io.helidon.common.config.Config)} (or {@link HttpBasicAuthProvider#create(Config)}).
*
* @return builder to fluently construct Basic security provider
*/
public static Builder builder() {
return new Builder();
}
/**
* Load this provider from configuration.
*
* @param config Configuration located at this provider's configuration (e.g. child is either http-basic-auth or
* http-digest-auth)
* @return instance of provider configured from provided config
*/
public static HttpBasicAuthProvider create(Config config) {
return builder().config(config).build();
}
private static OutboundSecurityResponse toBasicAuthOutbound(SecurityEnvironment outboundEnv,
TokenHandler tokenHandler,
String username,
char[] password) {
String b64 = Base64.getEncoder()
.encodeToString((username + ":" + new String(password)).getBytes(StandardCharsets.UTF_8));
Map> headers = new HashMap<>(outboundEnv.headers());
tokenHandler.addHeader(headers, b64);
return OutboundSecurityResponse
.withHeaders(headers);
}
@Override
public boolean isOutboundSupported(ProviderRequest providerRequest,
SecurityEnvironment outbondEnv,
EndpointConfig outboundEp) {
// explicitly overridden username and/or password
if (outboundEp.abacAttributeNames().contains(EndpointConfig.PROPERTY_OUTBOUND_ID)) {
return true;
}
return outboundTargetsExist;
}
@Override
public OutboundSecurityResponse outboundSecurity(ProviderRequest providerRequest,
SecurityEnvironment outboundEnv,
EndpointConfig outboundEp) {
// explicit username in request properties
Optional