Passed
Pull Request — master (#66)
by Stefano
08:01 queued 05:51
created

OAuth2Middleware::process()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 7
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 13
rs 9.6111
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 Cake\Utility\Hash;
19
use Laminas\Diactoros\Response\RedirectResponse;
20
use Psr\Http\Message\ResponseInterface;
21
use Psr\Http\Message\ServerRequestInterface;
22
use Psr\Http\Server\MiddlewareInterface;
23
use Psr\Http\Server\RequestHandlerInterface;
24
25
/**
26
 * Middleware to handle OAuth2 flow.
27
 */
28
class OAuth2Middleware implements MiddlewareInterface
29
{
30
    /**
31
     * @inheritDoc
32
     */
33
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
34
    {
35
        $result = $request->getAttribute('authenticationResult');
36
        if (empty($result) || !$result instanceof Result || !is_array($result->getData())) {
37
            return $handler->handle($request);
38
        }
39
40
        $authUrl = Hash::get($result->getData(), 'authUrl');
41
        if (empty($authUrl)) {
42
            return $handler->handle($request);
43
        }
44
45
        return new RedirectResponse($authUrl);
46
    }
47
}
48