Class DefaultProviderResolver

java.lang.Object
io.micronaut.security.oauth2.DefaultProviderResolver
All Implemented Interfaces:
ProviderResolver

@Singleton public class DefaultProviderResolver extends Object implements ProviderResolver
Default implementation of ProviderResolver.

The provider is read from the OauthAuthenticationMapper.PROVIDER_KEY attribute when present. The attribute is set by this application during the authorization code flow and by the UserInfo token validator; an ID token carrying a claim with that name is rejected by IdTokenClaimsValidator, so with micronaut.security.authentication=idtoken the attribute never comes from the token which authenticated the request.

Otherwise the provider is inferred from the token claims: the OpenID clients whose configured issuer matches the iss claim are the candidates. If several clients match the issuer (for example two Oracle Cloud identity domains, whose ID tokens share the same iss claim) the aud claim, and if needed the azp claim, are compared with the clients' client-id to pick the client the token was issued to. If exactly one client cannot be determined, no provider is resolved. OpenID clients whose OAuth 2.0 client is disabled are ignored.

Since:
1.2.0
Author:
James Kleeh
  • Constructor Details

    • DefaultProviderResolver

      @Inject public DefaultProviderResolver(List<OpenIdClientConfiguration> openIdClientConfigurations, Collection<OauthClientConfiguration> oauthClientConfigurations)
      Parameters:
      openIdClientConfigurations - OpenID client configurations
      oauthClientConfigurations - OAuth 2.0 client configurations. OpenID clients whose OAuth 2.0 client is disabled are ignored.
    • DefaultProviderResolver

      @Deprecated(since="5.5.2") public DefaultProviderResolver(List<OpenIdClientConfiguration> openIdClientConfigurations)
      Deprecated.
      Use DefaultProviderResolver(List, Collection) instead, which ignores disabled OAuth 2.0 clients. Without the OAuth 2.0 client configurations the aud claim cannot be used to tell apart OpenID clients whose issuers overlap, and no provider is resolved in that case.
      Parameters:
      openIdClientConfigurations - OpenID client configurations
  • Method Details

    • resolveProvider

      public Optional<String> resolveProvider(Authentication authentication)
      Description copied from interface: ProviderResolver
      Resolves the provider.
      Specified by:
      resolveProvider in interface ProviderResolver
      Parameters:
      authentication - The authentication
      Returns:
      An optional provider name
    • openIdClientNameWhichMatchesIssClaim

      protected Optional<String> openIdClientNameWhichMatchesIssClaim(Authentication authentication)
      Resolves the provider from the iss claim. openIdClientNameWhichMatchesIssuer(String) decides whether the issuer maps to a client: if it returns no client, no provider is resolved. If it returns one of several clients which match the issuer, openIdClientNameWhichMatchesAudience(Authentication, List) picks among them the client the token was issued to.
      Parameters:
      authentication - State of authentication
      Returns:
      Optional#empty() if iss claim not found, if the iss claim does not match the issuer of any open id client, or if the iss claim matches several open id clients and the aud and azp claims do not single out one of them. If exactly one open id client matches, its name is returned wrapped in an optional
    • openIdClientNameWhichMatchesIssuer

      protected @NonNull Optional<String> openIdClientNameWhichMatchesIssuer(@NonNull String issuer)
      The issuer matches an open id client when it is identical to the expected issuer of the client; only the case of the scheme and a single trailing slash are tolerated. See IssuerUtils.equivalent(String, String). When several clients match the issuer, the first one is returned and openIdClientNameWhichMatchesIssClaim(Authentication) uses the aud claim to pick among them.

      Subclasses may override this method to customize how the issuer is mapped to a client. If it returns no client, no provider is resolved. A client which is not one of the clients matching the issuer is used as is.

      Parameters:
      issuer - Token Issuer
      Returns:
      Optional#empty() if the issuer does not match the expected issuer (OpenIdClientConfiguration.getIssuerIdentifier()) of any open id client. If it matches, the open id client is returned wrapped in an optional
    • openIdClientNamesWhichMatchIssuer

      protected @NonNull List<String> openIdClientNamesWhichMatchIssuer(@NonNull String issuer)
      Parameters:
      issuer - Token Issuer
      Returns:
      The names of every open id client whose expected issuer (OpenIdClientConfiguration.getIssuerIdentifier()) matches the token issuer
      Since:
      5.5.2
    • openIdClientNameWhichMatchesAudience

      protected @NonNull Optional<String> openIdClientNameWhichMatchesAudience(@NonNull Authentication authentication, @NonNull List<String> candidateNames)
      Picks, among the open id clients whose issuer matches the token, the one whose client-id the token was issued to. The aud claim must contain the client id. If the aud claim contains the client id of several candidates, the azp claim must be equal to the client id of exactly one of them.
      Parameters:
      authentication - State of authentication
      candidateNames - Names of the open id clients whose issuer matches the token issuer
      Returns:
      The name of the only candidate the token was issued to, or Optional#empty() if the token audience does not single out one candidate
      Since:
      5.5.2