CAS   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 20
c 3
b 0
f 0
dl 0
loc 86
rs 10
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setAuthSource() 0 3 1
A __construct() 0 3 1
A linkback() 0 28 4
A setAuthState() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Module\cas\Controller;
6
7
use Exception;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\Auth;
10
use SimpleSAML\Configuration;
11
use SimpleSAML\Error;
12
use SimpleSAML\HTTP\RunnableResponse;
13
use SimpleSAML\Module\cas\Auth\Source\CAS as CASSource;
14
use SimpleSAML\XHTML\Template;
15
use Symfony\Component\HttpFoundation\Request;
16
17
/**
18
 * Controller class for the cas module.
19
 *
20
 * This class serves the different views available in the module.
21
 *
22
 * @package simplesamlphp/simplesamlphp-module-cas
23
 */
24
class CAS
25
{
26
    /**
27
     * @var \SimpleSAML\Auth\State|string
28
     * @psalm-var \SimpleSAML\Auth\State|class-string
29
     */
30
    protected $authState = Auth\State::class;
31
32
    /**
33
     * @var \SimpleSAML\Auth\Source|string
34
     * @psalm-var \SimpleSAML\Auth\Source|class-string
35
     */
36
    protected $authSource = Auth\Source::class;
37
38
39
    /**
40
     * Controller constructor.
41
     *
42
     * It initializes the global configuration and session for the controllers implemented here.
43
     *
44
     * @param \SimpleSAML\Configuration $config The configuration to use by the controllers.
45
     *
46
     * @throws \Exception
47
     */
48
    public function __construct(
49
        protected Configuration $config
50
    ) {
51
    }
52
53
54
    /**
55
     * Inject the \SimpleSAML\Auth\State dependency.
56
     *
57
     * @param \SimpleSAML\Auth\State $authState
58
     */
59
    public function setAuthState(Auth\State $authState): void
60
    {
61
        $this->authState = $authState;
62
    }
63
64
65
    /**
66
     * Inject the \SimpleSAML\Auth\Source dependency.
67
     *
68
     * @param \SimpleSAML\Auth\Source $authSource
69
     */
70
    public function setAuthSource(Auth\Source $authSource): void
71
    {
72
        $this->authSource = $authSource;
73
    }
74
75
76
    /**
77
     * Handle linkback-response from CAS.
78
     *
79
     * @param \Symfony\Component\HttpFoundation\Request $request
80
     * @return \SimpleSAML\HTTP\RunnableResponse
81
     */
82
    public function linkback(Request $request): RunnableResponse
83
    {
84
        if (!$request->query->has('stateId')) {
85
            throw new Error\BadRequest('Missing StateId parameter.');
86
        }
87
88
        $stateId = $request->query->get('stateId');
89
        $state = $this->authState::loadState($stateId, CASSource::STAGE_INIT);
90
91
        if (!$request->query->has('ticket')) {
92
            throw new Error\BadRequest('Missing ticket parameter.');
93
        }
94
95
        $ticket = $request->query->get('ticket');
96
        $state['cas:ticket'] = $ticket;
97
98
        // Find authentication source
99
        Assert::keyExists($state, CASSource::AUTHID);
100
        $sourceId = $state[CASSource::AUTHID];
101
102
        /** @var \SimpleSAML\Module\cas\Auth\Source\CAS|null $source */
103
        $source = $this->authSource::getById($sourceId);
104
        if ($source === null) {
105
            throw new Exception('Could not find authentication source with id ' . $sourceId);
106
        }
107
108
        $source->finalStep($state);
0 ignored issues
show
Bug introduced by
It seems like $state can also be of type null; however, parameter $state of SimpleSAML\Module\cas\Auth\Source\CAS::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

108
        $source->finalStep(/** @scrutinizer ignore-type */ $state);
Loading history...
109
        return new RunnableResponse([Auth\Source::class, 'completeAuth'], [&$state]);
110
    }
111
}
112