OAuth2Middleware   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 18
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A process() 0 13 5
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * BEdita, API-first content management framework
6
 * Copyright 2022 ChannelWeb Srl, Chialab Srl
7
 *
8
 * This file is part of BEdita: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published
10
 * by the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
14
 */
15
namespace BEdita\WebTools\Middleware;
16
17
use Authentication\Authenticator\Result;
18
use BEdita\WebTools\Authenticator\OAuth2Authenticator;
19
use Cake\Utility\Hash;
20
use Laminas\Diactoros\Response\RedirectResponse;
21
use Psr\Http\Message\ResponseInterface;
22
use Psr\Http\Message\ServerRequestInterface;
23
use Psr\Http\Server\MiddlewareInterface;
24
use Psr\Http\Server\RequestHandlerInterface;
25
26
/**
27
 * Middleware to handle OAuth2 flow.
28
 */
29
class OAuth2Middleware implements MiddlewareInterface
30
{
31
    /**
32
     * @inheritDoc
33
     */
34
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
35
    {
36
        $result = $request->getAttribute('authenticationResult');
37
        if (empty($result) || !$result instanceof Result || !is_array($result->getData())) {
38
            return $handler->handle($request);
39
        }
40
41
        $authUrl = Hash::get($result->getData(), OAuth2Authenticator::AUTH_URL_KEY);
42
        if (empty($authUrl)) {
43
            return $handler->handle($request);
44
        }
45
46
        return new RedirectResponse($authUrl);
47
    }
48
}
49