Completed
Push — develop ( ca252b...3db5f7 )
by Neomerx
04:41
created

PassportMiddleware::getLoggerIfEnabled()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 1
crap 12
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 Psr\Log\LoggerInterface;
29
use Zend\Diactoros\Response\EmptyResponse;
30
31
/**
32
 * @package Limoncello\Passport
33
 */
34
class PassportMiddleware implements MiddlewareInterface
35
{
36
    /**
37
     * @param ServerRequestInterface $request
38
     * @param Closure                $next
39
     * @param ContainerInterface     $container
40
     *
41
     * @return ResponseInterface
42 3
     */
43
    public static function handle(
44
        ServerRequestInterface $request,
45
        Closure $next,
46
        ContainerInterface $container
47 3
    ): ResponseInterface {
48
        $header = $request->getHeader('Authorization');
49 3
        // if value has Bearer token and it is a valid json with 2 required fields and they are strings
50 3
        if (empty($header) === false &&
51 3
            substr($value = $header[0], 0, 7) === 'Bearer ' &&
52 3
            is_string($tokenValue = substr($value, 7)) === true &&
53
            empty($tokenValue) === false
54 2
        ) {
55
            assert($container->has(PassportAccountManagerInterface::class));
56
57 2
            /** @var PassportAccountManagerInterface $accountManager */
58
            $accountManager = $container->get(PassportAccountManagerInterface::class);
59 2
            try {
60 1
                $accountManager->setAccountWithTokenValue($tokenValue);
61 1
            } catch (AuthenticationException $exception) {
62
                if (($logger = static::getLoggerIfEnabled($container)) !== null) {
63
                    $logger->info(
64
                        'Passport authentication failed for a given Bearer token value.',
65
                        ['token' => $tokenValue]
66 2
                    );
67
                }
68
69
                return static::createAuthenticationFailedResponse($container);
70
            }
71
        } else {
72
            if (($logger = static::getLoggerIfEnabled($container)) !== null) {
73
                $logger->debug(
74 1
                    'No Bearer token for Passport authentication. The request is not authenticated.'
75
                );
76
            }
77 1
        }
78 1
79 1
        // call next middleware handler
80
        return $next($request);
81 1
    }
82
83 1
    /**
84
     * @param ContainerInterface $container
85 1
     *
86
     * @return ResponseInterface
87
     */
88
    protected static function createAuthenticationFailedResponse(ContainerInterface $container): ResponseInterface
89
    {
90
        /** @var SettingsProviderInterface $provider */
91
        $provider = $container->get(SettingsProviderInterface::class);
92
        $settings = $provider->get(S::class);
93
        $factory  = $settings[S::KEY_FAILED_AUTHENTICATION_FACTORY] ?? null;
94
95
        assert($factory === null || is_callable($factory) === true);
96
97
        $response = $factory === null ? new EmptyResponse(401) : call_user_func($factory);
98
99
        return $response;
100
    }
101
102
    /**
103
     * @param ContainerInterface $container
104
     *
105
     * @return null|LoggerInterface
106
     */
107
    protected static function getLoggerIfEnabled(ContainerInterface $container): ?LoggerInterface
108
    {
109
        $logger = null;
110
        if ($container->has(LoggerInterface::class) === true &&
111
            $container->get(SettingsProviderInterface::class)->get(S::class)[S::KEY_IS_LOG_ENABLED] === true
112
        ) {
113
            $logger = $container->get(LoggerInterface::class);
114
        }
115
116
        return $logger;
117
    }
118
}
119