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); |
|
|
|
|
109
|
|
|
return new RunnableResponse([Auth\Source::class, 'completeAuth'], [&$state]); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|