|
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_TEMP); |
|
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
|
|
|
$requestToken = unserialize($state['authtwitter:authdata:requestToken']); |
|
83
|
|
|
|
|
84
|
|
|
$oauth_token = $request->get('oauth_token'); |
|
85
|
|
|
if ($oauth_token === null) { |
|
86
|
|
|
throw new Error\BadRequest("Missing oauth_token parameter."); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if ($requestToken->getIdentifier() !== $oauth_token) { |
|
90
|
|
|
throw new Error\BadRequest("Invalid oauth_token parameter."); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$oauth_verifier = $request->get('oauth_verifier'); |
|
94
|
|
|
if ($oauth_verifier === null) { |
|
95
|
|
|
throw new Error\BadRequest("Missing oauth_verifier parameter."); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
try { |
|
99
|
|
|
$source->finalStep($requestToken, $oauth_token, $oauth_verifier); |
|
100
|
|
|
} catch (Error\Exception $e) { |
|
101
|
|
|
Auth\State::throwException($state, $e); |
|
102
|
|
|
} catch (Exception $e) { |
|
103
|
|
|
Auth\State::throwException( |
|
104
|
|
|
$state, |
|
105
|
|
|
new Error\AuthSource($sourceId, 'Error on authtwitter linkback endpoint.', $e) |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return new RunnableResponse([Auth\Source::class, 'completeAuth'], [&$state]); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|