AuthClient.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\ApiClient;
  4. use App\ApiClient\DTOs\UserDto;
  5. /**
  6. * Wraps the api's `/api/v1/auth/*` endpoints. These exist *exclusively*
  7. * for the UI BFF; they're called with the service token but with no
  8. * `X-Acting-User-Id` header (would be circular — the endpoints exist to
  9. * resolve the user record we'd impersonate).
  10. */
  11. final class AuthClient
  12. {
  13. public function __construct(private readonly ApiClient $api)
  14. {
  15. }
  16. /**
  17. * @param list<string> $groups Entra group object IDs from the ID token's `groups` claim.
  18. */
  19. public function upsertOidc(string $subject, ?string $email, string $displayName, array $groups): UserDto
  20. {
  21. $payload = $this->api->request(
  22. 'POST',
  23. '/api/v1/auth/users/upsert-oidc',
  24. [
  25. 'json' => [
  26. 'subject' => $subject,
  27. 'email' => $email,
  28. 'display_name' => $displayName,
  29. 'groups' => array_values($groups),
  30. ],
  31. ],
  32. );
  33. return UserDto::fromArray($payload);
  34. }
  35. public function upsertLocal(string $username): UserDto
  36. {
  37. $payload = $this->api->request(
  38. 'POST',
  39. '/api/v1/auth/users/upsert-local',
  40. ['json' => ['username' => $username]],
  41. );
  42. return UserDto::fromArray($payload);
  43. }
  44. }