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 Summary
-
Constructor Summary
ConstructorDescriptionDefaultOAuthClientRegistrationPersistenceService
(OAuthClientRegistrationRepository<D> repository, SimplePayloadMapper mapper, com.broadleafcommerce.data.tracking.core.mapping.validation.EntityValidator entityValidator, ClientRegistrationPropertiesService<P> clientRegistrationPropertiesService, OAuthClientRegistrationUtils clientRegistrationUtils) -
Method Summary
Modifier and TypeMethodDescriptionThis implementation can enhance the given clientRegistration in a few ways.void
Delete a client registrationvoid
deleteById
(String id) Delete a client registrationboolean
existsById
(String id) Check if a client registration exists by its system IDorg.springframework.data.domain.Page<P>
findAll
(org.springframework.data.domain.Pageable pageable) Find all client registrations.org.springframework.data.domain.Page<P>
findAllByTenantId
(String tenantId, org.springframework.data.domain.Pageable pageable) Find allOAuthClientRegistrationWrapper
objects for the current tenantFind a client registration by its system IDFind a client registration by its system ID and tenant ID.findByRegistrationId
(String registrationId, String tenantId) Find aOAuthClientRegistrationWrapper
by registration id and tenant idFind the repository domain by ID without converting to business domain.fromRepositoryDomain
(Optional<D> optionalDomain) protected org.springframework.data.domain.Page<P>
fromRepositoryDomain
(org.springframework.data.domain.Page<D> page) protected org.springframework.security.oauth2.client.registration.ClientRegistration.Builder
getBuilder
(P clientRegistration, 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 bygetBuilderFromIssuerUri(OAuthClientRegistrationWrapper)
.protected org.springframework.security.oauth2.client.registration.ClientRegistration.Builder
getBuilderFromIssuerUri
(P clientRegistration) protected ClientRegistrationPropertiesService<P>
protected OAuthClientRegistrationUtils
protected Optional<org.springframework.security.config.oauth2.client.CommonOAuth2Provider>
getCommonOAuth2Provider
(String registrationId) Use the global ApplicationConversionService to find theCommonOAuth2Provider
enum that corresponds to the given registrationId.protected com.broadleafcommerce.data.tracking.core.mapping.validation.EntityValidator
protected SimplePayloadMapper
protected OAuthClientRegistrationRepository<D>
protected P
populateCommonProvider
(P clientRegistration) Populate the ProviderDetails from built-in values for providers listed inCommonOAuth2Provider
, e.g.protected P
populateFromCommonProvider
(P clientRegistration, @NonNull org.springframework.security.oauth2.client.registration.ClientRegistration commonClientRegistration) Uses a ModelMapper to populate aOAuthClientRegistrationProviderDetails
from aClientRegistration
.protected P
populateRegistrationFromProvider
(P clientRegistration) If anissuerUri
is provided when creating aOAuthClientRegistrationWrapper
, then this method should be invoked in order to populate theclientRegistration
with the details from the issuer.Replace a client registrationSave theOAuthClientRegistrationWrapper
to the databaseprotected void
protected void
validateProviderDetails
(P clientRegistration)
-
Field Details
-
CACHE_BY_CLIENT_REGISTRATION_ID
- See Also:
-
-
Constructor Details
-
DefaultOAuthClientRegistrationPersistenceService
public DefaultOAuthClientRegistrationPersistenceService(OAuthClientRegistrationRepository<D> repository, SimplePayloadMapper mapper, com.broadleafcommerce.data.tracking.core.mapping.validation.EntityValidator entityValidator, ClientRegistrationPropertiesService<P> clientRegistrationPropertiesService, OAuthClientRegistrationUtils clientRegistrationUtils)
-
-
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 interfaceOAuthClientRegistrationPersistenceService<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 allOAuthClientRegistrationWrapper
objects for the current tenant- Specified by:
findAllByTenantId
in interfaceOAuthClientRegistrationPersistenceService<P extends OAuthClientRegistrationWrapper>
- Parameters:
tenantId
- the tenant IDpageable
- 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 interfaceOAuthClientRegistrationPersistenceService<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 interfaceOAuthClientRegistrationPersistenceService<P extends OAuthClientRegistrationWrapper>
- Parameters:
id
- The client registration system IDtenantId
- ID of the owning tenant.- Returns:
- An optional of client registration
-
findByRegistrationId
Description copied from interface:OAuthClientRegistrationPersistenceService
Find aOAuthClientRegistrationWrapper
by registration id and tenant id- Specified by:
findByRegistrationId
in interfaceOAuthClientRegistrationPersistenceService<P extends OAuthClientRegistrationWrapper>
- Parameters:
registrationId
- TheOAuthClientRegistrationWrapper.getRegistrationId()
to filter bytenantId
- The tenant ID to filter by- Returns:
- The matching registration or empty
-
existsById
Description copied from interface:OAuthClientRegistrationPersistenceService
Check if a client registration exists by its system ID- Specified by:
existsById
in interfaceOAuthClientRegistrationPersistenceService<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)") public P replace(String id, @NonNull P clientRegistration) Description copied from interface:OAuthClientRegistrationPersistenceService
Replace a client registration- Specified by:
replace
in interfaceOAuthClientRegistrationPersistenceService<P extends OAuthClientRegistrationWrapper>
- Parameters:
id
- The id of the server to replaceclientRegistration
- 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 theOAuthClientRegistrationProviderDetails
are empty, check to see if there is provider data included inCommonOAuth2Provider
, based on theOAuthClientRegistrationWrapper.getRegistrationId()
. - If aissuerUri
is supplied, send a request to it and populate theOAuthClientRegistrationProviderDetails
from the response. - PopulateOAuthClientRegistrationProviderDetails.registrationId
fromOAuthClientRegistrationWrapper.registrationId
.- Specified by:
create
in interfaceOAuthClientRegistrationPersistenceService<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)") public P save(@NonNull P clientRegistration) Description copied from interface:OAuthClientRegistrationPersistenceService
Save theOAuthClientRegistrationWrapper
to the database- Specified by:
save
in interfaceOAuthClientRegistrationPersistenceService<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 interfaceOAuthClientRegistrationPersistenceService<P extends OAuthClientRegistrationWrapper>
- Parameters:
clientRegistration
- The client registration to delete.
-
deleteById
@CacheEvict(cacheNames="authCacheByClientRegistrationId", allEntries=true) public void deleteById(String id) Description copied from interface:OAuthClientRegistrationPersistenceService
Delete a client registration- Specified by:
deleteById
in interfaceOAuthClientRegistrationPersistenceService<P extends OAuthClientRegistrationWrapper>
- Parameters:
id
- The id of the client registration to delete.
-
validate
-
validateProviderDetails
-
fromRepositoryDomain
-
fromRepositoryDomain
-
findRepositoryDomainById
Find the repository domain by ID without converting to business domain.- Parameters:
id
- The entity ID- Returns:
- Optional of the entity.
-
populateRegistrationFromProvider
If anissuerUri
is provided when creating aOAuthClientRegistrationWrapper
, then this method should be invoked in order to populate theclientRegistration
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 theissuerUri
.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
- TheOAuthClientRegistrationWrapper
to populate with provider details using a configuredissuerUri
.- 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 aClientRegistration.Builder
based on theissuerUri
of theclientRegistration
. This takes advantage ofClientRegistrations.fromIssuerLocation(String)
to fetch the provider details and hydrate them onto the builder, which can then be used to populate a finalOAuthClientRegistrationWrapper
.- Parameters:
clientRegistration
- TheOAuthClientRegistrationWrapper
to populate with provider details using a configuredissuerUri
.- Returns:
- A
ClientRegistration.Builder
based on theissuerUri
-
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 bygetBuilderFromIssuerUri(OAuthClientRegistrationWrapper)
.- Parameters:
clientRegistration
- TheOAuthClientRegistrationWrapper
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
- TheClientRegistration.Builder
built from the results of the request to theissuerUri
configured on theclientRegistration
.- Returns:
- The fully hydrated
ClientRegistration.Builder
.
-
populateCommonProvider
Populate the ProviderDetails from built-in values for providers listed inCommonOAuth2Provider
, 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 theCommonOAuth2Provider
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 aOAuthClientRegistrationProviderDetails
from aClientRegistration
.- Parameters:
clientRegistration
-commonClientRegistration
-- Returns:
-
getRepository
-
getMapper
-
getEntityValidator
protected com.broadleafcommerce.data.tracking.core.mapping.validation.EntityValidator getEntityValidator() -
getClientRegistrationPropertiesService
-
getClientRegistrationUtils
-