Passed
Pull Request — master (#66)
by Stefano
07:49
created

OAuth2Middleware   A

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 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