Completed
Push — master ( 739d01...5ed129 )
by Neomerx
02:14
created

PassportMiddleware::getLoggerIfEnabled()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

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