This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #9838: Introduce CreditCardValidationDelegate and implement onC…
…reditCardSave in GeckoCreditCardsAddressesStorageDelegate - Introduces `CreditCardValidationDelegate` and a default implementation in `DefaultCreditCardValidationDelegate` - Implements `onCreditCardSave` in `GeckoCreditCardsAddressesStorageDelegate`
- Loading branch information
1 parent
713ee45
commit 415b0f4
Showing
6 changed files
with
329 additions
and
22 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
68 changes: 68 additions & 0 deletions
68
...main/java/mozilla/components/service/sync/autofill/DefaultCreditCardValidationDelegate.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,68 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.service.sync.autofill | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import mozilla.components.concept.storage.CreditCard | ||
import mozilla.components.concept.storage.CreditCardNumber | ||
import mozilla.components.concept.storage.CreditCardValidationDelegate | ||
import mozilla.components.concept.storage.CreditCardValidationDelegate.Result | ||
import mozilla.components.concept.storage.CreditCardsAddressesStorage | ||
|
||
/** | ||
* A delegate that will check against the [CreditCardsAddressesStorage] to determine if a given | ||
* [CreditCard] can be persisted and returns information about why it can or cannot. | ||
* | ||
* @param storage An instance of [CreditCardsAddressesStorage]. | ||
*/ | ||
class DefaultCreditCardValidationDelegate( | ||
private val storage: Lazy<CreditCardsAddressesStorage> | ||
) : CreditCardValidationDelegate { | ||
|
||
private val coroutineContext by lazy { Dispatchers.IO } | ||
|
||
override suspend fun validate(creditCard: CreditCard): Result = | ||
withContext(coroutineContext) { | ||
val creditCards = storage.value.getAllCreditCards() | ||
|
||
val foundCreditCard = if (creditCards.isEmpty()) { | ||
// No credit cards exist in the storage, create a new credit card to the storage. | ||
null | ||
} else { | ||
// Found a matching guid and credit card number -> update | ||
creditCards.find { | ||
it.guid == creditCard.guid && | ||
decrypt(it.encryptedCardNumber) == decrypt(creditCard.encryptedCardNumber) | ||
} | ||
// Found a matching guid -> update | ||
?: creditCards.find { it.guid == creditCard.guid } | ||
// Found a matching credit card number -> update | ||
?: creditCards.find { decrypt(it.encryptedCardNumber) == decrypt(creditCard.encryptedCardNumber) } | ||
// Found a non-matching guid and blank credit card number -> update | ||
?: creditCards.find { decrypt(it.encryptedCardNumber)!!.number.isEmpty() } | ||
// Else create a new credit card | ||
} | ||
|
||
if (foundCreditCard == null) { | ||
Result.CanBeCreated | ||
} else { | ||
Result.CanBeUpdated(foundCreditCard) | ||
} | ||
} | ||
|
||
/** | ||
* Helper function to decrypt a [CreditCardNumber.Encrypted] into its plaintext equivalent or | ||
* `null` if it fails to decrypt. | ||
* | ||
* @param encryptedCardNumber An encrypted credit card number to be decrypted. | ||
* @return A plaintext, non-encrypted credit card number. | ||
*/ | ||
private fun decrypt(encryptedCardNumber: CreditCardNumber.Encrypted): CreditCardNumber.Plaintext? { | ||
val crypto = storage.value.getCreditCardCrypto() | ||
val key = crypto.key() | ||
return crypto.decrypt(key, encryptedCardNumber) | ||
} | ||
} |
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
Oops, something went wrong.
415b0f4
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.
Uh oh! Looks like an error! Details
Failed to fetch task artifact
public/github/customCheckRunText.md
for GitHub integration.Make sure the artifact exists on the worker or other location.