generated from y9vad9/kotlin-project-template
-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: secure credentials using
credentials-storage-kt
(#115)
- Loading branch information
Showing
15 changed files
with
157 additions
and
26 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
14 changes: 12 additions & 2 deletions
14
...a/src/commonMain/kotlin/io/timemates/app/authorization/data/DatabaseAccessHashProvider.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 |
---|---|---|
@@ -1,14 +1,24 @@ | ||
package io.timemates.app.authorization.data | ||
|
||
import io.timemates.app.authorization.data.database.AccountDatabaseQueries | ||
import io.timemates.credentials.CredentialsStorage | ||
import io.timemates.sdk.authorization.types.value.AccessHash | ||
import io.timemates.sdk.common.constructor.createOrThrow | ||
import io.timemates.sdk.common.exceptions.UnauthorizedException | ||
import io.timemates.sdk.common.providers.AccessHashProvider | ||
|
||
class DatabaseAccessHashProvider(private val localQueries: AccountDatabaseQueries) : AccessHashProvider { | ||
class DatabaseAccessHashProvider( | ||
private val localQueries: AccountDatabaseQueries, | ||
private val credentialsStorage: CredentialsStorage, | ||
) : AccessHashProvider { | ||
override suspend fun getOrNull(): AccessHash? { | ||
// TODO cache in memory | ||
return localQueries.getCurrent().executeAsOneOrNull() | ||
?.let { AccessHash.createOrThrow(it.accessHashValue) } | ||
?.let { | ||
AccessHash.createOrThrow( | ||
credentialsStorage.getString("access_hash_${it.id}") | ||
?: throw UnauthorizedException("Authorization wasn't saved to system credentials.") | ||
) | ||
} | ||
} | ||
} |
16 changes: 12 additions & 4 deletions
16
...n/data/src/commonMain/kotlin/io/timemates/app/authorization/data/DbAuthorizationMapper.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
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
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
54 changes: 54 additions & 0 deletions
54
.../android/src/main/kotlin/io/timemates/app/credentials/AndroidEncryptedPrefsCredentials.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,54 @@ | ||
package io.timemates.app.credentials | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import io.timemates.credentials.CredentialsStorage | ||
|
||
/** | ||
* Android encrypted shared preferences implementation of credentials manager | ||
* | ||
* TODO: remove when https://github.com/vanniktech/gradle-maven-publish-plugin/issues/705 is fixed and we will be able to use library for android target. | ||
* | ||
* @param context Application context. | ||
* | ||
* @suppress Warning! In robolectric test encrypting is disabled | ||
* @see createSharedPreferences | ||
*/ | ||
class AndroidEncryptedPrefsCredentials( | ||
context: Context, | ||
fileName: String = DEFAULT_ENCRYPTED_PREFS_FILE, | ||
) : CredentialsStorage { | ||
private val prefs: SharedPreferences = createSharedPreferences(context, fileName) | ||
|
||
override fun getString(key: String): String? { | ||
return prefs.getString(key, null) | ||
} | ||
|
||
override fun getInt(key: String): Int? { | ||
return prefs.getString(key, null)?.toIntOrNull() | ||
} | ||
|
||
override fun getLong(key: String): Long? { | ||
return prefs.getString(key, null)?.toLongOrNull() | ||
} | ||
|
||
override fun getBoolean(key: String): Boolean? { | ||
return prefs.getString(key, null)?.toBooleanStrictOrNull() | ||
} | ||
|
||
override fun setString(key: String, value: String) { | ||
prefs.edit().apply { putString(key, value) }.apply() | ||
} | ||
|
||
override fun setInt(key: String, value: Int) { | ||
prefs.edit().apply { putString(key, value.toString()) }.apply() | ||
} | ||
|
||
override fun setLong(key: String, value: Long) { | ||
prefs.edit().apply { putString(key, value.toString()) }.apply() | ||
} | ||
|
||
override fun setBoolean(key: String, value: Boolean) { | ||
prefs.edit().apply { putString(key, value.toString()) }.apply() | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
.../android/src/main/kotlin/io/timemates/app/credentials/createEncryptedSharedPreferences.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,28 @@ | ||
package io.timemates.app.credentials | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import android.os.Build | ||
import androidx.security.crypto.EncryptedSharedPreferences | ||
import androidx.security.crypto.MasterKeys | ||
|
||
internal const val DEFAULT_ENCRYPTED_PREFS_FILE = "credentials_storage.txt" | ||
|
||
/** | ||
* Create an encrypted SharedPreferences. | ||
* | ||
* @param context Application context. | ||
* | ||
* @suppress Warning! In robolectric test encrypting is disabled | ||
*/ | ||
internal fun createSharedPreferences(context: Context, fileName: String = DEFAULT_ENCRYPTED_PREFS_FILE): SharedPreferences { | ||
return if(Build.FINGERPRINT.lowercase() == "robolectric") // For tests | ||
context.getSharedPreferences("test", Context.MODE_PRIVATE) | ||
else EncryptedSharedPreferences.create( // For app | ||
fileName, | ||
MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC), | ||
context, | ||
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, | ||
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM | ||
) | ||
} |
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.