-
Notifications
You must be signed in to change notification settings - Fork 13
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 DT cookie and fix IDX for org auth servers #202
Merged
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'm curious, is there a reason why the OAuth2Client is private? My Kotlin may be a bit rusty, but this will make it inaccessible to see by the developer, correct?
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.
That's correct. It's more of a legacy than an intentional decision to keep this private. I didn't think much about making this public because the developer can provide their own client to the constructor and have it available to them anyways. And it's also accessible to the developer through OAuth2Client.default if they construct the flow using the empty constructor. I can make this public