1
|
|
|
<?php namespace Limoncello\Passport\Authentication; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015-2017 [email protected] |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of 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, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use Closure; |
20
|
|
|
use Limoncello\Contracts\Application\MiddlewareInterface; |
21
|
|
|
use Limoncello\Contracts\Settings\SettingsProviderInterface; |
22
|
|
|
use Limoncello\Passport\Contracts\Authentication\PassportAccountManagerInterface; |
23
|
|
|
use Limoncello\Passport\Exceptions\AuthenticationException; |
24
|
|
|
use Limoncello\Passport\Package\PassportSettings as S; |
25
|
|
|
use Psr\Container\ContainerInterface; |
26
|
|
|
use Psr\Http\Message\ResponseInterface; |
27
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
28
|
|
|
use Zend\Diactoros\Response\EmptyResponse; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @package Limoncello\Passport |
32
|
|
|
*/ |
33
|
|
|
class PassportMiddleware implements MiddlewareInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @param ServerRequestInterface $request |
37
|
|
|
* @param Closure $next |
38
|
|
|
* @param ContainerInterface $container |
39
|
|
|
* |
40
|
|
|
* @return ResponseInterface |
41
|
|
|
*/ |
42
|
|
|
public static function handle( |
43
|
|
|
ServerRequestInterface $request, |
44
|
|
|
Closure $next, |
45
|
|
|
ContainerInterface $container |
46
|
|
|
): ResponseInterface { |
47
|
|
|
$header = $request->getHeader('Authorization'); |
48
|
|
|
// if value has Bearer token and it is a valid json with 2 required fields and they are strings |
49
|
|
|
if (empty($header) === false && |
50
|
|
|
substr($value = $header[0], 0, 7) === 'Bearer ' && |
51
|
|
|
is_string($tokenValue = substr($value, 7)) === true && |
52
|
|
|
empty($tokenValue) === false |
53
|
|
|
) { |
54
|
|
|
assert($container->has(PassportAccountManagerInterface::class)); |
55
|
|
|
|
56
|
|
|
/** @var PassportAccountManagerInterface $accountManager */ |
57
|
|
|
$accountManager = $container->get(PassportAccountManagerInterface::class); |
58
|
|
|
try { |
59
|
|
|
$accountManager->setAccountWithTokenValue($tokenValue); |
60
|
|
|
} catch (AuthenticationException $exception) { |
61
|
|
|
return static::createAuthenticationFailedResponse($container); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// call next middleware handler |
66
|
|
|
return $next($request); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param ContainerInterface $container |
71
|
|
|
* |
72
|
|
|
* @return ResponseInterface |
73
|
|
|
*/ |
74
|
|
|
protected static function createAuthenticationFailedResponse(ContainerInterface $container): ResponseInterface |
75
|
|
|
{ |
76
|
|
|
/** @var SettingsProviderInterface $provider */ |
77
|
|
|
$provider = $container->get(SettingsProviderInterface::class); |
78
|
|
|
$settings = $provider->get(S::class); |
79
|
|
|
$factory = $settings[S::KEY_FAILED_AUTHENTICATION_FACTORY] ?? null; |
80
|
|
|
|
81
|
|
|
assert($factory === null || is_callable($factory) === true); |
82
|
|
|
|
83
|
|
|
$response = $factory === null ? new EmptyResponse(401) : call_user_func($factory); |
84
|
|
|
|
85
|
|
|
return $response; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|