ServiceTokenBootstrapTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Integration\Auth;
  4. use App\Domain\Auth\TokenIssuer;
  5. use App\Domain\Auth\TokenKind;
  6. use App\Infrastructure\Auth\ServiceTokenBootstrap;
  7. use App\Tests\Integration\Support\AppTestCase;
  8. final class ServiceTokenBootstrapTest extends AppTestCase
  9. {
  10. public function testBootstrapInsertsServiceTokenRow(): void
  11. {
  12. /** @var ServiceTokenBootstrap $boot */
  13. $boot = $this->container->get(ServiceTokenBootstrap::class);
  14. /** @var TokenIssuer $issuer */
  15. $issuer = $this->container->get(TokenIssuer::class);
  16. $raw = $issuer->issue(TokenKind::Service);
  17. $boot->bootstrap($raw);
  18. $count = (int) $this->db->fetchOne(
  19. "SELECT COUNT(*) FROM api_tokens WHERE kind = 'service'"
  20. );
  21. self::assertSame(1, $count);
  22. }
  23. public function testBootstrapIsIdempotent(): void
  24. {
  25. /** @var ServiceTokenBootstrap $boot */
  26. $boot = $this->container->get(ServiceTokenBootstrap::class);
  27. /** @var TokenIssuer $issuer */
  28. $issuer = $this->container->get(TokenIssuer::class);
  29. $raw = $issuer->issue(TokenKind::Service);
  30. $boot->bootstrap($raw);
  31. $boot->bootstrap($raw);
  32. $boot->bootstrap($raw);
  33. self::assertSame(
  34. 1,
  35. (int) $this->db->fetchOne("SELECT COUNT(*) FROM api_tokens WHERE kind = 'service'")
  36. );
  37. }
  38. public function testBootstrapWithEmptyTokenIsNoOp(): void
  39. {
  40. /** @var ServiceTokenBootstrap $boot */
  41. $boot = $this->container->get(ServiceTokenBootstrap::class);
  42. $boot->bootstrap('');
  43. self::assertSame(
  44. 0,
  45. (int) $this->db->fetchOne("SELECT COUNT(*) FROM api_tokens WHERE kind = 'service'")
  46. );
  47. }
  48. public function testBootstrapWithMalformedTokenIsNoOp(): void
  49. {
  50. /** @var ServiceTokenBootstrap $boot */
  51. $boot = $this->container->get(ServiceTokenBootstrap::class);
  52. $boot->bootstrap('not-a-token');
  53. self::assertSame(
  54. 0,
  55. (int) $this->db->fetchOne("SELECT COUNT(*) FROM api_tokens WHERE kind = 'service'")
  56. );
  57. }
  58. /**
  59. * SEC_REVIEW F13. The previous implementation left the old service-kind
  60. * row valid forever (the operator was expected to revoke it manually).
  61. * Bootstrap now revokes any previously-active service-kind row when a
  62. * new value is provisioned, so a leaked old token cannot authenticate
  63. * after the next bootstrap of a fresh value.
  64. */
  65. public function testBootstrapWithDifferentTokenRevokesPreviousAndInsertsNewRow(): void
  66. {
  67. /** @var ServiceTokenBootstrap $boot */
  68. $boot = $this->container->get(ServiceTokenBootstrap::class);
  69. /** @var TokenIssuer $issuer */
  70. $issuer = $this->container->get(TokenIssuer::class);
  71. $first = $issuer->issue(TokenKind::Service);
  72. $second = $issuer->issue(TokenKind::Service);
  73. $boot->bootstrap($first);
  74. $boot->bootstrap($second);
  75. // Both rows are kept (audit + traceability), but only the second is active.
  76. self::assertSame(
  77. 2,
  78. (int) $this->db->fetchOne("SELECT COUNT(*) FROM api_tokens WHERE kind = 'service'")
  79. );
  80. self::assertSame(
  81. 1,
  82. (int) $this->db->fetchOne(
  83. "SELECT COUNT(*) FROM api_tokens WHERE kind = 'service' AND revoked_at IS NOT NULL"
  84. ),
  85. 'old service token must be marked revoked after rotation'
  86. );
  87. self::assertSame(
  88. 1,
  89. (int) $this->db->fetchOne(
  90. "SELECT COUNT(*) FROM api_tokens WHERE kind = 'service' AND revoked_at IS NULL"
  91. ),
  92. 'exactly one service token must remain active after rotation'
  93. );
  94. }
  95. /**
  96. * SEC_REVIEW F13. If the operator has accumulated multiple
  97. * service-kind rows (e.g. ran rotations on a pre-fix deploy),
  98. * a fresh bootstrap revokes ALL previously-valid service-kind rows,
  99. * not just the most recent one.
  100. */
  101. public function testBootstrapRotationRevokesEveryPreviouslyActiveServiceToken(): void
  102. {
  103. /** @var ServiceTokenBootstrap $boot */
  104. $boot = $this->container->get(ServiceTokenBootstrap::class);
  105. /** @var TokenIssuer $issuer */
  106. $issuer = $this->container->get(TokenIssuer::class);
  107. // Simulate a pre-fix history with three accumulated active service rows.
  108. /** @var \App\Domain\Auth\TokenHasher $hasher */
  109. $hasher = $this->container->get(\App\Domain\Auth\TokenHasher::class);
  110. foreach (range(1, 3) as $_) {
  111. $raw = $issuer->issue(TokenKind::Service);
  112. $this->db->insert('api_tokens', [
  113. 'token_hash' => $hasher->hash($raw),
  114. 'token_prefix' => substr($raw, 0, 8),
  115. 'kind' => 'service',
  116. 'reporter_id' => null,
  117. 'consumer_id' => null,
  118. 'role' => null,
  119. 'expires_at' => null,
  120. 'revoked_at' => null,
  121. 'last_used_at' => null,
  122. ]);
  123. }
  124. self::assertSame(
  125. 3,
  126. (int) $this->db->fetchOne(
  127. "SELECT COUNT(*) FROM api_tokens WHERE kind = 'service' AND revoked_at IS NULL"
  128. )
  129. );
  130. $fresh = $issuer->issue(TokenKind::Service);
  131. $boot->bootstrap($fresh);
  132. self::assertSame(
  133. 3,
  134. (int) $this->db->fetchOne(
  135. "SELECT COUNT(*) FROM api_tokens WHERE kind = 'service' AND revoked_at IS NOT NULL"
  136. ),
  137. 'all three pre-existing service tokens must be revoked'
  138. );
  139. self::assertSame(
  140. 1,
  141. (int) $this->db->fetchOne(
  142. "SELECT COUNT(*) FROM api_tokens WHERE kind = 'service' AND revoked_at IS NULL"
  143. ),
  144. 'only the new service token must remain active'
  145. );
  146. }
  147. /**
  148. * SEC_REVIEW F13. Rotation must surface in the audit log so SOC tooling
  149. * can attribute the change-of-state. The `token.revoked` row carries
  150. * a structured `reason: rotated_by_bootstrap` so a query can split
  151. * automatic-rotation from operator-initiated revoke.
  152. */
  153. public function testBootstrapRotationEmitsRevokedAndCreatedAuditRows(): void
  154. {
  155. /** @var ServiceTokenBootstrap $boot */
  156. $boot = $this->container->get(ServiceTokenBootstrap::class);
  157. /** @var TokenIssuer $issuer */
  158. $issuer = $this->container->get(TokenIssuer::class);
  159. $first = $issuer->issue(TokenKind::Service);
  160. $second = $issuer->issue(TokenKind::Service);
  161. $boot->bootstrap($first);
  162. $this->db->executeStatement("DELETE FROM audit_log");
  163. $boot->bootstrap($second);
  164. $revokedDetails = $this->db->fetchOne(
  165. "SELECT details_json FROM audit_log WHERE action = 'token.revoked' ORDER BY id DESC LIMIT 1"
  166. );
  167. self::assertIsString($revokedDetails);
  168. $details = json_decode($revokedDetails, true);
  169. self::assertIsArray($details);
  170. self::assertSame('service', $details['kind']);
  171. self::assertSame('rotated_by_bootstrap', $details['reason']);
  172. $createdRow = $this->db->fetchAssociative(
  173. "SELECT actor_kind, details_json FROM audit_log WHERE action = 'token.created' ORDER BY id DESC LIMIT 1"
  174. );
  175. self::assertIsArray($createdRow);
  176. self::assertSame('system', $createdRow['actor_kind']);
  177. $createdDetails = json_decode((string) $createdRow['details_json'], true);
  178. self::assertIsArray($createdDetails);
  179. self::assertSame('service', $createdDetails['kind']);
  180. self::assertSame('bootstrap', $createdDetails['source']);
  181. self::assertIsArray($createdDetails['rotated_from']);
  182. self::assertCount(1, $createdDetails['rotated_from']);
  183. }
  184. /**
  185. * SEC_REVIEW F13 corner case. If the operator put the old token back
  186. * into env after explicitly revoking it (intentionally or via env-var
  187. * rollback), bootstrap must NOT silently re-enable a known-bad hash.
  188. * It refuses; the operator must issue a fresh value.
  189. */
  190. public function testBootstrapRefusesToReEnablePreviouslyRevokedToken(): void
  191. {
  192. /** @var ServiceTokenBootstrap $boot */
  193. $boot = $this->container->get(ServiceTokenBootstrap::class);
  194. /** @var TokenIssuer $issuer */
  195. $issuer = $this->container->get(TokenIssuer::class);
  196. /** @var \App\Domain\Auth\TokenHasher $hasher */
  197. $hasher = $this->container->get(\App\Domain\Auth\TokenHasher::class);
  198. $raw = $issuer->issue(TokenKind::Service);
  199. $this->db->insert('api_tokens', [
  200. 'token_hash' => $hasher->hash($raw),
  201. 'token_prefix' => substr($raw, 0, 8),
  202. 'kind' => 'service',
  203. 'reporter_id' => null,
  204. 'consumer_id' => null,
  205. 'role' => null,
  206. 'expires_at' => null,
  207. 'revoked_at' => '2026-01-01 00:00:00',
  208. 'last_used_at' => null,
  209. ]);
  210. $boot->bootstrap($raw);
  211. self::assertSame(
  212. 1,
  213. (int) $this->db->fetchOne(
  214. "SELECT COUNT(*) FROM api_tokens WHERE kind = 'service' AND revoked_at IS NOT NULL"
  215. ),
  216. 'revoked row stays revoked'
  217. );
  218. self::assertSame(
  219. 0,
  220. (int) $this->db->fetchOne(
  221. "SELECT COUNT(*) FROM api_tokens WHERE kind = 'service' AND revoked_at IS NULL"
  222. ),
  223. 'no fresh active row inserted from a revoked-hash env value'
  224. );
  225. }
  226. }