-
Notifications
You must be signed in to change notification settings - Fork 35
Add webMain support #145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Add webMain support #145
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,10 @@ multiplatformSwiftPackage { | |
|
|
||
| kotlin { | ||
| jvm() | ||
| js(IR) { | ||
| browser() | ||
| binaries.library() | ||
| } | ||
|
Comment on lines
+28
to
+31
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we have this in a "configureJsTarget" function just like the others? |
||
| configureIosTargets(baseName = "OpenIdConnectClient") | ||
| configureWasmTarget(baseName = "OpenIdConnectClient") | ||
| sourceSets { | ||
|
|
@@ -55,7 +59,7 @@ kotlin { | |
| } | ||
| } | ||
|
|
||
| val wasmJsMain by getting { | ||
| val webMain by getting { | ||
| dependencies { | ||
| implementation(libs.kotlinx.browser) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package org.publicvalue.multiplatform.oidc.appsupport | ||
|
|
||
| import io.ktor.http.Url | ||
| import kotlinx.browser.window | ||
| import kotlinx.coroutines.suspendCancellableCoroutine | ||
| import org.publicvalue.multiplatform.oidc.ExperimentalOpenIdConnect | ||
| import org.w3c.dom.MessageEvent | ||
| import org.w3c.dom.Window | ||
| import org.w3c.dom.events.Event | ||
| import kotlin.coroutines.resume | ||
|
|
||
| @ExperimentalOpenIdConnect | ||
| internal actual class WebPopupFlow actual constructor( | ||
| private val windowTarget: String, | ||
| private val windowFeatures: String, | ||
| private val redirectOrigin: String, | ||
| ) : WebAuthenticationFlow { | ||
|
|
||
| actual override suspend fun startWebFlow( | ||
| requestUrl: Url, | ||
| redirectUrl: String | ||
| ): WebAuthenticationFlowResult = suspendCancellableCoroutine { continuation -> | ||
| val popup: Window? = window.open(requestUrl.toString(), windowTarget, windowFeatures) | ||
| if (popup == null) { | ||
| continuation.resume(WebAuthenticationFlowResult.Cancelled) | ||
| return@suspendCancellableCoroutine | ||
| } | ||
|
|
||
| var intervalId: Int? = null | ||
| lateinit var messageListener: (Event) -> Unit | ||
|
|
||
| messageListener = messageListener@{ event: Event -> | ||
| val messageEvent = event as? MessageEvent ?: return@messageListener | ||
| if (messageEvent.origin != redirectOrigin) { | ||
| return@messageListener | ||
| } | ||
| if (messageEvent.source != popup) { | ||
| return@messageListener | ||
| } | ||
| val urlString = when (val data = messageEvent.data) { | ||
| is String -> data | ||
| else -> JSON.stringify(data) | ||
| } | ||
| window.removeEventListener("message", messageListener) | ||
| intervalId?.let { window.clearInterval(it) } | ||
| continuation.resume(WebAuthenticationFlowResult.Success(Url(urlString))) | ||
| popup.close() | ||
| } | ||
|
|
||
| window.addEventListener("message", messageListener) | ||
|
|
||
| intervalId = window.setInterval({ | ||
| if (popup.closed) { | ||
| window.removeEventListener("message", messageListener) | ||
| intervalId?.let { window.clearInterval(it) } | ||
| if (continuation.isActive) { | ||
| continuation.resume(WebAuthenticationFlowResult.Cancelled) | ||
| } | ||
| } | ||
| }, 500) | ||
|
|
||
| continuation.invokeOnCancellation { | ||
| window.removeEventListener("message", messageListener) | ||
| intervalId.let { window.clearInterval(it) } | ||
| if (!popup.closed) { | ||
| popup.close() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| actual companion object { | ||
| @ExperimentalOpenIdConnect | ||
| actual fun handleRedirect() { | ||
| val openerWindow = window.opener as? Window ?: return | ||
| val targetOrigin = openerWindow.location.origin | ||
| openerWindow.postMessage(window.location.toString(), targetOrigin) | ||
| window.close() | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,27 +23,31 @@ actual class PlatformCodeAuthFlow( | |
|
|
||
| @ExperimentalOpenIdConnect | ||
| actual override suspend fun getAuthorizationCode(request: AuthCodeRequest): AuthCodeResponse { | ||
| val result = webFlow.startWebFlow(request.url, request.url.parameters.get("redirect_uri").orEmpty()) | ||
|
|
||
| return if (result is WebAuthenticationFlowResult.Success) { | ||
| when (val error = getErrorResult<AuthCodeResult>(result.responseUri)) { | ||
| null -> { | ||
| val state = result.responseUri.parameters.get("state") | ||
| val code = result.responseUri.parameters.get("code") | ||
| Result.success(AuthCodeResult(code, state)) | ||
| } | ||
| else -> { | ||
| return error | ||
| } | ||
| } | ||
| } else { | ||
| val result = webFlow.startWebFlow(request.url, request.url.parameters["redirect_uri"].orEmpty()) | ||
|
|
||
| if (result !is WebAuthenticationFlowResult.Success) { | ||
| // browser closed, no redirect | ||
| Result.failure(OpenIdConnectException.AuthenticationCancelled()) | ||
| return Result.failure(OpenIdConnectException.AuthenticationCancelled()) | ||
| } | ||
|
|
||
| if (result.responseUri == null) { | ||
| return Result.failure(OpenIdConnectException.AuthenticationFailure("No Uri in callback from browser.")) | ||
| } | ||
|
|
||
| return when (val error = getErrorResult<AuthCodeResult>(result.responseUri)) { | ||
| null -> { | ||
| val state = result.responseUri.parameters["state"] | ||
| val code = result.responseUri.parameters["code"] | ||
| Result.success(AuthCodeResult(code, state)) | ||
| } | ||
| else -> { | ||
| return error | ||
| } | ||
|
Comment on lines
+26
to
+45
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we keep this in the old-style nested form so it looks just like in iOS/Android/JVM? |
||
| } | ||
| } | ||
|
|
||
| actual override suspend fun endSession(request: EndSessionRequest): EndSessionResponse { | ||
| val redirectUrl = request.url.parameters.get("post_logout_redirect_uri").orEmpty() | ||
| val redirectUrl = request.url.parameters["post_logout_redirect_uri"].orEmpty() | ||
| webFlow.startWebFlow(request.url, redirectUrl) | ||
| return Result.success(Unit) | ||
| } | ||
|
|
@@ -54,5 +58,4 @@ actual class PlatformCodeAuthFlow( | |
| WebPopupFlow.handleRedirect() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package org.publicvalue.multiplatform.oidc.appsupport | ||
|
|
||
| import kotlinx.browser.window | ||
| import org.publicvalue.multiplatform.oidc.ExperimentalOpenIdConnect | ||
| import org.publicvalue.multiplatform.oidc.OpenIdConnectClient | ||
| import org.publicvalue.multiplatform.oidc.flows.EndSessionFlow | ||
|
|
||
| @ExperimentalOpenIdConnect | ||
| class WebMainCodeAuthFlowFactory( | ||
| private val windowTarget: String = "", | ||
| private val windowFeatures: String = "width=1000,height=800,resizable=yes,scrollbars=yes", | ||
| private val redirectOrigin: String = window.location.origin, | ||
| ) : CodeAuthFlowFactory { | ||
|
|
||
| override fun createAuthFlow(client: OpenIdConnectClient): PlatformCodeAuthFlow { | ||
| return PlatformCodeAuthFlow( | ||
| windowTarget = windowTarget, | ||
| windowFeatures = windowFeatures, | ||
| redirectOrigin = redirectOrigin, | ||
| client = client, | ||
| ) | ||
| } | ||
|
|
||
| override fun createEndSessionFlow(client: OpenIdConnectClient): EndSessionFlow { | ||
| return createAuthFlow(client) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package org.publicvalue.multiplatform.oidc.appsupport | ||
|
|
||
| import io.ktor.http.Url | ||
| import org.publicvalue.multiplatform.oidc.ExperimentalOpenIdConnect | ||
|
|
||
| @ExperimentalOpenIdConnect | ||
| internal expect class WebPopupFlow( | ||
| windowTarget: String = "", | ||
| windowFeatures: String = "width=1000,height=800,resizable=yes,scrollbars=yes", | ||
| redirectOrigin: String, | ||
| ) : WebAuthenticationFlow { | ||
| override suspend fun startWebFlow( | ||
| requestUrl: Url, | ||
| redirectUrl: String | ||
| ): WebAuthenticationFlowResult | ||
|
|
||
| companion object { | ||
| @ExperimentalOpenIdConnect | ||
| fun handleRedirect() | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ kotlin { | |
| } | ||
| } | ||
|
|
||
| wasmJsMain { | ||
| webMain { | ||
| dependencies { | ||
| implementation(libs.kotlinx.browser) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we need both webMain and wasmJsMain here?