1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace SimpleSAML\Module\authtwitter\Controller; |
6
|
|
|
|
7
|
|
|
use SimpleSAML\Auth; |
8
|
|
|
use SimpleSAML\Configuration; |
9
|
|
|
use SimpleSAML\Error; |
10
|
|
|
use SimpleSAML\HTTP\RunnableResponse; |
11
|
|
|
use SimpleSAML\Module\authtwitter\Auth\Source\Twitter as TwitterSource; |
12
|
|
|
use SimpleSAML\Session; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Controller class for the authtwitter module. |
17
|
|
|
* |
18
|
|
|
* This class serves the different views available in the module. |
19
|
|
|
* |
20
|
|
|
* @package simplesamlphp/simplesamlphp-module-twitter |
21
|
|
|
*/ |
22
|
|
|
class Twitter |
23
|
|
|
{ |
24
|
|
|
/** @var \SimpleSAML\Configuration */ |
25
|
|
|
protected Configuration $config; |
26
|
|
|
|
27
|
|
|
/** @var \SimpleSAML\Session */ |
28
|
|
|
protected Session $session; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Controller constructor. |
33
|
|
|
* |
34
|
|
|
* It initializes the global configuration and session for the controllers implemented here. |
35
|
|
|
* |
36
|
|
|
* @param \SimpleSAML\Configuration $config The configuration to use by the controllers. |
37
|
|
|
* @param \SimpleSAML\Session $session The session to use by the controllers. |
38
|
|
|
* |
39
|
|
|
* @throws \Exception |
40
|
|
|
*/ |
41
|
|
|
public function __construct( |
42
|
|
|
Configuration $config, |
43
|
|
|
Session $session |
44
|
|
|
) { |
45
|
|
|
$this->config = $config; |
46
|
|
|
$this->session = $session; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Linkback. |
52
|
|
|
* |
53
|
|
|
* @param \Symfony\Component\HttpFoundation\Request $request The current request. |
54
|
|
|
* |
55
|
|
|
*/ |
56
|
|
|
public function linkback(Request $request) |
57
|
|
|
{ |
58
|
|
|
$authState = $request->get('AuthState'); |
59
|
|
|
if ($authState === null) { |
60
|
|
|
throw new Error\BadRequest('Missing state parameter on twitter linkback endpoint.'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$state = Auth\State::loadState($authState, TwitterSource::STAGE_INIT); |
64
|
|
|
|
65
|
|
|
// Find authentication source |
66
|
|
|
if (is_null($state) || !array_key_exists(TwitterSource::AUTHID, $state)) { |
67
|
|
|
throw new Error\BadRequest('No data in state for ' . TwitterSource::AUTHID); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$sourceId = $state[TwitterSource::AUTHID]; |
71
|
|
|
|
72
|
|
|
/** @var \SimpleSAML\Module\authtwitter\Auth\Source\Twitter|null $source */ |
73
|
|
|
$source = Auth\Source::getById($sourceId); |
74
|
|
|
|
75
|
|
|
if ($source === null) { |
76
|
|
|
throw new Error\BadRequest( |
77
|
|
|
'Could not find authentication source with id ' . var_export($sourceId, true) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
try { |
82
|
|
|
if ($request->request->has('denied')) { |
83
|
|
|
throw new Error\UserAborted(); |
84
|
|
|
} |
85
|
|
|
$source->finalStep($state, $request); |
86
|
|
|
} catch (Error\Exception $e) { |
87
|
|
|
Auth\State::throwException($state, $e); |
88
|
|
|
} catch (Exception $e) { |
|
|
|
|
89
|
|
|
Auth\State::throwException( |
90
|
|
|
$state, |
91
|
|
|
new Error\AuthSource($sourceId, 'Error on authtwitter linkback endpoint.', $e) |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return RunnableResponse([Auth\Source::class, 'completeAuth'], [$state]); |
|
|
|
|
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|