-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add configurable webserver for jvm and fix default HTTP status …
…code. Fixes #65. It is now possible to configure the JvmCodeAuthFlowFactory / PlatformCodeAuthFlow using a webserverProvider. The pre-defined SimpleKtorWebserver is configurable to send custom results and now sends a default HTML response.
- Loading branch information
Showing
6 changed files
with
82 additions
and
42 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
5 changes: 4 additions & 1 deletion
5
...rc/jvmMain/kotlin/org/publicvalue/multiplatform/oidc/appsupport/JvmCodeAuthFlowFactory.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,11 +1,14 @@ | ||
package org.publicvalue.multiplatform.oidc.appsupport | ||
|
||
import org.publicvalue.multiplatform.oidc.OpenIdConnectClient | ||
import org.publicvalue.multiplatform.oidc.appsupport.webserver.SimpleKtorWebserver | ||
import org.publicvalue.multiplatform.oidc.appsupport.webserver.Webserver | ||
|
||
@Suppress("unused") | ||
class JvmCodeAuthFlowFactory( | ||
private val webserverProvider: () -> Webserver = { SimpleKtorWebserver() } | ||
): CodeAuthFlowFactory { | ||
override fun createAuthFlow(client: OpenIdConnectClient): PlatformCodeAuthFlow { | ||
return PlatformCodeAuthFlow(client) | ||
return PlatformCodeAuthFlow(client, webserverProvider = webserverProvider) | ||
} | ||
} |
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
61 changes: 61 additions & 0 deletions
61
...ain/kotlin/org/publicvalue/multiplatform/oidc/appsupport/webserver/SimpleKtorWebserver.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,61 @@ | ||
package org.publicvalue.multiplatform.oidc.appsupport.webserver | ||
|
||
import io.ktor.http.ContentType | ||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.server.application.ApplicationCall | ||
import io.ktor.server.application.call | ||
import io.ktor.server.cio.CIO | ||
import io.ktor.server.cio.CIOApplicationEngine | ||
import io.ktor.server.engine.embeddedServer | ||
import io.ktor.server.request.ApplicationRequest | ||
import io.ktor.server.response.respondText | ||
import io.ktor.server.routing.get | ||
import io.ktor.server.routing.routing | ||
import org.publicvalue.multiplatform.oidc.flows.AuthCodeResult | ||
|
||
class SimpleKtorWebserver( | ||
val createResponse: suspend io.ktor.util.pipeline.PipelineContext<Unit, ApplicationCall>.() -> Unit = { | ||
call.respondText( | ||
status = HttpStatusCode.OK, | ||
text = """ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Authorization redirect successful</title> | ||
</head> | ||
<body> | ||
<h1>You may now close this page and return to your app.</h1> | ||
</body> | ||
</html> | ||
""".trimIndent(), | ||
contentType = ContentType.parse("text/html") | ||
) | ||
} | ||
): Webserver { | ||
private var server: CIOApplicationEngine? = null | ||
|
||
override suspend fun startAndWaitForRedirect(port: Int, redirectPath: String): AuthCodeResult { | ||
var call: ApplicationRequest? = null | ||
server?.stop() | ||
embeddedServer(CIO, port = port) { | ||
routing { | ||
get(redirectPath) { | ||
createResponse() | ||
call = this.call.request | ||
server?.stop() | ||
} | ||
} | ||
}.apply { | ||
server = this | ||
start(wait = true) | ||
} | ||
val code = call?.queryParameters?.get("code") | ||
val state = call?.queryParameters?.get("code") | ||
return AuthCodeResult(code, state) | ||
} | ||
|
||
override suspend fun stop() { | ||
server?.stop() | ||
} | ||
} |
43 changes: 12 additions & 31 deletions
43
...t/src/jvmMain/kotlin/org/publicvalue/multiplatform/oidc/appsupport/webserver/Webserver.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,36 +1,17 @@ | ||
package org.publicvalue.multiplatform.oidc.appsupport.webserver | ||
|
||
import io.ktor.http.HttpStatusCode | ||
import io.ktor.server.application.* | ||
import io.ktor.server.cio.CIO | ||
import io.ktor.server.cio.CIOApplicationEngine | ||
import io.ktor.server.engine.embeddedServer | ||
import io.ktor.server.request.ApplicationRequest | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import org.publicvalue.multiplatform.oidc.flows.AuthCodeResult | ||
|
||
class Webserver() { | ||
private var server: CIOApplicationEngine? = null | ||
interface Webserver { | ||
/** | ||
* Start a local Webserver on the given port, waiting for the redirectPath to be called. | ||
* | ||
* @return RedirectResponse containing authCode + state. | ||
*/ | ||
suspend fun startAndWaitForRedirect(port: Int, redirectPath: String): AuthCodeResult | ||
|
||
suspend fun startAndWaitForRedirect(port: Int, redirectPath: String): ApplicationRequest? { | ||
var call: ApplicationRequest? = null | ||
server?.stop() | ||
embeddedServer(CIO, port = port) { | ||
routing { | ||
get(redirectPath) { | ||
this.call.respond(status = HttpStatusCode.OK, Unit) | ||
call = this.call.request | ||
server?.stop() | ||
} | ||
} | ||
}.apply { | ||
server = this | ||
start(wait = true) | ||
} | ||
return call | ||
} | ||
|
||
fun stop() { | ||
server?.stop() | ||
} | ||
/** | ||
* Stop the webserver. | ||
*/ | ||
suspend fun stop() | ||
} |