1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2018 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace OAuth2Framework\Component\AuthorizationEndpoint; |
15
|
|
|
|
16
|
|
|
use Http\Message\MessageFactory; |
17
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\AuthorizationRequest\AuthorizationRequest; |
18
|
|
|
use OAuth2Framework\Component\Core\Message\OAuth2Error; |
19
|
|
|
use Psr\Http\Message\ResponseInterface; |
20
|
|
|
use Psr\Http\Server\MiddlewareInterface; |
21
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
22
|
|
|
|
23
|
|
|
abstract class AbstractEndpoint implements MiddlewareInterface |
24
|
|
|
{ |
25
|
|
|
private $session; |
26
|
|
|
|
27
|
|
|
private $messageFactory; |
28
|
|
|
|
29
|
|
|
public function __construct(MessageFactory $messageFactory, SessionInterface $session) |
30
|
|
|
{ |
31
|
|
|
$this->messageFactory = $messageFactory; |
32
|
|
|
$this->session = $session; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function saveAuthorization(string $authorizationId, AuthorizationRequest $authorization) |
36
|
|
|
{ |
37
|
|
|
$this->session->set(sprintf('/authorization/%s', $authorizationId), $authorization); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function getAuthorization(string $authorizationId): AuthorizationRequest |
41
|
|
|
{ |
42
|
|
|
$authorization = $this->session->get(sprintf('/authorization/%s', $authorizationId)); |
43
|
|
|
if (null === $authorization) { |
44
|
|
|
throw new \InvalidArgumentException('Invalid authorization ID.'); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return $authorization; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function buildOAuth2Error(AuthorizationRequest $authorization, string $error, string $errorDescription): OAuth2Error |
51
|
|
|
{ |
52
|
|
|
$params = $authorization->getResponseParameters(); |
53
|
|
|
if (null === $authorization->getResponseMode() || null === $authorization->getRedirectUri()) { |
54
|
|
|
throw new OAuth2Error(400, $error, $errorDescription); |
55
|
|
|
} |
56
|
|
|
$params += [ |
57
|
|
|
'response_mode' => $authorization->getResponseMode(), |
58
|
|
|
'redirect_uri' => $authorization->getRedirectUri(), |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
return new OAuth2Error(303, $error, $errorDescription, $params); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function createRedirectResponse(string $redirectTo): ResponseInterface |
65
|
|
|
{ |
66
|
|
|
$response = $this->messageFactory->createResponse(303); |
67
|
|
|
$response->withHeader('location', $redirectTo); |
68
|
|
|
|
69
|
|
|
return $response; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|