Compare commits
No commits in common. "24026d7fbe5b6b3163bb01858dbb5c6e1d2e9561" and "17931ea1ca18049e8fab8655ac0331680f633730" have entirely different histories.
24026d7fbe
...
17931ea1ca
|
|
@ -12,7 +12,6 @@ import korlibs.korge.input.*
|
||||||
import korlibs.korge.tween.get
|
import korlibs.korge.tween.get
|
||||||
import korlibs.korge.tween.tween
|
import korlibs.korge.tween.tween
|
||||||
import korlibs.korge.ui.tooltip
|
import korlibs.korge.ui.tooltip
|
||||||
import korlibs.korge.ui.uiCheckBox
|
|
||||||
import korlibs.logger.AnsiEscape
|
import korlibs.logger.AnsiEscape
|
||||||
import korlibs.math.geom.*
|
import korlibs.math.geom.*
|
||||||
import korlibs.math.geom.shape.toShape2D
|
import korlibs.math.geom.shape.toShape2D
|
||||||
|
|
@ -90,8 +89,6 @@ class ViewDocument(private val document: Document) : Scene() {
|
||||||
val imageFile = localCurrentDirVfs["assets/xs-reg/00001.jpg"].readBitmap()
|
val imageFile = localCurrentDirVfs["assets/xs-reg/00001.jpg"].readBitmap()
|
||||||
image(imageFile)
|
image(imageFile)
|
||||||
|
|
||||||
uiCheckBox { text = "foo" }
|
|
||||||
|
|
||||||
document.retrieveOcrPages().first().words.forEach { word ->
|
document.retrieveOcrPages().first().words.forEach { word ->
|
||||||
solidRect(
|
solidRect(
|
||||||
width = word.rectangle.width,
|
width = word.rectangle.width,
|
||||||
|
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
dependencies {
|
|
||||||
implementation(project(":libraries:httpClient"))
|
|
||||||
}
|
|
||||||
|
|
@ -1,68 +0,0 @@
|
||||||
import com.github.ajalt.clikt.core.CliktCommand
|
|
||||||
import com.github.ajalt.clikt.core.subcommands
|
|
||||||
import com.github.ajalt.clikt.parameters.options.convert
|
|
||||||
import com.github.ajalt.clikt.parameters.options.option
|
|
||||||
import com.github.ajalt.clikt.parameters.options.required
|
|
||||||
import com.github.ajalt.clikt.parameters.types.int
|
|
||||||
import de.itkl.httpClient.clients.TaskWaitStatus
|
|
||||||
import de.itkl.httpClient.clients.XsClient
|
|
||||||
import de.itkl.httpClient.httpClientModule
|
|
||||||
import kotlinx.coroutines.*
|
|
||||||
import kotlinx.coroutines.Dispatchers.IO
|
|
||||||
import java.util.Random
|
|
||||||
import org.koin.core.component.KoinComponent
|
|
||||||
import org.koin.core.component.inject
|
|
||||||
import org.koin.core.context.GlobalContext.startKoin
|
|
||||||
import java.io.File
|
|
||||||
import java.nio.file.Path
|
|
||||||
import java.nio.file.Paths
|
|
||||||
import kotlin.time.Duration.Companion.minutes
|
|
||||||
|
|
||||||
class Cli : CliktCommand() {
|
|
||||||
override fun run() {}
|
|
||||||
|
|
||||||
inner class StressAnalyse : CliktCommand(name = "stress-analyse") {
|
|
||||||
val inputDirectory: File by option(help="Input directory path").convert { File(it) }.required()
|
|
||||||
val tasks: Int by option(help="Number of tasks").int().required()
|
|
||||||
|
|
||||||
override fun run() {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
init {
|
|
||||||
subcommands(StressAnalyse())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class XsCli : KoinComponent {
|
|
||||||
private val xsClient: XsClient by inject()
|
|
||||||
|
|
||||||
suspend fun run(tasks: Int, inputDirectory: Path) = coroutineScope {
|
|
||||||
val files = inputDirectory.toFile()
|
|
||||||
.listFiles()!!
|
|
||||||
.toList()
|
|
||||||
.filter { it.isFile }.filter { it.extension in listOf("pdf", "ttf", "jpg", "jpeg") }
|
|
||||||
.filter { !it.name.startsWith(".") }
|
|
||||||
val random = Random()
|
|
||||||
|
|
||||||
val (success, error) = (0..<tasks)
|
|
||||||
.map {
|
|
||||||
val file = files[random.nextInt(files.size)]
|
|
||||||
async {
|
|
||||||
val taskRef = xsClient.analyse(file.toPath())
|
|
||||||
xsClient.waitFor(taskRef)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.awaitAll()
|
|
||||||
.partition { it.status == TaskWaitStatus.SUCCESS }
|
|
||||||
|
|
||||||
println("Summary: ${success.size + error.size}: ${success.size}/${error.size}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun main(args: Array<String>) {
|
|
||||||
startKoin {
|
|
||||||
modules(httpClientModule)
|
|
||||||
}
|
|
||||||
XsCli().run(tasks = 100, inputDirectory = Paths.get("assets/xs-reg"))
|
|
||||||
}
|
|
||||||
|
|
@ -1,15 +0,0 @@
|
||||||
import korlibs.korge.gradle.korge
|
|
||||||
|
|
||||||
plugins {
|
|
||||||
id("com.soywiz.korge") version "5.3.0"
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
jvmMainImplementation("ch.qos.logback:logback-classic:1.4.14")
|
|
||||||
jvmMainImplementation(project(":libraries:docthor-core"))
|
|
||||||
}
|
|
||||||
|
|
||||||
korge {
|
|
||||||
targetJvm()
|
|
||||||
serializationJson()
|
|
||||||
}
|
|
||||||
|
|
@ -3,7 +3,7 @@ project(":libraries").subprojects {
|
||||||
}
|
}
|
||||||
|
|
||||||
project(":apps").subprojects {
|
project(":apps").subprojects {
|
||||||
if(name != "documentViewerKorge" && name != "xsViewer") {
|
if(name != "documentViewerKorge") {
|
||||||
apply(plugin = "docthor.kotlin-application-conventions")
|
apply(plugin = "docthor.kotlin-application-conventions")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
import gradle.kotlin.dsl.accessors._6ebf0b5d05ec1eff67605a516c5db18b.implementation
|
|
||||||
import gradle.kotlin.dsl.accessors._d9dcfd1a467b0b6fe90c5571a57aa558.api
|
import gradle.kotlin.dsl.accessors._d9dcfd1a467b0b6fe90c5571a57aa558.api
|
||||||
import gradle.kotlin.dsl.accessors._d9dcfd1a467b0b6fe90c5571a57aa558.testImplementation
|
|
||||||
import org.gradle.api.plugins.jvm.JvmTestSuite
|
import org.gradle.api.plugins.jvm.JvmTestSuite
|
||||||
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
|
||||||
|
|
||||||
|
|
@ -22,8 +20,6 @@ dependencies {
|
||||||
|
|
||||||
api("io.github.oshai:kotlin-logging-jvm:5.1.0")
|
api("io.github.oshai:kotlin-logging-jvm:5.1.0")
|
||||||
testImplementation("io.insert-koin:koin-test:$koin_version")
|
testImplementation("io.insert-koin:koin-test:$koin_version")
|
||||||
testImplementation("ch.qos.logback:logback-classic:1.4.14")
|
|
||||||
testImplementation("com.willowtreeapps.assertk:assertk:0.28.0")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
java {
|
java {
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,4 @@ dependencies {
|
||||||
implementation("io.ktor:ktor-client-cio:$ktorVersion")
|
implementation("io.ktor:ktor-client-cio:$ktorVersion")
|
||||||
implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
|
implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
|
||||||
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
|
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
|
||||||
implementation("com.akuleshov7:ktoml-core:0.5.1")
|
|
||||||
implementation("com.akuleshov7:ktoml-file:0.5.1")
|
|
||||||
implementation("com.github.ben-manes.caffeine:caffeine:3.1.8")
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,4 +0,0 @@
|
||||||
package de.itkl.httpClient
|
|
||||||
|
|
||||||
class BearerTokenCache {
|
|
||||||
}
|
|
||||||
|
|
@ -1,22 +0,0 @@
|
||||||
package de.itkl.httpClient
|
|
||||||
|
|
||||||
import de.itkl.httpClient.implementation.SmartCloudAuthStrategy
|
|
||||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
|
||||||
import io.ktor.client.plugins.api.*
|
|
||||||
import io.ktor.client.request.*
|
|
||||||
import io.ktor.http.*
|
|
||||||
import io.ktor.util.*
|
|
||||||
import org.koin.core.component.KoinComponent
|
|
||||||
|
|
||||||
private val Log = KotlinLogging.logger { }
|
|
||||||
val smartCloudAuthPlugin = createClientPlugin("SmartCloudAuth") {
|
|
||||||
val smartCloudAuthStrategy = SmartCloudAuthStrategy()
|
|
||||||
on(Send) { request ->
|
|
||||||
smartCloudAuthStrategy.login(client, request)?.let { token ->
|
|
||||||
request.headers {
|
|
||||||
set(HttpHeaders.Authorization, token.toBearer())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
proceed(request)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,12 +0,0 @@
|
||||||
package de.itkl.httpClient
|
|
||||||
|
|
||||||
import java.security.cert.X509Certificate
|
|
||||||
import javax.net.ssl.X509TrustManager
|
|
||||||
|
|
||||||
class TrustAllX509TrustManager : X509TrustManager {
|
|
||||||
override fun getAcceptedIssuers(): Array<X509Certificate?> = arrayOfNulls(0)
|
|
||||||
|
|
||||||
override fun checkClientTrusted(certs: Array<X509Certificate?>?, authType: String?) {}
|
|
||||||
|
|
||||||
override fun checkServerTrusted(certs: Array<X509Certificate?>?, authType: String?) {}
|
|
||||||
}
|
|
||||||
|
|
@ -1,11 +0,0 @@
|
||||||
package de.itkl.httpClient.auth
|
|
||||||
|
|
||||||
import io.ktor.client.*
|
|
||||||
import io.ktor.client.request.*
|
|
||||||
|
|
||||||
interface AuthStrategy {
|
|
||||||
suspend fun login(
|
|
||||||
httpClient: HttpClient,
|
|
||||||
request: HttpRequestBuilder
|
|
||||||
): AuthenticationToken?
|
|
||||||
}
|
|
||||||
|
|
@ -1,23 +0,0 @@
|
||||||
package de.itkl.httpClient.auth
|
|
||||||
|
|
||||||
import kotlinx.datetime.Instant
|
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
|
|
||||||
sealed class AuthenticationToken {
|
|
||||||
abstract val expires: Expires
|
|
||||||
abstract fun toBearer(): String
|
|
||||||
}
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data class BearerToken(val token: String, val validUntil: Instant) : AuthenticationToken() {
|
|
||||||
override val expires: Expires
|
|
||||||
get() = Expires.ExpiresAt(validUntil)
|
|
||||||
|
|
||||||
override fun toBearer(): String {
|
|
||||||
return "Bearer $token"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sealed class Expires {
|
|
||||||
data object Never : Expires()
|
|
||||||
data class ExpiresAt(val instant: Instant) : Expires()
|
|
||||||
}
|
|
||||||
|
|
@ -1,17 +0,0 @@
|
||||||
package de.itkl.httpClient.auth
|
|
||||||
|
|
||||||
import io.ktor.http.*
|
|
||||||
import org.koin.core.component.KoinComponent
|
|
||||||
|
|
||||||
typealias UrlMatcher = (Url) -> Boolean
|
|
||||||
class Authenticator : KoinComponent {
|
|
||||||
fun addStrategy(strategy: AuthStrategy, urlMatcher: UrlMatcher) {}
|
|
||||||
|
|
||||||
fun requiresAuthentication(url: Url): Boolean {
|
|
||||||
TODO()
|
|
||||||
}
|
|
||||||
suspend fun authenticate(url: Url, username: String): AuthenticationToken? {
|
|
||||||
TODO()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
@ -1,8 +0,0 @@
|
||||||
package de.itkl.httpClient.auth
|
|
||||||
|
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
|
|
||||||
sealed class Credentials {
|
|
||||||
@Serializable
|
|
||||||
data class LoginAndPassword(val username: String, val password: String) : Credentials()
|
|
||||||
}
|
|
||||||
|
|
@ -1,5 +0,0 @@
|
||||||
package de.itkl.httpClient.auth
|
|
||||||
|
|
||||||
interface CredentialsProvider {
|
|
||||||
suspend fun lookupByUsername(username: String): Credentials?
|
|
||||||
}
|
|
||||||
|
|
@ -1,9 +0,0 @@
|
||||||
package de.itkl.httpClient.clients
|
|
||||||
|
|
||||||
import io.ktor.client.*
|
|
||||||
import org.koin.core.component.KoinComponent
|
|
||||||
import org.koin.core.component.inject
|
|
||||||
|
|
||||||
abstract class RestClient : KoinComponent {
|
|
||||||
private val httpClient by inject<HttpClient>()
|
|
||||||
}
|
|
||||||
|
|
@ -1,134 +0,0 @@
|
||||||
package de.itkl.httpClient.clients
|
|
||||||
|
|
||||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
|
||||||
import io.ktor.client.*
|
|
||||||
import io.ktor.client.call.*
|
|
||||||
import io.ktor.client.request.*
|
|
||||||
import io.ktor.client.request.forms.*
|
|
||||||
import io.ktor.client.statement.*
|
|
||||||
import io.ktor.client.utils.EmptyContent.contentType
|
|
||||||
import io.ktor.http.*
|
|
||||||
import io.ktor.http.content.*
|
|
||||||
import io.ktor.util.*
|
|
||||||
import io.ktor.util.cio.*
|
|
||||||
import io.ktor.utils.io.*
|
|
||||||
import io.ktor.utils.io.streams.*
|
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
import org.koin.core.component.KoinComponent
|
|
||||||
import org.koin.core.component.inject
|
|
||||||
import java.awt.SystemColor.info
|
|
||||||
import java.nio.file.Files
|
|
||||||
import java.nio.file.Path
|
|
||||||
import java.util.UUID
|
|
||||||
import kotlin.io.path.exists
|
|
||||||
import kotlin.io.path.isRegularFile
|
|
||||||
import kotlin.io.path.name
|
|
||||||
|
|
||||||
private val Log = KotlinLogging.logger { }
|
|
||||||
|
|
||||||
class XsClient : KoinComponent {
|
|
||||||
private val httpClient by inject<HttpClient>()
|
|
||||||
|
|
||||||
suspend fun waitFor(task: XsTask): WaitForResponse {
|
|
||||||
Log.info { "Wait for competition: $task" }
|
|
||||||
val response = httpClient.get {
|
|
||||||
url("http://localhost:8080/api/v1/analyse/tasks/wait/${task.xsTaskId.taskId}")
|
|
||||||
user = "xs.dev.klara"
|
|
||||||
}
|
|
||||||
val result = response.body<WaitForResponse>()
|
|
||||||
Log.info { "Waiting done for task $task: ${response.status}: $result" }
|
|
||||||
return result
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun analyseResult(task: XsTask) {
|
|
||||||
val response = httpClient.get {
|
|
||||||
url("http://localhost:8080/api/v1/analyse-async-result/${task.xsTaskId.taskId}")
|
|
||||||
user = "xs.dev.klara"
|
|
||||||
}
|
|
||||||
check(response.status.isSuccess()) {
|
|
||||||
"HTTP Error"
|
|
||||||
}
|
|
||||||
val text = response.bodyAsText()
|
|
||||||
println(text)
|
|
||||||
}
|
|
||||||
suspend fun analyse(image: Path): TaskReference {
|
|
||||||
Log.info { "Starting analysis for image at path: $image" }
|
|
||||||
|
|
||||||
check(image.isRegularFile()) {
|
|
||||||
"The provided path $image is not a file"
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.info { "Submitting form with binary data to http://localhost:8080/api/v1/analyse-async" }
|
|
||||||
val response = httpClient.submitFormWithBinaryData(
|
|
||||||
url = "http://localhost:8080/api/v1/analyse-async",
|
|
||||||
formData {
|
|
||||||
append(
|
|
||||||
"image",
|
|
||||||
image.toChannelProvider(),
|
|
||||||
Headers.build {
|
|
||||||
append(HttpHeaders.ContentType, ContentType.defaultForFile(image))
|
|
||||||
append(HttpHeaders.ContentDisposition, """filename="${image.name}"""")
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
user = "xs.dev.klara"
|
|
||||||
}
|
|
||||||
|
|
||||||
Log.info { "Received the response from the server." }
|
|
||||||
|
|
||||||
if(response.status.isSuccess()) {
|
|
||||||
Log.info { "Successful response with status: ${response.status}" }
|
|
||||||
return response.body()
|
|
||||||
} else {
|
|
||||||
val responseText = response.bodyAsText()
|
|
||||||
Log.warn { "Error creating analyse task: ${response.status}: $responseText" }
|
|
||||||
error("Could not create analyse task: $responseText")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
var HttpRequestBuilder.user: String?
|
|
||||||
get() = this.attributes[AttributeKey(("username"))]
|
|
||||||
set(value) = value?.let { this.attributes.put(AttributeKey("username"), it) } ?: this.attributes.remove(
|
|
||||||
AttributeKey("username")
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
interface XsTask {
|
|
||||||
val xsTaskId: XsTaskId
|
|
||||||
}
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data class TaskReference(
|
|
||||||
override val xsTaskId: XsTaskId
|
|
||||||
) : XsTask {
|
|
||||||
override fun toString(): String {
|
|
||||||
return "Task($xsTaskId)"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data class XsTaskId(val tenantId: String, val taskId: String) {
|
|
||||||
override fun toString(): String {
|
|
||||||
return "$tenantId/$taskId"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data class WaitForResponse(
|
|
||||||
val xsTaskId: XsTaskId,
|
|
||||||
val status: TaskWaitStatus,
|
|
||||||
)
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
enum class TaskWaitStatus {
|
|
||||||
ERROR,
|
|
||||||
SUCCESS
|
|
||||||
}
|
|
||||||
|
|
||||||
fun Path.toChannelProvider(): ChannelProvider {
|
|
||||||
val file = toFile()
|
|
||||||
return ChannelProvider(file.length()) { file.readChannel() }
|
|
||||||
}
|
|
||||||
|
|
@ -1,41 +1,14 @@
|
||||||
package de.itkl.httpClient
|
package de.itkl.httpClient
|
||||||
|
|
||||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
|
||||||
import io.ktor.client.*
|
import io.ktor.client.*
|
||||||
import io.ktor.client.engine.*
|
|
||||||
import io.ktor.client.engine.cio.*
|
import io.ktor.client.engine.cio.*
|
||||||
import io.ktor.client.plugins.*
|
|
||||||
import io.ktor.client.plugins.contentnegotiation.*
|
import io.ktor.client.plugins.contentnegotiation.*
|
||||||
import io.ktor.http.*
|
|
||||||
import io.ktor.serialization.kotlinx.json.*
|
import io.ktor.serialization.kotlinx.json.*
|
||||||
import java.net.InetSocketAddress
|
|
||||||
import java.net.Socket
|
|
||||||
import kotlin.time.Duration.Companion.minutes
|
|
||||||
|
|
||||||
private val Log = KotlinLogging.logger { }
|
|
||||||
fun createHttpClient(): HttpClient {
|
fun createHttpClient(): HttpClient {
|
||||||
return HttpClient(CIO) {
|
return HttpClient(CIO) {
|
||||||
install(ContentNegotiation) {
|
install(ContentNegotiation) {
|
||||||
json()
|
json()
|
||||||
}
|
}
|
||||||
install(smartCloudAuthPlugin)
|
|
||||||
install(HttpTimeout) {
|
|
||||||
requestTimeoutMillis = 30.minutes.inWholeMilliseconds
|
|
||||||
}
|
|
||||||
|
|
||||||
engine {
|
|
||||||
val isPortOpen = try {
|
|
||||||
Socket().use { it.connect(InetSocketAddress("localhost", 9999), 200) }
|
|
||||||
true
|
|
||||||
} catch (ex: Exception) {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
if (isPortOpen) {
|
|
||||||
proxy = ProxyBuilder.http(Url("http://localhost:9999"))
|
|
||||||
}
|
|
||||||
https {
|
|
||||||
trustManager = TrustAllX509TrustManager()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,20 +1,10 @@
|
||||||
package de.itkl.httpClient
|
package de.itkl.httpClient
|
||||||
|
|
||||||
import de.itkl.httpClient.auth.CredentialsProvider
|
|
||||||
import de.itkl.httpClient.clients.MsOcr
|
import de.itkl.httpClient.clients.MsOcr
|
||||||
import de.itkl.httpClient.clients.XsClient
|
|
||||||
import de.itkl.httpClient.implementation.StaticCredentialsProvider
|
|
||||||
import io.ktor.client.*
|
import io.ktor.client.*
|
||||||
import org.koin.dsl.module
|
import org.koin.dsl.module
|
||||||
import java.nio.file.Paths
|
|
||||||
|
|
||||||
val httpClientModule = module {
|
val httpClientModule = module {
|
||||||
single<HttpClient> { createHttpClient() }
|
single<HttpClient> { createHttpClient() }
|
||||||
single<MsOcr> { MsOcr() }
|
single<MsOcr> { MsOcr() }
|
||||||
single<CredentialsProvider> {
|
|
||||||
val homeDirectory = System.getProperty("user.home")
|
|
||||||
val credentialsFilePath = Paths.get(homeDirectory, ".auth.toml")
|
|
||||||
StaticCredentialsProvider.load(credentialsFilePath)
|
|
||||||
}
|
|
||||||
single<XsClient> { XsClient() }
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,72 +0,0 @@
|
||||||
package de.itkl.httpClient.implementation
|
|
||||||
|
|
||||||
import com.github.benmanes.caffeine.cache.Caffeine
|
|
||||||
import com.github.benmanes.caffeine.cache.Cache
|
|
||||||
import de.itkl.httpClient.auth.*
|
|
||||||
import de.itkl.httpClient.clients.user
|
|
||||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
|
||||||
import io.ktor.client.*
|
|
||||||
import io.ktor.client.call.*
|
|
||||||
import io.ktor.client.request.*
|
|
||||||
import io.ktor.client.statement.*
|
|
||||||
import io.ktor.http.*
|
|
||||||
import io.ktor.util.*
|
|
||||||
import org.koin.core.component.KoinComponent
|
|
||||||
import org.koin.core.component.inject
|
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
import kotlinx.coroutines.sync.Mutex
|
|
||||||
import kotlinx.coroutines.sync.withLock
|
|
||||||
|
|
||||||
private val Log = KotlinLogging.logger { }
|
|
||||||
|
|
||||||
class SmartCloudAuthStrategy : AuthStrategy, KoinComponent {
|
|
||||||
private val credentialsProvider: CredentialsProvider by inject()
|
|
||||||
|
|
||||||
private val loginMutex = Mutex()
|
|
||||||
|
|
||||||
// Cache Helpers
|
|
||||||
private val tokenCache: Cache<String, AuthenticationToken> = Caffeine.newBuilder()
|
|
||||||
.expireAfterWrite(1, TimeUnit.HOURS)
|
|
||||||
.build()
|
|
||||||
|
|
||||||
override suspend fun login(httpClient: HttpClient, request: HttpRequestBuilder): AuthenticationToken? {
|
|
||||||
Log.debug { "Attempting login..." }
|
|
||||||
val user = request.attributes.getOrNull(AttributeKey<String>("username")) ?: run {
|
|
||||||
Log.info { "No username is specified for this request" }
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
tokenCache.getIfPresent(user)?.let {
|
|
||||||
Log.info { "Returning cached token for user: $user" }
|
|
||||||
return it
|
|
||||||
}
|
|
||||||
|
|
||||||
return loginMutex.withLock {
|
|
||||||
tokenCache.getIfPresent(user)?.let {
|
|
||||||
Log.info { "Returning cached token for user: $user" }
|
|
||||||
return it
|
|
||||||
}
|
|
||||||
|
|
||||||
val credentials: Credentials = credentialsProvider.lookupByUsername(user) ?: error("No credentials found for user: $user")
|
|
||||||
val loginAndPassword: Credentials.LoginAndPassword = credentials as? Credentials.LoginAndPassword ?: error("Only username and password is supported by smartcloud auth login")
|
|
||||||
|
|
||||||
Log.debug { "User: $user, using smartcloud auth login" }
|
|
||||||
|
|
||||||
val response = httpClient.post {
|
|
||||||
url("https://api.internal.insiders.cloud/1/rest/accounts/authentication/requesttoken")
|
|
||||||
contentType(ContentType.Application.Json)
|
|
||||||
setBody(loginAndPassword)
|
|
||||||
}
|
|
||||||
check(response.status.isSuccess()) {
|
|
||||||
"could not login into smart cloud: ${response.bodyAsText()}"
|
|
||||||
}
|
|
||||||
val token = response.body<BearerToken>()
|
|
||||||
Log.info { "Login successful. Valid until: ${token.validUntil}" }
|
|
||||||
|
|
||||||
// Cache the token after successful login
|
|
||||||
tokenCache.put(user, token)
|
|
||||||
|
|
||||||
token
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,49 +0,0 @@
|
||||||
package de.itkl.httpClient.implementation
|
|
||||||
|
|
||||||
import com.akuleshov7.ktoml.Toml
|
|
||||||
import de.itkl.httpClient.auth.Credentials
|
|
||||||
import de.itkl.httpClient.auth.CredentialsProvider
|
|
||||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
|
||||||
import kotlinx.serialization.Serializable
|
|
||||||
import kotlinx.serialization.decodeFromString
|
|
||||||
import java.nio.file.Files
|
|
||||||
import java.nio.file.Path
|
|
||||||
|
|
||||||
private val Log = KotlinLogging.logger { }
|
|
||||||
|
|
||||||
class StaticCredentialsProvider(private val credentials: Map<String, Credentials.LoginAndPassword>) : CredentialsProvider {
|
|
||||||
|
|
||||||
companion object {
|
|
||||||
fun load(path: Path): StaticCredentialsProvider {
|
|
||||||
Log.info { "Loading credentials from path: $path" }
|
|
||||||
val content = Files.readString(path)
|
|
||||||
val credentials = Toml.decodeFromString<CredentialsTable>(content).toCredentialsMap()
|
|
||||||
Log.info { "Credentials loaded successfully" }
|
|
||||||
return StaticCredentialsProvider(credentials)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override suspend fun lookupByUsername(username: String): Credentials? {
|
|
||||||
Log.info { "Looking up credentials by username: $username" }
|
|
||||||
val credentialsEntry = credentials[username]
|
|
||||||
if (credentialsEntry != null) {
|
|
||||||
Log.info { "Credentials found for username: $username" }
|
|
||||||
return credentialsEntry
|
|
||||||
}
|
|
||||||
Log.info { "No credentials found for username: $username" }
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data class CredentialsTable(
|
|
||||||
val credentials: Map<String, Map<String, String>>
|
|
||||||
) {
|
|
||||||
fun toCredentialsMap(): Map<String, Credentials.LoginAndPassword> {
|
|
||||||
return credentials.mapValues {
|
|
||||||
val login = it.value["Login"] ?: throw IllegalArgumentException("Missing login")
|
|
||||||
val password = it.value["Password"] ?: throw IllegalArgumentException("Missing password")
|
|
||||||
Credentials.LoginAndPassword(login, password)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,25 +0,0 @@
|
||||||
package de.itkl.httpClient.implementation
|
|
||||||
|
|
||||||
import assertk.assertThat
|
|
||||||
import assertk.assertions.isDataClassEqualTo
|
|
||||||
import assertk.assertions.isNotNull
|
|
||||||
import de.itkl.httpClient.auth.Credentials
|
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import org.junit.jupiter.api.Test
|
|
||||||
import java.nio.file.Paths
|
|
||||||
|
|
||||||
|
|
||||||
class StaticCredentialsProviderTest {
|
|
||||||
|
|
||||||
@Test
|
|
||||||
fun `can load credentials file`() = runBlocking {
|
|
||||||
val target = StaticCredentialsProvider.load(Paths.get("../../assets/credentials.toml"))
|
|
||||||
val credentials = target.lookupByUsername("test user")
|
|
||||||
assertThat(credentials)
|
|
||||||
.isNotNull()
|
|
||||||
.isDataClassEqualTo(Credentials.LoginAndPassword("TestiTest", "Secret"))
|
|
||||||
Unit
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue