-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #168 from MTES-MCT/feature/user-add-roles-token
feat(backend): user roles in token
- Loading branch information
Showing
9 changed files
with
100 additions
and
4 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
backend/src/main/kotlin/fr/gouv/dgampa/rapportnav/domain/entities/user/RoleTypeEnum.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package fr.gouv.dgampa.rapportnav.domain.entities.user | ||
|
||
enum class RoleTypeEnum { | ||
ADMIN, | ||
USER_PAM, | ||
USER_ULAM, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 4 additions & 1 deletion
5
...r/gouv/dgampa/rapportnav/infrastructure/api/auth/adapters/inputs/AuthRegisterDataInput.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
package fr.gouv.dgampa.rapportnav.infrastructure.api.auth.adapters.inputs | ||
|
||
import fr.gouv.dgampa.rapportnav.domain.entities.user.RoleTypeEnum | ||
|
||
data class AuthRegisterDataInput( | ||
val id: Int?, | ||
val firstName: String, | ||
val lastName: String, | ||
val email: String, | ||
val password: String, | ||
val serviceId: Int? = null | ||
val serviceId: Int? = null, | ||
val roles: List<RoleTypeEnum>? | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
backend/src/main/resources/db/migration/V1.2024.05.28.10.40__alter_user_add_roles.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
DO $$ | ||
BEGIN | ||
CREATE TYPE "RoleType" AS ENUM ('ADMIN', 'USER_PAM', 'USER_ULAM'); | ||
ALTER TABLE "user" | ||
ADD COLUMN roles "RoleType"[] DEFAULT ARRAY['USER_PAM']::"RoleType"[]; | ||
END $$; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,13 @@ | ||
enum RoleTypeEnum { | ||
ADMIN, | ||
USER_PAM, | ||
USER_ULAM | ||
} | ||
|
||
type User { | ||
id: ID | ||
name: String! | ||
email: String! | ||
token: String | ||
roles: [RoleTypeEnum]! | ||
} |
56 changes: 56 additions & 0 deletions
56
backend/src/test/kotlin/fr/gouv/gmampa/rapportnav/domain/use_cases/auth/TokenServiceTests.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package fr.gouv.gmampa.rapportnav.domain.use_cases.auth | ||
|
||
import fr.gouv.dgampa.rapportnav.domain.entities.user.RoleTypeEnum | ||
import fr.gouv.dgampa.rapportnav.domain.entities.user.User | ||
import fr.gouv.dgampa.rapportnav.domain.repositories.user.IUserRepository | ||
import fr.gouv.dgampa.rapportnav.domain.use_cases.auth.TokenService | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.extension.ExtendWith | ||
import org.mockito.InjectMocks | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.beans.factory.annotation.Qualifier | ||
import org.springframework.boot.test.context.SpringBootTest | ||
import org.springframework.boot.test.mock.mockito.MockBean | ||
import org.springframework.security.oauth2.jwt.JwtDecoder | ||
import org.springframework.security.oauth2.jwt.JwtEncoder | ||
import org.springframework.test.context.junit.jupiter.SpringExtension | ||
|
||
@ExtendWith(SpringExtension::class) | ||
@SpringBootTest(classes = [TokenService::class]) | ||
class TokenServiceTests { | ||
|
||
@MockBean | ||
private lateinit var jwtDecoder: JwtDecoder; | ||
|
||
@MockBean | ||
private lateinit var jwtEncoder: JwtEncoder; | ||
|
||
@MockBean | ||
private lateinit var userRepository: IUserRepository; | ||
|
||
|
||
private val user:User = User( | ||
id = 3, | ||
firstName = "Jean", | ||
lastName = "Dupont", | ||
email = "[email protected]", | ||
password = "MyBeautifulPassword", | ||
serviceId = 6, | ||
roles = listOf(RoleTypeEnum.USER_ULAM) | ||
); | ||
|
||
|
||
@Test | ||
fun `execute should have roles with claim user id and user role`() { | ||
val tokenService = TokenService(jwtDecoder, jwtEncoder, userRepository); | ||
val claims = tokenService.getClaims(user); | ||
assertThat(claims).isNotNull(); | ||
|
||
assertThat(claims.getClaim<Int>("userId")).isEqualTo(user.id); | ||
assertThat(claims.getClaim<List<RoleTypeEnum>>("roles")).isEqualTo(user.roles); | ||
} | ||
|
||
} | ||
|
||
|