Skip to content
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

Add state parameter to AuthorizationCodeFlow.start #278

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import kotlin.time.Duration.Companion.seconds
class DeviceTokenCookieJar(private val oidcClock: OidcClock) : CookieJar {
private val savedCookiesCache = mutableMapOf<String, List<Cookie>>()

private val deviceTokenCookieBuilder = Cookie.Builder()
.name("DT")
.value(DeviceTokenProvider.deviceToken)
.secure()
private val deviceTokenCookieBuilder by lazy {
Cookie.Builder()
.name("DT")
.value(DeviceTokenProvider.deviceToken)
.secure()
}

override fun loadForRequest(url: HttpUrl): List<Cookie> {
val deviceTokenCookie = deviceTokenCookieBuilder.domain(url.host).build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
package com.okta.authfoundation.client

import android.content.Context
import android.content.SharedPreferences
import androidx.annotation.VisibleForTesting
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKeys
import java.util.UUID

class DeviceTokenProvider private constructor(appContext: Context) {
class DeviceTokenProvider private constructor(private val appContext: Context) {
internal companion object {
private const val FILE_NAME = "com.okta.authfoundation.device_token_storage"
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
Expand All @@ -43,14 +44,26 @@ class DeviceTokenProvider private constructor(appContext: Context) {

private val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)

private fun createSharedPreferences(): SharedPreferences {
return EncryptedSharedPreferences.create(
FILE_NAME,
masterKeyAlias,
appContext,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
}

@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal val sharedPrefs = EncryptedSharedPreferences.create(
FILE_NAME,
masterKeyAlias,
appContext,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
internal val sharedPrefs: SharedPreferences by lazy {
try {
createSharedPreferences()
} catch (e: Exception) {
val sharedPreferences = appContext.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE)
sharedPreferences.edit().clear().commit()
createSharedPreferences()
}
}

private val sharedPrefsEditor = sharedPrefs.edit()

Expand Down
4 changes: 2 additions & 2 deletions oauth2/api/oauth2.api
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ public final class com/okta/oauth2/AuthorizationCodeFlow {
public static final field Companion Lcom/okta/oauth2/AuthorizationCodeFlow$Companion;
public synthetic fun <init> (Lcom/okta/authfoundation/client/OidcClient;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public final fun resume (Landroid/net/Uri;Lcom/okta/oauth2/AuthorizationCodeFlow$Context;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun start (Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun start$default (Lcom/okta/oauth2/AuthorizationCodeFlow;Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
public final fun start (Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static synthetic fun start$default (Lcom/okta/oauth2/AuthorizationCodeFlow;Ljava/lang/String;Ljava/util/Map;Ljava/lang/String;Ljava/lang/String;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object;
}

public final class com/okta/oauth2/AuthorizationCodeFlow$Companion {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,12 @@ class AuthorizationCodeFlow private constructor(
redirectUrl: String,
extraRequestParameters: Map<String, String> = emptyMap(),
scope: String = oidcClient.configuration.defaultScope,
state: String = UUID.randomUUID().toString()
): OidcClientResult<Context> {
return start(
redirectUrl = redirectUrl,
codeVerifier = PkceGenerator.codeVerifier(),
state = UUID.randomUUID().toString(),
state = state,
nonce = UUID.randomUUID().toString(),
extraRequestParameters = extraRequestParameters,
scope = scope
Expand Down
Loading