Twitter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
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\HTTP\RunnableResponse;
12
use SimpleSAML\Module\authtwitter\Auth\Source\Twitter as TwitterSource;
13
use SimpleSAML\Session;
14
use Symfony\Component\HttpFoundation\Request;
15
16
/**
17
 * Controller class for the authtwitter module.
18
 *
19
 * This class serves the different views available in the module.
20
 *
21
 * @package simplesamlphp/simplesamlphp-module-twitter
22
 */
23
class Twitter
24
{
25
    /**
26
     * Controller constructor.
27
     *
28
     * It initializes the global configuration and session for the controllers implemented here.
29
     *
30
     * @param \SimpleSAML\Configuration $config The configuration to use by the controllers.
31
     * @param \SimpleSAML\Session $session The session to use by the controllers.
32
     *
33
     * @throws \Exception
34
     */
35
    public function __construct(
36
        protected Configuration $config,
37
        protected Session $session,
38
    ) {
39
    }
40
41
42
    /**
43
     * Linkback.
44
     *
45
     * @param \Symfony\Component\HttpFoundation\Request $request The current request.
46
     * @return \SimpleSAML\HTTP\RunnableResponse
47
     */
48
    public function linkback(Request $request): RunnableResponse
49
    {
50
        $authState = $request->query->get('AuthState');
51
        if ($authState === null) {
52
            throw new Error\BadRequest('Missing state parameter on twitter linkback endpoint.');
53
        }
54
55
        $state = Auth\State::loadState(base64_decode($authState), TwitterSource::STAGE_INIT);
56
57
        // Find authentication source
58
        if (!array_key_exists(TwitterSource::AUTHID, $state)) {
59
            throw new Error\BadRequest('No data in state for ' . TwitterSource::AUTHID);
60
        }
61
62
        $sourceId = $state[TwitterSource::AUTHID];
63
64
        /** @var \SimpleSAML\Module\authtwitter\Auth\Source\Twitter|null $source */
65
        $source = Auth\Source::getById($sourceId);
66
67
        if ($source === null) {
68
            throw new Error\BadRequest(
69
                'Could not find authentication source with id ' . var_export($sourceId, true),
70
            );
71
        }
72
73
        try {
74
            $source->finalStep($state, $request);
0 ignored issues
show
Bug introduced by
It seems like $state can also be of type null; however, parameter $state of SimpleSAML\Module\authtw...ce\Twitter::finalStep() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
            $source->finalStep(/** @scrutinizer ignore-type */ $state, $request);
Loading history...
75
        } catch (Error\Exception $e) {
76
            Auth\State::throwException($state, $e);
77
        } catch (Exception $e) {
78
            Auth\State::throwException(
79
                $state,
80
                new Error\AuthSource($sourceId, 'Error on authtwitter linkback endpoint.', $e),
81
            );
82
        }
83
84
        return new RunnableResponse([Auth\Source::class, 'completeAuth'], [$state]);
85
    }
86
}
87