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
|
|
|
/** @var \SimpleSAML\Configuration */ |
27
|
|
|
protected Configuration $config; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var \SimpleSAML\Auth\State|string |
31
|
|
|
* @psalm-var \SimpleSAML\Auth\State|class-string |
32
|
|
|
*/ |
33
|
|
|
protected $authState = Auth\State::class; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var \SimpleSAML\Auth\Source|string |
37
|
|
|
* @psalm-var \SimpleSAML\Auth\Source|class-string |
38
|
|
|
*/ |
39
|
|
|
protected $authSource = Auth\Source::class; |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Controller constructor. |
44
|
|
|
* |
45
|
|
|
* It initializes the global configuration and session for the controllers implemented here. |
46
|
|
|
* |
47
|
|
|
* @param \SimpleSAML\Configuration $config The configuration to use by the controllers. |
48
|
|
|
* |
49
|
|
|
* @throws \Exception |
50
|
|
|
*/ |
51
|
|
|
public function __construct( |
52
|
|
|
Configuration $config |
53
|
|
|
) { |
54
|
|
|
$this->config = $config; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Inject the \SimpleSAML\Auth\State dependency. |
60
|
|
|
* |
61
|
|
|
* @param \SimpleSAML\Auth\State $authState |
62
|
|
|
*/ |
63
|
|
|
public function setAuthState(Auth\State $authState): void |
64
|
|
|
{ |
65
|
|
|
$this->authState = $authState; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Inject the \SimpleSAML\Auth\Source dependency. |
71
|
|
|
* |
72
|
|
|
* @param \SimpleSAML\Auth\Source $authSource |
73
|
|
|
*/ |
74
|
|
|
public function setAuthSource(Auth\Source $authSource): void |
75
|
|
|
{ |
76
|
|
|
$this->authSource = $authSource; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Handle linkback-response from CAS. |
82
|
|
|
* |
83
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request |
84
|
|
|
* @return \SimpleSAML\HTTP\RunnableResponse |
85
|
|
|
*/ |
86
|
|
|
public function linkback(Request $request): RunnableResponse |
87
|
|
|
{ |
88
|
|
|
if (!$request->query->has('StateId')) { |
89
|
|
|
throw new Error\BadRequest('Missing StateId parameter.'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$stateId = $request->query->get('StateId'); |
93
|
|
|
$state = $this->authState::loadState($stateId, CASSource::STAGE_INIT); |
94
|
|
|
|
95
|
|
|
if (!$request->query->has('ticket')) { |
96
|
|
|
throw new Error\BadRequest('Missing ticket parameter.'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$ticket = $request->query->get('ticket'); |
100
|
|
|
$state['cas:ticket'] = $ticket; |
101
|
|
|
|
102
|
|
|
// Find authentication source |
103
|
|
|
Assert::keyExists($state, CASSource::AUTHID); |
104
|
|
|
$sourceId = $state[CASSource::AUTHID]; |
105
|
|
|
|
106
|
|
|
/** @var \SimpleSAML\Module\cas\Auth\Source\CAS|null $source */ |
107
|
|
|
$source = $this->authSource::getById($sourceId); |
108
|
|
|
if ($source === null) { |
109
|
|
|
throw new Exception('Could not find authentication source with id ' . $sourceId); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$source->finalStep($state); |
|
|
|
|
113
|
|
|
return new RunnableResponse([Auth\Source::class, 'completeAuth'], [&$state]); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|