Skip to content

Commit 1a01097

Browse files
leandroBorgesFerreiraLeandro Ferreira
andauthored
sending token only when user is enabled (#456)
Co-authored-by: Leandro Ferreira <[email protected]>
1 parent d46a33e commit 1a01097

6 files changed

Lines changed: 42 additions & 11 deletions

File tree

backend/core/auth/src/main/java/io/writeopia/api/core/auth/AuthRouting.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import io.ktor.server.routing.put
1515
import io.writeopia.api.core.auth.hash.HashUtils
1616
import io.writeopia.api.core.auth.models.toApi
1717
import io.writeopia.api.core.auth.repository.deleteUserById
18+
import io.writeopia.api.core.auth.repository.getEnabledUserByEmail
1819
import io.writeopia.api.core.auth.repository.getUserByEmail
1920
import io.writeopia.api.core.auth.repository.getUserById
2021
import io.writeopia.sdk.serialization.data.auth.AuthResponse
@@ -25,11 +26,15 @@ import io.writeopia.sdk.serialization.data.auth.ResetPasswordRequest
2526
import io.writeopia.sdk.serialization.data.toApi
2627
import io.writeopia.sql.WriteopiaDbBackend
2728

28-
fun Routing.authRoute(writeopiaDb: WriteopiaDbBackend) {
29+
fun Routing.authRoute(writeopiaDb: WriteopiaDbBackend, debugMode: Boolean = false) {
2930
post("api/login") {
3031
try {
3132
val credentials = call.receive<LoginRequest>()
32-
val user = writeopiaDb.getUserByEmail(credentials.email)
33+
val user = if (debugMode) {
34+
writeopiaDb.getUserByEmail(credentials.email)
35+
} else {
36+
writeopiaDb.getEnabledUserByEmail(credentials.email)
37+
}
3338

3439
if (user != null) {
3540
val hash = user.password

backend/core/auth/src/main/java/io/writeopia/api/core/auth/repository/UserRepository.kt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,21 @@ fun WriteopiaDbBackend.getUserByEmail(email: String): WriteopiaBeUser? =
2020
)
2121
}
2222

23+
fun WriteopiaDbBackend.getEnabledUserByEmail(email: String): WriteopiaBeUser? =
24+
this.userEntityQueries
25+
.selectEnabledUserByEmail(email)
26+
.executeAsOneOrNull()
27+
?.let { userEntity ->
28+
WriteopiaBeUser(
29+
id = userEntity.id,
30+
email = userEntity.email,
31+
password = userEntity.password,
32+
name = userEntity.name,
33+
salt = userEntity.salt,
34+
companyDomain = userEntity.company
35+
)
36+
}
37+
2338
fun WriteopiaDbBackend.getUserById(id: String): WriteopiaBeUser? =
2439
this.userEntityQueries
2540
.selectUserById(id)

backend/core/database/src/main/sqldelight/io/writeopia/sql/UserEntity.sq

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ FROM user_entity
1515
WHERE email = ?
1616
LIMIT 1;
1717

18+
selectEnabledUserByEmail:
19+
SELECT *
20+
FROM user_entity
21+
WHERE email = ? AND enabled = TRUE
22+
LIMIT 1;
23+
1824
selectUserById:
1925
SELECT *
2026
FROM user_entity

backend/gateway/src/main/kotlin/io/writeopia/api/geteway/Application.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ fun main() {
1818

1919
fun Application.module(
2020
writeopiaDb: WriteopiaDbBackend = configurePersistence(),
21-
useAi: Boolean = System.getenv("WRITEOPIA_USE_AI")?.toBoolean() ?: false
21+
useAi: Boolean = System.getenv("WRITEOPIA_USE_AI")?.toBoolean() ?: false,
22+
debugMode: Boolean = false
2223
) {
2324
installAuth()
24-
configureRouting(writeopiaDb, useAi)
25+
configureRouting(writeopiaDb, useAi, debugMode = debugMode)
2526
configureSerialization()
2627
configureEditorSockets()
2728
configureHTTP()

backend/gateway/src/main/kotlin/io/writeopia/api/geteway/Routing.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@ import io.writeopia.api.core.auth.authRoute
99
import io.writeopia.api.documents.routing.documentsRoute
1010
import io.writeopia.sql.WriteopiaDbBackend
1111

12-
fun Application.configureRouting(writeopiaDb: WriteopiaDbBackend, useAi: Boolean) {
12+
fun Application.configureRouting(
13+
writeopiaDb: WriteopiaDbBackend,
14+
useAi: Boolean,
15+
debugMode: Boolean = false
16+
) {
1317
routing {
1418
documentsRoute(writeopiaDb, useAi)
1519

1620
get {
1721
call.respondText("Hi")
1822
}
1923

20-
authRoute(writeopiaDb)
24+
authRoute(writeopiaDb, debugMode)
2125
}
2226
}

backend/gateway/src/test/kotlin/io/writeopia/api/gateway/AuthIntegrationTest.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class AuthIntegrationTest {
4343
@Test
4444
fun `it should be possible to register an user`() = testApplication {
4545
application {
46-
module(db)
46+
module(db, debugMode = true)
4747
}
4848

4949
val client = defaultClient()
@@ -66,7 +66,7 @@ class AuthIntegrationTest {
6666
@Test
6767
fun `it should not be possible create 2 users with the same email`() = testApplication {
6868
application {
69-
module(db)
69+
module(db, debugMode = true)
7070
}
7171

7272
val client = defaultClient()
@@ -102,7 +102,7 @@ class AuthIntegrationTest {
102102
@Test
103103
fun `it should be possible to delete your account, if your logged in`() = testApplication {
104104
application {
105-
module(db)
105+
module(db, debugMode = true)
106106
}
107107

108108
val client = defaultClient()
@@ -143,7 +143,7 @@ class AuthIntegrationTest {
143143
fun `it should be possible to delete your account, if don't have the right token`() =
144144
testApplication {
145145
application {
146-
module(db)
146+
module(db, debugMode = true)
147147
}
148148

149149
val response2 = client.delete("api/account") {
@@ -157,7 +157,7 @@ class AuthIntegrationTest {
157157
@Test
158158
fun `it should be possible to reset my password`() = testApplication {
159159
application {
160-
module(db)
160+
module(db, debugMode = true)
161161
}
162162

163163
val client = defaultClient()

0 commit comments

Comments
 (0)