simplesamlphp /
simplesamlphp-module-authtwitter
| 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 | /** |
||
| 26 | * Controller constructor. |
||
| 27 | * |
||
| 28 | * It initializes the global configuration and session for the controllers implemented here. |
||
| 29 | * |
||
| 30 | * @param \SimpleSAML\Configuration $config The configuration to use by the controllers. |
||
| 31 | * @param \SimpleSAML\Session $session The session to use by the controllers. |
||
| 32 | * |
||
| 33 | * @throws \Exception |
||
| 34 | */ |
||
| 35 | public function __construct( |
||
| 36 | protected Configuration $config, |
||
| 37 | protected Session $session, |
||
| 38 | ) { |
||
| 39 | } |
||
| 40 | |||
| 41 | |||
| 42 | /** |
||
| 43 | * Linkback. |
||
| 44 | * |
||
| 45 | * @param \Symfony\Component\HttpFoundation\Request $request The current request. |
||
| 46 | * @return \SimpleSAML\HTTP\RunnableResponse |
||
| 47 | */ |
||
| 48 | public function linkback(Request $request): RunnableResponse |
||
| 49 | { |
||
| 50 | $authState = $request->query->get('AuthState'); |
||
| 51 | if ($authState === null) { |
||
| 52 | throw new Error\BadRequest('Missing state parameter on twitter linkback endpoint.'); |
||
| 53 | } |
||
| 54 | |||
| 55 | $state = Auth\State::loadState(base64_decode($authState), TwitterSource::STAGE_INIT); |
||
| 56 | |||
| 57 | // Find authentication source |
||
| 58 | if (!array_key_exists(TwitterSource::AUTHID, $state)) { |
||
| 59 | throw new Error\BadRequest('No data in state for ' . TwitterSource::AUTHID); |
||
| 60 | } |
||
| 61 | |||
| 62 | $sourceId = $state[TwitterSource::AUTHID]; |
||
| 63 | |||
| 64 | /** @var \SimpleSAML\Module\authtwitter\Auth\Source\Twitter|null $source */ |
||
| 65 | $source = Auth\Source::getById($sourceId); |
||
| 66 | |||
| 67 | if ($source === null) { |
||
| 68 | throw new Error\BadRequest( |
||
| 69 | 'Could not find authentication source with id ' . var_export($sourceId, true), |
||
| 70 | ); |
||
| 71 | } |
||
| 72 | |||
| 73 | try { |
||
| 74 | $source->finalStep($state, $request); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 75 | } catch (Error\Exception $e) { |
||
| 76 | Auth\State::throwException($state, $e); |
||
| 77 | } catch (Exception $e) { |
||
| 78 | Auth\State::throwException( |
||
| 79 | $state, |
||
| 80 | new Error\AuthSource($sourceId, 'Error on authtwitter linkback endpoint.', $e), |
||
| 81 | ); |
||
| 82 | } |
||
| 83 | |||
| 84 | return new RunnableResponse([Auth\Source::class, 'completeAuth'], [$state]); |
||
| 85 | } |
||
| 86 | } |
||
| 87 |