Skip to content
Snippets Groups Projects
Commit a8a65496 authored by Taras's avatar Taras
Browse files

Add savig password with password manager

parent 1ed2e7d3
No related branches found
No related tags found
No related merge requests found
...@@ -95,6 +95,8 @@ abstract class UIADataSource { ...@@ -95,6 +95,8 @@ abstract class UIADataSource {
else -> LoginFlowTypes.TERMS else -> LoginFlowTypes.TERMS
} }
fun getUserId() = "@${userName}:${domain}"
private fun isStageRetry(result: RegistrationResult?): Boolean { private fun isStageRetry(result: RegistrationResult?): Boolean {
val nextStageType = val nextStageType =
((result as? RegistrationResult.FlowResponse)?.flowResult?.missingStages?.firstOrNull() as? Stage.Other)?.type ((result as? RegistrationResult.FlowResponse)?.flowResult?.missingStages?.firstOrNull() as? Stage.Other)?.type
...@@ -129,6 +131,7 @@ abstract class UIADataSource { ...@@ -129,6 +131,7 @@ abstract class UIADataSource {
SUBSCRPTION_GOOGLE_TYPE -> UIANavigationEvent.Subscription SUBSCRPTION_GOOGLE_TYPE -> UIANavigationEvent.Subscription
LOGIN_EMAIL_REQUEST_TOKEN_TYPE, LOGIN_EMAIL_REQUEST_TOKEN_TYPE,
ENROLL_EMAIL_REQUEST_TOKEN_TYPE -> UIANavigationEvent.ValidateEmail ENROLL_EMAIL_REQUEST_TOKEN_TYPE -> UIANavigationEvent.ValidateEmail
LOGIN_EMAIL_SUBMIT_TOKEN_TYPE, LOGIN_EMAIL_SUBMIT_TOKEN_TYPE,
ENROLL_EMAIL_SUBMIT_TOKEN_TYPE -> null //stay on same screen ENROLL_EMAIL_SUBMIT_TOKEN_TYPE -> null //stay on same screen
ENROLL_USERNAME_TYPE -> UIANavigationEvent.Username ENROLL_USERNAME_TYPE -> UIANavigationEvent.Username
......
...@@ -53,7 +53,7 @@ class LoginStagesDataSource @Inject constructor( ...@@ -53,7 +53,7 @@ class LoginStagesDataSource @Inject constructor(
} }
private fun getIdentifier() = mapOf( private fun getIdentifier() = mapOf(
USER_PARAM_KEY to "@$userName:$domain", USER_PARAM_KEY to getUserId(),
TYPE_PARAM_KEY to LOGIN_PASSWORD_USER_ID_TYPE TYPE_PARAM_KEY to LOGIN_PASSWORD_USER_ID_TYPE
) )
} }
\ No newline at end of file
...@@ -49,7 +49,11 @@ class PasswordFragment : ParentBackPressOwnerFragment(R.layout.fragment_password ...@@ -49,7 +49,11 @@ class PasswordFragment : ParentBackPressOwnerFragment(R.layout.fragment_password
setText(getString(if (isSignupMode()) R.string.set_password else R.string.log_in)) setText(getString(if (isSignupMode()) R.string.set_password else R.string.log_in))
setOnClickListener { setOnClickListener {
startLoading(btnLogin) startLoading(btnLogin)
viewModel.processPasswordStage(tilPassword.getText()) viewModel.processPasswordStage(
tilPassword.getText(),
isSignupMode(),
requireContext()
)
} }
} }
tilPassword.editText?.apply { tilPassword.editText?.apply {
......
package org.futo.circles.auth.feature.uia.stages.password package org.futo.circles.auth.feature.uia.stages.password
import android.content.Context import android.content.Context
import androidx.credentials.CreatePasswordRequest
import androidx.credentials.CredentialManager import androidx.credentials.CredentialManager
import androidx.credentials.GetCredentialRequest import androidx.credentials.GetCredentialRequest
import androidx.credentials.GetPasswordOption import androidx.credentials.GetPasswordOption
...@@ -23,17 +24,17 @@ class PasswordViewModel @Inject constructor( ...@@ -23,17 +24,17 @@ class PasswordViewModel @Inject constructor(
val passwordResponseLiveData = SingleEventLiveData<Response<Unit>>() val passwordResponseLiveData = SingleEventLiveData<Response<Unit>>()
val passwordSelectedEventLiveData = SingleEventLiveData<String>() val passwordSelectedEventLiveData = SingleEventLiveData<String>()
fun processPasswordStage(password: String) { fun processPasswordStage(password: String, isSignup: Boolean, activityContext: Context) {
launchBg { handlePasswordRequest(password) } launchBg { handlePasswordRequest(password, isSignup, activityContext) }
} }
fun getCredentials(activityContext: Context) { fun getCredentials(activityContext: Context) {
launchBg { launchBg {
tryOrNull { tryOrNull {
val credentialManager = CredentialManager.create(activityContext) val credentialManager = CredentialManager.create(activityContext)
val userName = UIADataSourceProvider.getDataSourceOrThrow().userName val userId = UIADataSourceProvider.getDataSourceOrThrow().getUserId()
val request = GetCredentialRequest( val request = GetCredentialRequest(
listOf(GetPasswordOption(allowedUserIds = setOf(userName))) listOf(GetPasswordOption(allowedUserIds = setOf(userId)))
) )
val result = credentialManager.getCredential( val result = credentialManager.getCredential(
...@@ -44,17 +45,36 @@ class PasswordViewModel @Inject constructor( ...@@ -44,17 +45,36 @@ class PasswordViewModel @Inject constructor(
if (result is PasswordCredential) { if (result is PasswordCredential) {
val password = result.password val password = result.password
passwordSelectedEventLiveData.postValue(password) passwordSelectedEventLiveData.postValue(password)
handlePasswordRequest(password) handlePasswordRequest(password, false, activityContext)
} }
} }
} }
} }
private suspend fun handlePasswordRequest(password: String) { private suspend fun handlePasswordRequest(
password: String,
isSignup: Boolean,
activityContext: Context
) {
if (isSignup) registerPassword(activityContext, password)
val result = passwordDataSource.processPasswordStage(password) val result = passwordDataSource.processPasswordStage(password)
passwordResponseLiveData.postValue(result) passwordResponseLiveData.postValue(result)
} }
private suspend fun registerPassword(activityContext: Context, password: String) {
tryOrNull {
val uiaDataSource = UIADataSourceProvider.getDataSourceOrThrow()
val createPasswordRequest = CreatePasswordRequest(
id = uiaDataSource.getUserId(),
password = password
)
CredentialManager.create(activityContext).createCredential(
activityContext,
createPasswordRequest
)
}
}
fun isPasswordWarningConfirmed() = isPasswordWarningConfirmed fun isPasswordWarningConfirmed() = isPasswordWarningConfirmed
fun confirmPasswordWarning() { fun confirmPasswordWarning() {
isPasswordWarningConfirmed = true isPasswordWarningConfirmed = true
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment