diff --git a/build.sbt b/build.sbt index f526029..593b3da 100644 --- a/build.sbt +++ b/build.sbt @@ -18,7 +18,7 @@ ThisBuild / organization := "app.softnetwork" name := "payment" -ThisBuild / version := "0.9.12" +ThisBuild / version := "0.9.13" ThisBuild / scalaVersion := scala212 diff --git a/core/src/main/scala/app/softnetwork/payment/launch/PaymentGuardian.scala b/core/src/main/scala/app/softnetwork/payment/launch/PaymentGuardian.scala index 268d2f7..151a9bf 100644 --- a/core/src/main/scala/app/softnetwork/payment/launch/PaymentGuardian.scala +++ b/core/src/main/scala/app/softnetwork/payment/launch/PaymentGuardian.scala @@ -82,8 +82,9 @@ trait PaymentGuardian extends SessionGuardian { _: SchemaProvider with CsrfCheck .foreach(provider => { implicit val ec: ExecutionContext = system.executionContext softPayAccountDao.registerAccountWithProvider(provider)(system) map { - case Some(account) => - system.log.info(s"Registered provider account for ${provider.providerId}: $account") + case Some(_) => + // Never log the account itself: its clients carry the provider API key in clear. + system.log.info(s"Registered provider account ${provider.clientId}") case _ => system.log.warn(s"Failed to register provider account for ${provider.providerId}") } diff --git a/core/src/main/scala/app/softnetwork/payment/persistence/typed/SoftPayAccountBehavior.scala b/core/src/main/scala/app/softnetwork/payment/persistence/typed/SoftPayAccountBehavior.scala index 5b407a4..5cf3549 100644 --- a/core/src/main/scala/app/softnetwork/payment/persistence/typed/SoftPayAccountBehavior.scala +++ b/core/src/main/scala/app/softnetwork/payment/persistence/typed/SoftPayAccountBehavior.scala @@ -347,12 +347,8 @@ trait SoftPayAccountBehavior extends AccountBehavior[SoftPayAccount, BasicAccoun case AccountMessages.RegisterAccountWithProvider(provider) => state match { case Some(account) => - val updatedClient = account.clients.find(_.clientId == provider.clientId) match { - case Some(client) => - client.withClientApiKey(provider.client.getClientApiKey) - case _ => - provider.client - } + val updatedClient = + SoftPayClientRegistration.registeredClient(account, provider, provider.client) accountKeyDao.addAccountKey(provider.clientId, entityId) Effect .persist( @@ -466,3 +462,35 @@ trait SoftPayAccountBehavior extends AccountBehavior[SoftPayAccount, BasicAccoun case object SoftPayAccountBehavior extends SoftPayAccountBehavior with DefaultGenerator { override def persistenceId: String = "SoftPayAccount" } + +private[payment] object SoftPayClientRegistration { + + /** The client to persist when a provider is (re-)registered on an account, e.g. at every start + * from the configured provider credentials. + * + * An existing client keeps its own settings (name, contact emails, address, access token) and + * takes the provider credentials of the registration. Keeping the previously stored provider + * instead would leave every provider call on the old API key after a key rotation, while the + * client API key derived from the new one suggests the rotation was applied. + * + * @param account + * - the account the provider is registered on + * @param provider + * - the provider being registered + * @param newClient + * - the client derived from `provider`, used as is when the account has no client for it + * @return + * the client to persist + */ + def registeredClient( + account: SoftPayAccount, + provider: SoftPayAccount.Client.Provider, + newClient: => SoftPayAccount.Client + ): SoftPayAccount.Client = + account.clients.find(_.clientId == provider.clientId) match { + case Some(client) => + client.withProvider(provider).withClientApiKey(newClient.getClientApiKey) + case _ => + newClient + } +} diff --git a/core/src/test/scala/app/softnetwork/payment/persistence/typed/SoftPayClientRegistrationSpec.scala b/core/src/test/scala/app/softnetwork/payment/persistence/typed/SoftPayClientRegistrationSpec.scala new file mode 100644 index 0000000..3fcad1b --- /dev/null +++ b/core/src/test/scala/app/softnetwork/payment/persistence/typed/SoftPayClientRegistrationSpec.scala @@ -0,0 +1,64 @@ +package app.softnetwork.payment.persistence.typed + +import app.softnetwork.account.model.AccessToken +import app.softnetwork.payment.model.SoftPayAccount +import app.softnetwork.payment.model.SoftPayAccount.Client.Provider +import org.scalatest.matchers.should.Matchers +import org.scalatest.wordspec.AnyWordSpec + +class SoftPayClientRegistrationSpec extends AnyWordSpec with Matchers { + + private def provider(apiKey: String): Provider = + Provider.defaultInstance + .withProviderId("ca_example") + .withProviderApiKey(apiKey) + .withProviderType(Provider.ProviderType.STRIPE) + + private val oldProvider = provider("sk_test_old") + private val newProvider = provider("sk_test_new") + + private val accessToken = AccessToken.defaultInstance.withToken("access-token") + + private val storedClient: SoftPayAccount.Client = + SoftPayAccount.Client.defaultInstance + .withProvider(oldProvider) + .withClientId(oldProvider.clientId) + .withClientApiKey("hash-of-old-key") + .withName("client name") + .withTechnicalEmails(Seq("tech@example.com")) + .withAccessToken(accessToken) + + private def freshClient(p: Provider): SoftPayAccount.Client = + SoftPayAccount.Client.defaultInstance + .withProvider(p) + .withClientId(p.clientId) + .withClientApiKey(s"hash-of-${p.providerApiKey}") + + "Registering a provider on an account" should { + + "replace the stored provider credentials of an existing client" in { + val account = SoftPayAccount.defaultInstance.withClients(Seq(storedClient)) + val client = + SoftPayClientRegistration.registeredClient(account, newProvider, freshClient(newProvider)) + client.provider.providerApiKey shouldBe "sk_test_new" + client.getClientApiKey shouldBe "hash-of-sk_test_new" + } + + "keep the existing client's identity and settings" in { + val account = SoftPayAccount.defaultInstance.withClients(Seq(storedClient)) + val client = + SoftPayClientRegistration.registeredClient(account, newProvider, freshClient(newProvider)) + client.clientId shouldBe storedClient.clientId + client.getName shouldBe "client name" + client.technicalEmails shouldBe Seq("tech@example.com") + client.accessToken shouldBe Some(accessToken) + } + + "use the new client as is when the account has no client for that provider" in { + val other = provider("sk_test_other").withProviderId("ca_other") + val account = SoftPayAccount.defaultInstance.withClients(Seq(freshClient(other))) + val created = freshClient(newProvider) + SoftPayClientRegistration.registeredClient(account, newProvider, created) shouldBe created + } + } +}