kikocis/dataspace-portal public
Changesets · Branches · Tags · Compare
| Changeset | d0dea3578ccf7be3ca1672cf7897787b6275d1a98880a1ed58a99607ebfceeb8 |
|---|---|
| Author | Kamil Czaja <46053356+kamilczaja@users.noreply.github.com> · 2024-12-03 11:03 +01:00 |
| Committer | GitHub <noreply@github.com> · 2024-12-03 11:03 +01:00 |
| Parents | 5d7ed408c69d |
11 files changed
modified CHANGELOG.md +1 −0
| @@ -28,6 +28,7 @@ | ||
| 28 | 28 | - Fixed security vulnerabilities |
| 29 | 29 | - Fixed the user not being redirected to the correct URL after login ([#324](https://github.com/sovity/authority-portal/issues/324)) |
| 30 | 30 | - Fixed an issue wherein it was possible to bypass the CaaS request limit in an organization ([PR #384](https://github.com/sovity/authority-portal/pull/384)) |
| 31 | +- Fixed an issue wherein a user registration could fail due to a mismatch of the internal database and the Keycloak database | |
| 31 | 32 | - Fixed an issue where entries in the connector overview would randomly switch places ([PR #386](https://github.com/sovity/authority-portal/pull/386)) |
| 32 | 33 | |
| 33 | 34 | ### Known issues |
modified authority-portal-backend/authority-portal-quarkus/src/main/kotlin/de/sovity/authorityportal/web/pages/organizationmanagement/OrganizationInvitationApiService.kt +14 −16
| @@ -23,6 +23,7 @@ | ||
| 23 | 23 | import de.sovity.authorityportal.web.thirdparty.keycloak.model.OrganizationRole |
| 24 | 24 | import de.sovity.authorityportal.web.utils.TimeUtils |
| 25 | 25 | import de.sovity.authorityportal.web.utils.idmanagement.OrganizationIdUtils |
| 26 | +import de.sovity.authorityportal.web.utils.resourceAlreadyExists | |
| 26 | 27 | import io.quarkus.logging.Log |
| 27 | 28 | import jakarta.enterprise.context.ApplicationScoped |
| 28 | 29 | |
| @@ -36,8 +37,20 @@ | ||
| 36 | 37 | ) { |
| 37 | 38 | |
| 38 | 39 | fun inviteOrganization(invitationInformation: InviteOrganizationRequest, adminUserId: String): IdResponse { |
| 40 | + if (userService.userExistsInDb(invitationInformation.userEmail)) { | |
| 41 | + resourceAlreadyExists("User with email ${invitationInformation.userEmail} already exists.") | |
| 42 | + } | |
| 43 | + | |
| 39 | 44 | val organizationId = organizationIdUtils.generateOrganizationId() |
| 40 | - val userId = createKeycloakUserAndOrganization(organizationId, invitationInformation) | |
| 45 | + val userId = keycloakService.createKeycloakUserAndOrganization( | |
| 46 | + organizationId = organizationId, | |
| 47 | + userEmail = invitationInformation.userEmail, | |
| 48 | + userFirstName = invitationInformation.userFirstName, | |
| 49 | + userLastName = invitationInformation.userLastName, | |
| 50 | + userOrganizationRole = OrganizationRole.PARTICIPANT_ADMIN, | |
| 51 | + userPassword = null | |
| 52 | + ) | |
| 53 | + | |
| 41 | 54 | createDbUserAndOrganization(userId, organizationId, invitationInformation) |
| 42 | 55 | keycloakService.sendInvitationEmailWithPasswordReset(userId) |
| 43 | 56 | |
| @@ -46,21 +59,6 @@ | ||
| 46 | 59 | return IdResponse(organizationId, timeUtils.now()) |
| 47 | 60 | } |
| 48 | 61 | |
| 49 | - private fun createKeycloakUserAndOrganization( | |
| 50 | - organizationId: String, | |
| 51 | - invitationInformation: InviteOrganizationRequest | |
| 52 | - ): String { | |
| 53 | - val userId = keycloakService.createUser( | |
| 54 | - invitationInformation.userEmail, | |
| 55 | - invitationInformation.userFirstName, | |
| 56 | - invitationInformation.userLastName | |
| 57 | - ) | |
| 58 | - keycloakService.createOrganization(organizationId) | |
| 59 | - keycloakService.joinOrganization(userId, organizationId, OrganizationRole.PARTICIPANT_ADMIN) | |
| 60 | - | |
| 61 | - return userId | |
| 62 | - } | |
| 63 | - | |
| 64 | 62 | private fun createDbUserAndOrganization( |
| 65 | 63 | userId: String, |
| 66 | 64 | organizationId: String, |
modified authority-portal-backend/authority-portal-quarkus/src/main/kotlin/de/sovity/authorityportal/web/pages/registration/RegistrationApiService.kt +14 −14
| @@ -27,6 +27,7 @@ | ||
| 27 | 27 | import de.sovity.authorityportal.web.thirdparty.keycloak.model.OrganizationRole |
| 28 | 28 | import de.sovity.authorityportal.web.utils.TimeUtils |
| 29 | 29 | import de.sovity.authorityportal.web.utils.idmanagement.OrganizationIdUtils |
| 30 | +import de.sovity.authorityportal.web.utils.resourceAlreadyExists | |
| 30 | 31 | import io.quarkus.logging.Log |
| 31 | 32 | import jakarta.enterprise.context.ApplicationScoped |
| 32 | 33 | |
| @@ -41,8 +42,20 @@ | ||
| 41 | 42 | ) { |
| 42 | 43 | |
| 43 | 44 | fun registerUserAndOrganization(registrationRequest: RegistrationRequestDto): IdResponse { |
| 45 | + if (userService.userExistsInDb(registrationRequest.userEmail)) { | |
| 46 | + resourceAlreadyExists("User with email ${registrationRequest.userEmail} already exists.") | |
| 47 | + } | |
| 48 | + | |
| 44 | 49 | val organizationId = organizationIdUtils.generateOrganizationId() |
| 45 | - val userId = createKeycloakUserAndOrganization(organizationId, registrationRequest) | |
| 50 | + val userId = keycloakService.createKeycloakUserAndOrganization( | |
| 51 | + organizationId = organizationId, | |
| 52 | + userEmail = registrationRequest.userEmail, | |
| 53 | + userFirstName = registrationRequest.userFirstName, | |
| 54 | + userLastName = registrationRequest.userLastName, | |
| 55 | + userOrganizationRole = OrganizationRole.PARTICIPANT_ADMIN, | |
| 56 | + userPassword = registrationRequest.userPassword, | |
| 57 | + ) | |
| 58 | + | |
| 46 | 59 | createDbUserAndOrganization(userId, organizationId, registrationRequest) |
| 47 | 60 | keycloakService.sendInvitationEmail(userId) |
| 48 | 61 | firstUserService.setupFirstUserIfRequired(userId, organizationId) |
| @@ -52,19 +65,6 @@ | ||
| 52 | 65 | return IdResponse(userId, timeUtils.now()) |
| 53 | 66 | } |
| 54 | 67 | |
| 55 | - private fun createKeycloakUserAndOrganization(organizationId: String, registrationRequest: RegistrationRequestDto): String { | |
| 56 | - val userId = keycloakService.createUser( | |
| 57 | - email = registrationRequest.userEmail, | |
| 58 | - firstName = registrationRequest.userFirstName, | |
| 59 | - lastName = registrationRequest.userLastName, | |
| 60 | - password = registrationRequest.userPassword | |
| 61 | - ) | |
| 62 | - keycloakService.createOrganization(organizationId) | |
| 63 | - keycloakService.joinOrganization(userId, organizationId, OrganizationRole.PARTICIPANT_ADMIN) | |
| 64 | - | |
| 65 | - return userId | |
| 66 | - } | |
| 67 | - | |
| 68 | 68 | private fun createDbUserAndOrganization( |
| 69 | 69 | userId: String, |
| 70 | 70 | organizationId: String, |
modified authority-portal-backend/authority-portal-quarkus/src/main/kotlin/de/sovity/authorityportal/web/pages/usermanagement/UserInvitationApiService.kt +8 −0
| @@ -19,6 +19,7 @@ | ||
| 19 | 19 | import de.sovity.authorityportal.web.services.UserService |
| 20 | 20 | import de.sovity.authorityportal.web.thirdparty.keycloak.KeycloakService |
| 21 | 21 | import de.sovity.authorityportal.web.utils.TimeUtils |
| 22 | +import de.sovity.authorityportal.web.utils.resourceAlreadyExists | |
| 22 | 23 | import io.quarkus.logging.Log |
| 23 | 24 | import jakarta.enterprise.context.ApplicationScoped |
| 24 | 25 | |
| @@ -35,6 +36,13 @@ | ||
| 35 | 36 | organizationId: String, |
| 36 | 37 | adminUserId: String |
| 37 | 38 | ): IdResponse { |
| 39 | + if (userService.userExistsInDb(userInformation.email)) { | |
| 40 | + resourceAlreadyExists("User with email ${userInformation.email} already exists.") | |
| 41 | + } | |
| 42 | + | |
| 43 | + // DB is source of truth, so we delete a potentially existing user in Keycloak | |
| 44 | + keycloakService.getUserIdByEmail(userInformation.email)?.let { keycloakService.deleteUser(it) } | |
| 45 | + | |
| 38 | 46 | val userId = |
| 39 | 47 | keycloakService.createUser(userInformation.email, userInformation.firstName, userInformation.lastName) |
| 40 | 48 | keycloakService.sendInvitationEmailWithPasswordReset(userId) |
modified authority-portal-backend/authority-portal-quarkus/src/main/kotlin/de/sovity/authorityportal/web/services/UserService.kt +9 −0
| @@ -65,6 +65,15 @@ | ||
| 65 | 65 | .fetchMap(u.ORGANIZATION_ID, DSL.count()) |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | + fun userExistsInDb(email: String): Boolean { | |
| 69 | + val u = Tables.USER | |
| 70 | + | |
| 71 | + return dsl.fetchExists( | |
| 72 | + dsl.selectFrom(u) | |
| 73 | + .where(u.EMAIL.eq(email)) | |
| 74 | + ) | |
| 75 | + } | |
| 76 | + | |
| 68 | 77 | private fun getUser(userId: String): UserRecord? { |
| 69 | 78 | val u = Tables.USER |
| 70 | 79 |
modified authority-portal-backend/authority-portal-quarkus/src/main/kotlin/de/sovity/authorityportal/web/thirdparty/keycloak/KeycloakService.kt +39 −13
| @@ -19,7 +19,6 @@ | ||
| 19 | 19 | import de.sovity.authorityportal.web.thirdparty.keycloak.model.RequiredAction |
| 20 | 20 | import io.quarkus.logging.Log |
| 21 | 21 | import jakarta.enterprise.context.ApplicationScoped |
| 22 | -import jakarta.inject.Inject | |
| 23 | 22 | import jakarta.ws.rs.WebApplicationException |
| 24 | 23 | import jakarta.ws.rs.core.Response |
| 25 | 24 | import org.eclipse.microprofile.config.inject.ConfigProperty |
| @@ -29,22 +28,19 @@ | ||
| 29 | 28 | import org.keycloak.representations.idm.UserRepresentation |
| 30 | 29 | |
| 31 | 30 | @ApplicationScoped |
| 32 | -class KeycloakService { | |
| 33 | - | |
| 34 | - @Inject | |
| 35 | - lateinit var keycloak: Keycloak | |
| 36 | - | |
| 37 | - @Inject | |
| 38 | - lateinit var keycloakUserMapper: KeycloakUserMapper | |
| 31 | +class KeycloakService( | |
| 32 | + val keycloak: Keycloak, | |
| 33 | + val keycloakUserMapper: KeycloakUserMapper, | |
| 39 | 34 | |
| 40 | 35 | @ConfigProperty(name = "quarkus.keycloak.admin-client.realm") |
| 41 | - lateinit var keycloakRealm: String | |
| 36 | + val keycloakRealm: String, | |
| 42 | 37 | |
| 43 | 38 | @ConfigProperty(name = "quarkus.keycloak.admin-client.client-id") |
| 44 | - lateinit var keycloakClientId: String | |
| 39 | + val keycloakClientId: String, | |
| 45 | 40 | |
| 46 | 41 | @ConfigProperty(name = "authority-portal.base-url") |
| 47 | - lateinit var baseUrl: String | |
| 42 | + val baseUrl: String | |
| 43 | +) { | |
| 48 | 44 | |
| 49 | 45 | fun createUser(email: String, firstName: String, lastName: String, password: String? = null): String { |
| 50 | 46 | val user = UserRepresentation().also { |
| @@ -60,7 +56,7 @@ | ||
| 60 | 56 | |
| 61 | 57 | if (password != null) { |
| 62 | 58 | it.credentials = listOf( |
| 63 | - CredentialRepresentation().also {credentials -> | |
| 59 | + CredentialRepresentation().also { credentials -> | |
| 64 | 60 | credentials.isTemporary = false |
| 65 | 61 | credentials.type = CredentialRepresentation.PASSWORD |
| 66 | 62 | credentials.value = password |
| @@ -77,6 +73,36 @@ | ||
| 77 | 73 | return keycloak.realm(keycloakRealm).users().search(email).first().id |
| 78 | 74 | } |
| 79 | 75 | |
| 76 | + fun createKeycloakUserAndOrganization( | |
| 77 | + organizationId: String, | |
| 78 | + userEmail: String, | |
| 79 | + userFirstName: String, | |
| 80 | + userLastName: String, | |
| 81 | + userOrganizationRole: OrganizationRole, | |
| 82 | + userPassword: String? | |
| 83 | + ): String { | |
| 84 | + // To avoid syncing issues, we assume our DB to be the source of truth, so we need to delete the potentially existing user in KC | |
| 85 | + val userIdIfExists = getUserIdByEmail(userEmail) | |
| 86 | + if (userIdIfExists != null) { | |
| 87 | + deleteUser(userIdIfExists) | |
| 88 | + } | |
| 89 | + | |
| 90 | + val userId = createUser( | |
| 91 | + email = userEmail, | |
| 92 | + firstName = userFirstName, | |
| 93 | + lastName = userLastName, | |
| 94 | + password = userPassword | |
| 95 | + ) | |
| 96 | + createOrganization(organizationId) | |
| 97 | + joinOrganization(userId, organizationId, userOrganizationRole) | |
| 98 | + | |
| 99 | + return userId | |
| 100 | + } | |
| 101 | + | |
| 102 | + fun getUserIdByEmail(email: String): String? { | |
| 103 | + return keycloak.realm(keycloakRealm).users().search(email).firstOrNull()?.id | |
| 104 | + } | |
| 105 | + | |
| 80 | 106 | fun deleteUser(userId: String) { |
| 81 | 107 | keycloak.realm(keycloakRealm).users().delete(userId) |
| 82 | 108 | } |
| @@ -278,7 +304,7 @@ | ||
| 278 | 304 | keycloak.realm(keycloakRealm).users().get(userId).executeActionsEmail(keycloakClientId, baseUrl, actions) |
| 279 | 305 | } |
| 280 | 306 | |
| 281 | - private fun getSubGroupIds(groupId: String) = | |
| 307 | + private fun getSubGroupIds(groupId: String) = | |
| 282 | 308 | keycloak.realm(keycloakRealm).groups().query("", true) |
| 283 | 309 | .first { it.id == groupId }.subGroups.associate { it.name to it.id } |
| 284 | 310 | } |
modified authority-portal-backend/authority-portal-quarkus/src/main/kotlin/de/sovity/authorityportal/web/thirdparty/uptimekuma/model/ComponentStatus.kt +3 −0
| @@ -13,6 +13,9 @@ | ||
| 13 | 13 | |
| 14 | 14 | package de.sovity.authorityportal.web.thirdparty.uptimekuma.model |
| 15 | 15 | |
| 16 | +import de.sovity.authorityportal.web.thirdparty.uptimekuma.model.ComponentStatus.entries | |
| 17 | + | |
| 18 | + | |
| 16 | 19 | enum class ComponentStatus(val intValue: Int) { |
| 17 | 20 | UP(1), |
| 18 | 21 | DOWN(0), |
modified authority-portal-backend/authority-portal-quarkus/src/main/kotlin/de/sovity/authorityportal/web/utils/HttpExceptionUtils.kt +10 −0
| @@ -15,6 +15,7 @@ | ||
| 15 | 15 | |
| 16 | 16 | import jakarta.ws.rs.NotAuthorizedException |
| 17 | 17 | import jakarta.ws.rs.NotFoundException |
| 18 | +import jakarta.ws.rs.WebApplicationException | |
| 18 | 19 | import jakarta.ws.rs.core.Response |
| 19 | 20 | |
| 20 | 21 | /** |
| @@ -37,3 +38,12 @@ | ||
| 37 | 38 | .build() |
| 38 | 39 | ) |
| 39 | 40 | } |
| 41 | + | |
| 42 | +fun resourceAlreadyExists(message: String = ""): Nothing { | |
| 43 | + throw WebApplicationException( | |
| 44 | + "Resource already exists. $message", | |
| 45 | + Response.status(Response.Status.CONFLICT) | |
| 46 | + .header("WWW-Authenticate", "Bearer realm=\"authority-portal\"") | |
| 47 | + .build() | |
| 48 | + ) | |
| 49 | +} |
modified authority-portal-backend/authority-portal-quarkus/src/test/kotlin/de/sovity/authorityportal/web/tests/services/FirstUserRegistrationTest.kt +2 −3
| @@ -93,10 +93,9 @@ | ||
| 93 | 93 | useMockNow(now) |
| 94 | 94 | |
| 95 | 95 | doNothing().whenever(keycloakService).sendInvitationEmail(any()) |
| 96 | - doNothing().whenever(keycloakService).createOrganization(any()) | |
| 97 | 96 | doNothing().whenever(keycloakService).joinOrganization(any(), any(), any()) |
| 98 | - whenever(keycloakService.createUser( | |
| 99 | - eq(request.userEmail), eq(request.userFirstName), eq(request.userLastName), eq(request.userPassword) | |
| 97 | + whenever(keycloakService.createKeycloakUserAndOrganization( | |
| 98 | + any(), eq(request.userEmail), eq(request.userFirstName), eq(request.userLastName), any(), eq(request.userPassword) | |
| 100 | 99 | )).thenReturn(dummyDevUserUuid(0)) |
| 101 | 100 | |
| 102 | 101 | // act |
modified authority-portal-backend/authority-portal-quarkus/src/test/kotlin/de/sovity/authorityportal/web/tests/services/RegistrationApiServiceTest.kt +26 −4
| @@ -33,7 +33,9 @@ | ||
| 33 | 33 | import io.quarkus.test.TestTransaction |
| 34 | 34 | import io.quarkus.test.junit.QuarkusTest |
| 35 | 35 | import jakarta.inject.Inject |
| 36 | +import jakarta.ws.rs.WebApplicationException | |
| 36 | 37 | import org.assertj.core.api.Assertions.assertThat |
| 38 | +import org.assertj.core.api.Assertions.assertThatThrownBy | |
| 37 | 39 | import org.jooq.DSLContext |
| 38 | 40 | import org.junit.jupiter.api.Test |
| 39 | 41 | import org.junit.jupiter.api.extension.ExtendWith |
| @@ -41,6 +43,7 @@ | ||
| 41 | 43 | import org.mockito.kotlin.any |
| 42 | 44 | import org.mockito.kotlin.doNothing |
| 43 | 45 | import org.mockito.kotlin.eq |
| 46 | +import org.mockito.kotlin.verify | |
| 44 | 47 | import org.mockito.kotlin.whenever |
| 45 | 48 | import java.time.OffsetDateTime |
| 46 | 49 | |
| @@ -95,10 +98,8 @@ | ||
| 95 | 98 | useMockNow(now) |
| 96 | 99 | |
| 97 | 100 | doNothing().whenever(keycloakService).sendInvitationEmail(any()) |
| 98 | - doNothing().whenever(keycloakService).createOrganization(any()) | |
| 99 | - doNothing().whenever(keycloakService).joinOrganization(any(), any(), any()) | |
| 100 | - whenever(keycloakService.createUser( | |
| 101 | - eq(request.userEmail), eq(request.userFirstName), eq(request.userLastName), eq(request.userPassword) | |
| 101 | + whenever(keycloakService.createKeycloakUserAndOrganization( | |
| 102 | + any(), eq(request.userEmail), eq(request.userFirstName), eq(request.userLastName), any(), eq(request.userPassword) | |
| 102 | 103 | )).thenReturn(dummyDevUserUuid(1)) |
| 103 | 104 | |
| 104 | 105 | ScenarioData().apply { |
| @@ -174,4 +175,25 @@ | ||
| 174 | 175 | .withStrictTypeChecking() |
| 175 | 176 | .isEqualTo(expectedUser.copy()) |
| 176 | 177 | } |
| 178 | + | |
| 179 | + @Test | |
| 180 | + @TestTransaction | |
| 181 | + fun `registration fails because user is already in db`() { | |
| 182 | + // arrange | |
| 183 | + useUnauthenticated() | |
| 184 | + | |
| 185 | + ScenarioData().apply { | |
| 186 | + organization(0, 0) | |
| 187 | + user(0, 0) { | |
| 188 | + it.email = request.userEmail | |
| 189 | + } | |
| 190 | + scenarioInstaller.install(this) | |
| 191 | + } | |
| 192 | + | |
| 193 | + // act & assert | |
| 194 | + assertThatThrownBy { uiResource.registerUser(request) } | |
| 195 | + .isInstanceOf(WebApplicationException::class.java) | |
| 196 | + .extracting { (it as WebApplicationException).response.status } | |
| 197 | + .isEqualTo(409) | |
| 198 | + } | |
| 177 | 199 | } |
modified authority-portal-backend/authority-portal-quarkus/src/test/kotlin/de/sovity/authorityportal/web/tests/services/organization/OrganizationInvitationApiServiceTest.kt +3 −7
| @@ -97,13 +97,9 @@ | ||
| 97 | 97 | useMockNow(now) |
| 98 | 98 | |
| 99 | 99 | whenever(organizationIdUtils.generateOrganizationId()).thenReturn(dummyDevOrganizationId(1)) |
| 100 | - whenever(keycloakService.createUser( | |
| 101 | - eq("new.user@test.sovity.io"), | |
| 102 | - eq("New"), | |
| 103 | - eq("User"), | |
| 104 | - isNull())).thenReturn(dummyDevUserUuid(1)) | |
| 105 | - doNothing().whenever(keycloakService).createOrganization(eq(dummyDevOrganizationId(1))) | |
| 106 | - doNothing().whenever(keycloakService).joinOrganization(eq(dummyDevUserUuid(1)), eq(dummyDevOrganizationId(1)), any()) | |
| 100 | + whenever(keycloakService.createKeycloakUserAndOrganization( | |
| 101 | + any(), eq("new.user@test.sovity.io"), eq("New"), eq("User"), any(), eq(null) | |
| 102 | + )).thenReturn(dummyDevUserUuid(1)) | |
| 107 | 103 | |
| 108 | 104 | ScenarioData().apply { |
| 109 | 105 | user(0, 0) |