@@ -57,12 +57,12 @@ class DescopeFlowCoordinator(val webView: WebView) {
57
57
internal var listener: DescopeFlowView .Listener ? = null
58
58
internal var state: DescopeFlowView .State = Initial
59
59
60
- private lateinit var flow: DescopeFlow
60
+ private var flow: DescopeFlow ? = null
61
61
private val handler: Handler = Handler (Looper .getMainLooper())
62
- private val sdk: DescopeSdk
63
- get() = if ( this :: flow.isInitialized) flow. sdk ? : Descope .sdk else Descope .sdk
62
+ private val sdk: DescopeSdk ?
63
+ get() = flow?. sdk ? : if ( Descope .isInitialized) Descope .sdk else null
64
64
private val logger: DescopeLogger ?
65
- get() = sdk.client.config.logger
65
+ get() = sdk? .client? .config? .logger
66
66
private var currentFlowUrl: Uri ? = null
67
67
private var alreadySetUp = false
68
68
@@ -95,9 +95,8 @@ class DescopeFlowCoordinator(val webView: WebView) {
95
95
val jwtServerResponse = JwtServerResponse .fromJson(success, emptyList())
96
96
// take tokens from cookies if missing
97
97
val cookieString = CookieManager .getInstance().getCookie(url)
98
- val projectId = sdk.client.config.projectId
99
- jwtServerResponse.sessionJwt = jwtServerResponse.sessionJwt ? : findJwtInCookies(cookieString, projectId = projectId, name = SESSION_COOKIE_NAME )
100
- jwtServerResponse.refreshJwt = jwtServerResponse.refreshJwt ? : findJwtInCookies(cookieString, projectId = projectId, name = REFRESH_COOKIE_NAME )
98
+ jwtServerResponse.sessionJwt = jwtServerResponse.sessionJwt ? : findJwtInCookies(cookieString, name = SESSION_COOKIE_NAME )
99
+ jwtServerResponse.refreshJwt = jwtServerResponse.refreshJwt ? : findJwtInCookies(cookieString, name = REFRESH_COOKIE_NAME )
101
100
handler.post {
102
101
try {
103
102
val authResponse = jwtServerResponse.convert()
@@ -153,13 +152,13 @@ class DescopeFlowCoordinator(val webView: WebView) {
153
152
154
153
is NativePayload .OAuthWeb -> {
155
154
logger?.log(Info , " Launching custom tab for web-based oauth" )
156
- launchCustomTab(webView.context, nativePayload.startUrl, flow.presentation?.createCustomTabsIntent(webView.context))
155
+ launchCustomTab(webView.context, nativePayload.startUrl, flow? .presentation?.createCustomTabsIntent(webView.context))
157
156
return @launch
158
157
}
159
158
160
159
is NativePayload .Sso -> {
161
160
logger?.log(Info , " Launching custom tab for sso" )
162
- launchCustomTab(webView.context, nativePayload.startUrl, flow.presentation?.createCustomTabsIntent(webView.context))
161
+ launchCustomTab(webView.context, nativePayload.startUrl, flow? .presentation?.createCustomTabsIntent(webView.context))
163
162
return @launch
164
163
}
165
164
@@ -231,7 +230,7 @@ class DescopeFlowCoordinator(val webView: WebView) {
231
230
DoNothing -> true
232
231
OpenBrowser -> {
233
232
try {
234
- launchCustomTab(webView.context, uri, flow.presentation?.createCustomTabsIntent(webView.context))
233
+ launchCustomTab(webView.context, uri, flow? .presentation?.createCustomTabsIntent(webView.context))
235
234
} catch (e: DescopeException ) {
236
235
logger?.log(Error , " Failed to open URL in browser" , e)
237
236
}
@@ -259,10 +258,10 @@ class DescopeFlowCoordinator(val webView: WebView) {
259
258
evaluateJavascript(
260
259
setupScript(
261
260
origin = origin,
262
- oauthNativeProvider = flow.oauthNativeProvider?.name ? : " " ,
263
- oauthRedirect = pickRedirectUrl(flow.oauthRedirect, flow.oauthRedirectCustomScheme, useCustomSchemeFallback),
264
- ssoRedirect = pickRedirectUrl(flow.ssoRedirect, flow.ssoRedirectCustomScheme, useCustomSchemeFallback),
265
- magicLinkRedirect = flow.magicLinkRedirect ? : " " ,
261
+ oauthNativeProvider = flow? .oauthNativeProvider?.name ? : " " ,
262
+ oauthRedirect = pickRedirectUrl(flow? .oauthRedirect, flow? .oauthRedirectCustomScheme, useCustomSchemeFallback),
263
+ ssoRedirect = pickRedirectUrl(flow? .ssoRedirect, flow? .ssoRedirectCustomScheme, useCustomSchemeFallback),
264
+ magicLinkRedirect = flow? .magicLinkRedirect ? : " " ,
266
265
isWebAuthnSupported = isWebAuthnSupported,
267
266
)
268
267
) {}
@@ -332,7 +331,10 @@ document.head.appendChild(element)
332
331
}
333
332
334
333
internal fun resumeFromDeepLink (deepLink : Uri ) {
335
- if (! this ::flow.isInitialized) throw DescopeException .flowFailed.with (desc = " `resumeFromDeepLink` cannot be called before `startFlow`" )
334
+ if (flow == null ) {
335
+ logger?.log(Error , " resumeFromDeepLink cannot be called before startFlow" )
336
+ return
337
+ }
336
338
activityHelper.closeCustomTab(webView.context)
337
339
val response = JSONObject ().apply { put(" url" , deepLink.toString()) }
338
340
val type = if (deepLink.queryParameterNames.contains(" t" )) " magicLink" else " oauthWeb"
@@ -344,7 +346,7 @@ document.head.appendChild(element)
344
346
private fun executeHooks (event : Event ) {
345
347
val hooks = mutableListOf<DescopeFlowHook >().apply {
346
348
addAll(DescopeFlowHook .defaults)
347
- if ( this @DescopeFlowCoordinator::flow.isInitialized) addAll(flow.hooks)
349
+ addAll(flow? .hooks ? : emptyList() )
348
350
}
349
351
hooks.filter { it.events.contains(event) }
350
352
.forEach { it.execute(event, this ) }
@@ -499,7 +501,7 @@ private fun String.escapeForBackticks() = replace("\\", "\\\\")
499
501
500
502
// Cookies
501
503
502
- internal fun findJwtInCookies (cookieString : String? , projectId : String , name : String ): String? {
504
+ internal fun findJwtInCookies (cookieString : String? , name : String ): String? {
503
505
// split and aggregate all cookies
504
506
val cookies = mutableListOf<HttpCookie >().apply {
505
507
cookieString?.split(" ; " )?.forEach {
@@ -518,7 +520,6 @@ internal fun findJwtInCookies(cookieString: String?, projectId: String, name: St
518
520
null
519
521
}
520
522
}
521
- .filter { it.projectId == projectId } // enforce projectId
522
523
.maxByOrNull { it.issuedAt }?.jwt // take latest
523
524
}
524
525
0 commit comments