-
Notifications
You must be signed in to change notification settings - Fork 40
/
AuthController.php
113 lines (98 loc) · 3.88 KB
/
AuthController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
declare(strict_types=1);
namespace App\Controller;
use App\Classes\SessionUserInterface;
use App\Repository\AuthenticationRepository;
use App\Repository\UserRepository;
use App\Service\AuthenticationInterface;
use App\Service\JsonWebTokenManager;
use App\Entity\UserInterface;
use Exception;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use function sleep;
class AuthController extends AbstractController
{
/**
* Authenticate someone
* Passes off the task of authentication to the service selected by the config
* option authentication_type.
*/
public function loginAction(Request $request, AuthenticationInterface $authenticator): Response
{
return $authenticator->login($request);
}
/**
* Get the id from the currently authenticated user
*/
public function whoamiAction(TokenStorageInterface $tokenStorage): JsonResponse
{
$token = $tokenStorage->getToken();
$sessionUser = $token?->getUser();
if (!$token?->isAuthenticated() || !$sessionUser instanceof SessionUserInterface) {
throw new Exception('Attempted to access whoami with no valid user');
}
return new JsonResponse(['userId' => $sessionUser->getId()], Response::HTTP_OK);
}
/**
* Get a new token
* Useful when the time limit is approaching but the user is still active
*/
public function tokenAction(
Request $request,
TokenStorageInterface $tokenStorage,
JsonWebTokenManager $jwtManager
): JsonResponse {
$token = $tokenStorage->getToken();
$sessionUser = $token?->getUser();
if (!$token?->isAuthenticated() || !$sessionUser instanceof SessionUserInterface) {
throw new Exception('Attempted to access token with no valid user');
}
$ttl = $request->get('ttl') ? $request->get('ttl') : 'PT8H';
$jwt = $jwtManager->createJwtFromSessionUser($sessionUser, $ttl);
return new JsonResponse(['jwt' => $jwt], Response::HTTP_OK);
}
/**
* Logout
* Passes off the task of logout to the service selected by the config
* option authentication_type.
*/
public function logoutAction(Request $request, AuthenticationInterface $authenticator): JsonResponse
{
return $authenticator->logout($request);
}
/**
* Invalidate all tokens issued before now
* Resets authentication in case a token is compromised
*
* @throws Exception
*/
public function invalidateTokensAction(
TokenStorageInterface $tokenStorage,
UserRepository $userRepository,
AuthenticationRepository $authenticationRepository,
JsonWebTokenManager $jwtManager
): JsonResponse {
$now = new \DateTime();
$token = $tokenStorage->getToken();
$sessionUser = $token?->getUser();
if (!$token?->isAuthenticated() || !$sessionUser instanceof SessionUserInterface) {
throw new Exception('Attempted to access invalidate tokens with no valid user');
}
/** @var UserInterface $user */
$user = $userRepository->findOneBy(['id' => $sessionUser->getId()]);
$authentication = $authenticationRepository->findOneBy(['user' => $user->getId()]);
if (!$authentication) {
$authentication = $authenticationRepository->create();
$authentication->setUser($user);
}
$authentication->setInvalidateTokenIssuedBefore($now);
$authenticationRepository->update($authentication);
sleep(1);
$jwt = $jwtManager->createJwtFromSessionUser($sessionUser);
return new JsonResponse(['jwt' => $jwt], Response::HTTP_OK);
}
}