-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
13 changed files
with
167 additions
and
75 deletions.
There are no files selected for viewing
58 changes: 0 additions & 58 deletions
58
botalka/src/main/kotlin/ru/vityaman/lms/botalka/app/spring/security/SpringAdamCreation.kt
This file was deleted.
Oops, something went wrong.
42 changes: 42 additions & 0 deletions
42
botalka/src/main/kotlin/ru/vityaman/lms/botalka/app/spring/security/SpringAdamSource.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,42 @@ | ||
package ru.vityaman.lms.botalka.app.spring.security | ||
|
||
import kotlinx.coroutines.runBlocking | ||
import org.springframework.beans.factory.annotation.Qualifier | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
import ru.vityaman.lms.botalka.app.spring.storage.MainR2dbcConfig | ||
import ru.vityaman.lms.botalka.app.spring.storage.SpringMigration | ||
import ru.vityaman.lms.botalka.core.logging.Slf4jLog | ||
import ru.vityaman.lms.botalka.core.security.Adam | ||
import ru.vityaman.lms.botalka.core.security.AdamSource | ||
import ru.vityaman.lms.botalka.core.security.auth.TokenService | ||
import ru.vityaman.lms.botalka.core.storage.ConfigStorage | ||
import ru.vityaman.lms.botalka.core.storage.UserStorage | ||
import ru.vityaman.lms.botalka.core.tx.TxEnv | ||
|
||
@Configuration | ||
class SpringAdamSource( | ||
migration: SpringMigration, | ||
|
||
users: UserStorage, | ||
tokens: TokenService, | ||
config: ConfigStorage, | ||
|
||
@Qualifier(MainR2dbcConfig.BeanName.TX_ENV) | ||
txEnv: TxEnv, | ||
) { | ||
private val source = AdamSource( | ||
users = users, | ||
tokens = tokens, | ||
config = config, | ||
txEnv = txEnv, | ||
log = Slf4jLog("SpringAdamCreation"), | ||
) | ||
|
||
init { | ||
migration.let { } | ||
} | ||
|
||
@Bean | ||
fun adam(): Adam = runBlocking { source.adam() } | ||
} |
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
45 changes: 45 additions & 0 deletions
45
botalka/src/main/kotlin/ru/vityaman/lms/botalka/core/security/Adam.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,45 @@ | ||
package ru.vityaman.lms.botalka.core.security | ||
|
||
import ru.vityaman.lms.botalka.core.logging.Log | ||
import ru.vityaman.lms.botalka.core.model.User | ||
import ru.vityaman.lms.botalka.core.security.auth.AccessToken | ||
import ru.vityaman.lms.botalka.core.security.auth.TokenService | ||
import ru.vityaman.lms.botalka.core.storage.ConfigStorage | ||
import ru.vityaman.lms.botalka.core.storage.FetchPolicy | ||
import ru.vityaman.lms.botalka.core.storage.UserStorage | ||
import ru.vityaman.lms.botalka.core.tx.TxEnv | ||
|
||
data class Adam(val user: User, val token: AccessToken?) | ||
|
||
class AdamSource( | ||
private val users: UserStorage, | ||
private val tokens: TokenService, | ||
private val config: ConfigStorage, | ||
private val txEnv: TxEnv, | ||
private val log: Log, | ||
) { | ||
suspend fun adam(): Adam = txEnv.transactional { | ||
val user = users.getByAlias(alias, FetchPolicy.WRITE_LOCKED)!! | ||
log.info("Got $alias as $user") | ||
|
||
val token = if (config.isInitialized()) { | ||
buildString { | ||
append("Service is already initialized, ") | ||
append("don't issue access token") | ||
}.let { log.info(it) } | ||
|
||
null | ||
} else { | ||
log.info("Creating the world of LMS...") | ||
tokens.issue(AccessToken.Payload(user.id)) | ||
.also { config.markInitialized() } | ||
.also { log.warn("Adam token: '${it.text}'") } | ||
} | ||
|
||
Adam(user, token) | ||
} | ||
|
||
companion object { | ||
private val alias = User.Alias("adam") | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
botalka/src/main/kotlin/ru/vityaman/lms/botalka/core/storage/FetchPolicy.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,6 @@ | ||
package ru.vityaman.lms.botalka.core.storage | ||
|
||
enum class FetchPolicy { | ||
SNAPSHOT, | ||
WRITE_LOCKED, | ||
} |
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
12 changes: 12 additions & 0 deletions
12
botalka/src/main/kotlin/ru/vityaman/lms/botalka/storage/jooq/JooqFetchPolicy.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,12 @@ | ||
package ru.vityaman.lms.botalka.storage.jooq | ||
|
||
import org.jooq.Record | ||
import org.jooq.SelectConditionStep | ||
import ru.vityaman.lms.botalka.core.storage.FetchPolicy | ||
|
||
internal fun <R : Record> SelectConditionStep<R>.withPolicy( | ||
policy: FetchPolicy, | ||
) = when (policy) { | ||
FetchPolicy.SNAPSHOT -> this | ||
FetchPolicy.WRITE_LOCKED -> this.forUpdate() | ||
} |
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
33 changes: 33 additions & 0 deletions
33
botalka/src/test/kotlin/ru/vityaman/lms/botalka/app/spring/security/AdamTest.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,33 @@ | ||
package ru.vityaman.lms.botalka.app.spring.security | ||
|
||
import io.kotest.common.runBlocking | ||
import io.kotest.matchers.shouldBe | ||
import kotlinx.coroutines.coroutineScope | ||
import kotlinx.coroutines.launch | ||
import org.junit.jupiter.api.Test | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import ru.vityaman.lms.botalka.app.spring.BotalkaTestSuite | ||
import java.util.concurrent.atomic.AtomicInteger | ||
|
||
class AdamTest( | ||
@Autowired private val adam: SpringAdamSource, | ||
) : BotalkaTestSuite() { | ||
@Test | ||
fun issueTokenExactlyOnce(): Unit = runBlocking { | ||
val tokenIssuedCount = AtomicInteger(0) | ||
|
||
coroutineScope { | ||
repeat(4) { | ||
launch { | ||
val adam = adam.adam() | ||
if (adam.token != null) { | ||
tokenIssuedCount.addAndGet(1) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// As Adam was already created at startup | ||
tokenIssuedCount shouldBe 0 | ||
} | ||
} |