From 19f0143e4a7b9fe190dbc4f946f6ef0bd0916660 Mon Sep 17 00:00:00 2001 From: Nariman Abdullin Date: Thu, 20 Jul 2023 16:07:00 +0300 Subject: [PATCH] Replaced Authentication.toUser() by `Authentication.username()` ### What's done: - a small refactoring It's part of #2336 --- ...oringServerAuthenticationSuccessHandler.kt | 2 +- .../save/authservice/utils/SecurityUtils.kt | 19 ------------------- .../LnkUserOrganizationController.kt | 4 ++-- .../controllers/PermissionController.kt | 4 ++-- 4 files changed, 5 insertions(+), 24 deletions(-) diff --git a/api-gateway/src/main/kotlin/com/saveourtool/save/gateway/utils/StoringServerAuthenticationSuccessHandler.kt b/api-gateway/src/main/kotlin/com/saveourtool/save/gateway/utils/StoringServerAuthenticationSuccessHandler.kt index e1d6bc2a46..2753d40f2d 100644 --- a/api-gateway/src/main/kotlin/com/saveourtool/save/gateway/utils/StoringServerAuthenticationSuccessHandler.kt +++ b/api-gateway/src/main/kotlin/com/saveourtool/save/gateway/utils/StoringServerAuthenticationSuccessHandler.kt @@ -53,7 +53,7 @@ class StoringServerAuthenticationSuccessHandler( /** * @return [User] with data from this [Authentication] */ -fun Authentication.toUser(): User = User( +private fun Authentication.toUser(): User = User( userName(), null, authorities.joinToString(",") { it.authority }, diff --git a/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/utils/SecurityUtils.kt b/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/utils/SecurityUtils.kt index c3eaae716f..fd299ce66a 100644 --- a/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/utils/SecurityUtils.kt +++ b/authentication-service/src/main/kotlin/com/saveourtool/save/authservice/utils/SecurityUtils.kt @@ -56,25 +56,6 @@ fun Authentication.extractUserNameAndIdentitySource(): Pair = th identitySource } -/** - * Convert [Authentication] to [User] based on convention in backend. - * We assume here that all authentications are created by [ConvertingAuthenticationManager], - * so `principal` is a String, containing identity source. - * - * @return [User] - */ -fun Authentication.toUser(): User { - val (identitySource, name) = (principal as String).split(':') - return User( - name = name, - password = null, - email = null, - role = (this as UsernamePasswordAuthenticationToken).authorities.joinToString(separator = ","), - source = identitySource, - status = UserStatus.CREATED, - ) -} - /** * Set role hierarchy for spring security * diff --git a/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/LnkUserOrganizationController.kt b/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/LnkUserOrganizationController.kt index 56a31cefb1..8f24c7a144 100644 --- a/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/LnkUserOrganizationController.kt +++ b/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/LnkUserOrganizationController.kt @@ -7,8 +7,8 @@ package com.saveourtool.save.backend.controllers -import com.saveourtool.save.authservice.utils.toUser import com.saveourtool.save.authservice.utils.userId +import com.saveourtool.save.authservice.utils.username import com.saveourtool.save.backend.security.OrganizationPermissionEvaluator import com.saveourtool.save.backend.service.LnkUserOrganizationService import com.saveourtool.save.backend.service.OrganizationService @@ -106,7 +106,7 @@ class LnkUserOrganizationController( authentication: Authentication?, ): Mono = authentication?.let { getUserAndOrganizationWithPermissions( - authentication.toUser().name, + authentication.username(), organizationName, Permission.READ, authentication, diff --git a/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/PermissionController.kt b/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/PermissionController.kt index 5096ff3cc5..c8942227da 100644 --- a/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/PermissionController.kt +++ b/save-backend/src/main/kotlin/com/saveourtool/save/backend/controllers/PermissionController.kt @@ -1,6 +1,6 @@ package com.saveourtool.save.backend.controllers -import com.saveourtool.save.authservice.utils.toUser +import com.saveourtool.save.authservice.utils.username import com.saveourtool.save.backend.security.OrganizationPermissionEvaluator import com.saveourtool.save.backend.security.ProjectPermissionEvaluator import com.saveourtool.save.backend.service.OrganizationService @@ -78,7 +78,7 @@ class PermissionController( @PathVariable projectName: String, @RequestParam(required = false) userName: String?, authentication: Authentication, - ): Mono = getUserAndProjectOrNotFound(userName ?: authentication.toUser().name, projectName, organizationName, authentication) + ): Mono = getUserAndProjectOrNotFound(userName ?: authentication.username(), projectName, organizationName, authentication) .map { (user, project) -> permissionService.getRole(user, project) .also {