Skip to content

Commit

Permalink
update :: String to uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
4mjeo committed Mar 14, 2024
1 parent c5e612e commit 45f18ef
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -67,6 +71,8 @@ class CustomOAuth2AuthorizationService(
}
}

private val logger: Logger = LoggerFactory.getLogger(CustomOAuth2AuthorizationService::class.java)

private fun saveAuthorizationAndAuthorizationCode(
oAuth2Authorization: OAuth2Authorization
) {
Expand All @@ -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)
Expand Down

0 comments on commit 45f18ef

Please sign in to comment.