1
0

AuthEndpointsTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. * Defense-in-depth: even if application code regresses, the partial
  78. * unique index added in 20260504100000_add_unique_local_user_index
  79. * must reject a second is_local=1 insert.
  80. */
  81. public function testDbLayerRejectsSecondLocalAdminInsert(): void
  82. {
  83. $this->createUser(Role::Admin, isLocal: true);
  84. $this->expectException(\Doctrine\DBAL\Exception\UniqueConstraintViolationException::class);
  85. $this->db->insert('users', [
  86. 'subject' => null,
  87. 'email' => null,
  88. 'display_name' => 'second-local',
  89. 'role' => Role::Admin->value,
  90. 'is_local' => 1,
  91. ]);
  92. }
  93. public function testUpsertLocalIsIdempotent(): void
  94. {
  95. $token = $this->createToken(TokenKind::Service);
  96. $adminId = $this->createUser(Role::Admin, isLocal: true);
  97. $headers = [
  98. 'Authorization' => 'Bearer ' . $token,
  99. 'X-Acting-User-Id' => (string) $adminId,
  100. 'Content-Type' => 'application/json',
  101. ];
  102. $body = json_encode(['username' => 'idempotent']) ?: null;
  103. $first = $this->decode($this->request('POST', '/api/v1/auth/users/upsert-local', $headers, $body));
  104. $second = $this->decode($this->request('POST', '/api/v1/auth/users/upsert-local', $headers, $body));
  105. self::assertSame($first['user_id'], $second['user_id']);
  106. }
  107. public function testUpsertOidcResolvesRoleFromGroups(): void
  108. {
  109. $token = $this->createToken(TokenKind::Service);
  110. $adminId = $this->createUser(Role::Admin, isLocal: true);
  111. // Seed a role mapping: group "ops-group" → operator
  112. $this->db->insert('oidc_role_mappings', [
  113. 'group_id' => 'ops-group',
  114. 'role' => Role::Operator->value,
  115. ]);
  116. $this->db->insert('oidc_role_mappings', [
  117. 'group_id' => 'admin-group',
  118. 'role' => Role::Admin->value,
  119. ]);
  120. $response = $this->request(
  121. 'POST',
  122. '/api/v1/auth/users/upsert-oidc',
  123. [
  124. 'Authorization' => 'Bearer ' . $token,
  125. 'X-Acting-User-Id' => (string) $adminId,
  126. 'Content-Type' => 'application/json',
  127. ],
  128. json_encode([
  129. 'subject' => 'sub-1',
  130. 'email' => 'alice@example.com',
  131. 'display_name' => 'Alice',
  132. 'groups' => ['ops-group', 'admin-group'],
  133. ]) ?: null
  134. );
  135. self::assertSame(200, $response->getStatusCode());
  136. $body = $this->decode($response);
  137. self::assertSame('admin', $body['role'], 'highest matching role wins');
  138. self::assertSame('alice@example.com', $body['email']);
  139. self::assertSame('Alice', $body['display_name']);
  140. self::assertFalse($body['is_local']);
  141. }
  142. public function testUpsertOidcFallsBackToDefaultRoleWithNoMatchingGroup(): void
  143. {
  144. $token = $this->createToken(TokenKind::Service);
  145. $adminId = $this->createUser(Role::Admin, isLocal: true);
  146. $response = $this->request(
  147. 'POST',
  148. '/api/v1/auth/users/upsert-oidc',
  149. [
  150. 'Authorization' => 'Bearer ' . $token,
  151. 'X-Acting-User-Id' => (string) $adminId,
  152. 'Content-Type' => 'application/json',
  153. ],
  154. json_encode([
  155. 'subject' => 'sub-default',
  156. 'email' => 'b@example.com',
  157. 'display_name' => 'B',
  158. 'groups' => ['unknown-group'],
  159. ]) ?: null
  160. );
  161. self::assertSame(200, $response->getStatusCode());
  162. // Default in tests is Role::Viewer.
  163. self::assertSame('viewer', $this->decode($response)['role']);
  164. }
  165. public function testUpsertOidcRecomputesRoleOnSubsequentLogins(): void
  166. {
  167. $token = $this->createToken(TokenKind::Service);
  168. $adminId = $this->createUser(Role::Admin, isLocal: true);
  169. $this->db->insert('oidc_role_mappings', [
  170. 'group_id' => 'g1',
  171. 'role' => Role::Operator->value,
  172. ]);
  173. $headers = [
  174. 'Authorization' => 'Bearer ' . $token,
  175. 'X-Acting-User-Id' => (string) $adminId,
  176. 'Content-Type' => 'application/json',
  177. ];
  178. $first = $this->decode($this->request(
  179. 'POST',
  180. '/api/v1/auth/users/upsert-oidc',
  181. $headers,
  182. json_encode([
  183. 'subject' => 'churn',
  184. 'email' => 'c@example.com',
  185. 'display_name' => 'C',
  186. 'groups' => ['g1'],
  187. ]) ?: null
  188. ));
  189. self::assertSame('operator', $first['role']);
  190. // Subsequent login with no matching group → role drops to default viewer.
  191. $second = $this->decode($this->request(
  192. 'POST',
  193. '/api/v1/auth/users/upsert-oidc',
  194. $headers,
  195. json_encode([
  196. 'subject' => 'churn',
  197. 'email' => 'c@example.com',
  198. 'display_name' => 'C',
  199. 'groups' => [],
  200. ]) ?: null
  201. ));
  202. self::assertSame($first['user_id'], $second['user_id']);
  203. self::assertSame('viewer', $second['role']);
  204. }
  205. public function testUpsertOidcRejectsAdminToken(): void
  206. {
  207. // Even an admin token can't call /auth/* — those are service-only.
  208. $token = $this->createToken(TokenKind::Admin, role: Role::Admin);
  209. $response = $this->request(
  210. 'POST',
  211. '/api/v1/auth/users/upsert-local',
  212. [
  213. 'Authorization' => 'Bearer ' . $token,
  214. 'Content-Type' => 'application/json',
  215. ],
  216. json_encode(['username' => 'admin']) ?: null
  217. );
  218. self::assertSame(403, $response->getStatusCode());
  219. }
  220. }