From 5ab0402e39b0fe4f8db649990d3af2426fb1425f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Manciot?= Date: Thu, 19 Nov 2026 14:54:42 +0100 Subject: [PATCH] fix(account): refresh stored provider credentials on re-registration; stop logging them at INFO At every start, the configured providers are registered on their accounts (`RegisterAccountWithProvider`). For an account that already had a client for the provider, only the client API key (a hash of the provider key) was updated: the stored provider, and with it the provider API key that every provider call uses, was kept. A rotated provider key therefore never reached the provider calls, while the refreshed client API key suggested it had. The existing client now takes the provider of the registration and keeps its own settings (name, contact emails, address, access token). The rule lives in `SoftPayClientRegistration.registeredClient`, covered by a unit test. `PaymentGuardian.registerProvidersAccount` also logged the whole registered account at INFO, whose `toString` prints each client's provider API key in clear at every start. It now logs only the client id. Version 0.9.13. Co-Authored-By: Claude Opus 5.5 --- build.sbt | 2 +- .../payment/launch/PaymentGuardian.scala | 5 +- .../typed/SoftPayAccountBehavior.scala | 40 ++++++++++-- .../typed/SoftPayClientRegistrationSpec.scala | 64 +++++++++++++++++++ 4 files changed, 102 insertions(+), 9 deletions(-) create mode 100644 core/src/test/scala/app/softnetwork/payment/persistence/typed/SoftPayClientRegistrationSpec.scala 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 + } + } +}