diff --git a/src/main/kotlin/com/example/v1oauthauthorizationservice/infrastructure/configuration/oauth2/authorizationserver/CustomOAuth2AuthorizationService.kt b/src/main/kotlin/com/example/v1oauthauthorizationservice/infrastructure/configuration/oauth2/authorizationserver/CustomOAuth2AuthorizationService.kt index d7b1713..f01c205 100644 --- a/src/main/kotlin/com/example/v1oauthauthorizationservice/infrastructure/configuration/oauth2/authorizationserver/CustomOAuth2AuthorizationService.kt +++ b/src/main/kotlin/com/example/v1oauthauthorizationservice/infrastructure/configuration/oauth2/authorizationserver/CustomOAuth2AuthorizationService.kt @@ -23,8 +23,11 @@ import com.example.v1oauthauthorizationservice.infrastructure.configuration.oaut import com.example.v1oauthauthorizationservice.infrastructure.configuration.oauth2.utils.OAuth2AuthorizationBuilder.toOAuth2Authorization import com.example.v1oauthauthorizationservice.infrastructure.configuration.objectmapper.ObjectMapperConfiguration import com.example.v1oauthauthorizationservice.infrastructure.configuration.uuid.UuidUtils.toUUID +import com.example.v1oauthauthorizationservice.infrastructure.configuration.uuid.exceptions.IllegalUuidStringException import com.fasterxml.jackson.databind.ObjectMapper import jakarta.annotation.Resource +import org.slf4j.Logger +import org.slf4j.LoggerFactory import org.springframework.cache.annotation.Cacheable import org.springframework.data.repository.findByIdOrNull import org.springframework.security.core.context.SecurityContextHolder @@ -41,6 +44,7 @@ import org.springframework.security.oauth2.server.authorization.OAuth2TokenType import org.springframework.security.oauth2.server.authorization.client.RegisteredClientRepository import org.springframework.stereotype.Component import org.springframework.transaction.annotation.Transactional +import java.util.* @Transactional @Component @@ -67,6 +71,8 @@ class CustomOAuth2AuthorizationService( } } + private val logger: Logger = LoggerFactory.getLogger(CustomOAuth2AuthorizationService::class.java) + private fun saveAuthorizationAndAuthorizationCode( oAuth2Authorization: OAuth2Authorization ) { @@ -76,7 +82,14 @@ class CustomOAuth2AuthorizationService( ?: throw RegisteredClientNotFoundException(RegisteredClientNotFoundException.ID_NOT_FOUND_MESSAGE) val authenticationName = SecurityContextHolder.getContext().authentication.name - val userId = authenticationName.toUUID() + + val userId = try { + authenticationName.toUUID() + } catch (e: IllegalUuidStringException) { + logger.error("Failed to convert authenticationName to UUID: $authenticationName") + UUID + .randomUUID() + } val authorizationEntityToSave = buildAuthorizationEntity(oAuth2Authorization, registeredClientEntity, userId) val savedAuthorizationEntity = authorizationEntityRepository.save(authorizationEntityToSave)