Twitter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 23
c 2
b 0
f 0
dl 0
loc 64
rs 10
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B linkback() 0 39 7
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\authtwitter\Controller;
6
7
use Exception;
8
use SimpleSAML\Auth;
9
use SimpleSAML\Configuration;
10
use SimpleSAML\Error;
11
use SimpleSAML\Module\authtwitter\Auth\Source\Twitter as TwitterSource;
12
use SimpleSAML\Session;
13
use Symfony\Component\HttpFoundation\{Request, StreamedResponse};
14
15
/**
16
 * Controller class for the authtwitter module.
17
 *
18
 * This class serves the different views available in the module.
19
 *
20
 * @package simplesamlphp/simplesamlphp-module-twitter
21
 */
22
class Twitter
23
{
24
    /**
25
     * Controller constructor.
26
     *
27
     * It initializes the global configuration and session for the controllers implemented here.
28
     *
29
     * @param \SimpleSAML\Configuration $config The configuration to use by the controllers.
30
     * @param \SimpleSAML\Session $session The session to use by the controllers.
31
     *
32
     * @throws \Exception
33
     */
34
    public function __construct(
35
        protected Configuration $config,
36
        protected Session $session
37
    ) {
38
    }
39
40
41
    /**
42
     * Linkback.
43
     *
44
     * @param \Symfony\Component\HttpFoundation\Request $request The current request.
45
     * @return \Symfony\Component\HttpFoundation\StreamedResponse
46
     */
47
    public function linkback(Request $request): StreamedResponse
48
    {
49
        $authState = $request->query->get('AuthState');
50
        if ($authState === null) {
51
            throw new Error\BadRequest('Missing state parameter on twitter linkback endpoint.');
52
        }
53
54
        $state = Auth\State::loadState(base64_decode($authState), TwitterSource::STAGE_INIT);
55
56
        // Find authentication source
57
        if (is_null($state) || !array_key_exists(TwitterSource::AUTHID, $state)) {
58
            throw new Error\BadRequest('No data in state for ' . TwitterSource::AUTHID);
59
        }
60
61
        $sourceId = $state[TwitterSource::AUTHID];
62
63
        /** @var \SimpleSAML\Module\authtwitter\Auth\Source\Twitter|null $source */
64
        $source = Auth\Source::getById($sourceId);
65
66
        if ($source === null) {
67
            throw new Error\BadRequest(
68
                'Could not find authentication source with id ' . var_export($sourceId, true)
69
            );
70
        }
71
72
        try {
73
            $source->finalStep($state, $request);
74
        } catch (Error\Exception $e) {
75
            Auth\State::throwException($state, $e);
76
        } catch (Exception $e) {
77
            Auth\State::throwException(
78
                $state,
79
                new Error\AuthSource($sourceId, 'Error on authtwitter linkback endpoint.', $e)
80
            );
81
        }
82
83
        return new StreamedResponse(
84
            function () use (&$state): never {
85
                Auth\Source::completeAuth($state);
86
            }
87
        );
88
    }
89
}
90