-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement PIN and Biometric authentication (#48)
Fixes #20
- Loading branch information
Showing
30 changed files
with
1,660 additions
and
33 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
13 changes: 13 additions & 0 deletions
13
app/src/main/java/com/xinto/mauth/core/auth/AuthManager.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,13 @@ | ||
package com.xinto.mauth.core.auth | ||
|
||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface AuthManager { | ||
|
||
fun getCode(): Flow<String?> | ||
|
||
fun setCode(code: String) | ||
|
||
fun removeCode() | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
app/src/main/java/com/xinto/mauth/core/auth/DefaultAuthManager.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 com.xinto.mauth.core.auth | ||
|
||
import android.content.Context | ||
import android.content.SharedPreferences | ||
import androidx.core.content.edit | ||
import androidx.security.crypto.EncryptedSharedPreferences | ||
import androidx.security.crypto.MasterKey | ||
import kotlinx.coroutines.channels.awaitClose | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.callbackFlow | ||
|
||
class DefaultAuthManager( | ||
context: Context | ||
) : AuthManager { | ||
|
||
private val prefs = EncryptedSharedPreferences.create( | ||
context, | ||
"auth", | ||
MasterKey(context = context), | ||
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, | ||
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM, | ||
) | ||
|
||
override fun getCode(): Flow<String?> { | ||
return callbackFlow { | ||
val listener = SharedPreferences.OnSharedPreferenceChangeListener { sharedPreferences, key -> | ||
if (key == KEY_CODE) { | ||
trySend(sharedPreferences.getString(KEY_CODE, null)) | ||
} | ||
} | ||
send(prefs.getString(KEY_CODE, null)) | ||
prefs.registerOnSharedPreferenceChangeListener(listener) | ||
awaitClose { | ||
prefs.unregisterOnSharedPreferenceChangeListener(listener) | ||
} | ||
} | ||
} | ||
|
||
override fun setCode(code: String) { | ||
prefs.edit { | ||
putString(KEY_CODE, code) | ||
} | ||
} | ||
|
||
override fun removeCode() { | ||
prefs.edit { | ||
remove(KEY_CODE) | ||
} | ||
} | ||
|
||
private companion object { | ||
const val KEY_CODE = "code" | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
app/src/main/java/com/xinto/mauth/domain/AuthRepository.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,34 @@ | ||
package com.xinto.mauth.domain | ||
|
||
import com.xinto.mauth.core.auth.AuthManager | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.first | ||
import kotlinx.coroutines.flow.last | ||
import kotlinx.coroutines.flow.map | ||
|
||
class AuthRepository( | ||
private val authManager: AuthManager | ||
) { | ||
|
||
private val liveCode = authManager.getCode() | ||
|
||
fun observeIsProtected(): Flow<Boolean> { | ||
return liveCode.map { it != null } | ||
} | ||
|
||
suspend fun isProtected(): Boolean { | ||
return liveCode.first() != null | ||
} | ||
|
||
suspend fun validate(code: String): Boolean { | ||
return liveCode.first() == code | ||
} | ||
|
||
fun updateCode(code: String) { | ||
authManager.setCode(code) | ||
} | ||
|
||
fun removeCode() { | ||
authManager.removeCode() | ||
} | ||
} |
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.