-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #202 from okta/dt_cookie
Add DT cookie and fix IDX for org auth servers
- Loading branch information
Showing
11 changed files
with
368 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
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
55 changes: 55 additions & 0 deletions
55
okta-idx-kotlin/src/main/java/com/okta/idx/kotlin/client/DeviceTokenCookieJar.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,55 @@ | ||
/* | ||
* Copyright 2024-Present Okta, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.okta.idx.kotlin.client | ||
|
||
import com.okta.authfoundation.AuthFoundationDefaults | ||
import com.okta.authfoundation.client.DeviceTokenProvider | ||
import kotlinx.coroutines.runBlocking | ||
import okhttp3.Cookie | ||
import okhttp3.CookieJar | ||
import okhttp3.HttpUrl | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
internal class DeviceTokenCookieJar : CookieJar { | ||
private val savedCookiesCache = mutableMapOf<String, List<Cookie>>() | ||
private val oidcClock by lazy { AuthFoundationDefaults.clock } | ||
|
||
private val deviceTokenCookieBuilder: Cookie.Builder | ||
get() { | ||
val deviceToken = runBlocking { DeviceTokenProvider.instance.getDeviceToken() } | ||
return getDtCookieBuilderWith(deviceToken) | ||
} | ||
|
||
private fun getDtCookieBuilderWith(deviceToken: String): Cookie.Builder { | ||
return Cookie.Builder() | ||
.name("DT") | ||
.value(deviceToken) | ||
.secure() | ||
} | ||
|
||
override fun loadForRequest(url: HttpUrl): List<Cookie> { | ||
val deviceTokenCookie = deviceTokenCookieBuilder.domain(url.host).build() | ||
val savedCookiesForDomain = savedCookiesCache[url.host]?.filter { | ||
it.expiresAt > oidcClock.currentTimeEpochSecond().seconds.inWholeMilliseconds | ||
} ?: emptyList() | ||
val deviceTokenCookieList = listOf(deviceTokenCookie) | ||
return savedCookiesForDomain + deviceTokenCookieList | ||
} | ||
|
||
override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) { | ||
savedCookiesCache[url.host] = cookies | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
okta-idx-kotlin/src/main/java/com/okta/idx/kotlin/client/DeviceTokenProvider.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,72 @@ | ||
/* | ||
* Copyright 2024-Present Okta, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.okta.authfoundation.client | ||
|
||
import android.content.Context | ||
import androidx.datastore.core.DataStore | ||
import androidx.datastore.preferences.core.Preferences | ||
import androidx.datastore.preferences.core.edit | ||
import androidx.datastore.preferences.core.stringPreferencesKey | ||
import androidx.datastore.preferences.preferencesDataStore | ||
import com.okta.authfoundation.InternalAuthFoundationApi | ||
import com.okta.authfoundation.util.AesEncryptionHandler | ||
import com.okta.idx.kotlin.client.LegacyDeviceTokenProvider | ||
import kotlinx.coroutines.flow.firstOrNull | ||
import java.util.UUID | ||
|
||
@InternalAuthFoundationApi | ||
class DeviceTokenProvider( | ||
private val aesEncryptionHandler: AesEncryptionHandler = AesEncryptionHandler() | ||
) { | ||
companion object { | ||
const val PREFERENCE_NAME = "com.okta.idx.client.deviceToken" | ||
val PREFERENCE_KEY = stringPreferencesKey("encryptedDeviceToken") | ||
val instance by lazy { DeviceTokenProvider() } | ||
} | ||
|
||
private val Context.dataStore: DataStore<Preferences> by preferencesDataStore(PREFERENCE_NAME) | ||
private val context by lazy { ApplicationContextHolder.appContext } | ||
|
||
suspend fun getDeviceToken(): String { | ||
val encryptedDeviceToken = context.dataStore.data.firstOrNull()?.get(PREFERENCE_KEY) | ||
return encryptedDeviceToken?.let { | ||
aesEncryptionHandler.decryptString(it) | ||
} ?: run { | ||
val deviceToken = getLegacyDeviceToken() ?: createNewDeviceToken() | ||
setDeviceToken(deviceToken) | ||
deviceToken | ||
} | ||
} | ||
|
||
private fun getLegacyDeviceToken(): String? { | ||
return try { | ||
val legacyDeviceTokenProvider = LegacyDeviceTokenProvider(context) | ||
if (legacyDeviceTokenProvider.containsDeviceToken()) { | ||
legacyDeviceTokenProvider.deviceToken | ||
} else null | ||
} catch (e: Exception) { | ||
null | ||
} | ||
} | ||
|
||
private fun createNewDeviceToken(): String = | ||
UUID.randomUUID().toString().filter { it.isLetterOrDigit() } | ||
|
||
private suspend fun setDeviceToken(deviceToken: String) = | ||
context.dataStore.edit { preferences -> | ||
preferences[PREFERENCE_KEY] = aesEncryptionHandler.encryptString(deviceToken) | ||
} | ||
} |
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
72 changes: 72 additions & 0 deletions
72
okta-idx-kotlin/src/main/java/com/okta/idx/kotlin/client/LegacyDeviceTokenProvider.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,72 @@ | ||
/* | ||
* Copyright 2024-Present Okta, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.okta.idx.kotlin.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 | ||
|
||
internal class LegacyDeviceTokenProvider(private val appContext: Context) { | ||
internal companion object { | ||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) | ||
internal const val FILE_NAME = "com.okta.authfoundation.device_token_storage" | ||
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE) | ||
internal const val PREFERENCE_KEY = "com.okta.authfoundation.device_token_key" | ||
} | ||
|
||
private val masterKeyAlias by lazy { 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: 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 by lazy { sharedPrefs.edit() } | ||
|
||
internal val deviceTokenUUID: String | ||
get() { | ||
return sharedPrefs.getString(PREFERENCE_KEY, null) ?: run { | ||
val newDeviceToken = UUID.randomUUID().toString() | ||
sharedPrefsEditor.putString(PREFERENCE_KEY, newDeviceToken) | ||
sharedPrefsEditor.commit() | ||
newDeviceToken | ||
} | ||
} | ||
|
||
internal fun containsDeviceToken(): Boolean = sharedPrefs.contains(PREFERENCE_KEY) | ||
|
||
internal val deviceToken: String | ||
get() = deviceTokenUUID.filter { it.isLetterOrDigit() } | ||
} |
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
Oops, something went wrong.