From ffb4eb13076758c73959c6a6987c1eb76c86686c Mon Sep 17 00:00:00 2001 From: Z-100 Date: Sun, 2 Jun 2024 14:11:01 +0200 Subject: [PATCH 01/24] feature(#4): Make modules --- dmz-app/pom.xml | 51 +++++++++++++++++++ pom.xml | 127 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100644 dmz-app/pom.xml create mode 100644 pom.xml diff --git a/dmz-app/pom.xml b/dmz-app/pom.xml new file mode 100644 index 0000000..9e7184c --- /dev/null +++ b/dmz-app/pom.xml @@ -0,0 +1,51 @@ + + + 4.0.0 + + + ch.zindustries.dmz + dmz-root + 1.0.0-SNAPSHOT + + + dmz-app + + + UTF-8 + official + 1.8 + + + + + + + + src/test/kotlin + + + + org.jetbrains.kotlin + kotlin-maven-plugin + + + + maven-surefire-plugin + 2.22.2 + + + + maven-failsafe-plugin + 2.22.2 + + + + org.codehaus.mojo + exec-maven-plugin + + + + + diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..09736c8 --- /dev/null +++ b/pom.xml @@ -0,0 +1,127 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 3.2.4 + + + + ch.zindustries.dmz + dmz-root + + 1.0.0-SNAPSHOT + pom + + + dmz-app + + + + UTF-8 + official + 1.17 + + + + + + + + ch.zindustries.dmz + dmz-app + 1.0.0-SNAPSHOT + + + + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin.version} + + + + org.jetbrains.kotlin + kotlin-test-junit5 + ${kotlin.version} + test + + + + + + + src/main/kotlin + + + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + 17 + + + + + kapt + generate-sources + + kapt + + + + + src/main/kotlin + + + + + + compile + compile + + compile + + + + + test-compile + test-compile + + test-compile + + + + + + + maven-surefire-plugin + 2.22.2 + + + + maven-failsafe-plugin + 2.22.2 + + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + + MainKt + + + + + + + From f33df9c01c145ba743fd30b829656239f089d291 Mon Sep 17 00:00:00 2001 From: Z-100 Date: Sun, 2 Jun 2024 14:17:26 +0200 Subject: [PATCH 02/24] feature(#4): Add spring application + test --- dmz-app/pom.xml | 17 +++++++++++++++++ .../ch/zindustries/dmz/app/DmzApplication.kt | 11 +++++++++++ dmz-app/src/main/resources/application.yml | 15 +++++++++++++++ .../dmz/app/DmzAppApplicationKtTest.kt | 14 ++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 dmz-app/src/main/kotlin/ch/zindustries/dmz/app/DmzApplication.kt create mode 100644 dmz-app/src/main/resources/application.yml create mode 100644 dmz-app/src/test/kotlin/ch/zindustries/dmz/app/DmzAppApplicationKtTest.kt diff --git a/dmz-app/pom.xml b/dmz-app/pom.xml index 9e7184c..a70e9ef 100644 --- a/dmz-app/pom.xml +++ b/dmz-app/pom.xml @@ -20,6 +20,23 @@ + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-test + + + + + org.jetbrains.kotlin + kotlin-stdlib + + diff --git a/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/DmzApplication.kt b/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/DmzApplication.kt new file mode 100644 index 0000000..4b1f639 --- /dev/null +++ b/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/DmzApplication.kt @@ -0,0 +1,11 @@ +package ch.zindustries.dmz.app + +import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.runApplication + +@SpringBootApplication +open class DmzAppApplication + +fun main(args: Array) { + runApplication(*args) +} diff --git a/dmz-app/src/main/resources/application.yml b/dmz-app/src/main/resources/application.yml new file mode 100644 index 0000000..f46ffb4 --- /dev/null +++ b/dmz-app/src/main/resources/application.yml @@ -0,0 +1,15 @@ +server: + port: 8001 +spring: + application: + name: dmz + datasource: + url: jdbc:postgresql://localhost:5432/dmz-pgsql + username: postgres + password: postgres + jpa: + properties: + hibernate: + dialect: org.hibernate.dialect.PostgreSQLDialect + hibernate: + ddl-auto: validate diff --git a/dmz-app/src/test/kotlin/ch/zindustries/dmz/app/DmzAppApplicationKtTest.kt b/dmz-app/src/test/kotlin/ch/zindustries/dmz/app/DmzAppApplicationKtTest.kt new file mode 100644 index 0000000..8b4d48e --- /dev/null +++ b/dmz-app/src/test/kotlin/ch/zindustries/dmz/app/DmzAppApplicationKtTest.kt @@ -0,0 +1,14 @@ +package ch.zindustries.dmz.app + +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test +import org.springframework.boot.test.context.SpringBootTest + +@SpringBootTest +class DmzAppApplicationKtTest { + + @Test + fun `Test server startup`() { + assertTrue(true) + } +} From 8adb04d9ec3af5b2398ba3488263bfdcd04cc019 Mon Sep 17 00:00:00 2001 From: Z-100 Date: Sun, 2 Jun 2024 14:25:29 +0200 Subject: [PATCH 03/24] feature(#4): Add modules --- dmz-api/pom.xml | 39 +++++++++++++++++++++++++++++++++++++++ dmz-app/pom.xml | 6 ------ dmz-rs-api/pom.xml | 39 +++++++++++++++++++++++++++++++++++++++ dmz-rs-server/pom.xml | 41 +++++++++++++++++++++++++++++++++++++++++ dmz/pom.xml | 41 +++++++++++++++++++++++++++++++++++++++++ pom.xml | 4 ++++ 6 files changed, 164 insertions(+), 6 deletions(-) create mode 100644 dmz-api/pom.xml create mode 100644 dmz-rs-api/pom.xml create mode 100644 dmz-rs-server/pom.xml create mode 100644 dmz/pom.xml diff --git a/dmz-api/pom.xml b/dmz-api/pom.xml new file mode 100644 index 0000000..5ccb0f6 --- /dev/null +++ b/dmz-api/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + + + ch.zindustries.dmz + dmz-root + 1.0.0-SNAPSHOT + + + dmz-api + + + + + org.jetbrains.kotlin + kotlin-maven-plugin + + + + maven-surefire-plugin + 2.22.2 + + + + maven-failsafe-plugin + 2.22.2 + + + + org.codehaus.mojo + exec-maven-plugin + + + + + diff --git a/dmz-app/pom.xml b/dmz-app/pom.xml index a70e9ef..f36af5f 100644 --- a/dmz-app/pom.xml +++ b/dmz-app/pom.xml @@ -12,12 +12,6 @@ dmz-app - - UTF-8 - official - 1.8 - - diff --git a/dmz-rs-api/pom.xml b/dmz-rs-api/pom.xml new file mode 100644 index 0000000..734b106 --- /dev/null +++ b/dmz-rs-api/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + + + ch.zindustries.dmz + dmz-root + 1.0.0-SNAPSHOT + + + dmz-rs-api + + + + + org.jetbrains.kotlin + kotlin-maven-plugin + + + + maven-surefire-plugin + 2.22.2 + + + + maven-failsafe-plugin + 2.22.2 + + + + org.codehaus.mojo + exec-maven-plugin + + + + + diff --git a/dmz-rs-server/pom.xml b/dmz-rs-server/pom.xml new file mode 100644 index 0000000..04561c8 --- /dev/null +++ b/dmz-rs-server/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + + ch.zindustries.dmz + dmz-root + 1.0.0-SNAPSHOT + + + dmz-rs-server + + + src/test/kotlin + + + + org.jetbrains.kotlin + kotlin-maven-plugin + + + + maven-surefire-plugin + 2.22.2 + + + + maven-failsafe-plugin + 2.22.2 + + + + org.codehaus.mojo + exec-maven-plugin + + + + + diff --git a/dmz/pom.xml b/dmz/pom.xml new file mode 100644 index 0000000..20711c1 --- /dev/null +++ b/dmz/pom.xml @@ -0,0 +1,41 @@ + + + 4.0.0 + + + ch.zindustries.dmz + dmz-root + 1.0.0-SNAPSHOT + + + dmz + + + src/test/kotlin + + + + org.jetbrains.kotlin + kotlin-maven-plugin + + + + maven-surefire-plugin + 2.22.2 + + + + maven-failsafe-plugin + 2.22.2 + + + + org.codehaus.mojo + exec-maven-plugin + + + + + diff --git a/pom.xml b/pom.xml index 09736c8..663f335 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,11 @@ pom + dmz + dmz-api dmz-app + dmz-rs-api + dmz-rs-server From c9274b0c1bb79055a64e1fd8ce1e18de9dbe8dbb Mon Sep 17 00:00:00 2001 From: Z-100 Date: Sun, 2 Jun 2024 14:31:10 +0200 Subject: [PATCH 04/24] feature(#4): Add dependencies in between modules --- dmz-api/pom.xml | 10 ++++++++++ dmz-app/pom.xml | 12 ++++++------ dmz-rs-api/pom.xml | 10 ++++++++++ dmz-rs-server/pom.xml | 10 ++++++++++ dmz/pom.xml | 10 ++++++++++ pom.xml | 24 ++++++++++++++++++++++++ 6 files changed, 70 insertions(+), 6 deletions(-) diff --git a/dmz-api/pom.xml b/dmz-api/pom.xml index 5ccb0f6..3c258af 100644 --- a/dmz-api/pom.xml +++ b/dmz-api/pom.xml @@ -12,6 +12,16 @@ dmz-api + + + + + org.jetbrains.kotlin + kotlin-stdlib + + + + diff --git a/dmz-app/pom.xml b/dmz-app/pom.xml index f36af5f..95a9231 100644 --- a/dmz-app/pom.xml +++ b/dmz-app/pom.xml @@ -14,6 +14,12 @@ + + + ch.zindustries.dmz + dmz-rs-server + + org.springframework.boot @@ -25,12 +31,6 @@ spring-boot-starter-test - - - org.jetbrains.kotlin - kotlin-stdlib - - diff --git a/dmz-rs-api/pom.xml b/dmz-rs-api/pom.xml index 734b106..8729e2b 100644 --- a/dmz-rs-api/pom.xml +++ b/dmz-rs-api/pom.xml @@ -12,6 +12,16 @@ dmz-rs-api + + + + + ch.zindustries.dmz + dmz-api + + + + diff --git a/dmz-rs-server/pom.xml b/dmz-rs-server/pom.xml index 04561c8..c776362 100644 --- a/dmz-rs-server/pom.xml +++ b/dmz-rs-server/pom.xml @@ -12,6 +12,16 @@ dmz-rs-server + + + + + ch.zindustries.dmz + dmz + + + + src/test/kotlin diff --git a/dmz/pom.xml b/dmz/pom.xml index 20711c1..c72e0f3 100644 --- a/dmz/pom.xml +++ b/dmz/pom.xml @@ -12,6 +12,16 @@ dmz + + + + + ch.zindustries.dmz + dmz-api + + + + src/test/kotlin diff --git a/pom.xml b/pom.xml index 663f335..83f885f 100644 --- a/pom.xml +++ b/pom.xml @@ -35,12 +35,36 @@ + + ch.zindustries.dmz + dmz + 1.0.0-SNAPSHOT + + + + ch.zindustries.dmz + dmz-api + 1.0.0-SNAPSHOT + + ch.zindustries.dmz dmz-app 1.0.0-SNAPSHOT + + ch.zindustries.dmz + dmz-rs-api + 1.0.0-SNAPSHOT + + + + ch.zindustries.dmz + dmz-rs-server + 1.0.0-SNAPSHOT + + org.jetbrains.kotlin From bbeb83e12f89ef6a34e4b911f49ce88bb9408fe9 Mon Sep 17 00:00:00 2001 From: Z-100 Date: Sun, 2 Jun 2024 14:45:54 +0200 Subject: [PATCH 05/24] feature(#4): Build call chain --- .../kotlin/ch/zindustries/dmz/api/TestService.kt | 6 ++++++ dmz-app/pom.xml | 6 ------ .../ch/zindustries/dmz/app/DmzApplication.kt | 4 +++- dmz-rs-api/pom.xml | 6 ++++++ .../zindustries/dmz/rs/api/TestResourceService.kt | 12 ++++++++++++ dmz-rs-server/pom.xml | 5 +++++ .../dmz/rs/server/TestResourceServiceImpl.kt | 14 ++++++++++++++ dmz/pom.xml | 6 ++++++ .../kotlin/ch/zindustries/dmz/TestServiceImpl.kt | 10 ++++++++++ 9 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 dmz-api/src/main/kotlin/ch/zindustries/dmz/api/TestService.kt create mode 100644 dmz-rs-api/src/main/kotlin/ch/zindustries/dmz/rs/api/TestResourceService.kt create mode 100644 dmz-rs-server/src/main/kotlin/ch/zindustries/dmz/rs/server/TestResourceServiceImpl.kt create mode 100644 dmz/src/main/kotlin/ch/zindustries/dmz/TestServiceImpl.kt diff --git a/dmz-api/src/main/kotlin/ch/zindustries/dmz/api/TestService.kt b/dmz-api/src/main/kotlin/ch/zindustries/dmz/api/TestService.kt new file mode 100644 index 0000000..e570a1a --- /dev/null +++ b/dmz-api/src/main/kotlin/ch/zindustries/dmz/api/TestService.kt @@ -0,0 +1,6 @@ +package ch.zindustries.dmz.api + +interface TestService { + + fun test(): String +} diff --git a/dmz-app/pom.xml b/dmz-app/pom.xml index 95a9231..2b2186d 100644 --- a/dmz-app/pom.xml +++ b/dmz-app/pom.xml @@ -20,17 +20,11 @@ dmz-rs-server - org.springframework.boot spring-boot-starter-web - - org.springframework.boot - spring-boot-starter-test - - diff --git a/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/DmzApplication.kt b/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/DmzApplication.kt index 4b1f639..ef0052b 100644 --- a/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/DmzApplication.kt +++ b/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/DmzApplication.kt @@ -3,7 +3,9 @@ package ch.zindustries.dmz.app import org.springframework.boot.autoconfigure.SpringBootApplication import org.springframework.boot.runApplication -@SpringBootApplication +@SpringBootApplication( + scanBasePackages = ["ch.zindustries.dmz"] +) open class DmzAppApplication fun main(args: Array) { diff --git a/dmz-rs-api/pom.xml b/dmz-rs-api/pom.xml index 8729e2b..0ce1232 100644 --- a/dmz-rs-api/pom.xml +++ b/dmz-rs-api/pom.xml @@ -20,6 +20,12 @@ dmz-api + + + org.springframework.boot + spring-boot-starter-web + + diff --git a/dmz-rs-api/src/main/kotlin/ch/zindustries/dmz/rs/api/TestResourceService.kt b/dmz-rs-api/src/main/kotlin/ch/zindustries/dmz/rs/api/TestResourceService.kt new file mode 100644 index 0000000..7762c78 --- /dev/null +++ b/dmz-rs-api/src/main/kotlin/ch/zindustries/dmz/rs/api/TestResourceService.kt @@ -0,0 +1,12 @@ +package ch.zindustries.dmz.rs.api + +import org.springframework.http.ResponseEntity +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.RequestMapping + +@RequestMapping +interface TestResourceService { + + @GetMapping("test") + fun test() : ResponseEntity +} diff --git a/dmz-rs-server/pom.xml b/dmz-rs-server/pom.xml index c776362..56ce056 100644 --- a/dmz-rs-server/pom.xml +++ b/dmz-rs-server/pom.xml @@ -20,6 +20,11 @@ dmz + + ch.zindustries.dmz + dmz-rs-api + + diff --git a/dmz-rs-server/src/main/kotlin/ch/zindustries/dmz/rs/server/TestResourceServiceImpl.kt b/dmz-rs-server/src/main/kotlin/ch/zindustries/dmz/rs/server/TestResourceServiceImpl.kt new file mode 100644 index 0000000..ce0b808 --- /dev/null +++ b/dmz-rs-server/src/main/kotlin/ch/zindustries/dmz/rs/server/TestResourceServiceImpl.kt @@ -0,0 +1,14 @@ +package ch.zindustries.dmz.rs.server + +import ch.zindustries.dmz.api.TestService +import ch.zindustries.dmz.rs.api.TestResourceService +import org.springframework.http.ResponseEntity +import org.springframework.web.bind.annotation.RestController + +@RestController +class TestResourceServiceImpl( + private val testService: TestService +) : TestResourceService { + + override fun test(): ResponseEntity = ResponseEntity.ok(testService.test()) +} diff --git a/dmz/pom.xml b/dmz/pom.xml index c72e0f3..bf5ed23 100644 --- a/dmz/pom.xml +++ b/dmz/pom.xml @@ -20,6 +20,12 @@ dmz-api + + + org.springframework.boot + spring-boot-starter-test + + diff --git a/dmz/src/main/kotlin/ch/zindustries/dmz/TestServiceImpl.kt b/dmz/src/main/kotlin/ch/zindustries/dmz/TestServiceImpl.kt new file mode 100644 index 0000000..c8f73c3 --- /dev/null +++ b/dmz/src/main/kotlin/ch/zindustries/dmz/TestServiceImpl.kt @@ -0,0 +1,10 @@ +package ch.zindustries.dmz + +import ch.zindustries.dmz.api.TestService +import org.springframework.stereotype.Service + +@Service +class TestServiceImpl : TestService { + + override fun test() = "Test" +} From 5918770e4f06235fc195889d0654f8e9bbc102b4 Mon Sep 17 00:00:00 2001 From: Z-100 Date: Sun, 2 Jun 2024 14:54:48 +0200 Subject: [PATCH 06/24] feature(#5): Add auth module --- dmz-auth/pom.xml | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ pom.xml | 7 +++++++ 2 files changed, 58 insertions(+) create mode 100644 dmz-auth/pom.xml diff --git a/dmz-auth/pom.xml b/dmz-auth/pom.xml new file mode 100644 index 0000000..4fc3d37 --- /dev/null +++ b/dmz-auth/pom.xml @@ -0,0 +1,51 @@ + + + 4.0.0 + + + ch.zindustries.dmz + dmz-root + 1.0.0-SNAPSHOT + + + dmz-auth + + + + + + org.jetbrains.kotlin + kotlin-stdlib + + + + + + src/test/kotlin + + + + org.jetbrains.kotlin + kotlin-maven-plugin + + + + maven-surefire-plugin + 2.22.2 + + + + maven-failsafe-plugin + 2.22.2 + + + + org.codehaus.mojo + exec-maven-plugin + + + + + diff --git a/pom.xml b/pom.xml index 83f885f..4822463 100644 --- a/pom.xml +++ b/pom.xml @@ -21,6 +21,7 @@ dmz dmz-api dmz-app + dmz-auth dmz-rs-api dmz-rs-server @@ -53,6 +54,12 @@ 1.0.0-SNAPSHOT + + ch.zindustries.dmz + dmz-auth + 1.0.0-SNAPSHOT + + ch.zindustries.dmz dmz-rs-api From 0502a61b05710f959be03d102b026b1011691788 Mon Sep 17 00:00:00 2001 From: Z-100 Date: Sun, 2 Jun 2024 15:03:18 +0200 Subject: [PATCH 07/24] feature(#5): Add annotations --- dmz-auth/pom.xml | 7 +++++++ .../src/main/kotlin/ch/zindustries/dmz/auth/Roles.kt | 8 ++++++++ .../dmz/auth/annotations/AccessibleByAdmin.kt | 7 +++++++ .../dmz/auth/annotations/AccessibleByUser.kt | 7 +++++++ dmz-rs-api/pom.xml | 5 +++++ .../ch/zindustries/dmz/rs/api/TestResourceService.kt | 11 +++++++++-- .../dmz/rs/server/TestResourceServiceImpl.kt | 4 +++- 7 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/Roles.kt create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/annotations/AccessibleByAdmin.kt create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/annotations/AccessibleByUser.kt diff --git a/dmz-auth/pom.xml b/dmz-auth/pom.xml index 4fc3d37..c7b544f 100644 --- a/dmz-auth/pom.xml +++ b/dmz-auth/pom.xml @@ -14,6 +14,13 @@ + + + org.springframework.boot + spring-boot-starter-security + + + org.jetbrains.kotlin diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/Roles.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/Roles.kt new file mode 100644 index 0000000..be14c69 --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/Roles.kt @@ -0,0 +1,8 @@ +package ch.zindustries.dmz.auth + +class Roles { + companion object { + const val ADMIN = "admin" + const val USER = "user" + } +} diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/annotations/AccessibleByAdmin.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/annotations/AccessibleByAdmin.kt new file mode 100644 index 0000000..3952a3c --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/annotations/AccessibleByAdmin.kt @@ -0,0 +1,7 @@ +package ch.zindustries.dmz.auth.annotations + +import ch.zindustries.dmz.auth.Roles +import org.springframework.security.access.annotation.Secured + +@Secured(value = [Roles.ADMIN]) +annotation class AccessibleByAdmin diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/annotations/AccessibleByUser.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/annotations/AccessibleByUser.kt new file mode 100644 index 0000000..1dae586 --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/annotations/AccessibleByUser.kt @@ -0,0 +1,7 @@ +package ch.zindustries.dmz.auth.annotations + +import ch.zindustries.dmz.auth.Roles +import org.springframework.security.access.annotation.Secured + +@Secured(value = [Roles.ADMIN, Roles.USER]) +annotation class AccessibleByUser diff --git a/dmz-rs-api/pom.xml b/dmz-rs-api/pom.xml index 0ce1232..f00a129 100644 --- a/dmz-rs-api/pom.xml +++ b/dmz-rs-api/pom.xml @@ -20,6 +20,11 @@ dmz-api + + ch.zindustries.dmz + dmz-auth + + org.springframework.boot diff --git a/dmz-rs-api/src/main/kotlin/ch/zindustries/dmz/rs/api/TestResourceService.kt b/dmz-rs-api/src/main/kotlin/ch/zindustries/dmz/rs/api/TestResourceService.kt index 7762c78..7dff141 100644 --- a/dmz-rs-api/src/main/kotlin/ch/zindustries/dmz/rs/api/TestResourceService.kt +++ b/dmz-rs-api/src/main/kotlin/ch/zindustries/dmz/rs/api/TestResourceService.kt @@ -1,5 +1,7 @@ package ch.zindustries.dmz.rs.api +import ch.zindustries.dmz.auth.annotations.AccessibleByAdmin +import ch.zindustries.dmz.auth.annotations.AccessibleByUser import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestMapping @@ -7,6 +9,11 @@ import org.springframework.web.bind.annotation.RequestMapping @RequestMapping interface TestResourceService { - @GetMapping("test") - fun test() : ResponseEntity + @AccessibleByAdmin + @GetMapping("test-admin") + fun testAdmin() : ResponseEntity + + @AccessibleByUser + @GetMapping("test-user") + fun testUser() : ResponseEntity } diff --git a/dmz-rs-server/src/main/kotlin/ch/zindustries/dmz/rs/server/TestResourceServiceImpl.kt b/dmz-rs-server/src/main/kotlin/ch/zindustries/dmz/rs/server/TestResourceServiceImpl.kt index ce0b808..f362495 100644 --- a/dmz-rs-server/src/main/kotlin/ch/zindustries/dmz/rs/server/TestResourceServiceImpl.kt +++ b/dmz-rs-server/src/main/kotlin/ch/zindustries/dmz/rs/server/TestResourceServiceImpl.kt @@ -10,5 +10,7 @@ class TestResourceServiceImpl( private val testService: TestService ) : TestResourceService { - override fun test(): ResponseEntity = ResponseEntity.ok(testService.test()) + override fun testAdmin(): ResponseEntity = ResponseEntity.ok(testService.test()) + + override fun testUser(): ResponseEntity = ResponseEntity.ok(testService.test()) } From cc7abbba39130c043ba8d4ca3efb9434eebbfea7 Mon Sep 17 00:00:00 2001 From: Z-100 Date: Tue, 25 Jun 2024 15:37:13 +0200 Subject: [PATCH 08/24] feature(#5): Add user and right entities --- dmz-auth/pom.xml | 4 +++ .../ch/zindustries/dmz/auth/dtos/UserDTO.kt | 16 +++++++++ .../dmz/auth/entities/BaseEntity.kt | 27 +++++++++++++++ .../ch/zindustries/dmz/auth/entities/Right.kt | 15 ++++++++ .../ch/zindustries/dmz/auth/entities/User.kt | 34 +++++++++++++++++++ .../dmz/auth/repositories/RightRepository.kt | 9 +++++ .../dmz/auth/repositories/UserRepository.kt | 8 +++++ 7 files changed, 113 insertions(+) create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/UserDTO.kt create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/BaseEntity.kt create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Right.kt create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/User.kt create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/RightRepository.kt create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/UserRepository.kt diff --git a/dmz-auth/pom.xml b/dmz-auth/pom.xml index c7b544f..6b84651 100644 --- a/dmz-auth/pom.xml +++ b/dmz-auth/pom.xml @@ -20,6 +20,10 @@ spring-boot-starter-security + + org.springframework.boot + spring-boot-starter-data-jpa + diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/UserDTO.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/UserDTO.kt new file mode 100644 index 0000000..5947917 --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/UserDTO.kt @@ -0,0 +1,16 @@ +package ch.zindustries.dmz.auth.dtos + +import ch.zindustries.dmz.auth.entities.Right +import java.util.* + +data class UserDTO( + var id: Long, + var username: String, + var email: String, + var phone: String, + var multiFactorActivated: Boolean, + var rights: Set, + var roles: Set, + var createdAt: Date, + var updatedAt: Date +) diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/BaseEntity.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/BaseEntity.kt new file mode 100644 index 0000000..0e38ecb --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/BaseEntity.kt @@ -0,0 +1,27 @@ +package ch.zindustries.dmz.auth.entities + +import jakarta.persistence.* +import java.util.* + +@MappedSuperclass +open class BaseEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "id", nullable = false) + var id: Long? = null + + // TODO +// @Column +// var createdBy: User = User() + + // TODO +// @Column +// var modifiedBy: User = User() + + @Column + var createdAt: Date = Date() + + @Column + var updatedAt: Date = Date() +} diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Right.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Right.kt new file mode 100644 index 0000000..fbb8da4 --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Right.kt @@ -0,0 +1,15 @@ +package ch.zindustries.dmz.auth.entities + +import jakarta.persistence.* +import java.util.Date + +@Entity +@Table(name = "right") +class Right : BaseEntity() { + + @Column(length = 255) + var code: String = "" + + @ManyToMany(mappedBy = "rights") + var users: Set = HashSet() +} diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/User.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/User.kt new file mode 100644 index 0000000..6ea2ac7 --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/User.kt @@ -0,0 +1,34 @@ +package ch.zindustries.dmz.auth.entities + +import jakarta.persistence.* + +@Entity +@Table(name = "user") +class User : BaseEntity() { + + @Column(length = 255, nullable = false) + var username: String = "" + + @Column(length = 255, nullable = false) + var password: String = "" + + @Column(length = 255, nullable = false) + var email: String = "" + + @Column(length = 15) + var phone: String? = null + + @Column + var multiFactorActivated: Boolean = false + + @ManyToMany + @JoinTable( + name = "right_user", + joinColumns = [JoinColumn(name = "right_id")], + inverseJoinColumns = [JoinColumn(name = "user_id")], + ) + var rights: Set = HashSet() + + @ElementCollection + var roles: Set = HashSet() +} diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/RightRepository.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/RightRepository.kt new file mode 100644 index 0000000..0e1f861 --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/RightRepository.kt @@ -0,0 +1,9 @@ +package ch.zindustries.dmz.auth.repositories + +import ch.zindustries.dmz.auth.entities.Right +import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.stereotype.Repository +import java.util.LongSummaryStatistics + +@Repository +interface RightRepository : JpaRepository diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/UserRepository.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/UserRepository.kt new file mode 100644 index 0000000..9c61121 --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/UserRepository.kt @@ -0,0 +1,8 @@ +package ch.zindustries.dmz.auth.repositories + +import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.security.core.userdetails.User +import org.springframework.stereotype.Repository + +@Repository +interface UserRepository : JpaRepository From 47c9b2c948b48f894ee8fa73e7710056e6902b0c Mon Sep 17 00:00:00 2001 From: Z-100 Date: Tue, 25 Jun 2024 15:57:29 +0200 Subject: [PATCH 09/24] feature(#5): Add access control service + usage example --- .../exceptions/ServiceAccessDeniedException.kt | 13 +++++++++++++ .../dmz/auth/services/AccessControlService.kt | 14 ++++++++++++++ .../ch/zindustries/dmz/auth/types/ServiceType.kt | 8 ++++++++ .../ch/zindustries/dmz/auth/types/TestServices.kt | 12 ++++++++++++ dmz/pom.xml | 5 +++++ .../kotlin/ch/zindustries/dmz/TestServiceImpl.kt | 13 +++++++++++-- 6 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/exceptions/ServiceAccessDeniedException.kt create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/AccessControlService.kt create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/types/ServiceType.kt create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/types/TestServices.kt diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/exceptions/ServiceAccessDeniedException.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/exceptions/ServiceAccessDeniedException.kt new file mode 100644 index 0000000..539dc9a --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/exceptions/ServiceAccessDeniedException.kt @@ -0,0 +1,13 @@ +package ch.zindustries.dmz.auth.exceptions + +import org.springframework.security.access.AccessDeniedException + +class ServiceAccessDeniedException( + msg: String?, + throwable: Throwable?, +) : AccessDeniedException(msg, throwable) { + + constructor(msg: String?) : this(msg, null) + + constructor() : this(null, null) +} diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/AccessControlService.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/AccessControlService.kt new file mode 100644 index 0000000..4d10c84 --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/AccessControlService.kt @@ -0,0 +1,14 @@ +package ch.zindustries.dmz.auth.services + +import ch.zindustries.dmz.auth.exceptions.ServiceAccessDeniedException +import ch.zindustries.dmz.auth.types.ServiceType +import org.springframework.stereotype.Service + +@Service +class AccessControlService { + + fun assertCanAccessService(service: ServiceType) { + + throw ServiceAccessDeniedException("Cannot access ${service.serviceName()}") + } +} diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/types/ServiceType.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/types/ServiceType.kt new file mode 100644 index 0000000..4ebf9ab --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/types/ServiceType.kt @@ -0,0 +1,8 @@ +package ch.zindustries.dmz.auth.types + +interface ServiceType { + + fun serviceName(): String + + fun serviceId(): Long +} diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/types/TestServices.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/types/TestServices.kt new file mode 100644 index 0000000..633abb1 --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/types/TestServices.kt @@ -0,0 +1,12 @@ +package ch.zindustries.dmz.auth.types + +enum class TestServices( + private val serviceName: String, + private val serviceId: Long, +) : ServiceType { + TEST_SERVICE("testService", 0L); + + override fun serviceName() = this.serviceName + + override fun serviceId() = this.serviceId +} diff --git a/dmz/pom.xml b/dmz/pom.xml index bf5ed23..afdb3b2 100644 --- a/dmz/pom.xml +++ b/dmz/pom.xml @@ -20,6 +20,11 @@ dmz-api + + ch.zindustries.dmz + dmz-auth + + org.springframework.boot diff --git a/dmz/src/main/kotlin/ch/zindustries/dmz/TestServiceImpl.kt b/dmz/src/main/kotlin/ch/zindustries/dmz/TestServiceImpl.kt index c8f73c3..e27ff73 100644 --- a/dmz/src/main/kotlin/ch/zindustries/dmz/TestServiceImpl.kt +++ b/dmz/src/main/kotlin/ch/zindustries/dmz/TestServiceImpl.kt @@ -1,10 +1,19 @@ package ch.zindustries.dmz import ch.zindustries.dmz.api.TestService +import ch.zindustries.dmz.auth.services.AccessControlService +import ch.zindustries.dmz.auth.types.TestServices import org.springframework.stereotype.Service @Service -class TestServiceImpl : TestService { +class TestServiceImpl( + private val accessControl: AccessControlService, +) : TestService { - override fun test() = "Test" + override fun test(): String { + + accessControl.assertCanAccessService(TestServices.TEST_SERVICE) + + return "Test" + } } From 11cde591c9d8b4af5fbf299bfe81719c63a135b1 Mon Sep 17 00:00:00 2001 From: Z-100 Date: Thu, 22 Aug 2024 20:12:28 +0200 Subject: [PATCH 10/24] feature(#5): Add asserter --- .../ch/zindustries/dmz/app/DmzApplication.kt | 6 +++++- .../ch/zindustries/dmz/auth/dtos/RightDTO.kt | 6 ++++++ .../ch/zindustries/dmz/auth/dtos/UserDTO.kt | 3 +-- .../dmz/auth/dtos/utils/UserDTOUtil.kt | 15 +++++++++++++++ .../ch/zindustries/dmz/auth/entities/Right.kt | 9 +++++++-- .../auth/exceptions/SecurityContextException.kt | 13 +++++++++++++ .../dmz/auth/services/AccessControlService.kt | 11 +++++++++-- .../dmz/auth/services/SecurityContextHolder.kt | 15 +++++++++++++++ .../zindustries/dmz/rs/api/TestResourceService.kt | 6 +++--- 9 files changed, 74 insertions(+), 10 deletions(-) create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/RightDTO.kt create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/utils/UserDTOUtil.kt create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/exceptions/SecurityContextException.kt create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/SecurityContextHolder.kt diff --git a/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/DmzApplication.kt b/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/DmzApplication.kt index ef0052b..b57ba86 100644 --- a/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/DmzApplication.kt +++ b/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/DmzApplication.kt @@ -1,10 +1,14 @@ package ch.zindustries.dmz.app import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration import org.springframework.boot.runApplication @SpringBootApplication( - scanBasePackages = ["ch.zindustries.dmz"] + scanBasePackages = ["ch.zindustries.dmz"], + exclude = [ + SecurityAutoConfiguration::class, + ] ) open class DmzAppApplication diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/RightDTO.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/RightDTO.kt new file mode 100644 index 0000000..8a22e55 --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/RightDTO.kt @@ -0,0 +1,6 @@ +package ch.zindustries.dmz.auth.dtos + +data class RightDTO( + var name: String, + var code: String, +) diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/UserDTO.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/UserDTO.kt index 5947917..215e87f 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/UserDTO.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/UserDTO.kt @@ -1,6 +1,5 @@ package ch.zindustries.dmz.auth.dtos -import ch.zindustries.dmz.auth.entities.Right import java.util.* data class UserDTO( @@ -9,7 +8,7 @@ data class UserDTO( var email: String, var phone: String, var multiFactorActivated: Boolean, - var rights: Set, + var rights: Set, var roles: Set, var createdAt: Date, var updatedAt: Date diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/utils/UserDTOUtil.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/utils/UserDTOUtil.kt new file mode 100644 index 0000000..847f61a --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/utils/UserDTOUtil.kt @@ -0,0 +1,15 @@ +package ch.zindustries.dmz.auth.dtos.utils + +import ch.zindustries.dmz.auth.dtos.RightDTO +import ch.zindustries.dmz.auth.dtos.UserDTO +import ch.zindustries.dmz.auth.types.ServiceType + +class UserDTOUtil { + companion object { + + fun UserDTO.hasRight(right: RightDTO) = this.rights.contains(right) + + fun UserDTO.canAccessService(serviceRight: ServiceType) = + this.rights.find { it.name == serviceRight.serviceName() } != null + } +} diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Right.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Right.kt index fbb8da4..705f8a8 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Right.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Right.kt @@ -1,12 +1,17 @@ package ch.zindustries.dmz.auth.entities -import jakarta.persistence.* -import java.util.Date +import jakarta.persistence.Column +import jakarta.persistence.Entity +import jakarta.persistence.ManyToMany +import jakarta.persistence.Table @Entity @Table(name = "right") class Right : BaseEntity() { + @Column(length = 255) + var name: String = "" + @Column(length = 255) var code: String = "" diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/exceptions/SecurityContextException.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/exceptions/SecurityContextException.kt new file mode 100644 index 0000000..b819335 --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/exceptions/SecurityContextException.kt @@ -0,0 +1,13 @@ +package ch.zindustries.dmz.auth.exceptions + +import org.springframework.security.access.AccessDeniedException + +class SecurityContextException( + msg: String?, + throwable: Throwable?, +) : AccessDeniedException(msg, throwable) { + + constructor(msg: String?) : this(msg, null) + + constructor() : this(null, null) +} diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/AccessControlService.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/AccessControlService.kt index 4d10c84..ebf5321 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/AccessControlService.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/AccessControlService.kt @@ -1,14 +1,21 @@ package ch.zindustries.dmz.auth.services +import ch.zindustries.dmz.auth.dtos.utils.UserDTOUtil.Companion.canAccessService import ch.zindustries.dmz.auth.exceptions.ServiceAccessDeniedException import ch.zindustries.dmz.auth.types.ServiceType import org.springframework.stereotype.Service @Service -class AccessControlService { +class AccessControlService( + private val contextHolder: SecurityContextHolder, +) { fun assertCanAccessService(service: ServiceType) { - throw ServiceAccessDeniedException("Cannot access ${service.serviceName()}") + val user = contextHolder.getUser() + + if (!user.canAccessService(service)) { + throw ServiceAccessDeniedException("Cannot access ${service.serviceName()}") + } } } diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/SecurityContextHolder.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/SecurityContextHolder.kt new file mode 100644 index 0000000..4824f1b --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/SecurityContextHolder.kt @@ -0,0 +1,15 @@ +package ch.zindustries.dmz.auth.services + +import ch.zindustries.dmz.auth.dtos.UserDTO +import ch.zindustries.dmz.auth.exceptions.SecurityContextException +import org.springframework.security.core.context.SecurityContextHolder +import org.springframework.stereotype.Service + +@Service +class SecurityContextHolder { + + fun getUser(): UserDTO { + return SecurityContextHolder.getContext()?.authentication?.principal as UserDTO + ?: throw SecurityContextException("No security context found.") + } +} diff --git a/dmz-rs-api/src/main/kotlin/ch/zindustries/dmz/rs/api/TestResourceService.kt b/dmz-rs-api/src/main/kotlin/ch/zindustries/dmz/rs/api/TestResourceService.kt index 7dff141..a020e87 100644 --- a/dmz-rs-api/src/main/kotlin/ch/zindustries/dmz/rs/api/TestResourceService.kt +++ b/dmz-rs-api/src/main/kotlin/ch/zindustries/dmz/rs/api/TestResourceService.kt @@ -6,14 +6,14 @@ import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestMapping -@RequestMapping +@RequestMapping("test") interface TestResourceService { @AccessibleByAdmin @GetMapping("test-admin") - fun testAdmin() : ResponseEntity + fun testAdmin(): ResponseEntity @AccessibleByUser @GetMapping("test-user") - fun testUser() : ResponseEntity + fun testUser(): ResponseEntity } From 065c23bf77d313e36e70b6bc7e816c5875b69618 Mon Sep 17 00:00:00 2001 From: Z-100 Date: Fri, 23 Aug 2024 20:36:42 +0200 Subject: [PATCH 11/24] feature(#5): Add scanners and make pgsql work --- .../src/main/kotlin/ch/zindustries/dmz/app/DmzApplication.kt | 4 ++++ dmz-app/src/main/resources/application.yml | 2 +- docker/pgsql-compose.yml | 3 ++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/DmzApplication.kt b/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/DmzApplication.kt index b57ba86..7b219b0 100644 --- a/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/DmzApplication.kt +++ b/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/DmzApplication.kt @@ -1,8 +1,10 @@ package ch.zindustries.dmz.app import org.springframework.boot.autoconfigure.SpringBootApplication +import org.springframework.boot.autoconfigure.domain.EntityScan import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration import org.springframework.boot.runApplication +import org.springframework.data.jpa.repository.config.EnableJpaRepositories @SpringBootApplication( scanBasePackages = ["ch.zindustries.dmz"], @@ -10,6 +12,8 @@ import org.springframework.boot.runApplication SecurityAutoConfiguration::class, ] ) +@EntityScan("ch.zindustries.dmz") +@EnableJpaRepositories("ch.zindustries.dmz") open class DmzAppApplication fun main(args: Array) { diff --git a/dmz-app/src/main/resources/application.yml b/dmz-app/src/main/resources/application.yml index f46ffb4..a1ad8f9 100644 --- a/dmz-app/src/main/resources/application.yml +++ b/dmz-app/src/main/resources/application.yml @@ -12,4 +12,4 @@ spring: hibernate: dialect: org.hibernate.dialect.PostgreSQLDialect hibernate: - ddl-auto: validate + ddl-auto: update diff --git a/docker/pgsql-compose.yml b/docker/pgsql-compose.yml index 483e0fe..826b879 100644 --- a/docker/pgsql-compose.yml +++ b/docker/pgsql-compose.yml @@ -1,5 +1,6 @@ services: + # You might have to manually kill your pgsql service in task manager dmz_pgsql: container_name: dkr_postgres image: bitnami/postgresql:16.2.0 @@ -9,7 +10,7 @@ services: - POSTGRES_DB=dmz-pgsql - DB_ENABLE_SSL=false volumes: - - dmz_pgsql_vol:/data/postgres + - dmz_pgsql_vol:/var/lib/data/postgres ports: - "5432:5432" networks: From 07db8763544ce00699e16bee3868ee0c579fe6b8 Mon Sep 17 00:00:00 2001 From: Z-100 Date: Fri, 23 Aug 2024 20:37:10 +0200 Subject: [PATCH 12/24] feature(#5): Fix dependencies --- dmz-app/pom.xml | 8 +++++++- dmz-auth/pom.xml | 5 +++++ dmz/pom.xml | 6 ------ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/dmz-app/pom.xml b/dmz-app/pom.xml index 2b2186d..ba39114 100644 --- a/dmz-app/pom.xml +++ b/dmz-app/pom.xml @@ -20,9 +20,15 @@ dmz-rs-server + org.springframework.boot - spring-boot-starter-web + spring-boot-starter-data-jpa + + + + org.postgresql + postgresql diff --git a/dmz-auth/pom.xml b/dmz-auth/pom.xml index 6b84651..1c69d80 100644 --- a/dmz-auth/pom.xml +++ b/dmz-auth/pom.xml @@ -25,6 +25,11 @@ spring-boot-starter-data-jpa + + org.springframework.boot + spring-boot-starter-test + + org.jetbrains.kotlin diff --git a/dmz/pom.xml b/dmz/pom.xml index afdb3b2..7bcc283 100644 --- a/dmz/pom.xml +++ b/dmz/pom.xml @@ -25,12 +25,6 @@ dmz-auth - - - org.springframework.boot - spring-boot-starter-test - - From 7928cd712d339ab31f5af973e33142b253fbd06a Mon Sep 17 00:00:00 2001 From: Z-100 Date: Fri, 23 Aug 2024 21:15:14 +0200 Subject: [PATCH 13/24] feature(#5): Fix dependency issues with kotlin --- dmz-app/pom.xml | 11 +++++++++++ dmz-app/src/main/resources/application.yml | 2 +- .../dmz/auth/entities/{User.kt => Account.kt} | 12 ++++++------ .../dmz/auth/entities/{Right.kt => Authority.kt} | 8 ++++---- .../{UserRepository.kt => AccountRepository.kt} | 4 ++-- .../{RightRepository.kt => AuthorityRepository.kt} | 5 ++--- .../dmz/auth/services/SecurityContextHolder.kt | 2 +- dmz-rs-api/pom.xml | 6 ++++++ dmz-rs-server/pom.xml | 6 ++++++ dmz/pom.xml | 6 ++++++ pom.xml | 11 +++-------- 11 files changed, 48 insertions(+), 25 deletions(-) rename dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/{User.kt => Account.kt} (66%) rename dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/{Right.kt => Authority.kt} (67%) rename dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/{UserRepository.kt => AccountRepository.kt} (60%) rename dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/{RightRepository.kt => AuthorityRepository.kt} (51%) diff --git a/dmz-app/pom.xml b/dmz-app/pom.xml index ba39114..faff659 100644 --- a/dmz-app/pom.xml +++ b/dmz-app/pom.xml @@ -31,6 +31,17 @@ postgresql + + + org.jetbrains.kotlin + kotlin-stdlib + + + + org.jetbrains.kotlin + kotlin-reflect + + diff --git a/dmz-app/src/main/resources/application.yml b/dmz-app/src/main/resources/application.yml index a1ad8f9..ffc52a1 100644 --- a/dmz-app/src/main/resources/application.yml +++ b/dmz-app/src/main/resources/application.yml @@ -12,4 +12,4 @@ spring: hibernate: dialect: org.hibernate.dialect.PostgreSQLDialect hibernate: - ddl-auto: update + ddl-auto: create-drop diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/User.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Account.kt similarity index 66% rename from dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/User.kt rename to dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Account.kt index 6ea2ac7..38606f1 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/User.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Account.kt @@ -3,8 +3,8 @@ package ch.zindustries.dmz.auth.entities import jakarta.persistence.* @Entity -@Table(name = "user") -class User : BaseEntity() { +@Table(name = "account") +class Account : BaseEntity() { @Column(length = 255, nullable = false) var username: String = "" @@ -23,11 +23,11 @@ class User : BaseEntity() { @ManyToMany @JoinTable( - name = "right_user", - joinColumns = [JoinColumn(name = "right_id")], - inverseJoinColumns = [JoinColumn(name = "user_id")], + name = "authority_account", + joinColumns = [JoinColumn(name = "authority_id")], + inverseJoinColumns = [JoinColumn(name = "account_id")], ) - var rights: Set = HashSet() + var authorities: Set = HashSet() @ElementCollection var roles: Set = HashSet() diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Right.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Authority.kt similarity index 67% rename from dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Right.kt rename to dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Authority.kt index 705f8a8..f16c79f 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Right.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Authority.kt @@ -6,8 +6,8 @@ import jakarta.persistence.ManyToMany import jakarta.persistence.Table @Entity -@Table(name = "right") -class Right : BaseEntity() { +@Table(name = "authority") +class Authority : BaseEntity() { @Column(length = 255) var name: String = "" @@ -15,6 +15,6 @@ class Right : BaseEntity() { @Column(length = 255) var code: String = "" - @ManyToMany(mappedBy = "rights") - var users: Set = HashSet() + @ManyToMany(mappedBy = "authorities") + var accounts: Set = HashSet() } diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/UserRepository.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/AccountRepository.kt similarity index 60% rename from dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/UserRepository.kt rename to dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/AccountRepository.kt index 9c61121..8b943cd 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/UserRepository.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/AccountRepository.kt @@ -1,8 +1,8 @@ package ch.zindustries.dmz.auth.repositories +import ch.zindustries.dmz.auth.entities.Account import org.springframework.data.jpa.repository.JpaRepository -import org.springframework.security.core.userdetails.User import org.springframework.stereotype.Repository @Repository -interface UserRepository : JpaRepository +interface AccountRepository : JpaRepository diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/RightRepository.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/AuthorityRepository.kt similarity index 51% rename from dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/RightRepository.kt rename to dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/AuthorityRepository.kt index 0e1f861..7b5cf95 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/RightRepository.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/AuthorityRepository.kt @@ -1,9 +1,8 @@ package ch.zindustries.dmz.auth.repositories -import ch.zindustries.dmz.auth.entities.Right +import ch.zindustries.dmz.auth.entities.Authority import org.springframework.data.jpa.repository.JpaRepository import org.springframework.stereotype.Repository -import java.util.LongSummaryStatistics @Repository -interface RightRepository : JpaRepository +interface AuthorityRepository : JpaRepository diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/SecurityContextHolder.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/SecurityContextHolder.kt index 4824f1b..2ac0ad8 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/SecurityContextHolder.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/SecurityContextHolder.kt @@ -9,7 +9,7 @@ import org.springframework.stereotype.Service class SecurityContextHolder { fun getUser(): UserDTO { - return SecurityContextHolder.getContext()?.authentication?.principal as UserDTO + return SecurityContextHolder.getContext()?.authentication?.principal as UserDTO? ?: throw SecurityContextException("No security context found.") } } diff --git a/dmz-rs-api/pom.xml b/dmz-rs-api/pom.xml index f00a129..77a8cf7 100644 --- a/dmz-rs-api/pom.xml +++ b/dmz-rs-api/pom.xml @@ -31,6 +31,12 @@ spring-boot-starter-web + + + org.jetbrains.kotlin + kotlin-stdlib + + diff --git a/dmz-rs-server/pom.xml b/dmz-rs-server/pom.xml index 56ce056..7dffaed 100644 --- a/dmz-rs-server/pom.xml +++ b/dmz-rs-server/pom.xml @@ -25,6 +25,12 @@ dmz-rs-api + + + org.jetbrains.kotlin + kotlin-stdlib + + diff --git a/dmz/pom.xml b/dmz/pom.xml index 7bcc283..8a00cf3 100644 --- a/dmz/pom.xml +++ b/dmz/pom.xml @@ -25,6 +25,12 @@ dmz-auth + + + org.jetbrains.kotlin + kotlin-stdlib + + diff --git a/pom.xml b/pom.xml index 4822463..d73c8a0 100644 --- a/pom.xml +++ b/pom.xml @@ -75,15 +75,10 @@ org.jetbrains.kotlin - kotlin-stdlib + kotlin-bom ${kotlin.version} - - - - org.jetbrains.kotlin - kotlin-test-junit5 - ${kotlin.version} - test + pom + import From e441be561f31e9fa565cb10e3d77c9c55908b9d8 Mon Sep 17 00:00:00 2001 From: Z-100 Date: Tue, 27 Aug 2024 12:41:56 +0200 Subject: [PATCH 14/24] feature(#5): Upgrade Spring Boot --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index d73c8a0..0456335 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ org.springframework.boot spring-boot-starter-parent - 3.2.4 + 3.3.3 From f6ea285519eb660111c3cca5e0d36aa80b6b01eb Mon Sep 17 00:00:00 2001 From: Z-100 Date: Tue, 27 Aug 2024 13:16:46 +0200 Subject: [PATCH 15/24] feature(#5): Run configs --- .run/DmzAppApplication.run.xml | 16 ++++++++++++++++ .run/Docker - pgsql full.run.xml | 11 +++++++++++ 2 files changed, 27 insertions(+) create mode 100644 .run/DmzAppApplication.run.xml create mode 100644 .run/Docker - pgsql full.run.xml diff --git a/.run/DmzAppApplication.run.xml b/.run/DmzAppApplication.run.xml new file mode 100644 index 0000000..490afbd --- /dev/null +++ b/.run/DmzAppApplication.run.xml @@ -0,0 +1,16 @@ + + + + \ No newline at end of file diff --git a/.run/Docker - pgsql full.run.xml b/.run/Docker - pgsql full.run.xml new file mode 100644 index 0000000..189d8f4 --- /dev/null +++ b/.run/Docker - pgsql full.run.xml @@ -0,0 +1,11 @@ + + + + + + + + + \ No newline at end of file From da867d32bbf637c51641c04c8c903bf661bbc374 Mon Sep 17 00:00:00 2001 From: Z-100 Date: Tue, 27 Aug 2024 14:00:35 +0200 Subject: [PATCH 16/24] feature(#5): Add dtos and change to match structure --- dmz-auth/pom.xml | 6 +++++ .../kotlin/ch/zindustries/dmz/auth/Roles.kt | 1 + .../auth/annotations/AccessibleByAnonymous.kt | 7 ++++++ .../zindustries/dmz/auth/dtos/AccountDTO.kt | 22 +++++++++++++++++++ .../dtos/{RightDTO.kt => AuthorityDTO.kt} | 2 +- .../ch/zindustries/dmz/auth/dtos/DMZToken.kt | 7 ++++++ .../ch/zindustries/dmz/auth/dtos/UserDTO.kt | 15 ------------- .../dmz/auth/dtos/utils/UserDTOUtil.kt | 15 ------------- .../auth/repositories/AccountRepository.kt | 5 ++++- .../dmz/auth/services/AccessControlService.kt | 2 +- .../auth/services/SecurityContextHolder.kt | 6 ++--- pom.xml | 8 +++++++ 12 files changed, 60 insertions(+), 36 deletions(-) create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/annotations/AccessibleByAnonymous.kt create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/AccountDTO.kt rename dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/{RightDTO.kt => AuthorityDTO.kt} (77%) create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/DMZToken.kt delete mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/UserDTO.kt delete mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/utils/UserDTOUtil.kt diff --git a/dmz-auth/pom.xml b/dmz-auth/pom.xml index 1c69d80..bea10c7 100644 --- a/dmz-auth/pom.xml +++ b/dmz-auth/pom.xml @@ -30,6 +30,12 @@ spring-boot-starter-test + + + jakarta.servlet + jakarta.servlet-api + + org.jetbrains.kotlin diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/Roles.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/Roles.kt index be14c69..c478599 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/Roles.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/Roles.kt @@ -4,5 +4,6 @@ class Roles { companion object { const val ADMIN = "admin" const val USER = "user" + const val ANONYMOUS = "anonymous" } } diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/annotations/AccessibleByAnonymous.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/annotations/AccessibleByAnonymous.kt new file mode 100644 index 0000000..2b5356a --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/annotations/AccessibleByAnonymous.kt @@ -0,0 +1,7 @@ +package ch.zindustries.dmz.auth.annotations + +import ch.zindustries.dmz.auth.Roles +import org.springframework.security.access.annotation.Secured + +@Secured(value = [Roles.ANONYMOUS]) +annotation class AccessibleByAnonymous diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/AccountDTO.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/AccountDTO.kt new file mode 100644 index 0000000..380917b --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/AccountDTO.kt @@ -0,0 +1,22 @@ +package ch.zindustries.dmz.auth.dtos + +import ch.zindustries.dmz.auth.types.ServiceType +import java.util.* + +data class AccountDTO( + var id: Long, + var username: String, + var email: String, + var phone: String, + var multiFactorActivated: Boolean, + var authorities: Set, + var roles: Set, + var createdAt: Date, + var updatedAt: Date +) + + +fun AccountDTO.hasRight(right: AuthorityDTO) = this.authorities.contains(right) + +fun AccountDTO.canAccessService(serviceRight: ServiceType) = + this.authorities.find { it.name == serviceRight.serviceName() } != null diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/RightDTO.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/AuthorityDTO.kt similarity index 77% rename from dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/RightDTO.kt rename to dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/AuthorityDTO.kt index 8a22e55..e4a2ff3 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/RightDTO.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/AuthorityDTO.kt @@ -1,6 +1,6 @@ package ch.zindustries.dmz.auth.dtos -data class RightDTO( +data class AuthorityDTO( var name: String, var code: String, ) diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/DMZToken.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/DMZToken.kt new file mode 100644 index 0000000..4e13873 --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/DMZToken.kt @@ -0,0 +1,7 @@ +package ch.zindustries.dmz.auth.dtos + +data class DMZToken( + var access_token: String, + var refresh_token: String, + var expires_in: Int, +) diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/UserDTO.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/UserDTO.kt deleted file mode 100644 index 215e87f..0000000 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/UserDTO.kt +++ /dev/null @@ -1,15 +0,0 @@ -package ch.zindustries.dmz.auth.dtos - -import java.util.* - -data class UserDTO( - var id: Long, - var username: String, - var email: String, - var phone: String, - var multiFactorActivated: Boolean, - var rights: Set, - var roles: Set, - var createdAt: Date, - var updatedAt: Date -) diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/utils/UserDTOUtil.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/utils/UserDTOUtil.kt deleted file mode 100644 index 847f61a..0000000 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/utils/UserDTOUtil.kt +++ /dev/null @@ -1,15 +0,0 @@ -package ch.zindustries.dmz.auth.dtos.utils - -import ch.zindustries.dmz.auth.dtos.RightDTO -import ch.zindustries.dmz.auth.dtos.UserDTO -import ch.zindustries.dmz.auth.types.ServiceType - -class UserDTOUtil { - companion object { - - fun UserDTO.hasRight(right: RightDTO) = this.rights.contains(right) - - fun UserDTO.canAccessService(serviceRight: ServiceType) = - this.rights.find { it.name == serviceRight.serviceName() } != null - } -} diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/AccountRepository.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/AccountRepository.kt index 8b943cd..d3d0b9d 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/AccountRepository.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/repositories/AccountRepository.kt @@ -5,4 +5,7 @@ import org.springframework.data.jpa.repository.JpaRepository import org.springframework.stereotype.Repository @Repository -interface AccountRepository : JpaRepository +interface AccountRepository : JpaRepository { + + fun findByUsername(username: String): Account? +} diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/AccessControlService.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/AccessControlService.kt index ebf5321..12f58ee 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/AccessControlService.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/AccessControlService.kt @@ -1,6 +1,6 @@ package ch.zindustries.dmz.auth.services -import ch.zindustries.dmz.auth.dtos.utils.UserDTOUtil.Companion.canAccessService +import ch.zindustries.dmz.auth.dtos.canAccessService import ch.zindustries.dmz.auth.exceptions.ServiceAccessDeniedException import ch.zindustries.dmz.auth.types.ServiceType import org.springframework.stereotype.Service diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/SecurityContextHolder.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/SecurityContextHolder.kt index 2ac0ad8..5ad8ada 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/SecurityContextHolder.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/SecurityContextHolder.kt @@ -1,6 +1,6 @@ package ch.zindustries.dmz.auth.services -import ch.zindustries.dmz.auth.dtos.UserDTO +import ch.zindustries.dmz.auth.dtos.AccountDTO import ch.zindustries.dmz.auth.exceptions.SecurityContextException import org.springframework.security.core.context.SecurityContextHolder import org.springframework.stereotype.Service @@ -8,8 +8,8 @@ import org.springframework.stereotype.Service @Service class SecurityContextHolder { - fun getUser(): UserDTO { - return SecurityContextHolder.getContext()?.authentication?.principal as UserDTO? + fun getUser(): AccountDTO { + return SecurityContextHolder.getContext()?.authentication?.principal as AccountDTO? ?: throw SecurityContextException("No security context found.") } } diff --git a/pom.xml b/pom.xml index 0456335..57a11d2 100644 --- a/pom.xml +++ b/pom.xml @@ -72,6 +72,14 @@ 1.0.0-SNAPSHOT + + + jakarta.servlet + jakarta.servlet-api + 6.1.0 + provided + + org.jetbrains.kotlin From 11034fe2bf23390f809006ffbb561edecdbf7920 Mon Sep 17 00:00:00 2001 From: Z-100 Date: Tue, 27 Aug 2024 15:23:39 +0200 Subject: [PATCH 17/24] feature(#5): Hash password on save & add shitty service for default users --- dmz-api/pom.xml | 6 +++ .../ch/zindustries/dmz/api/TestService.kt | 4 ++ .../zindustries/dmz/auth/dtos/AccountDTO.kt | 2 +- .../zindustries/dmz/auth/entities/Account.kt | 6 ++- .../entities/sas/AccountEntityListener.kt | 23 ++++++++ .../dmz/rs/api/TestResourceService.kt | 7 +++ .../dmz/rs/server/TestResourceServiceImpl.kt | 3 ++ dmz/pom.xml | 5 -- .../ch/zindustries/dmz/TestServiceImpl.kt | 53 ++++++++++++++++++- 9 files changed, 101 insertions(+), 8 deletions(-) create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/sas/AccountEntityListener.kt diff --git a/dmz-api/pom.xml b/dmz-api/pom.xml index 3c258af..8f4d788 100644 --- a/dmz-api/pom.xml +++ b/dmz-api/pom.xml @@ -14,6 +14,12 @@ + + + ch.zindustries.dmz + dmz-auth + + org.jetbrains.kotlin diff --git a/dmz-api/src/main/kotlin/ch/zindustries/dmz/api/TestService.kt b/dmz-api/src/main/kotlin/ch/zindustries/dmz/api/TestService.kt index e570a1a..969e59b 100644 --- a/dmz-api/src/main/kotlin/ch/zindustries/dmz/api/TestService.kt +++ b/dmz-api/src/main/kotlin/ch/zindustries/dmz/api/TestService.kt @@ -1,6 +1,10 @@ package ch.zindustries.dmz.api +import ch.zindustries.dmz.auth.dtos.AccountDTO + interface TestService { fun test(): String + + fun createTestUsers(): List } diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/AccountDTO.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/AccountDTO.kt index 380917b..124f2ff 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/AccountDTO.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/AccountDTO.kt @@ -7,7 +7,7 @@ data class AccountDTO( var id: Long, var username: String, var email: String, - var phone: String, + var phone: String?, var multiFactorActivated: Boolean, var authorities: Set, var roles: Set, diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Account.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Account.kt index 38606f1..43cc4af 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Account.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Account.kt @@ -1,9 +1,11 @@ package ch.zindustries.dmz.auth.entities +import ch.zindustries.dmz.auth.entities.sas.AccountEntityListener import jakarta.persistence.* @Entity @Table(name = "account") +@EntityListeners(AccountEntityListener::class) class Account : BaseEntity() { @Column(length = 255, nullable = false) @@ -21,7 +23,9 @@ class Account : BaseEntity() { @Column var multiFactorActivated: Boolean = false - @ManyToMany + @ManyToMany( + cascade = [CascadeType.ALL], + ) @JoinTable( name = "authority_account", joinColumns = [JoinColumn(name = "authority_id")], diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/sas/AccountEntityListener.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/sas/AccountEntityListener.kt new file mode 100644 index 0000000..0902a96 --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/sas/AccountEntityListener.kt @@ -0,0 +1,23 @@ +package ch.zindustries.dmz.auth.entities.sas + +import ch.zindustries.dmz.auth.entities.Account +import jakarta.persistence.PrePersist +import jakarta.persistence.PreUpdate +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.context.annotation.Lazy +import org.springframework.security.crypto.password.PasswordEncoder +import org.springframework.stereotype.Component + +@Component +class AccountEntityListener { + + @Lazy + @Autowired + private lateinit var passwordEncoder: PasswordEncoder + + @PrePersist + @PreUpdate + fun hashPassword(account: Account) { + account.password = passwordEncoder.encode(account.password) + } +} diff --git a/dmz-rs-api/src/main/kotlin/ch/zindustries/dmz/rs/api/TestResourceService.kt b/dmz-rs-api/src/main/kotlin/ch/zindustries/dmz/rs/api/TestResourceService.kt index a020e87..d026e45 100644 --- a/dmz-rs-api/src/main/kotlin/ch/zindustries/dmz/rs/api/TestResourceService.kt +++ b/dmz-rs-api/src/main/kotlin/ch/zindustries/dmz/rs/api/TestResourceService.kt @@ -1,9 +1,12 @@ package ch.zindustries.dmz.rs.api import ch.zindustries.dmz.auth.annotations.AccessibleByAdmin +import ch.zindustries.dmz.auth.annotations.AccessibleByAnonymous import ch.zindustries.dmz.auth.annotations.AccessibleByUser +import ch.zindustries.dmz.auth.dtos.AccountDTO import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestMapping @RequestMapping("test") @@ -16,4 +19,8 @@ interface TestResourceService { @AccessibleByUser @GetMapping("test-user") fun testUser(): ResponseEntity + + @AccessibleByAnonymous + @PostMapping("create-test-users") + fun createTestUsers(): ResponseEntity> } diff --git a/dmz-rs-server/src/main/kotlin/ch/zindustries/dmz/rs/server/TestResourceServiceImpl.kt b/dmz-rs-server/src/main/kotlin/ch/zindustries/dmz/rs/server/TestResourceServiceImpl.kt index f362495..2b4f441 100644 --- a/dmz-rs-server/src/main/kotlin/ch/zindustries/dmz/rs/server/TestResourceServiceImpl.kt +++ b/dmz-rs-server/src/main/kotlin/ch/zindustries/dmz/rs/server/TestResourceServiceImpl.kt @@ -1,6 +1,7 @@ package ch.zindustries.dmz.rs.server import ch.zindustries.dmz.api.TestService +import ch.zindustries.dmz.auth.dtos.AccountDTO import ch.zindustries.dmz.rs.api.TestResourceService import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.RestController @@ -13,4 +14,6 @@ class TestResourceServiceImpl( override fun testAdmin(): ResponseEntity = ResponseEntity.ok(testService.test()) override fun testUser(): ResponseEntity = ResponseEntity.ok(testService.test()) + + override fun createTestUsers(): ResponseEntity> = ResponseEntity.ok(testService.createTestUsers()) } diff --git a/dmz/pom.xml b/dmz/pom.xml index 8a00cf3..31e8735 100644 --- a/dmz/pom.xml +++ b/dmz/pom.xml @@ -20,11 +20,6 @@ dmz-api - - ch.zindustries.dmz - dmz-auth - - org.jetbrains.kotlin diff --git a/dmz/src/main/kotlin/ch/zindustries/dmz/TestServiceImpl.kt b/dmz/src/main/kotlin/ch/zindustries/dmz/TestServiceImpl.kt index e27ff73..7decf75 100644 --- a/dmz/src/main/kotlin/ch/zindustries/dmz/TestServiceImpl.kt +++ b/dmz/src/main/kotlin/ch/zindustries/dmz/TestServiceImpl.kt @@ -1,13 +1,20 @@ package ch.zindustries.dmz import ch.zindustries.dmz.api.TestService +import ch.zindustries.dmz.auth.dtos.AccountDTO +import ch.zindustries.dmz.auth.dtos.AuthorityDTO +import ch.zindustries.dmz.auth.entities.Account +import ch.zindustries.dmz.auth.entities.Authority +import ch.zindustries.dmz.auth.repositories.AccountRepository import ch.zindustries.dmz.auth.services.AccessControlService import ch.zindustries.dmz.auth.types.TestServices import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional @Service -class TestServiceImpl( +open class TestServiceImpl( private val accessControl: AccessControlService, + private val accountRepository: AccountRepository, ) : TestService { override fun test(): String { @@ -16,4 +23,48 @@ class TestServiceImpl( return "Test" } + + @Transactional + override fun createTestUsers(): List { + + val admin = Account() + admin.username = "admin" + admin.password = "admin" + admin.email = "admin@zindustries.ch" + admin.phone = null + admin.multiFactorActivated = false + val testAuthority = Authority() + testAuthority.name = "test" + testAuthority.code = "1" + admin.authorities = setOf(testAuthority) + admin.roles = setOf("ROLE_ADMIN", "ROLE_USER") + + val user = Account() + user.username = "user" + user.password = "user" + user.email = "user@zindustries.ch" + user.phone = null + user.multiFactorActivated = false + val testAuthority1 = Authority() + testAuthority1.name = "test1" + testAuthority1.code = "2" + user.authorities = setOf(testAuthority1) + user.roles = setOf("ROLE_USER") + + val saveAll = accountRepository.saveAll(listOf(admin, user)) + + return saveAll.map { + AccountDTO( + it.id!!, + it.username, + it.email, + it.phone, + it.multiFactorActivated, + it.authorities.map { auth -> AuthorityDTO(auth.name, auth.code) }.toSet(), + it.roles, + it.createdAt, + it.updatedAt, + ) + } + } } From b480fb6e551c37f6ea274bf0882d43fad72c7eb3 Mon Sep 17 00:00:00 2001 From: Z-100 Date: Tue, 27 Aug 2024 16:14:21 +0200 Subject: [PATCH 18/24] feature(#5): Add filters, security config & user details service and dependencies for jwt --- .../dmz/app/configs/SecurityConfig.kt | 57 +++++++++++++++++++ dmz-auth/pom.xml | 5 ++ .../ch/zindustries/dmz/auth/dtos/DMZToken.kt | 1 + .../dmz/auth/filters/AuthenticationFilter.kt | 50 ++++++++++++++++ .../dmz/auth/filters/AuthorizationFilter.kt | 23 ++++++++ .../dmz/auth/services/UserDetailsService.kt | 33 +++++++++++ pom.xml | 24 +++++--- 7 files changed, 186 insertions(+), 7 deletions(-) create mode 100644 dmz-app/src/main/kotlin/ch/zindustries/dmz/app/configs/SecurityConfig.kt create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/filters/AuthenticationFilter.kt create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/filters/AuthorizationFilter.kt create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/UserDetailsService.kt diff --git a/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/configs/SecurityConfig.kt b/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/configs/SecurityConfig.kt new file mode 100644 index 0000000..13060bf --- /dev/null +++ b/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/configs/SecurityConfig.kt @@ -0,0 +1,57 @@ +package ch.zindustries.dmz.app.configs + +import ch.zindustries.dmz.auth.filters.AuthenticationFilter +import ch.zindustries.dmz.auth.filters.AuthorizationFilter +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.security.authentication.AuthenticationManager +import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration +import org.springframework.security.config.annotation.web.builders.HttpSecurity +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity +import org.springframework.security.core.userdetails.UserDetailsService +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder +import org.springframework.security.crypto.password.PasswordEncoder +import org.springframework.security.web.SecurityFilterChain +import org.springframework.security.web.authentication.www.BasicAuthenticationFilter + +@Configuration +@EnableWebSecurity +open class SecurityConfig( + private val authConfig: AuthenticationConfiguration, + private val userDetailsService: UserDetailsService +) { + + @SuppressWarnings("PrivatePropertyUnderscore") + private val AUTH_WHITELIST = arrayOf( + "/anonymous/**", + "/test/**" + ) + + @Bean + open fun filterChain(httpSecurity: HttpSecurity): SecurityFilterChain { + + val jwtAuthenticationFilter = AuthenticationFilter(authenticationManager()) + jwtAuthenticationFilter.setFilterProcessesUrl("/auth/login") + + val authorizationFilter = AuthorizationFilter(userDetailsService) + + httpSecurity + .csrf { it.disable() } + .httpBasic { it.disable() } + .formLogin { it.disable() } + .authorizeHttpRequests { + it.requestMatchers(*AUTH_WHITELIST).permitAll() + .anyRequest().authenticated() + } + .addFilterBefore(jwtAuthenticationFilter, BasicAuthenticationFilter::class.java) + .addFilterAfter(authorizationFilter, AuthenticationFilter::class.java) + + return httpSecurity.build() + } + + @Bean + open fun authenticationManager(): AuthenticationManager = authConfig.authenticationManager + + @Bean + open fun passwordEncoder(): PasswordEncoder = BCryptPasswordEncoder() +} diff --git a/dmz-auth/pom.xml b/dmz-auth/pom.xml index bea10c7..ba6f647 100644 --- a/dmz-auth/pom.xml +++ b/dmz-auth/pom.xml @@ -36,6 +36,11 @@ jakarta.servlet-api + + io.jsonwebtoken + jjwt-api + + org.jetbrains.kotlin diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/DMZToken.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/DMZToken.kt index 4e13873..0c211c0 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/DMZToken.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/DMZToken.kt @@ -1,5 +1,6 @@ package ch.zindustries.dmz.auth.dtos +// TODO: Replace by keycloak stuff data class DMZToken( var access_token: String, var refresh_token: String, diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/filters/AuthenticationFilter.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/filters/AuthenticationFilter.kt new file mode 100644 index 0000000..406ff19 --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/filters/AuthenticationFilter.kt @@ -0,0 +1,50 @@ +package ch.zindustries.dmz.auth.filters + +import ch.zindustries.dmz.auth.exceptions.SecurityContextException +import jakarta.servlet.FilterChain +import jakarta.servlet.http.HttpServletRequest +import jakarta.servlet.http.HttpServletResponse +import org.springframework.security.authentication.AuthenticationManager +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken +import org.springframework.security.core.Authentication +import org.springframework.security.core.AuthenticationException +import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter +import org.springframework.stereotype.Service + +@Service +class AuthenticationFilter( + authenticationManager: AuthenticationManager, +) : UsernamePasswordAuthenticationFilter(authenticationManager) { + + override fun attemptAuthentication(request: HttpServletRequest?, response: HttpServletResponse?): Authentication { + + val username = super.obtainUsername(request) + val password = super.obtainPassword(request) + + if (username == null || password == null) { + throw SecurityContextException("Username and password must not be null") + } + + val unauthenticatedToken = UsernamePasswordAuthenticationToken.unauthenticated(username, password) + setDetails(request, unauthenticatedToken) + + return authenticationManager.authenticate(unauthenticatedToken) + } + + override fun successfulAuthentication( + request: HttpServletRequest?, + response: HttpServletResponse?, + chain: FilterChain?, + authResult: Authentication? + ) { + super.successfulAuthentication(request, response, chain, authResult) + } + + override fun unsuccessfulAuthentication( + request: HttpServletRequest?, + response: HttpServletResponse?, + failed: AuthenticationException? + ) { + super.unsuccessfulAuthentication(request, response, failed) + } +} diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/filters/AuthorizationFilter.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/filters/AuthorizationFilter.kt new file mode 100644 index 0000000..4b17e25 --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/filters/AuthorizationFilter.kt @@ -0,0 +1,23 @@ +package ch.zindustries.dmz.auth.filters + +import jakarta.servlet.FilterChain +import jakarta.servlet.http.HttpServletRequest +import jakarta.servlet.http.HttpServletResponse +import org.springframework.security.core.userdetails.UserDetailsService +import org.springframework.stereotype.Service +import org.springframework.web.filter.OncePerRequestFilter + +@Service +class AuthorizationFilter( + userDetailsService: UserDetailsService, +) : OncePerRequestFilter() { + + override fun doFilterInternal( + request: HttpServletRequest, + response: HttpServletResponse, + filterChain: FilterChain + ) { + // Do nothing + filterChain.doFilter(request, response) + } +} diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/UserDetailsService.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/UserDetailsService.kt new file mode 100644 index 0000000..84eb46a --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/UserDetailsService.kt @@ -0,0 +1,33 @@ +package ch.zindustries.dmz.auth.services + +import ch.zindustries.dmz.auth.exceptions.SecurityContextException +import ch.zindustries.dmz.auth.repositories.AccountRepository +import org.springframework.security.core.authority.SimpleGrantedAuthority +import org.springframework.security.core.userdetails.User +import org.springframework.security.core.userdetails.UserDetails +import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional +import org.springframework.security.core.userdetails.UserDetailsService as SpringUserDetailsService + +@Service +open class UserDetailsService( + private val accountRepository: AccountRepository +) : SpringUserDetailsService { + + @Transactional + override fun loadUserByUsername(username: String?): UserDetails { + + if (username.isNullOrEmpty()) { + throw SecurityContextException("Username cannot be null or empty") + } + + val account = accountRepository.findByUsername(username) + ?: throw SecurityContextException("No user with username '$username' found.") + + return User( + account.username, + account.password, + account.authorities.map { SimpleGrantedAuthority(it.name) } + ) + } +} diff --git a/pom.xml b/pom.xml index 57a11d2..ef4cae0 100644 --- a/pom.xml +++ b/pom.xml @@ -27,6 +27,10 @@ + + 0.12.6 + 6.1.0 + UTF-8 official 1.17 @@ -39,47 +43,53 @@ ch.zindustries.dmz dmz - 1.0.0-SNAPSHOT + ${project.version} ch.zindustries.dmz dmz-api - 1.0.0-SNAPSHOT + ${project.version} ch.zindustries.dmz dmz-app - 1.0.0-SNAPSHOT + ${project.version} ch.zindustries.dmz dmz-auth - 1.0.0-SNAPSHOT + ${project.version} ch.zindustries.dmz dmz-rs-api - 1.0.0-SNAPSHOT + ${project.version} ch.zindustries.dmz dmz-rs-server - 1.0.0-SNAPSHOT + ${project.version} jakarta.servlet jakarta.servlet-api - 6.1.0 + ${jakarta.servlet-api.version} provided + + io.jsonwebtoken + jjwt-api + ${jjwt-api.version} + + org.jetbrains.kotlin From 969848a50c28fbb4c73b5b489217b001c7aedd8f Mon Sep 17 00:00:00 2001 From: Z-100 Date: Tue, 27 Aug 2024 16:18:20 +0200 Subject: [PATCH 19/24] feature(#5): Fix package name --- dmz-app/src/main/resources/application.yml | 2 +- .../src/main/kotlin/ch/zindustries/dmz/auth/entities/Account.kt | 2 +- .../auth/entities/{sas => listeners}/AccountEntityListener.kt | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) rename dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/{sas => listeners}/AccountEntityListener.kt (92%) diff --git a/dmz-app/src/main/resources/application.yml b/dmz-app/src/main/resources/application.yml index ffc52a1..a1ad8f9 100644 --- a/dmz-app/src/main/resources/application.yml +++ b/dmz-app/src/main/resources/application.yml @@ -12,4 +12,4 @@ spring: hibernate: dialect: org.hibernate.dialect.PostgreSQLDialect hibernate: - ddl-auto: create-drop + ddl-auto: update diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Account.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Account.kt index 43cc4af..b42300e 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Account.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Account.kt @@ -1,6 +1,6 @@ package ch.zindustries.dmz.auth.entities -import ch.zindustries.dmz.auth.entities.sas.AccountEntityListener +import ch.zindustries.dmz.auth.entities.listeners.AccountEntityListener import jakarta.persistence.* @Entity diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/sas/AccountEntityListener.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/listeners/AccountEntityListener.kt similarity index 92% rename from dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/sas/AccountEntityListener.kt rename to dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/listeners/AccountEntityListener.kt index 0902a96..319d782 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/sas/AccountEntityListener.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/listeners/AccountEntityListener.kt @@ -1,4 +1,4 @@ -package ch.zindustries.dmz.auth.entities.sas +package ch.zindustries.dmz.auth.entities.listeners import ch.zindustries.dmz.auth.entities.Account import jakarta.persistence.PrePersist From 92e0a46acc0bcce42449b86283c46af3b70f7a6f Mon Sep 17 00:00:00 2001 From: Z-100 Date: Tue, 27 Aug 2024 16:55:24 +0200 Subject: [PATCH 20/24] feature(#5): Add custom principal to sec context --- dmz-auth/pom.xml | 4 ++-- .../dmz/auth/dtos/DMZUserDetails.kt | 21 +++++++++++++++++ .../dmz/auth/services/UserDetailsService.kt | 23 ++++++++++++++----- pom.xml | 8 +++---- 4 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/DMZUserDetails.kt diff --git a/dmz-auth/pom.xml b/dmz-auth/pom.xml index ba6f647..366c10a 100644 --- a/dmz-auth/pom.xml +++ b/dmz-auth/pom.xml @@ -37,8 +37,8 @@ - io.jsonwebtoken - jjwt-api + com.auth0 + java-jwt diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/DMZUserDetails.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/DMZUserDetails.kt new file mode 100644 index 0000000..c2fb0dc --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/DMZUserDetails.kt @@ -0,0 +1,21 @@ +package ch.zindustries.dmz.auth.dtos + +import org.springframework.security.core.authority.SimpleGrantedAuthority +import org.springframework.security.core.userdetails.User + +class DMZUserDetails( + account: AccountDTO, + password: String?, + enabled: Boolean = true, + accountNonExpired: Boolean = true, + credentialsNonExpired: Boolean = true, + accountNonLocked: Boolean = true, +) : User( + account.username, + password, + enabled, + accountNonExpired, + credentialsNonExpired, + accountNonLocked, + account.authorities.map { SimpleGrantedAuthority(it.name) } +) diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/UserDetailsService.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/UserDetailsService.kt index 84eb46a..70dc0bf 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/UserDetailsService.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/UserDetailsService.kt @@ -1,9 +1,10 @@ package ch.zindustries.dmz.auth.services +import ch.zindustries.dmz.auth.dtos.AccountDTO +import ch.zindustries.dmz.auth.dtos.AuthorityDTO +import ch.zindustries.dmz.auth.dtos.DMZUserDetails import ch.zindustries.dmz.auth.exceptions.SecurityContextException import ch.zindustries.dmz.auth.repositories.AccountRepository -import org.springframework.security.core.authority.SimpleGrantedAuthority -import org.springframework.security.core.userdetails.User import org.springframework.security.core.userdetails.UserDetails import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @@ -24,10 +25,20 @@ open class UserDetailsService( val account = accountRepository.findByUsername(username) ?: throw SecurityContextException("No user with username '$username' found.") - return User( - account.username, - account.password, - account.authorities.map { SimpleGrantedAuthority(it.name) } + // TODO: Replace by mapstruct + return DMZUserDetails( + AccountDTO( + account.id!!, + account.username, + account.email, + account.phone, + account.multiFactorActivated, + account.authorities.map { AuthorityDTO(it.name, it.code) }.toSet(), + account.roles, + account.createdAt, + account.updatedAt, + ), + account.password ) } } diff --git a/pom.xml b/pom.xml index ef4cae0..d6a2c16 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ - 0.12.6 + 4.4.0 6.1.0 UTF-8 @@ -85,9 +85,9 @@ - io.jsonwebtoken - jjwt-api - ${jjwt-api.version} + com.auth0 + java-jwt + ${java-jwt.version} From 17e75b7a2f83bda9a470496ba644d89c91df9f58 Mon Sep 17 00:00:00 2001 From: Z-100 Date: Tue, 27 Aug 2024 18:40:41 +0200 Subject: [PATCH 21/24] feature(#5): Add full authentication for pgsql-users --- .../dmz/app/configs/SecurityConfig.kt | 17 ++++++----- dmz-auth/pom.xml | 6 ++++ .../dmz/auth/dtos/DMZUserDetails.kt | 2 +- .../dmz/auth/filters/AuthenticationFilter.kt | 30 +++++++++++++------ .../dmz/auth/services/JWTService.kt | 21 +++++++++++++ 5 files changed, 58 insertions(+), 18 deletions(-) create mode 100644 dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/JWTService.kt diff --git a/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/configs/SecurityConfig.kt b/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/configs/SecurityConfig.kt index 13060bf..478a3d1 100644 --- a/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/configs/SecurityConfig.kt +++ b/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/configs/SecurityConfig.kt @@ -16,21 +16,20 @@ import org.springframework.security.web.authentication.www.BasicAuthenticationFi @Configuration @EnableWebSecurity -open class SecurityConfig( - private val authConfig: AuthenticationConfiguration, - private val userDetailsService: UserDetailsService -) { +open class SecurityConfig { - @SuppressWarnings("PrivatePropertyUnderscore") private val AUTH_WHITELIST = arrayOf( "/anonymous/**", "/test/**" ) @Bean - open fun filterChain(httpSecurity: HttpSecurity): SecurityFilterChain { + open fun filterChain( + httpSecurity: HttpSecurity, + userDetailsService: UserDetailsService, + jwtAuthenticationFilter: AuthenticationFilter, + ): SecurityFilterChain { - val jwtAuthenticationFilter = AuthenticationFilter(authenticationManager()) jwtAuthenticationFilter.setFilterProcessesUrl("/auth/login") val authorizationFilter = AuthorizationFilter(userDetailsService) @@ -50,7 +49,9 @@ open class SecurityConfig( } @Bean - open fun authenticationManager(): AuthenticationManager = authConfig.authenticationManager + open fun authenticationManager( + authConfig: AuthenticationConfiguration + ): AuthenticationManager = authConfig.authenticationManager @Bean open fun passwordEncoder(): PasswordEncoder = BCryptPasswordEncoder() diff --git a/dmz-auth/pom.xml b/dmz-auth/pom.xml index 366c10a..c3d476c 100644 --- a/dmz-auth/pom.xml +++ b/dmz-auth/pom.xml @@ -41,6 +41,12 @@ java-jwt + + com.google.code.gson + gson + + + org.jetbrains.kotlin diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/DMZUserDetails.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/DMZUserDetails.kt index c2fb0dc..26ab94c 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/DMZUserDetails.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/dtos/DMZUserDetails.kt @@ -4,7 +4,7 @@ import org.springframework.security.core.authority.SimpleGrantedAuthority import org.springframework.security.core.userdetails.User class DMZUserDetails( - account: AccountDTO, + var account: AccountDTO, password: String?, enabled: Boolean = true, accountNonExpired: Boolean = true, diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/filters/AuthenticationFilter.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/filters/AuthenticationFilter.kt index 406ff19..86721d7 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/filters/AuthenticationFilter.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/filters/AuthenticationFilter.kt @@ -1,21 +1,28 @@ package ch.zindustries.dmz.auth.filters +import ch.zindustries.dmz.auth.dtos.DMZToken +import ch.zindustries.dmz.auth.dtos.DMZUserDetails import ch.zindustries.dmz.auth.exceptions.SecurityContextException +import ch.zindustries.dmz.auth.services.JWTService +import com.google.gson.Gson import jakarta.servlet.FilterChain import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletResponse import org.springframework.security.authentication.AuthenticationManager import org.springframework.security.authentication.UsernamePasswordAuthenticationToken import org.springframework.security.core.Authentication -import org.springframework.security.core.AuthenticationException import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter import org.springframework.stereotype.Service +import java.io.BufferedWriter +import java.io.OutputStreamWriter @Service class AuthenticationFilter( authenticationManager: AuthenticationManager, ) : UsernamePasswordAuthenticationFilter(authenticationManager) { + private val jwtService: JWTService = JWTService() + override fun attemptAuthentication(request: HttpServletRequest?, response: HttpServletResponse?): Authentication { val username = super.obtainUsername(request) @@ -37,14 +44,19 @@ class AuthenticationFilter( chain: FilterChain?, authResult: Authentication? ) { - super.successfulAuthentication(request, response, chain, authResult) - } - override fun unsuccessfulAuthentication( - request: HttpServletRequest?, - response: HttpServletResponse?, - failed: AuthenticationException? - ) { - super.unsuccessfulAuthentication(request, response, failed) + val userDetails = authResult!!.principal as DMZUserDetails + val account = userDetails.account + + val accessToken = jwtService.generateToken(account) + val refreshToken = "TODO" + val expiresIn = 60 + + val dmzToken = DMZToken(accessToken, refreshToken, expiresIn) + + response!!.addHeader("Content-Type", "application/json") + BufferedWriter(OutputStreamWriter(response.outputStream)).use { + it.write(Gson().toJson(dmzToken)) + } } } diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/JWTService.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/JWTService.kt new file mode 100644 index 0000000..b9ef9e7 --- /dev/null +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/services/JWTService.kt @@ -0,0 +1,21 @@ +package ch.zindustries.dmz.auth.services + +import ch.zindustries.dmz.auth.dtos.AccountDTO +import com.auth0.jwt.JWT +import com.auth0.jwt.algorithms.Algorithm +import java.util.* + +class JWTService { + + fun generateToken(account: AccountDTO): String { + + val token = JWT.create() + .withIssuer("DMZ - JWTService") + .withExpiresAt(Date(System.currentTimeMillis() + 60 * 1000)) + .withSubject(account.username) + .withClaim("authorities", account.authorities.map { it.name }) + .sign(Algorithm.HMAC256("secret :)")) + + return token + } +} From fcade00c3cf711b7b17e662036d4901c8434d0ba Mon Sep 17 00:00:00 2001 From: Z-100 Date: Tue, 27 Aug 2024 19:49:58 +0200 Subject: [PATCH 22/24] feature(#5): Enable global method security --- .../kotlin/ch/zindustries/dmz/app/DmzApplication.kt | 2 ++ .../ch/zindustries/dmz/app/configs/SecurityConfig.kt | 10 +++++----- .../dmz/rs/server/TestResourceServiceImpl.kt | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/DmzApplication.kt b/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/DmzApplication.kt index 7b219b0..8f5100a 100644 --- a/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/DmzApplication.kt +++ b/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/DmzApplication.kt @@ -5,6 +5,7 @@ import org.springframework.boot.autoconfigure.domain.EntityScan import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration import org.springframework.boot.runApplication import org.springframework.data.jpa.repository.config.EnableJpaRepositories +import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity @SpringBootApplication( scanBasePackages = ["ch.zindustries.dmz"], @@ -14,6 +15,7 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories ) @EntityScan("ch.zindustries.dmz") @EnableJpaRepositories("ch.zindustries.dmz") +@EnableMethodSecurity(securedEnabled = true) open class DmzAppApplication fun main(args: Array) { diff --git a/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/configs/SecurityConfig.kt b/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/configs/SecurityConfig.kt index 478a3d1..ee6d829 100644 --- a/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/configs/SecurityConfig.kt +++ b/dmz-app/src/main/kotlin/ch/zindustries/dmz/app/configs/SecurityConfig.kt @@ -27,12 +27,12 @@ open class SecurityConfig { open fun filterChain( httpSecurity: HttpSecurity, userDetailsService: UserDetailsService, - jwtAuthenticationFilter: AuthenticationFilter, + authenticationFilter: AuthenticationFilter, + authorizationFilter: AuthorizationFilter ): SecurityFilterChain { - jwtAuthenticationFilter.setFilterProcessesUrl("/auth/login") - - val authorizationFilter = AuthorizationFilter(userDetailsService) + // Custom login url + authenticationFilter.setFilterProcessesUrl("/auth/login") httpSecurity .csrf { it.disable() } @@ -42,7 +42,7 @@ open class SecurityConfig { it.requestMatchers(*AUTH_WHITELIST).permitAll() .anyRequest().authenticated() } - .addFilterBefore(jwtAuthenticationFilter, BasicAuthenticationFilter::class.java) + .addFilterBefore(authenticationFilter, BasicAuthenticationFilter::class.java) .addFilterAfter(authorizationFilter, AuthenticationFilter::class.java) return httpSecurity.build() diff --git a/dmz-rs-server/src/main/kotlin/ch/zindustries/dmz/rs/server/TestResourceServiceImpl.kt b/dmz-rs-server/src/main/kotlin/ch/zindustries/dmz/rs/server/TestResourceServiceImpl.kt index 2b4f441..f7fdbb1 100644 --- a/dmz-rs-server/src/main/kotlin/ch/zindustries/dmz/rs/server/TestResourceServiceImpl.kt +++ b/dmz-rs-server/src/main/kotlin/ch/zindustries/dmz/rs/server/TestResourceServiceImpl.kt @@ -7,7 +7,7 @@ import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.RestController @RestController -class TestResourceServiceImpl( +open class TestResourceServiceImpl( private val testService: TestService ) : TestResourceService { From 1e6eeead6b62e397ea6c517fc5f43f35d6d4fa28 Mon Sep 17 00:00:00 2001 From: Z-100 Date: Tue, 27 Aug 2024 19:57:56 +0200 Subject: [PATCH 23/24] feature(#5): Add authorizationfilter --- .../kotlin/ch/zindustries/dmz/auth/Roles.kt | 6 +- .../zindustries/dmz/auth/entities/Account.kt | 2 +- .../dmz/auth/filters/AuthorizationFilter.kt | 56 ++++++++++++++++++- .../dmz/rs/api/TestResourceService.kt | 3 +- .../ch/zindustries/dmz/TestServiceImpl.kt | 6 -- 5 files changed, 59 insertions(+), 14 deletions(-) diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/Roles.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/Roles.kt index c478599..c9952d1 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/Roles.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/Roles.kt @@ -2,8 +2,8 @@ package ch.zindustries.dmz.auth class Roles { companion object { - const val ADMIN = "admin" - const val USER = "user" - const val ANONYMOUS = "anonymous" + const val ADMIN = "ROLE_ADMIN" + const val USER = "ROLE_USER" + const val ANONYMOUS = "ROLE_ANONYMOUS" } } diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Account.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Account.kt index b42300e..18590c3 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Account.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Account.kt @@ -33,6 +33,6 @@ class Account : BaseEntity() { ) var authorities: Set = HashSet() - @ElementCollection + @ElementCollection(fetch = FetchType.EAGER) var roles: Set = HashSet() } diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/filters/AuthorizationFilter.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/filters/AuthorizationFilter.kt index 4b17e25..7ae9c1b 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/filters/AuthorizationFilter.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/filters/AuthorizationFilter.kt @@ -1,15 +1,24 @@ package ch.zindustries.dmz.auth.filters +import ch.zindustries.dmz.auth.dtos.DMZUserDetails +import ch.zindustries.dmz.auth.exceptions.SecurityContextException +import com.auth0.jwt.JWT +import com.auth0.jwt.exceptions.JWTDecodeException import jakarta.servlet.FilterChain import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletResponse +import org.springframework.http.HttpHeaders.AUTHORIZATION +import org.springframework.security.authentication.UsernamePasswordAuthenticationToken +import org.springframework.security.core.authority.SimpleGrantedAuthority +import org.springframework.security.core.context.SecurityContextHolder import org.springframework.security.core.userdetails.UserDetailsService import org.springframework.stereotype.Service import org.springframework.web.filter.OncePerRequestFilter +import java.util.* @Service class AuthorizationFilter( - userDetailsService: UserDetailsService, + private val userDetailsService: UserDetailsService, ) : OncePerRequestFilter() { override fun doFilterInternal( @@ -17,7 +26,50 @@ class AuthorizationFilter( response: HttpServletResponse, filterChain: FilterChain ) { - // Do nothing + + val bearer = request.getHeader(AUTHORIZATION) + + if (bearer.isNullOrEmpty()) { + logger.info("No token provided. Skipping authorization.") + filterChain.doFilter(request, response) + return + } + + if (!bearer.startsWith("Bearer ")) { + throw SecurityContextException("Invalid Bearer token provided") + } + + val decodedBearer = try { + JWT.decode(bearer.removePrefix("Bearer ")) + } catch (ex: JWTDecodeException) { + throw SecurityContextException("Invalid Bearer token provided", ex) + } + + // TODO improve readability + val subject = decodedBearer.getClaim("sub").asString() + val expiry = decodedBearer.getClaim("exp").asDate() + + if (expiry.before(Date())) { + throw SecurityContextException("Bearer Token expired") + } + + val userDetails = userDetailsService.loadUserByUsername(subject) as DMZUserDetails + userDetails.eraseCredentials() + + if ( + !userDetails.isEnabled || + !userDetails.isAccountNonExpired || + !userDetails.isCredentialsNonExpired || + !userDetails.isAccountNonLocked + ) { + // TODO: Actually implement + throw SecurityContextException("Account is expired") + } + + val authenticatedToken = UsernamePasswordAuthenticationToken(userDetails, null, userDetails.account.roles.map { SimpleGrantedAuthority(it) }) + + SecurityContextHolder.getContext().authentication = authenticatedToken + filterChain.doFilter(request, response) } } diff --git a/dmz-rs-api/src/main/kotlin/ch/zindustries/dmz/rs/api/TestResourceService.kt b/dmz-rs-api/src/main/kotlin/ch/zindustries/dmz/rs/api/TestResourceService.kt index d026e45..3be2692 100644 --- a/dmz-rs-api/src/main/kotlin/ch/zindustries/dmz/rs/api/TestResourceService.kt +++ b/dmz-rs-api/src/main/kotlin/ch/zindustries/dmz/rs/api/TestResourceService.kt @@ -2,7 +2,6 @@ package ch.zindustries.dmz.rs.api import ch.zindustries.dmz.auth.annotations.AccessibleByAdmin import ch.zindustries.dmz.auth.annotations.AccessibleByAnonymous -import ch.zindustries.dmz.auth.annotations.AccessibleByUser import ch.zindustries.dmz.auth.dtos.AccountDTO import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.GetMapping @@ -16,7 +15,7 @@ interface TestResourceService { @GetMapping("test-admin") fun testAdmin(): ResponseEntity - @AccessibleByUser + @AccessibleByAnonymous @GetMapping("test-user") fun testUser(): ResponseEntity diff --git a/dmz/src/main/kotlin/ch/zindustries/dmz/TestServiceImpl.kt b/dmz/src/main/kotlin/ch/zindustries/dmz/TestServiceImpl.kt index 7decf75..6468916 100644 --- a/dmz/src/main/kotlin/ch/zindustries/dmz/TestServiceImpl.kt +++ b/dmz/src/main/kotlin/ch/zindustries/dmz/TestServiceImpl.kt @@ -6,21 +6,15 @@ import ch.zindustries.dmz.auth.dtos.AuthorityDTO import ch.zindustries.dmz.auth.entities.Account import ch.zindustries.dmz.auth.entities.Authority import ch.zindustries.dmz.auth.repositories.AccountRepository -import ch.zindustries.dmz.auth.services.AccessControlService -import ch.zindustries.dmz.auth.types.TestServices import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Transactional @Service open class TestServiceImpl( - private val accessControl: AccessControlService, private val accountRepository: AccountRepository, ) : TestService { override fun test(): String { - - accessControl.assertCanAccessService(TestServices.TEST_SERVICE) - return "Test" } From f55d6c53984e3425dff38ffbe369541daecc6b09 Mon Sep 17 00:00:00 2001 From: Z-100 Date: Tue, 27 Aug 2024 20:05:36 +0200 Subject: [PATCH 24/24] feature(#4): Minor fixup --- .../kotlin/ch/zindustries/dmz/auth/entities/Account.kt | 4 +--- .../kotlin/ch/zindustries/dmz/auth/entities/BaseEntity.kt | 8 -------- .../dmz/auth/entities/listeners/AccountEntityListener.kt | 2 +- dmz/src/main/kotlin/ch/zindustries/dmz/TestServiceImpl.kt | 6 ++++-- docker/mongodb-compose.yml | 4 ++-- 5 files changed, 8 insertions(+), 16 deletions(-) diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Account.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Account.kt index 18590c3..a2935da 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Account.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/Account.kt @@ -23,9 +23,7 @@ class Account : BaseEntity() { @Column var multiFactorActivated: Boolean = false - @ManyToMany( - cascade = [CascadeType.ALL], - ) + @ManyToMany(cascade = [CascadeType.ALL]) @JoinTable( name = "authority_account", joinColumns = [JoinColumn(name = "authority_id")], diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/BaseEntity.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/BaseEntity.kt index 0e38ecb..c292913 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/BaseEntity.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/BaseEntity.kt @@ -11,14 +11,6 @@ open class BaseEntity { @Column(name = "id", nullable = false) var id: Long? = null - // TODO -// @Column -// var createdBy: User = User() - - // TODO -// @Column -// var modifiedBy: User = User() - @Column var createdAt: Date = Date() diff --git a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/listeners/AccountEntityListener.kt b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/listeners/AccountEntityListener.kt index 319d782..d56918c 100644 --- a/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/listeners/AccountEntityListener.kt +++ b/dmz-auth/src/main/kotlin/ch/zindustries/dmz/auth/entities/listeners/AccountEntityListener.kt @@ -16,7 +16,7 @@ class AccountEntityListener { private lateinit var passwordEncoder: PasswordEncoder @PrePersist - @PreUpdate + @PreUpdate // TODO: Might has to be removed fun hashPassword(account: Account) { account.password = passwordEncoder.encode(account.password) } diff --git a/dmz/src/main/kotlin/ch/zindustries/dmz/TestServiceImpl.kt b/dmz/src/main/kotlin/ch/zindustries/dmz/TestServiceImpl.kt index 6468916..37a149e 100644 --- a/dmz/src/main/kotlin/ch/zindustries/dmz/TestServiceImpl.kt +++ b/dmz/src/main/kotlin/ch/zindustries/dmz/TestServiceImpl.kt @@ -21,6 +21,8 @@ open class TestServiceImpl( @Transactional override fun createTestUsers(): List { + // TODO: Cleanup this mess some day + val admin = Account() admin.username = "admin" admin.password = "admin" @@ -31,7 +33,7 @@ open class TestServiceImpl( testAuthority.name = "test" testAuthority.code = "1" admin.authorities = setOf(testAuthority) - admin.roles = setOf("ROLE_ADMIN", "ROLE_USER") + admin.roles = setOf("ROLE_ADMIN", "ROLE_USER") val user = Account() user.username = "user" @@ -43,7 +45,7 @@ open class TestServiceImpl( testAuthority1.name = "test1" testAuthority1.code = "2" user.authorities = setOf(testAuthority1) - user.roles = setOf("ROLE_USER") + user.roles = setOf("ROLE_USER") val saveAll = accountRepository.saveAll(listOf(admin, user)) diff --git a/docker/mongodb-compose.yml b/docker/mongodb-compose.yml index 6e2185d..7e4e190 100644 --- a/docker/mongodb-compose.yml +++ b/docker/mongodb-compose.yml @@ -7,9 +7,9 @@ services: MONGO_INITDB_ROOT_USERNAME: root MONGO_INITDB_ROOT_PASSWORD: root ports: - - 27017:27017 + - "27017:27017" volumes: - dmz_mongodb_vol:/data/mongodb volumes: - dmz_mongodb_vol + dmz_mongodb_vol: