|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Drupal\os2loop_login_hack\Controller; |
| 6 | + |
| 7 | +use Drupal\Component\Datetime\TimeInterface; |
| 8 | +use Drupal\Core\Controller\ControllerBase; |
| 9 | +use Drupal\Core\Entity\EntityTypeManagerInterface; |
| 10 | +use Drupal\Core\Routing\TrustedRedirectResponse; |
| 11 | +use Drupal\Core\Url; |
| 12 | +use Drupal\user\Entity\User; |
| 13 | +use Drupal\user\UserStorageInterface; |
| 14 | +use Firebase\JWT\JWT; |
| 15 | +use Firebase\JWT\Key; |
| 16 | +use Psr\Log\LoggerInterface; |
| 17 | +use Symfony\Component\DependencyInjection\Attribute\Autowire; |
| 18 | +use Symfony\Component\HttpFoundation\Exception\BadRequestException; |
| 19 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 20 | +use Symfony\Component\HttpFoundation\Request; |
| 21 | +use Symfony\Component\HttpFoundation\Response; |
| 22 | +use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
| 23 | + |
| 24 | +/** |
| 25 | + * Returns responses for os2loop_login_hack routes. |
| 26 | + */ |
| 27 | +final class Os2loopLoginHackController extends ControllerBase { |
| 28 | + private const JWT_KEY = 'os2loop_login_hack'; |
| 29 | + |
| 30 | + /** |
| 31 | + * The user storage. |
| 32 | + */ |
| 33 | + private readonly UserStorageInterface $userStorage; |
| 34 | + |
| 35 | + /** |
| 36 | + * Constructor. |
| 37 | + */ |
| 38 | + public function __construct( |
| 39 | + EntityTypeManagerInterface $entityTypeManager, |
| 40 | + private readonly TimeInterface $time, |
| 41 | + #[Autowire(service: 'logger.channel.os2loop_login_hack')] |
| 42 | + private readonly LoggerInterface $logger, |
| 43 | + ) { |
| 44 | + $this->userStorage = $entityTypeManager->getStorage('user'); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Start user authentication. |
| 49 | + */ |
| 50 | + public function start(Request $request): Response { |
| 51 | + try { |
| 52 | + $data = json_decode($request->getContent(), associative: TRUE, flags: JSON_THROW_ON_ERROR); |
| 53 | + $username = $data['username'] ?? NULL; |
| 54 | + if (empty($username)) { |
| 55 | + throw new BadRequestHttpException('Missing username'); |
| 56 | + } |
| 57 | + |
| 58 | + $user = $this->loadUser($username); |
| 59 | + if (empty($user)) { |
| 60 | + // Don't disclose whether or not the user exists. |
| 61 | + throw new BadRequestHttpException(); |
| 62 | + } |
| 63 | + |
| 64 | + // Check that we can get userinfo. |
| 65 | + $userinfo = $this->getUserinfo($user); |
| 66 | + if (empty($userinfo)) { |
| 67 | + throw new BadRequestHttpException(); |
| 68 | + } |
| 69 | + |
| 70 | + // https://github.com/firebase/php-jwt?tab=readme-ov-file#example |
| 71 | + $payload = [ |
| 72 | + // Issued at. |
| 73 | + 'iat' => $this->time->getRequestTime(), |
| 74 | + // Expire af 60 seconds. |
| 75 | + 'exp' => $this->time->getRequestTime() + 60, |
| 76 | + 'username' => $username, |
| 77 | + ]; |
| 78 | + $jwt = JWT::encode($payload, self::JWT_KEY, 'HS256'); |
| 79 | + |
| 80 | + $url = Url::fromRoute('os2loop_login_hack.authenticate', [ |
| 81 | + 'username' => $username, |
| 82 | + 'jwt' => $jwt, |
| 83 | + ])->setAbsolute()->toString(TRUE)->getGeneratedUrl(); |
| 84 | + |
| 85 | + return new JsonResponse([ |
| 86 | + 'authenticate_url' => $url, |
| 87 | + 'jwt' => $jwt, |
| 88 | + ]); |
| 89 | + } |
| 90 | + catch (\Exception $exception) { |
| 91 | + $this->logger->error('start: @message', ['@message' => $exception->getMessage(), $exception]); |
| 92 | + throw new BadRequestException($exception->getMessage()); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Authenticate user. |
| 98 | + */ |
| 99 | + public function authenticate(Request $request): Response { |
| 100 | + try { |
| 101 | + $username = $request->get('username'); |
| 102 | + $jwt = $request->get('jwt'); |
| 103 | + if (empty($username) || empty($jwt)) { |
| 104 | + throw new BadRequestHttpException(); |
| 105 | + } |
| 106 | + |
| 107 | + $payload = (array) JWT::decode($jwt, new Key(self::JWT_KEY, 'HS256')); |
| 108 | + $username = $payload['username'] ?? NULL; |
| 109 | + if (empty($username)) { |
| 110 | + throw new BadRequestHttpException(); |
| 111 | + } |
| 112 | + |
| 113 | + $user = $this->loadUser($username); |
| 114 | + if (empty($user)) { |
| 115 | + // Don't disclose whether or not the user exists. |
| 116 | + throw new BadRequestHttpException(); |
| 117 | + } |
| 118 | + |
| 119 | + $this->updateUser($user); |
| 120 | + |
| 121 | + user_login_finalize($user); |
| 122 | + |
| 123 | + $url = Url::fromRoute('<front>')->setAbsolute()->toString(TRUE)->getGeneratedUrl(); |
| 124 | + $this->messenger()->addStatus($this->t('Welcome @user.', ['@user' => $user->getDisplayName()])); |
| 125 | + |
| 126 | + return new TrustedRedirectResponse($url); |
| 127 | + } |
| 128 | + catch (\Exception $exception) { |
| 129 | + $this->logger->error('start: @message', ['@message' => $exception->getMessage(), $exception]); |
| 130 | + throw new BadRequestException($exception->getMessage()); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Load user by username. |
| 136 | + * |
| 137 | + * @param string $username |
| 138 | + * The username. |
| 139 | + * |
| 140 | + * @return \Drupal\user\Entity\User|null |
| 141 | + * The user if any. |
| 142 | + */ |
| 143 | + private function loadUser(string $username): ?User { |
| 144 | + $users = $this->userStorage->loadByProperties(['name' => $username]); |
| 145 | + |
| 146 | + return reset($users) ?: NULL; |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Update user with info from IdP. |
| 151 | + */ |
| 152 | + private function updateUser(User $user): User { |
| 153 | + // $userinfo = $this->getUserinfo($user); |
| 154 | + // @todo Update user. |
| 155 | + return $user; |
| 156 | + } |
| 157 | + |
| 158 | + /** |
| 159 | + * Get user info from userinfo endpoint. |
| 160 | + */ |
| 161 | + private function getUserinfo(): array { |
| 162 | + return []; |
| 163 | + } |
| 164 | + |
| 165 | +} |
0 commit comments