11package com.example.sw0b_001.data
22
3+ import android.R.attr.phoneNumber
34import android.content.Context
45import android.util.Base64
56import android.util.Log
@@ -23,6 +24,9 @@ import java.security.DigestException
2324import java.security.MessageDigest
2425import androidx.core.content.edit
2526import com.example.sw0b_001.data.Publishers.Companion.PUBLISHER_ATTRIBUTE_FILES
27+ import com.example.sw0b_001.data.Publishers.Companion.storeArtifacts
28+ import com.example.sw0b_001.extensions.context.Settings
29+ import com.example.sw0b_001.extensions.context.settingsSetIsEmailLogin
2630
2731class Vaults (val context : Context ) {
2832 private val DEVICE_ID_KEYSTORE_ALIAS = " DEVICE_ID_KEYSTORE_ALIAS"
@@ -108,13 +112,16 @@ class Vaults(val context: Context) {
108112 }
109113 }
110114
111- private fun processVaultArtifacts (context : Context ,
112- encodedLlt : String ,
113- deviceIdPubKey : String ,
114- publisherPubKey : String ,
115- phoneNumber : String ,
116- clientDeviceIDPubKey : ByteArray ,
117- clientPublisherPubKey : ByteArray ) {
115+ private fun processVaultArtifacts (
116+ context : Context ,
117+ encodedLlt : String ,
118+ deviceIdPubKey : String ,
119+ publisherPubKey : String ,
120+ authValue : String ,
121+ clientDeviceIDPubKey : ByteArray ,
122+ clientPublisherPubKey : ByteArray ,
123+ isEmailLogin : Boolean ,
124+ ) {
118125 val deviceIdSharedKey = Cryptography .calculateSharedSecret(
119126 context,
120127 DEVICE_ID_KEYSTORE_ALIAS ,
@@ -126,10 +133,11 @@ class Vaults(val context: Context) {
126133
127134 val deviceId = getDeviceID(
128135 deviceIdSharedKey,
129- phoneNumber ,
136+ authValue ,
130137 clientDeviceIDPubKey
131138 )
132139
140+ context.settingsSetIsEmailLogin(isEmailLogin)
133141 storeArtifacts(context, llt, deviceId, clientDeviceIDPubKey)
134142 Publishers .storeArtifacts(context, publisherPubKey,
135143 Base64 .encodeToString(clientPublisherPubKey,
@@ -143,6 +151,7 @@ class Vaults(val context: Context) {
143151 fun createEntity (
144152 context : Context ,
145153 phoneNumber : String ,
154+ email : String ,
146155 countryCode : String ,
147156 password : String ,
148157 recaptchaToken : String ,
@@ -161,7 +170,8 @@ class Vaults(val context: Context) {
161170 setPassword(password)
162171 setClientPublishPubKey(Base64 .encodeToString(publishPubKey, Base64 .DEFAULT ))
163172 setClientDeviceIdPubKey(Base64 .encodeToString(deviceIdPubKey, Base64 .DEFAULT ))
164- setRecaptchaToken(recaptchaToken)
173+ setCaptchaToken(recaptchaToken)
174+ setEmailAddress(email)
165175 }.build()
166176
167177 val response = entityStub.createEntity(createEntityRequest1)
@@ -171,15 +181,18 @@ class Vaults(val context: Context) {
171181 response.longLivedToken,
172182 response.serverDeviceIdPubKey,
173183 response.serverPublishPubKey,
174- phoneNumber,
175- deviceIdPubKey, publishPubKey)
184+ email.ifEmpty { phoneNumber },
185+ deviceIdPubKey, publishPubKey,
186+ email.isNotEmpty(),
187+ )
176188 }
177189 return response
178190 }
179191
180192 fun authenticateEntity (
181193 context : Context ,
182194 phoneNumber : String ,
195+ email : String ,
183196 password : String ,
184197 recaptchaToken : String ,
185198 ownershipResponse : String = ""
@@ -197,7 +210,8 @@ class Vaults(val context: Context) {
197210 Base64 .DEFAULT ))
198211 setClientDeviceIdPubKey(Base64 .encodeToString(deviceIdPubKey,
199212 Base64 .DEFAULT ))
200- setRecaptchaToken(recaptchaToken)
213+ setCaptchaToken(recaptchaToken)
214+ setEmailAddress(email)
201215
202216 if (ownershipResponse.isNotBlank()) {
203217 setOwnershipProofResponse(ownershipResponse)
@@ -215,15 +229,18 @@ class Vaults(val context: Context) {
215229 response.longLivedToken,
216230 response.serverDeviceIdPubKey,
217231 response.serverPublishPubKey,
218- phoneNumber,
219- deviceIdPubKey, publishPubKey)
232+ email.ifEmpty { phoneNumber },
233+ deviceIdPubKey, publishPubKey,
234+ email.isNotEmpty(),
235+ )
220236 }
221237 return response
222238 }
223239
224240 fun recoverEntityPassword (
225241 context : Context ,
226242 phoneNumber : String ,
243+ email : String ,
227244 newPassword : String ,
228245 recaptchaToken : String ,
229246 ownershipResponse : String? = null
@@ -241,7 +258,8 @@ class Vaults(val context: Context) {
241258 Base64 .DEFAULT ))
242259 setClientDeviceIdPubKey(Base64 .encodeToString(deviceIdPubKey,
243260 Base64 .DEFAULT ))
244- setRecaptchaToken(recaptchaToken)
261+ setCaptchaToken(recaptchaToken)
262+ setEmailAddress(email)
245263
246264 ownershipResponse?.let {
247265 setOwnershipProofResponse(ownershipResponse)
@@ -254,8 +272,10 @@ class Vaults(val context: Context) {
254272 response.longLivedToken,
255273 response.serverDeviceIdPubKey,
256274 response.serverPublishPubKey,
257- phoneNumber,
258- deviceIdPubKey, publishPubKey)
275+ email.ifEmpty { phoneNumber },
276+ deviceIdPubKey, publishPubKey,
277+ email.isNotEmpty(),
278+ )
259279 }
260280 return response
261281 }
@@ -366,6 +386,10 @@ class Vaults(val context: Context) {
366386 PUBLISHER_ATTRIBUTE_FILES , Context .MODE_PRIVATE )
367387 sharedPreferences.edit { clear() }
368388
389+ context.getSharedPreferences(Settings .FILENAME , Context .MODE_PRIVATE ).apply {
390+ edit { putBoolean(Settings .SETTINGS_IS_EMAIL_LOGIN , false ).apply () }
391+ }
392+
369393 KeystoreHelpers .removeFromKeystore(context, DEVICE_ID_KEYSTORE_ALIAS )
370394 KeystoreHelpers .removeFromKeystore(context, DEVICE_ID_SECRET_KEY_KEYSTORE_ALIAS )
371395 KeystoreHelpers .removeFromKeystore(context, DEVICE_ID_PUB_KEY )
0 commit comments