1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
/** |
4
|
|
|
* Minotaur |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may not |
7
|
|
|
* use this file except in compliance with the License. You may obtain a copy of |
8
|
|
|
* the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
14
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
15
|
|
|
* License for the specific language governing permissions and limitations under |
16
|
|
|
* the License. |
17
|
|
|
* |
18
|
|
|
* @copyright 2015-2017 Appertly |
19
|
|
|
* @license Apache-2.0 |
20
|
|
|
*/ |
21
|
|
|
namespace Minotaur\Http; |
22
|
|
|
|
23
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
24
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Authentication plugin for the middleware dispatcher. |
28
|
|
|
*/ |
29
|
|
|
class Authenticator implements \Minotaur\Route\Plugin |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var \Caridea\Auth\Service |
33
|
|
|
*/ |
34
|
|
|
protected $service; |
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $authUrl; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Creates a new AuthPlugin. |
42
|
|
|
* |
43
|
|
|
* @param $service - The auth service |
44
|
|
|
* @param $authUrl - The URL for the standalone login form |
45
|
|
|
*/ |
46
|
2 |
|
public function __construct(\Caridea\Auth\Service $service, string $authUrl) |
47
|
|
|
{ |
48
|
2 |
|
$this->service = $service; |
49
|
2 |
|
$this->authUrl = $authUrl; |
50
|
2 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Gets the plugin priority; larger means first. |
54
|
|
|
* |
55
|
|
|
* @return - The plugin priority |
|
|
|
|
56
|
|
|
*/ |
57
|
|
|
public function getPriority(): int |
58
|
|
|
{ |
59
|
|
|
return 2000000; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Allows a plugin to issue a response before the request is dispatched. |
64
|
|
|
* |
65
|
|
|
* @param $request - The server request |
66
|
|
|
* @param $response - The response |
67
|
|
|
* @param callable $next The next layer. (function (Request,Response): Response) |
68
|
|
|
* @return - The response |
|
|
|
|
69
|
|
|
*/ |
70
|
2 |
|
public function __invoke(Request $request, Response $response, callable $next): Response |
71
|
|
|
{ |
72
|
2 |
|
$request = $request->withAttribute('principal', $this->service->getPrincipal()); |
73
|
|
|
try { |
74
|
2 |
|
return $next($request, $response); |
75
|
1 |
|
} catch (\Minotaur\Route\Exception\Unroutable $e) { |
76
|
1 |
|
if ($e->getCode() === 403) { |
77
|
1 |
|
return $response->withStatus(303)->withHeader( |
78
|
1 |
|
'Location', |
79
|
1 |
|
$this->authUrl . '?then=' . (string)$request->getRequestTarget() |
80
|
|
|
); |
81
|
|
|
} |
82
|
|
|
throw $e; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.