-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ace9019
commit 3d960f2
Showing
24 changed files
with
493 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
...c/commonMain/kotlin/me/nathanfallet/extopy/usecases/auth/GetJWTPrincipalForCallUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package me.nathanfallet.extopy.usecases.auth | ||
|
||
import io.ktor.server.application.* | ||
import io.ktor.server.auth.* | ||
import io.ktor.server.auth.jwt.* | ||
|
||
class GetJWTPrincipalForCallUseCase : IGetJWTPrincipalForCallUseCase { | ||
|
||
override fun invoke(input: ApplicationCall): JWTPrincipal? { | ||
return input.principal() | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
...nd/src/commonMain/kotlin/me/nathanfallet/extopy/usecases/auth/GetSessionForCallUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package me.nathanfallet.extopy.usecases.auth | ||
|
||
import io.ktor.server.application.* | ||
import io.ktor.server.sessions.* | ||
import me.nathanfallet.extopy.models.auth.SessionPayload | ||
import me.nathanfallet.ktorx.usecases.auth.IGetSessionForCallUseCase | ||
import me.nathanfallet.usecases.users.ISessionPayload | ||
|
||
class GetSessionForCallUseCase : IGetSessionForCallUseCase { | ||
|
||
override fun invoke(input: ApplicationCall): ISessionPayload? { | ||
return input.sessions.get<SessionPayload>() | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
...backend/src/commonMain/kotlin/me/nathanfallet/extopy/usecases/auth/HashPasswordUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package me.nathanfallet.extopy.usecases.auth | ||
|
||
import at.favre.lib.crypto.bcrypt.BCrypt | ||
|
||
class HashPasswordUseCase : IHashPasswordUseCase { | ||
|
||
override fun invoke(input: String): String { | ||
return BCrypt.withDefaults().hashToString(12, input.toCharArray()) | ||
} | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
.../commonMain/kotlin/me/nathanfallet/extopy/usecases/auth/IGetJWTPrincipalForCallUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package me.nathanfallet.extopy.usecases.auth | ||
|
||
import io.ktor.server.application.* | ||
import io.ktor.server.auth.jwt.* | ||
import me.nathanfallet.usecases.base.IUseCase | ||
|
||
interface IGetJWTPrincipalForCallUseCase : IUseCase<ApplicationCall, JWTPrincipal?> |
5 changes: 5 additions & 0 deletions
5
...ackend/src/commonMain/kotlin/me/nathanfallet/extopy/usecases/auth/IHashPasswordUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package me.nathanfallet.extopy.usecases.auth | ||
|
||
import me.nathanfallet.usecases.base.IUseCase | ||
|
||
interface IHashPasswordUseCase : IUseCase<String, String> |
5 changes: 5 additions & 0 deletions
5
...kend/src/commonMain/kotlin/me/nathanfallet/extopy/usecases/auth/IVerifyPasswordUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package me.nathanfallet.extopy.usecases.auth | ||
|
||
import me.nathanfallet.usecases.base.IPairUseCase | ||
|
||
interface IVerifyPasswordUseCase : IPairUseCase<String, String, Boolean> |
19 changes: 19 additions & 0 deletions
19
extopy-backend/src/commonMain/kotlin/me/nathanfallet/extopy/usecases/auth/LoginUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package me.nathanfallet.extopy.usecases.auth | ||
|
||
import me.nathanfallet.extopy.models.auth.LoginPayload | ||
import me.nathanfallet.extopy.repositories.users.IUsersRepository | ||
import me.nathanfallet.ktorx.usecases.auth.ILoginUseCase | ||
import me.nathanfallet.usecases.users.IUser | ||
|
||
class LoginUseCase( | ||
private val repository: IUsersRepository, | ||
private val verifyPasswordUseCase: IVerifyPasswordUseCase, | ||
) : ILoginUseCase<LoginPayload> { | ||
|
||
override suspend fun invoke(input: LoginPayload): IUser? { | ||
return repository.getForEmail(input.email, true)?.takeIf { | ||
verifyPasswordUseCase(input.password, it.password ?: "") | ||
} | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
...ckend/src/commonMain/kotlin/me/nathanfallet/extopy/usecases/auth/VerifyPasswordUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package me.nathanfallet.extopy.usecases.auth | ||
|
||
import at.favre.lib.crypto.bcrypt.BCrypt | ||
|
||
class VerifyPasswordUseCase : IVerifyPasswordUseCase { | ||
|
||
override fun invoke(input1: String, input2: String): Boolean { | ||
return BCrypt.verifyer().verify(input1.toCharArray(), input2).verified | ||
} | ||
|
||
} |
28 changes: 26 additions & 2 deletions
28
...kend/src/commonMain/kotlin/me/nathanfallet/extopy/usecases/users/GetUserForCallUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,37 @@ | ||
package me.nathanfallet.extopy.usecases.users | ||
|
||
import io.ktor.server.application.* | ||
import io.ktor.util.* | ||
import me.nathanfallet.extopy.models.auth.SessionPayload | ||
import me.nathanfallet.extopy.models.users.User | ||
import me.nathanfallet.extopy.models.users.UserContext | ||
import me.nathanfallet.extopy.usecases.auth.IGetJWTPrincipalForCallUseCase | ||
import me.nathanfallet.ktorx.usecases.auth.IGetSessionForCallUseCase | ||
import me.nathanfallet.ktorx.usecases.users.IGetUserForCallUseCase | ||
import me.nathanfallet.usecases.models.get.context.IGetModelWithContextSuspendUseCase | ||
import me.nathanfallet.usecases.users.IUser | ||
|
||
class GetUserForCallUseCase : IGetUserForCallUseCase { | ||
class GetUserForCallUseCase( | ||
private val getJWTPrincipalForCall: IGetJWTPrincipalForCallUseCase, | ||
private val getSessionForCallUseCase: IGetSessionForCallUseCase, | ||
private val getUserUseCase: IGetModelWithContextSuspendUseCase<User, String>, | ||
) : IGetUserForCallUseCase { | ||
|
||
private data class UserForCall( | ||
val user: User?, | ||
) | ||
|
||
private val userKey = AttributeKey<UserForCall>("extopy-user") | ||
|
||
override suspend fun invoke(input: ApplicationCall): IUser? { | ||
return null | ||
// Note: we cannot use `computeIfAbsent` because it does not support suspending functions | ||
return input.attributes.getOrNull(userKey)?.user ?: run { | ||
val id = | ||
getJWTPrincipalForCall(input)?.subject ?: (getSessionForCallUseCase(input) as? SessionPayload)?.userId | ||
val computed = UserForCall(id?.let { getUserUseCase(it, UserContext(it)) }) | ||
input.attributes.put(userKey, computed) | ||
computed.user | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
.../jvmTest/kotlin/me/nathanfallet/extopy/usecases/auth/GetJWTPrincipalForCallUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package me.nathanfallet.extopy.usecases.auth | ||
|
||
import io.ktor.server.application.* | ||
import io.ktor.server.auth.* | ||
import io.ktor.server.auth.jwt.* | ||
import io.mockk.every | ||
import io.mockk.mockk | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class GetJWTPrincipalForCallUseCaseTest { | ||
|
||
@Test | ||
fun invoke() { | ||
val useCase = GetJWTPrincipalForCallUseCase() | ||
val call = mockk<ApplicationCall>() | ||
val principal = mockk<JWTPrincipal>() | ||
every { call.principal<JWTPrincipal>() } returns principal | ||
assertEquals(principal, useCase(call)) | ||
} | ||
|
||
@Test | ||
fun invokeNull() { | ||
val useCase = GetJWTPrincipalForCallUseCase() | ||
val call = mockk<ApplicationCall>() | ||
every { call.principal<JWTPrincipal>() } returns null | ||
assertEquals(null, useCase(call)) | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...d/src/jvmTest/kotlin/me/nathanfallet/extopy/usecases/auth/GetSessionForCallUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package me.nathanfallet.extopy.usecases.auth | ||
|
||
import io.ktor.client.request.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.config.* | ||
import io.ktor.server.routing.* | ||
import io.ktor.server.sessions.* | ||
import io.ktor.server.testing.* | ||
import me.nathanfallet.extopy.models.auth.SessionPayload | ||
import me.nathanfallet.extopy.plugins.configureSessions | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class GetSessionForCallUseCaseTest { | ||
|
||
@Test | ||
fun invoke() = testApplication { | ||
environment { | ||
config = ApplicationConfig("application.test.conf") | ||
} | ||
application { | ||
configureSessions() | ||
} | ||
routing { | ||
get { | ||
val useCase = GetSessionForCallUseCase() | ||
assertEquals(null, useCase(call)) | ||
call.sessions.set(SessionPayload("id")) | ||
assertEquals(SessionPayload("id"), useCase(call)) | ||
} | ||
} | ||
client.get("/") | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
...ackend/src/jvmTest/kotlin/me/nathanfallet/extopy/usecases/auth/HashPasswordUseCaseTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package me.nathanfallet.extopy.usecases.auth | ||
|
||
import at.favre.lib.crypto.bcrypt.BCrypt | ||
import kotlin.test.Test | ||
import kotlin.test.assertTrue | ||
|
||
class HashPasswordUseCaseTest { | ||
|
||
@Test | ||
fun invoke() { | ||
val useCase = HashPasswordUseCase() | ||
val hash = useCase("password") | ||
assertTrue(BCrypt.verifyer().verify("password".toCharArray(), hash).verified) | ||
} | ||
|
||
} |
Oops, something went wrong.