okta / okta-mobile-kotlin

Okta's Android Authentication SDK
https://okta.github.io/okta-mobile-kotlin/
Apache License 2.0
35 stars 12 forks source link

DeviceTokenProvider not initialized #272

Closed FilipGimpel closed 9 months ago

FilipGimpel commented 10 months ago

Describe the bug?

Exception originating from the library crashes the app when instantiating OidcConfiguration.

What is expected to happen?

App doesn't crash

What is the actual behavior?

App crashes and the issue seems to be that DeviceTokenInitalizer doesn't initialize DeviceTokenProvider, which results in Exception

Caused by: kotlin.UninitializedPropertyAccessException: lateinit property instance has not been initialized
    at com.okta.authfoundation.client.DeviceTokenProvider$Companion.getDeviceToken$auth_foundation_release(DeviceTokenProvider.kt:33)
    at com.okta.authfoundation.client.DeviceTokenCookieJar.<init>(DeviceTokenCookieJar.kt:28)
    at com.okta.authfoundation.AuthFoundationDefaults$cookieJar$2.invoke(AuthFoundationDefaults.kt:75)
    at com.okta.authfoundation.AuthFoundationDefaults$cookieJar$2.invoke(AuthFoundationDefaults.kt:75)
    at com.okta.authfoundation.NoSetAfterGetWithLazyDefaultFactory.getValue(NoSetAfterGetWithLazyDefaultFactory.kt:30)
    at com.okta.authfoundation.AuthFoundationDefaults.getCookieJar(AuthFoundationDefaults.kt:75)
    at com.okta.authfoundation.client.OidcConfiguration.<init>(OidcConfiguration.kt:98)

Reproduction Steps?

I added the following packages as dependencies:

    implementation(platform("com.okta.kotlin:bom:1.2.0"))
    implementation("com.okta.kotlin:auth-foundation")
    implementation("com.okta.kotlin:auth-foundation-bootstrap")
    implementation("com.okta.kotlin:oauth2")

    implementation("com.okta.sdk:okta-sdk-okhttp:2.0.0")
    implementation("com.squareup.okhttp3:okhttp:4.11.0")

    implementation("com.okta.authn.sdk:okta-authn-sdk-api:2.0.0")
    runtimeOnly("com.okta.authn.sdk:okta-authn-sdk-impl:2.0.0") {
        exclude(group:"com.okta.sdk", module: "okta-sdk-httpclient")
        exclude(group: "org.bouncycastle")
    }

Im trying to set up OidcConfiguration in my Application class:

val oidcConfiguration = OidcConfiguration(
    clientId = "myClientId",
    defaultScope = "openid email profile offline_access"
)

Additional Information?

1) I am using hilt to inject Application class, thought this error persists when I move the instantiation code elsewhere 2) I am using gradle 7.6.1 3) I have a separate sample project using gradle 8+ without hilt and and it works just fine

SDK Version and Artifact(s) used.

implementation(platform("com.okta.kotlin:bom:1.2.0"))
implementation("com.okta.kotlin:auth-foundation")
implementation("com.okta.kotlin:auth-foundation-bootstrap")
implementation("com.okta.kotlin:oauth2")

implementation("com.okta.sdk:okta-sdk-okhttp:2.0.0")
implementation("com.squareup.okhttp3:okhttp:4.11.0")

implementation("com.okta.authn.sdk:okta-authn-sdk-api:2.0.0")
runtimeOnly("com.okta.authn.sdk:okta-authn-sdk-impl:2.0.0") {
    exclude(group:"com.okta.sdk", module: "okta-sdk-httpclient")
    exclude(group: "org.bouncycastle")
}

Build Information

No response

rajdeepnanua-okta commented 10 months ago

Hi @FilipGimpel, this is an interesting bug. The SDK uses Jetpack Startup for initializing DeviceTokenProvider, so it's surprising that it doesn't get initialized before Hilt injection. I will look into this shortly.

rajdeepnanua-okta commented 9 months ago

@FilipGimpel, this issue should be fixed in version 1.2.1.