Passed
Pull Request — master (#2)
by Tim
02:17
created

Twitter::linkback()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 7
eloc 20
c 4
b 0
f 0
nc 6
nop 1
dl 0
loc 37
rs 8.6666
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
    /** @var \SimpleSAML\Configuration */
26
    protected Configuration $config;
27
28
    /** @var \SimpleSAML\Session */
29
    protected Session $session;
30
31
32
    /**
33
     * Controller constructor.
34
     *
35
     * It initializes the global configuration and session for the controllers implemented here.
36
     *
37
     * @param \SimpleSAML\Configuration $config The configuration to use by the controllers.
38
     * @param \SimpleSAML\Session $session The session to use by the controllers.
39
     *
40
     * @throws \Exception
41
     */
42
    public function __construct(
43
        Configuration $config,
44
        Session $session
45
    ) {
46
        $this->config = $config;
47
        $this->session = $session;
48
    }
49
50
51
    /**
52
     * Linkback.
53
     *
54
     * @param \Symfony\Component\HttpFoundation\Request $request The current request.
55
     *
56
     */
57
    public function linkback(Request $request)
58
    {
59
        $authState = $request->get('AuthState');
60
        if ($authState === null) {
61
            throw new Error\BadRequest('Missing state parameter on twitter linkback endpoint.');
62
        }
63
64
        $state = Auth\State::loadState(base64_decode($authState), TwitterSource::STAGE_INIT);
65
66
        // Find authentication source
67
        if (is_null($state) || !array_key_exists(TwitterSource::AUTHID, $state)) {
68
            throw new Error\BadRequest('No data in state for ' . TwitterSource::AUTHID);
69
        }
70
71
        $sourceId = $state[TwitterSource::AUTHID];
72
73
        /** @var \SimpleSAML\Module\authtwitter\Auth\Source\Twitter|null $source */
74
        $source = Auth\Source::getById($sourceId);
75
76
        if ($source === null) {
77
            throw new Error\BadRequest(
78
                'Could not find authentication source with id ' . var_export($sourceId, true)
79
            );
80
        }
81
82
        try {
83
            $source->finalStep($state, $request);
84
        } catch (Error\Exception $e) {
85
            Auth\State::throwException($state, $e);
86
        } catch (Exception $e) {
87
            Auth\State::throwException(
88
                $state,
89
                new Error\AuthSource($sourceId, 'Error on authtwitter linkback endpoint.', $e)
90
            );
91
        }
92
93
        return new RunnableResponse([Auth\Source::class, 'completeAuth'], [&$state]);
94
    }
95
}
96