Class DefaultOAuthClientRegistrationPersistenceService<P extends OAuthClientRegistrationWrapper,D extends com.broadleafcommerce.data.tracking.core.mapping.BusinessTypeAware & com.broadleafcommerce.data.tracking.core.mapping.ModelMapperMappable>

java.lang.Object
com.broadleafcommerce.auth.client.service.DefaultOAuthClientRegistrationPersistenceService<P,D>
Type Parameters:
P - Projection, OAuthClientRegistrationWrapper
D - Domain, JpaOAuthClientRegistration
All Implemented Interfaces:
OAuthClientRegistrationPersistenceService<P>

public class DefaultOAuthClientRegistrationPersistenceService<P extends OAuthClientRegistrationWrapper,D extends com.broadleafcommerce.data.tracking.core.mapping.BusinessTypeAware & com.broadleafcommerce.data.tracking.core.mapping.ModelMapperMappable> extends Object implements OAuthClientRegistrationPersistenceService<P>
Interacts with the OAuthClientRegistrationRepository to read domain objects and map them to the projection OAuthClientRegistrationWrapper
Author:
Cade Rea (cade-rea)
  • Field Details

  • Constructor Details

  • Method Details

    • findAll

      public org.springframework.data.domain.Page<P> findAll(@Nullable org.springframework.data.domain.Pageable pageable)
      Description copied from interface: OAuthClientRegistrationPersistenceService
      Find all client registrations.
      Specified by:
      findAll in interface OAuthClientRegistrationPersistenceService<P extends OAuthClientRegistrationWrapper>
      Parameters:
      pageable - the pageable
      Returns:
      A page of client registrations
    • findAllByTenantId

      public org.springframework.data.domain.Page<P> findAllByTenantId(String tenantId, @Nullable org.springframework.data.domain.Pageable pageable)
      Description copied from interface: OAuthClientRegistrationPersistenceService
      Find all OAuthClientRegistrationWrapper objects for the current tenant
      Specified by:
      findAllByTenantId in interface OAuthClientRegistrationPersistenceService<P extends OAuthClientRegistrationWrapper>
      Parameters:
      tenantId - the tenant ID
      pageable - the pageable
      Returns:
      All registrations for the tenant
    • findById

      @Cacheable(cacheNames="authCacheByClientRegistrationId", key="#a0", condition="@cacheStateManager.isEnabled(#root.caches, #root.args)") public Optional<P> findById(String id)
      Description copied from interface: OAuthClientRegistrationPersistenceService
      Find a client registration by its system ID
      Specified by:
      findById in interface OAuthClientRegistrationPersistenceService<P extends OAuthClientRegistrationWrapper>
      Parameters:
      id - The client registration system ID
      Returns:
      An optional of client registration
    • findById

      @Cacheable(cacheNames="authCacheByClientRegistrationId", key="#a0.concat(#a1)", condition="@cacheStateManager.isEnabled(#root.caches, #root.args)") public Optional<P> findById(String id, String tenantId)
      Description copied from interface: OAuthClientRegistrationPersistenceService
      Find a client registration by its system ID and tenant ID.
      Specified by:
      findById in interface OAuthClientRegistrationPersistenceService<P extends OAuthClientRegistrationWrapper>
      Parameters:
      id - The client registration system ID
      tenantId - ID of the owning tenant.
      Returns:
      An optional of client registration
    • findByRegistrationId

      public Optional<P> findByRegistrationId(String registrationId, String tenantId)
      Description copied from interface: OAuthClientRegistrationPersistenceService
      Find a OAuthClientRegistrationWrapper by registration id and tenant id
      Specified by:
      findByRegistrationId in interface OAuthClientRegistrationPersistenceService<P extends OAuthClientRegistrationWrapper>
      Parameters:
      registrationId - The OAuthClientRegistrationWrapper.getRegistrationId() to filter by
      tenantId - The tenant ID to filter by
      Returns:
      The matching registration or empty
    • existsById

      public boolean existsById(String id)
      Description copied from interface: OAuthClientRegistrationPersistenceService
      Check if a client registration exists by its system ID
      Specified by:
      existsById in interface OAuthClientRegistrationPersistenceService<P extends OAuthClientRegistrationWrapper>
      Parameters:
      id - The client registration system ID
      Returns:
      whether or not the client registration exists
    • replace

      @CachePut(cacheNames="authCacheByClientRegistrationId",key="#result.id") @CachePut(cacheNames="authCacheByClientRegistrationId",key="#result.id.concat(#result.tenantId)") @CacheEvict(cacheNames="oauth2IdentityProviders", allEntries=true) public P replace(String id, @NonNull P clientRegistration)
      Description copied from interface: OAuthClientRegistrationPersistenceService
      Replace a client registration
      Specified by:
      replace in interface OAuthClientRegistrationPersistenceService<P extends OAuthClientRegistrationWrapper>
      Parameters:
      id - The id of the server to replace
      clientRegistration - The replacement
      Returns:
      The replaced client registration
    • create

      @CachePut(cacheNames="authCacheByClientRegistrationId",key="#result.id") @CachePut(cacheNames="authCacheByClientRegistrationId",key="#result.id.concat(#result.tenantId)") public P create(@NonNull P clientRegistration)
      This implementation can enhance the given clientRegistration in a few ways. - If the OAuthClientRegistrationProviderDetails are empty, check to see if there is provider data included in CommonOAuth2Provider, based on the OAuthClientRegistrationWrapper.getRegistrationId(). - If a issuerUri is supplied, send a request to it and populate the OAuthClientRegistrationProviderDetails from the response. - Populate OAuthClientRegistrationProviderDetails.registrationId from OAuthClientRegistrationWrapper.registrationId.
      Specified by:
      create in interface OAuthClientRegistrationPersistenceService<P extends OAuthClientRegistrationWrapper>
      Parameters:
      clientRegistration - The client registration to create
      Returns:
      The persisted registration.
    • save

      @CachePut(cacheNames="authCacheByClientRegistrationId",key="#result.id") @CachePut(cacheNames="authCacheByClientRegistrationId",key="#result.id.concat(#result.tenantId)") @CacheEvict(cacheNames="oauth2IdentityProviders", allEntries=true) public P save(@NonNull P clientRegistration)
      Description copied from interface: OAuthClientRegistrationPersistenceService
      Save the OAuthClientRegistrationWrapper to the database
      Specified by:
      save in interface OAuthClientRegistrationPersistenceService<P extends OAuthClientRegistrationWrapper>
      Parameters:
      clientRegistration - Instance to persist
      Returns:
      Persited client registration
    • delete

      @CacheEvict(cacheNames="authCacheByClientRegistrationId",key="#a0.id") @CacheEvict(cacheNames="authCacheByClientRegistrationId",key="#a0.id.concat(#a0.tenantId)") public void delete(@NonNull P clientRegistration)
      Description copied from interface: OAuthClientRegistrationPersistenceService
      Delete a client registration
      Specified by:
      delete in interface OAuthClientRegistrationPersistenceService<P extends OAuthClientRegistrationWrapper>
      Parameters:
      clientRegistration - The client registration to delete.
    • deleteById

      @CacheEvict(cacheNames={"authCacheByClientRegistrationId","oauth2IdentityProviders"}, allEntries=true) public void deleteById(String id)
      Description copied from interface: OAuthClientRegistrationPersistenceService
      Delete a client registration
      Specified by:
      deleteById in interface OAuthClientRegistrationPersistenceService<P extends OAuthClientRegistrationWrapper>
      Parameters:
      id - The id of the client registration to delete.
    • validate

      protected void validate(@NonNull P clientRegistration)
    • buildErrors

      protected org.springframework.validation.Errors buildErrors(P clientRegistration)
    • throwIfHasErrors

      protected void throwIfHasErrors(org.springframework.validation.Errors errors)
    • validateProviderDetails

      protected void validateProviderDetails(@NonNull P clientRegistration)
    • fromRepositoryDomain

      protected org.springframework.data.domain.Page<P> fromRepositoryDomain(org.springframework.data.domain.Page<D> page)
    • fromRepositoryDomain

      protected Optional<P> fromRepositoryDomain(Optional<D> optionalDomain)
    • findRepositoryDomainById

      protected Optional<D> findRepositoryDomainById(String id)
      Find the repository domain by ID without converting to business domain.
      Parameters:
      id - The entity ID
      Returns:
      Optional of the entity.
    • populateRegistrationFromProvider

      protected P populateRegistrationFromProvider(@NonNull P clientRegistration)
      If an issuerUri is provided when creating a OAuthClientRegistrationWrapper, then this method should be invoked in order to populate the clientRegistration with the details from the issuer. This means the caller does not need to know all of the details themselves—in some cases the provider will not even provide the details except through the issuerUri.

      The approach taken here is based on OAuth2ClientPropertiesMapper.

      Note: This method does not mutate the given clientRegistration but creates a copy of it with the details populated there on.
      Parameters:
      clientRegistration - The OAuthClientRegistrationWrapper to populate with provider details using a configured issuerUri.
      Returns:
      A copy of clientRegistration with the provider details populated.
    • getBuilderFromIssuerUri

      protected org.springframework.security.oauth2.client.registration.ClientRegistration.Builder getBuilderFromIssuerUri(@NonNull P clientRegistration)
      Gets a ClientRegistration.Builder based on the issuerUri of the clientRegistration. This takes advantage of ClientRegistrations.fromIssuerLocation(String) to fetch the provider details and hydrate them onto the builder, which can then be used to populate a final OAuthClientRegistrationWrapper.
      Parameters:
      clientRegistration - The OAuthClientRegistrationWrapper to populate with provider details using a configured issuerUri.
      Returns:
      A ClientRegistration.Builder based on the issuerUri
    • getBuilder

      protected org.springframework.security.oauth2.client.registration.ClientRegistration.Builder getBuilder(@NonNull P clientRegistration, @NonNull org.springframework.security.oauth2.client.registration.ClientRegistration.Builder builder)
      Populates the user-overrides and user-specific values (e.g., client id and secret) onto the builder produced by getBuilderFromIssuerUri(OAuthClientRegistrationWrapper).
      Parameters:
      clientRegistration - The OAuthClientRegistrationWrapper to be populated. This is used to glean any user overrides of the default provider data and provide any user-specific details like the client ID and secret.
      builder - The ClientRegistration.Builder built from the results of the request to the issuerUri configured on the clientRegistration.
      Returns:
      The fully hydrated ClientRegistration.Builder.
    • populateCommonProvider

      protected P populateCommonProvider(@NonNull P clientRegistration)
      Populate the ProviderDetails from built-in values for providers listed in CommonOAuth2Provider, e.g. CommonOAuth2Provider.GOOGLE, CommonOAuth2Provider.OKTA.
      Parameters:
      clientRegistration -
    • getCommonOAuth2Provider

      protected Optional<org.springframework.security.config.oauth2.client.CommonOAuth2Provider> getCommonOAuth2Provider(String registrationId)
      Use the global ApplicationConversionService to find the CommonOAuth2Provider enum that corresponds to the given registrationId.
      Parameters:
      registrationId -
      Returns:
    • populateFromCommonProvider

      protected P populateFromCommonProvider(@NonNull P clientRegistration, @NonNull @NonNull org.springframework.security.oauth2.client.registration.ClientRegistration commonClientRegistration)
      Uses a ModelMapper to populate a OAuthClientRegistrationProviderDetails from a ClientRegistration.
      Parameters:
      clientRegistration -
      commonClientRegistration -
      Returns:
    • getRepository

      protected OAuthClientRegistrationRepository<D> getRepository()
    • getMapper

      protected SimplePayloadMapper getMapper()
    • getEntityValidator

      protected com.broadleafcommerce.data.tracking.core.mapping.validation.EntityValidator getEntityValidator()
    • getClientRegistrationPropertiesService

      protected ClientRegistrationPropertiesService<P> getClientRegistrationPropertiesService()
    • getClientRegistrationUtils

      protected OAuthClientRegistrationUtils getClientRegistrationUtils()