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 2017-2023 original authors
*
* 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
*
* https://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.micronaut.security.oauth2.client;
import io.micronaut.context.annotation.Requires;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.util.StringUtils;
import io.micronaut.security.config.SecurityConfigurationProperties;
import io.micronaut.security.oauth2.configuration.OauthClientConfiguration;
import io.micronaut.security.oauth2.configuration.OpenIdClientConfiguration;
import io.micronaut.security.token.Claims;
import io.micronaut.security.token.jwt.validator.GenericJwtClaimsValidator;
import io.micronaut.security.token.jwt.validator.JwtClaimsValidatorConfigurationProperties;
import jakarta.inject.Singleton;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* For {@link io.micronaut.security.authentication.AuthenticationMode#IDTOKEN} authentication mode performs the following verification as described in the OpenID Connect Spec.
*
* - The Issuer Identifier for the OpenID Provider (which is typically obtained during Discovery) MUST exactly match the value of the iss (issuer) Claim.
* - The Client MUST validate that the aud (audience) Claim contains its client_id value registered at the Issuer identified by the iss (issuer) Claim as an audience. The aud (audience) Claim MAY contain an array with more than one element.
* - If the ID Token contains multiple audiences, the Client SHOULD verify that an azp Claim is present.
* - If an azp (authorized party) Claim is present, the Client SHOULD verify that its client_id is the Claim Value.
* * @see ID Token Validation
*
* @author Sergio del Amo
* @since 2.2.0
* @param request
*/
@Requires(property = SecurityConfigurationProperties.PREFIX + ".authentication", value = "idtoken")
@Requires(property = JwtClaimsValidatorConfigurationProperties.PREFIX + ".openid-idtoken", notEquals = StringUtils.FALSE)
@Singleton
public class IdTokenClaimsValidator implements GenericJwtClaimsValidator {
protected static final Logger LOG = LoggerFactory.getLogger(IdTokenClaimsValidator.class);
protected static final String AUTHORIZED_PARTY = "azp";
protected final Collection oauthClientConfigurations;
/**
*
* @param oauthClientConfigurations OpenId client configurations
*/
public IdTokenClaimsValidator(Collection oauthClientConfigurations) {
this.oauthClientConfigurations = oauthClientConfigurations;
}
@Override
public boolean validate(@NonNull Claims claims, @Nullable T request) {
Optional claimIssuerOptional = parseIssuerClaim(claims);
if (!claimIssuerOptional.isPresent()) {
if (LOG.isDebugEnabled()) {
LOG.debug("issuer claim not present");
}
return false;
}
String iss = claimIssuerOptional.get();
Optional> audiencesOptional = parseAudiences(claims);
if (!audiencesOptional.isPresent()) {
if (LOG.isDebugEnabled()) {
LOG.debug("audiences claim not present");
}
return false;
}
List audiences = audiencesOptional.get();
return validateIssuerAudienceAndAzp(claims, iss, audiences);
}
/**
*
* @param claims JWT Claims
* @return the iss claim value wrapped in an {@link Optional}. If not found, an empty {@link Optional} is returned.
*/
protected Optional parseIssuerClaim(Claims claims) {
return parseClaimString(claims, Claims.ISSUER);
}
/**
*
* @param claims JWT Claims
* @param claimName Claim Name
* @return the claim value wrapped in an {@link Optional}. If not found, an empty {@link Optional} is returned.
*/
protected Optional