-
Notifications
You must be signed in to change notification settings - Fork 10
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
12 changed files
with
745 additions
and
25 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
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
126 changes: 126 additions & 0 deletions
126
model-client/src/jvmTest/kotlin/org/modelix/model/client2/OAuthTest.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,126 @@ | ||
package org.modelix.model.client2 | ||
|
||
import io.ktor.client.HttpClient | ||
import io.ktor.client.engine.cio.CIO | ||
import io.ktor.client.plugins.cookies.AcceptAllCookiesStorage | ||
import io.ktor.client.plugins.cookies.CookiesStorage | ||
import io.ktor.client.plugins.cookies.HttpCookies | ||
import io.ktor.client.request.forms.submitForm | ||
import io.ktor.client.request.get | ||
import io.ktor.client.statement.bodyAsText | ||
import io.ktor.http.Cookie | ||
import io.ktor.http.HttpHeaders | ||
import io.ktor.http.Url | ||
import io.ktor.http.parameters | ||
import kotlinx.coroutines.runBlocking | ||
import kotlinx.coroutines.withTimeout | ||
import org.modelix.model.api.ITree | ||
import org.modelix.model.lazy.RepositoryId | ||
import org.testcontainers.containers.ComposeContainer | ||
import org.testcontainers.containers.GenericContainer | ||
import org.testcontainers.containers.Network | ||
import org.testcontainers.images.builder.ImageFromDockerfile | ||
import org.testcontainers.utility.MountableFile | ||
import java.nio.file.Path | ||
import kotlin.io.path.absolute | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.time.Duration.Companion.minutes | ||
import kotlin.time.ExperimentalTime | ||
|
||
private val modelServerDir = Path.of("../model-server").absolute().normalize() | ||
private val modelServerImage = ImageFromDockerfile() | ||
.withDockerfile(modelServerDir.resolve("Dockerfile")) | ||
|
||
class OAuthTest { | ||
|
||
@Test | ||
fun test() = runWithModelServer { url -> | ||
val client = ModelClientV2.builder() | ||
.url(url) | ||
.retries(1U) | ||
.authRequestBrowser { authUrl -> | ||
runBlocking { | ||
handleOAuthLogin(authUrl, "user1", "abc") | ||
} | ||
} | ||
.build() | ||
client.init() | ||
|
||
val version = client.initRepository(RepositoryId("oauth-test-repo")) | ||
assertEquals(0, version.getTree().getAllChildren(ITree.ROOT_ID).count()) | ||
} | ||
|
||
private suspend fun handleOAuthLogin(authUrl: String, user: String, password: String) { | ||
val acceptAllCookiesStorage = AcceptAllCookiesStorage() | ||
val cookiesStorage = object : CookiesStorage by acceptAllCookiesStorage { | ||
override suspend fun addCookie(requestUrl: Url, cookie: Cookie) { | ||
acceptAllCookiesStorage.addCookie(requestUrl, cookie.copy(secure = false)) | ||
} | ||
} | ||
val httpClient = HttpClient(CIO) { | ||
install(HttpCookies) { | ||
storage = cookiesStorage | ||
} | ||
} | ||
val html = httpClient.get(authUrl).also { println(it.headers.entries()) }.bodyAsText() | ||
|
||
val loginUrl = Regex("""[^"]+/login-actions/authenticate[^"]+""").find(html)!!.value | ||
|
||
val callbackUrl = httpClient.submitForm( | ||
url = loginUrl, | ||
formParameters = parameters { | ||
set("username", user) | ||
set("password", password) | ||
}, | ||
).headers[HttpHeaders.Location]!! | ||
|
||
httpClient.get(callbackUrl).bodyAsText() | ||
} | ||
|
||
private fun runWithModelServer(body: suspend (url: String) -> Unit) = runBlocking { | ||
@OptIn(ExperimentalTime::class) | ||
withTimeout(3.minutes) { | ||
val network = Network.newNetwork() | ||
|
||
val keycloak: GenericContainer<*> = GenericContainer("quay.io/keycloak/keycloak:${System.getenv("KEYCLOAK_VERSION")}") | ||
.withExposedPorts(8080) | ||
.withCommand("start-dev", "--import-realm") | ||
.withEnv("KEYCLOAK_ADMIN", "admin") | ||
.withEnv("KEYCLOAK_ADMIN_PASSWORD", "admin") | ||
.withEnv("KC_HTTP_PORT", "8080") | ||
.withEnv("KC_HOSTNAME", "localhost") | ||
.withCopyFileToContainer(MountableFile.forHostPath("../model-server-with-auth/realm.json"), "/opt/keycloak/data/import/realm.json") | ||
.withNetwork(network) | ||
.withNetworkAliases("keycloak") | ||
.withLogConsumer { println("[KEYCLOAK] " + it.utf8StringWithoutLineEnding) } | ||
keycloak.start() | ||
val keycloakPort = keycloak.getMappedPort(8080) | ||
|
||
val modelServer: GenericContainer<*> = GenericContainer(modelServerImage) | ||
.withExposedPorts(28101) | ||
.withCommand("--inmemory") | ||
.withEnv("MODELIX_AUTHORIZATION_URI", "http://localhost:$keycloakPort/realms/modelix/protocol/openid-connect/auth") | ||
.withEnv("MODELIX_TOKEN_URI", "http://localhost:$keycloakPort/realms/modelix/protocol/openid-connect/token") | ||
.withEnv("MODELIX_PERMISSION_CHECKS_ENABLED", "true") | ||
.withEnv("MODELIX_JWK_URI_KEYCLOAK", "http://keycloak:8080/realms/modelix/protocol/openid-connect/certs") | ||
.withNetwork(network) | ||
.withNetworkAliases("model-server") | ||
.withLogConsumer { println("[MODEL] " + it.utf8StringWithoutLineEnding) } | ||
modelServer.start() | ||
|
||
try { | ||
body("http://localhost:${modelServer.firstMappedPort}/") | ||
} finally { | ||
modelServer.stop() | ||
keycloak.stop() | ||
} | ||
} | ||
} | ||
} | ||
|
||
private fun ComposeContainer.getServiceUrl(name: String, port: Int): String { | ||
val h = getServiceHost(name, port) | ||
val p = getServicePort(name, port) | ||
return "http://$h:$p/" | ||
} |
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,21 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<configuration debug="true"> | ||
<appender name="console" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="DEBUG"> | ||
<appender-ref ref="console" /> | ||
</root> | ||
|
||
<!-- | ||
Reduce log output crated by testcontainers. | ||
See https://java.testcontainers.org/supported_docker_environment/logging_config/ | ||
--> | ||
<logger name="org.testcontainers" level="INFO"/> | ||
<logger name="tc" level="INFO"/> | ||
<logger name="com.github.dockerjava" level="WARN"/> | ||
<logger name="com.github.dockerjava.zerodep.shaded.org.apache.hc.client5.http.wire" level="OFF"/> | ||
</configuration> |
Oops, something went wrong.