|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Chadicus\Slim\OAuth2\Routes; |
|
4
|
|
|
|
|
5
|
|
|
use Chadicus\Slim\OAuth2\Http; |
|
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
use OAuth2; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Slim route for /authorization endpoint. |
|
12
|
|
|
*/ |
|
13
|
|
|
final class Authorize implements RouteCallbackInterface |
|
14
|
|
|
{ |
|
15
|
|
|
const ROUTE = '/authorize'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The slim framework view helper. |
|
19
|
|
|
* |
|
20
|
|
|
* @var object |
|
21
|
|
|
*/ |
|
22
|
|
|
private $view; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The oauth2 server imstance. |
|
26
|
|
|
* |
|
27
|
|
|
* @var OAuth2\Server |
|
28
|
|
|
*/ |
|
29
|
|
|
private $server; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* The template for /authorize |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private $template; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Extracts user_id from the incoming request. |
|
40
|
|
|
* |
|
41
|
|
|
* @var UserIdProviderInterface |
|
42
|
|
|
*/ |
|
43
|
|
|
private $userIdProvider; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Construct a new instance of Authorize. |
|
47
|
|
|
* |
|
48
|
|
|
* @param OAuth2\Server $server The oauth2 server imstance. |
|
49
|
|
|
* @param object $view The slim framework view helper. |
|
50
|
|
|
* @param string $template The template for /authorize. |
|
51
|
|
|
* @param UserIdProviderInterface $userIdProvider Object to extract a user_id based on the incoming request. |
|
52
|
|
|
* |
|
53
|
|
|
* @throws \InvalidArgumentException Thrown if $view is not an object implementing a render method. |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct( |
|
56
|
|
|
OAuth2\Server $server, |
|
57
|
|
|
$view, |
|
58
|
|
|
$template = '/authorize.phtml', |
|
59
|
|
|
UserIdProviderInterface $userIdProvider = null |
|
60
|
|
|
) { |
|
61
|
|
|
if (!is_object($view) || !method_exists($view, 'render')) { |
|
62
|
|
|
throw new \InvalidArgumentException('$view must implement a render() method'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$this->server = $server; |
|
66
|
|
|
$this->view = $view; |
|
67
|
|
|
$this->template = $template; |
|
68
|
|
|
|
|
69
|
|
|
if ($userIdProvider == null) { |
|
70
|
|
|
$userIdProvider = new UserIdProvider(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$this->userIdProvider = $userIdProvider; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Invoke this route callback. |
|
78
|
|
|
* |
|
79
|
|
|
* @param ServerRequestInterface $request Represents the current HTTP request. |
|
80
|
|
|
* @param ResponseInterface $response Represents the current HTTP response. |
|
81
|
|
|
* @param array $arguments Values for the current route’s named placeholders. |
|
82
|
|
|
* |
|
83
|
|
|
* @return ResponseInterface |
|
84
|
|
|
*/ |
|
85
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $arguments = []) |
|
86
|
|
|
{ |
|
87
|
|
|
$oauth2Request = Http\RequestBridge::toOAuth2($request); |
|
88
|
|
|
$oauth2Response = new OAuth2\Response(); |
|
89
|
|
|
$isValid = $this->server->validateAuthorizeRequest($oauth2Request, $oauth2Response); |
|
90
|
|
|
if (!$isValid) { |
|
91
|
|
|
return Http\ResponseBridge::fromOAuth2($oauth2Response); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$authorized = $oauth2Request->request('authorized'); |
|
95
|
|
|
if (empty($authorized)) { |
|
96
|
|
|
$response = Http\ResponseBridge::fromOAuth2($oauth2Response); |
|
97
|
|
|
$this->view->render($response, $this->template, ['client_id' => $oauth2Request->query('client_id')]); |
|
98
|
|
|
return $response->withHeader('Content-Type', 'text/html'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$this->server->handleAuthorizeRequest( |
|
102
|
|
|
$oauth2Request, |
|
103
|
|
|
$oauth2Response, |
|
104
|
|
|
$authorized === 'yes', |
|
105
|
|
|
$this->userIdProvider->getUserId($request, $arguments) |
|
106
|
|
|
); |
|
107
|
|
|
|
|
108
|
|
|
return Http\ResponseBridge::fromOAuth2($oauth2Response); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|