1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bone\User\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use Bone\Http\Response; |
6
|
|
|
use Bone\Paseto\PasetoService; |
7
|
|
|
use Bone\Server\SessionAwareInterface; |
8
|
|
|
use Bone\Server\Traits\HasSessionTrait; |
9
|
|
|
use Del\Exception\UserException; |
10
|
|
|
use Del\Service\UserService; |
11
|
|
|
use Del\SessionManager; |
12
|
|
|
use Exception; |
13
|
|
|
use Laminas\Diactoros\Response\RedirectResponse; |
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
15
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
16
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
17
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
18
|
|
|
|
19
|
|
|
class SessionAuth implements MiddlewareInterface, SessionAwareInterface |
20
|
|
|
{ |
21
|
|
|
use HasSessionTrait; |
22
|
|
|
|
23
|
|
|
/** @var UserService $userService */ |
24
|
|
|
private $userService; |
25
|
|
|
|
26
|
|
|
/** @var PasetoService $pasetoService */ |
27
|
|
|
private $pasetoService; |
28
|
|
|
|
29
|
|
|
/** @var string $redirectUrl */ |
30
|
|
|
private $redirectUrl; |
31
|
|
|
|
32
|
6 |
|
public function __construct(SessionManager $sessionManager, UserService $userService, PasetoService $pasetoService, $redirectUrl = '/user/home') |
33
|
|
|
{ |
34
|
6 |
|
$this->setSession($sessionManager); |
35
|
6 |
|
$this->userService = $userService; |
36
|
6 |
|
$this->pasetoService = $pasetoService; |
37
|
6 |
|
$this->redirectUrl = $redirectUrl; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param ServerRequestInterface $request |
42
|
|
|
* @param RequestHandlerInterface $handler |
43
|
|
|
* @return ResponseInterface |
44
|
|
|
*/ |
45
|
4 |
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
46
|
|
|
{ |
47
|
4 |
|
$cookies = $request->getCookieParams(); |
48
|
4 |
|
$id = $this->getSession()->get('user'); |
49
|
|
|
|
50
|
4 |
|
if (!$id && isset($cookies['resu'])) { |
51
|
2 |
|
$string = $cookies['resu']; |
52
|
|
|
|
53
|
|
|
try { |
54
|
2 |
|
$token = $this->pasetoService->decryptToken($string); |
55
|
1 |
|
$id = $token->getClaims()['user']; |
56
|
1 |
|
$this->getSession()->set('user', $id); |
57
|
1 |
|
} catch (Exception $e) { |
58
|
1 |
|
return new RedirectResponse($this->redirectUrl); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
if ($id) { |
63
|
2 |
|
$user = $this->userService->findUserById($id); |
64
|
2 |
|
$request = $request->withAttribute('user', $user); |
65
|
2 |
|
$response = $handler->handle($request); |
66
|
|
|
|
67
|
2 |
|
if ($response instanceof Response) { |
68
|
2 |
|
$response->setAttribute('user', $user); |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
$person = $user->getPerson(); |
72
|
2 |
|
$person = $this->userService->getPersonSvc()->toArray($person); |
|
|
|
|
73
|
2 |
|
$userArray = $this->userService->toArray($user); |
|
|
|
|
74
|
2 |
|
$userArray['person'] = $person; |
75
|
|
|
|
76
|
2 |
|
return $response->withHeader('user', json_encode($userArray)); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
throw new UserException(UserException::UNAUTHORISED, 401); |
80
|
|
|
} |
81
|
|
|
} |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.