AuthEndpointsTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Integration\Auth;
  4. use App\Domain\Auth\Role;
  5. use App\Domain\Auth\TokenKind;
  6. use App\Tests\Integration\Support\AppTestCase;
  7. /**
  8. * Behavioural tests for the /api/v1/auth/* endpoints. These verify the
  9. * upsert flows and OIDC role resolution from group mappings.
  10. */
  11. final class AuthEndpointsTest extends AppTestCase
  12. {
  13. public function testUpsertLocalCreatesUserOnFirstCall(): void
  14. {
  15. $token = $this->createToken(TokenKind::Service);
  16. // No local row exists yet; the first upsert mints it.
  17. $response = $this->request(
  18. 'POST',
  19. '/api/v1/auth/users/upsert-local',
  20. [
  21. 'Authorization' => 'Bearer ' . $token,
  22. 'Content-Type' => 'application/json',
  23. ],
  24. json_encode(['username' => 'admin']) ?: null
  25. );
  26. self::assertSame(200, $response->getStatusCode());
  27. $body = $this->decode($response);
  28. self::assertIsInt($body['user_id']);
  29. self::assertSame('admin', $body['role']);
  30. self::assertNull($body['email']);
  31. self::assertSame('admin', $body['display_name']);
  32. self::assertTrue($body['is_local']);
  33. self::assertSame(
  34. 1,
  35. (int) $this->db->fetchOne('SELECT COUNT(*) FROM users WHERE is_local = 1'),
  36. 'first call must create exactly one local row'
  37. );
  38. }
  39. /**
  40. * SEC_REVIEW F3 regression. A service-token holder must not be able
  41. * to mint additional Admin user rows by rotating the `username` field
  42. * over `POST /api/v1/auth/users/upsert-local`. Before the fix every
  43. * fresh username produced a new is_local=1, role=admin row that the
  44. * caller could then impersonate via X-Acting-User-Id.
  45. */
  46. public function testRotatingUsernamesNeverCreatesAdditionalLocalAdmins(): void
  47. {
  48. $token = $this->createToken(TokenKind::Service);
  49. $adminId = $this->createUser(Role::Admin, isLocal: true);
  50. $headers = [
  51. 'Authorization' => 'Bearer ' . $token,
  52. 'Content-Type' => 'application/json',
  53. ];
  54. $userIds = [];
  55. foreach (['attacker-1', 'attacker-2', 'attacker-3'] as $name) {
  56. $body = $this->decode($this->request(
  57. 'POST',
  58. '/api/v1/auth/users/upsert-local',
  59. $headers,
  60. json_encode(['username' => $name]) ?: null
  61. ));
  62. $userIds[] = $body['user_id'];
  63. self::assertSame($adminId, $body['user_id']);
  64. self::assertSame('admin', $body['role']);
  65. self::assertSame($name, $body['display_name']);
  66. }
  67. // Same user_id returned every call.
  68. self::assertSame([$adminId, $adminId, $adminId], $userIds);
  69. // And only one is_local=1 row in the DB.
  70. self::assertSame(
  71. 1,
  72. (int) $this->db->fetchOne('SELECT COUNT(*) FROM users WHERE is_local = 1'),
  73. 'rotating usernames must not mint additional local-admin rows'
  74. );
  75. }
  76. /**
  77. * SEC_REVIEW F5: account creation must be audited. The first
  78. * upsert-local emits a `user.created` row attributed to the
  79. * service-token call (kind=system, no acting user yet) so SOC
  80. * tooling can see when the local-admin row first comes into
  81. * existence.
  82. */
  83. public function testFirstUpsertLocalEmitsUserCreatedAudit(): void
  84. {
  85. $token = $this->createToken(TokenKind::Service);
  86. $response = $this->request(
  87. 'POST',
  88. '/api/v1/auth/users/upsert-local',
  89. [
  90. 'Authorization' => 'Bearer ' . $token,
  91. 'Content-Type' => 'application/json',
  92. ],
  93. json_encode(['username' => 'admin']) ?: null
  94. );
  95. self::assertSame(200, $response->getStatusCode());
  96. $userId = $this->decode($response)['user_id'];
  97. $row = $this->db->fetchAssociative(
  98. "SELECT actor_kind, action, target_type, target_id, details_json FROM audit_log WHERE action = 'user.created' ORDER BY id DESC LIMIT 1"
  99. );
  100. self::assertIsArray($row, 'user.created audit row must exist');
  101. self::assertSame('system', $row['actor_kind']);
  102. self::assertSame('user', $row['target_type']);
  103. self::assertSame((string) $userId, $row['target_id']);
  104. $details = json_decode((string) $row['details_json'], true);
  105. self::assertIsArray($details);
  106. self::assertSame('local', $details['source']);
  107. self::assertSame('admin', $details['display_name']);
  108. self::assertSame('admin', $details['role']);
  109. }
  110. public function testRotatingUsernamesEmitsOnlyOneUserCreatedAudit(): void
  111. {
  112. // The first upsert mints the row + emits user.created.
  113. // Subsequent renames update display_name but must NOT emit
  114. // user.created again (no new account is created).
  115. $token = $this->createToken(TokenKind::Service);
  116. $headers = [
  117. 'Authorization' => 'Bearer ' . $token,
  118. 'Content-Type' => 'application/json',
  119. ];
  120. foreach (['admin', 'renamed-1', 'renamed-2'] as $name) {
  121. $this->request('POST', '/api/v1/auth/users/upsert-local', $headers, json_encode(['username' => $name]) ?: null);
  122. }
  123. $count = (int) $this->db->fetchOne(
  124. "SELECT COUNT(*) FROM audit_log WHERE action = 'user.created'"
  125. );
  126. self::assertSame(1, $count, 'only the bootstrap call should emit user.created');
  127. }
  128. public function testNewOidcLoginEmitsUserCreatedAudit(): void
  129. {
  130. $token = $this->createToken(TokenKind::Service);
  131. $response = $this->request(
  132. 'POST',
  133. '/api/v1/auth/users/upsert-oidc',
  134. [
  135. 'Authorization' => 'Bearer ' . $token,
  136. 'Content-Type' => 'application/json',
  137. ],
  138. json_encode([
  139. 'subject' => 'sub-new',
  140. 'email' => 'newcomer@example.com',
  141. 'display_name' => 'Newcomer',
  142. 'groups' => [],
  143. ]) ?: null
  144. );
  145. self::assertSame(200, $response->getStatusCode());
  146. $userId = $this->decode($response)['user_id'];
  147. $row = $this->db->fetchAssociative(
  148. "SELECT actor_kind, action, target_type, target_id, target_label, details_json FROM audit_log WHERE action = 'user.created' ORDER BY id DESC LIMIT 1"
  149. );
  150. self::assertIsArray($row);
  151. self::assertSame('user', $row['target_type']);
  152. self::assertSame((string) $userId, $row['target_id']);
  153. self::assertSame('newcomer@example.com', $row['target_label']);
  154. $details = json_decode((string) $row['details_json'], true);
  155. self::assertIsArray($details);
  156. self::assertSame('oidc', $details['source']);
  157. self::assertSame('sub-new', $details['subject']);
  158. self::assertSame('viewer', $details['role']);
  159. }
  160. public function testOidcRoleDriftEmitsRoleChangedAudit(): void
  161. {
  162. $token = $this->createToken(TokenKind::Service);
  163. $headers = [
  164. 'Authorization' => 'Bearer ' . $token,
  165. 'Content-Type' => 'application/json',
  166. ];
  167. $this->db->insert('oidc_role_mappings', [
  168. 'group_id' => 'admin-grp',
  169. 'role' => Role::Admin->value,
  170. ]);
  171. // First login: admin role.
  172. $this->request('POST', '/api/v1/auth/users/upsert-oidc', $headers, json_encode([
  173. 'subject' => 'drift-sub',
  174. 'email' => 'drift@example.com',
  175. 'display_name' => 'Drift',
  176. 'groups' => ['admin-grp'],
  177. ]) ?: null);
  178. // Second login: no admin group → role drops to default viewer.
  179. $this->request('POST', '/api/v1/auth/users/upsert-oidc', $headers, json_encode([
  180. 'subject' => 'drift-sub',
  181. 'email' => 'drift@example.com',
  182. 'display_name' => 'Drift',
  183. 'groups' => [],
  184. ]) ?: null);
  185. $row = $this->db->fetchAssociative(
  186. "SELECT details_json FROM audit_log WHERE action = 'user.role_changed' ORDER BY id DESC LIMIT 1"
  187. );
  188. self::assertIsArray($row, 'role drift must emit user.role_changed');
  189. $details = json_decode((string) $row['details_json'], true);
  190. self::assertIsArray($details);
  191. self::assertSame('admin', $details['changes']['role']['from']);
  192. self::assertSame('viewer', $details['changes']['role']['to']);
  193. }
  194. /**
  195. * Defense-in-depth: even if application code regresses, the partial
  196. * unique index added in 20260504100000_add_unique_local_user_index
  197. * must reject a second is_local=1 insert.
  198. */
  199. public function testDbLayerRejectsSecondLocalAdminInsert(): void
  200. {
  201. $this->createUser(Role::Admin, isLocal: true);
  202. $this->expectException(\Doctrine\DBAL\Exception\UniqueConstraintViolationException::class);
  203. $this->db->insert('users', [
  204. 'subject' => null,
  205. 'email' => null,
  206. 'display_name' => 'second-local',
  207. 'role' => Role::Admin->value,
  208. 'is_local' => 1,
  209. ]);
  210. }
  211. /**
  212. * SEC_REVIEW F12 regression. Even if direct DB tampering or a
  213. * compromised "data-fix" script tries to insert a row with
  214. * `is_local=1` AND a non-null `subject` (an OIDC identity flagged
  215. * as local), the migration
  216. * 20260505100000_add_users_local_subject_invariant rejects the
  217. * write — MySQL via CHECK constraint, SQLite via BEFORE INSERT
  218. * trigger.
  219. */
  220. public function testDbLayerRejectsInsertingLocalRowWithNonNullSubject(): void
  221. {
  222. $this->expectException(\Doctrine\DBAL\Exception::class);
  223. $this->db->insert('users', [
  224. 'subject' => 'hijacked-oidc-sub',
  225. 'email' => 'hijacked@example.com',
  226. 'display_name' => 'admin',
  227. 'role' => Role::Admin->value,
  228. 'is_local' => 1,
  229. ]);
  230. }
  231. /**
  232. * SEC_REVIEW F12 regression. The classic threat model is "OIDC row
  233. * already exists, attacker flips its is_local to 1 via a data-fix
  234. * script". The BEFORE UPDATE trigger / CHECK constraint must catch
  235. * this transition too — not only fresh inserts.
  236. */
  237. public function testDbLayerRejectsFlippingIsLocalOnOidcRow(): void
  238. {
  239. $this->db->insert('users', [
  240. 'subject' => 'oidc-sub',
  241. 'email' => 'oidc@example.com',
  242. 'display_name' => 'admin',
  243. 'role' => Role::Viewer->value,
  244. 'is_local' => 0,
  245. ]);
  246. $oidcId = (int) $this->db->lastInsertId();
  247. $this->expectException(\Doctrine\DBAL\Exception::class);
  248. $this->db->update('users', ['is_local' => 1], ['id' => $oidcId]);
  249. }
  250. /**
  251. * SEC_REVIEW F12 regression. Belt-and-suspenders to the DB-level
  252. * constraint: `findLocal()` must additionally filter by
  253. * `subject IS NULL`, so that even if some future code path bypasses
  254. * the constraint (or it is dropped during ops), a hijacked OIDC row
  255. * cannot bind to local-admin login. We cannot exercise this via a
  256. * direct insert (the DB constraint blocks it), so we drop the
  257. * constraint for the duration of the test, inject the malformed
  258. * row, and assert findLocal does not return it.
  259. */
  260. public function testFindLocalIgnoresHijackedRowEvenIfDbConstraintIsBypassed(): void
  261. {
  262. // Bypass the SQLite triggers so we can simulate a tampered DB.
  263. $this->db->executeStatement('DROP TRIGGER IF EXISTS trg_users_local_subject_null_insert');
  264. $this->db->executeStatement('DROP TRIGGER IF EXISTS trg_users_local_subject_null_update');
  265. $this->db->insert('users', [
  266. 'subject' => 'oidc-sub-hijacked',
  267. 'email' => 'hijack@example.com',
  268. 'display_name' => 'admin',
  269. 'role' => Role::Admin->value,
  270. 'is_local' => 1,
  271. ]);
  272. /** @var \App\Infrastructure\Auth\UserRepository $users */
  273. $users = $this->container->get(\App\Infrastructure\Auth\UserRepository::class);
  274. self::assertNull($users->findLocal(), 'findLocal must skip rows with non-null subject');
  275. }
  276. public function testUpsertLocalIsIdempotent(): void
  277. {
  278. $token = $this->createToken(TokenKind::Service);
  279. $adminId = $this->createUser(Role::Admin, isLocal: true);
  280. $headers = [
  281. 'Authorization' => 'Bearer ' . $token,
  282. 'X-Acting-User-Id' => (string) $adminId,
  283. 'Content-Type' => 'application/json',
  284. ];
  285. $body = json_encode(['username' => 'idempotent']) ?: null;
  286. $first = $this->decode($this->request('POST', '/api/v1/auth/users/upsert-local', $headers, $body));
  287. $second = $this->decode($this->request('POST', '/api/v1/auth/users/upsert-local', $headers, $body));
  288. self::assertSame($first['user_id'], $second['user_id']);
  289. }
  290. public function testUpsertOidcResolvesRoleFromGroups(): void
  291. {
  292. $token = $this->createToken(TokenKind::Service);
  293. $adminId = $this->createUser(Role::Admin, isLocal: true);
  294. // Seed a role mapping: group "ops-group" → operator
  295. $this->db->insert('oidc_role_mappings', [
  296. 'group_id' => 'ops-group',
  297. 'role' => Role::Operator->value,
  298. ]);
  299. $this->db->insert('oidc_role_mappings', [
  300. 'group_id' => 'admin-group',
  301. 'role' => Role::Admin->value,
  302. ]);
  303. $response = $this->request(
  304. 'POST',
  305. '/api/v1/auth/users/upsert-oidc',
  306. [
  307. 'Authorization' => 'Bearer ' . $token,
  308. 'X-Acting-User-Id' => (string) $adminId,
  309. 'Content-Type' => 'application/json',
  310. ],
  311. json_encode([
  312. 'subject' => 'sub-1',
  313. 'email' => 'alice@example.com',
  314. 'display_name' => 'Alice',
  315. 'groups' => ['ops-group', 'admin-group'],
  316. ]) ?: null
  317. );
  318. self::assertSame(200, $response->getStatusCode());
  319. $body = $this->decode($response);
  320. self::assertSame('admin', $body['role'], 'highest matching role wins');
  321. self::assertSame('alice@example.com', $body['email']);
  322. self::assertSame('Alice', $body['display_name']);
  323. self::assertFalse($body['is_local']);
  324. }
  325. public function testUpsertOidcFallsBackToDefaultRoleWithNoMatchingGroup(): void
  326. {
  327. $token = $this->createToken(TokenKind::Service);
  328. $adminId = $this->createUser(Role::Admin, isLocal: true);
  329. $response = $this->request(
  330. 'POST',
  331. '/api/v1/auth/users/upsert-oidc',
  332. [
  333. 'Authorization' => 'Bearer ' . $token,
  334. 'X-Acting-User-Id' => (string) $adminId,
  335. 'Content-Type' => 'application/json',
  336. ],
  337. json_encode([
  338. 'subject' => 'sub-default',
  339. 'email' => 'b@example.com',
  340. 'display_name' => 'B',
  341. 'groups' => ['unknown-group'],
  342. ]) ?: null
  343. );
  344. self::assertSame(200, $response->getStatusCode());
  345. // Default in tests is Role::Viewer.
  346. self::assertSame('viewer', $this->decode($response)['role']);
  347. }
  348. public function testUpsertOidcRecomputesRoleOnSubsequentLogins(): void
  349. {
  350. $token = $this->createToken(TokenKind::Service);
  351. $adminId = $this->createUser(Role::Admin, isLocal: true);
  352. $this->db->insert('oidc_role_mappings', [
  353. 'group_id' => 'g1',
  354. 'role' => Role::Operator->value,
  355. ]);
  356. $headers = [
  357. 'Authorization' => 'Bearer ' . $token,
  358. 'X-Acting-User-Id' => (string) $adminId,
  359. 'Content-Type' => 'application/json',
  360. ];
  361. $first = $this->decode($this->request(
  362. 'POST',
  363. '/api/v1/auth/users/upsert-oidc',
  364. $headers,
  365. json_encode([
  366. 'subject' => 'churn',
  367. 'email' => 'c@example.com',
  368. 'display_name' => 'C',
  369. 'groups' => ['g1'],
  370. ]) ?: null
  371. ));
  372. self::assertSame('operator', $first['role']);
  373. // Subsequent login with no matching group → role drops to default viewer.
  374. $second = $this->decode($this->request(
  375. 'POST',
  376. '/api/v1/auth/users/upsert-oidc',
  377. $headers,
  378. json_encode([
  379. 'subject' => 'churn',
  380. 'email' => 'c@example.com',
  381. 'display_name' => 'C',
  382. 'groups' => [],
  383. ]) ?: null
  384. ));
  385. self::assertSame($first['user_id'], $second['user_id']);
  386. self::assertSame('viewer', $second['role']);
  387. }
  388. public function testUpsertOidcRejectsAdminToken(): void
  389. {
  390. // Even an admin token can't call /auth/* — those are service-only.
  391. $token = $this->createToken(TokenKind::Admin, role: Role::Admin);
  392. $response = $this->request(
  393. 'POST',
  394. '/api/v1/auth/users/upsert-local',
  395. [
  396. 'Authorization' => 'Bearer ' . $token,
  397. 'Content-Type' => 'application/json',
  398. ],
  399. json_encode(['username' => 'admin']) ?: null
  400. );
  401. self::assertSame(403, $response->getStatusCode());
  402. }
  403. }