* @copyright 2001-2021 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*/
-final class TwitterAuthAction extends AbstractAction
+final class TwitterAuthAction extends AbstractOauth2AuthAction
{
- /**
- * @inheritDoc
- */
- public $neededModules = ['TWITTER_PUBLIC_KEY', 'TWITTER_PRIVATE_KEY'];
+ const AVAILABLE_DURING_OFFLINE_MODE = true;
+
+ #[\Override]
+ protected function getClientId(): string
+ {
+ return StringUtil::trim(TWITTER_PUBLIC_KEY);
+ }
- private ClientInterface $httpClient;
+ #[\Override]
+ protected function getClientSecret(): string
+ {
+ return StringUtil::trim(TWITTER_PRIVATE_KEY);
+ }
- /**
- * @inheritDoc
- */
- public function readParameters()
+ #[\Override]
+ protected function getCallbackUrl(): string
{
- parent::readParameters();
+ return LinkHandler::getInstance()->getControllerLink(self::class);
+ }
- if (WCF::getSession()->spiderID) {
- throw new PermissionDeniedException();
- }
+ #[\Override]
+ protected function getTokenEndpoint(): string
+ {
+ return 'https://api.twitter.com/oauth/access_token';
}
- /**
- * @inheritDoc
- */
- public function execute(): ResponseInterface
+ #[\Override]
+ protected function supportsState(): bool
{
- parent::execute();
+ return false;
+ }
- try {
- if (isset($_GET['oauth_token']) && isset($_GET['oauth_verifier'])) {
- $token = $this->verifierToAccessToken(
- $_GET['oauth_token'],
- $_GET['oauth_verifier']
- );
-
- $oauthUser = $this->getUser($token);
-
- return $this->processUser($oauthUser);
- } elseif (isset($_GET['denied'])) {
- throw new NamedUserException(
- WCF::getLanguage()->getDynamicVariable('wcf.user.3rdparty.login.error.denied')
- );
- } else {
- return $this->initiate();
- }
- } catch (NamedUserException | PermissionDeniedException $e) {
- throw $e;
- } catch (StateValidationException $e) {
- throw new NamedUserException(WCF::getLanguage()->getDynamicVariable(
- 'wcf.user.3rdparty.login.error.stateValidation'
- ));
- } catch (\Exception $e) {
- $exceptionID = \wcf\functions\exception\logThrowable($e);
-
- $type = 'genericException';
- if ($e instanceof ClientExceptionInterface) {
- $type = 'httpError';
- }
+ #[\Override]
+ protected function getProviderName(): string
+ {
+ return 'twitter';
+ }
- throw new NamedUserException(WCF::getLanguage()->getDynamicVariable(
- 'wcf.user.3rdparty.login.error.' . $type,
- [
- 'exceptionID' => $exceptionID,
- ]
- ));
- }
+ #[\Override]
+ protected function getScope(): string
+ {
+ // Twitter OAuth 1.0a does not support scopes
+ return '';
+ }
- throw new \LogicException("Unreachable");
+ #[\Override]
+ protected function getAuthorizeUrl(): string
+ {
+ return 'https://api.twitter.com/oauth/authenticate';
}
- /**
- * Processes the user (e.g. by registering session variables and redirecting somewhere).
- */
- protected function processUser(OauthUser $oauthUser): ResponseInterface
+ #[\Override]
+ protected function mapParameters(ServerRequestInterface $request): OAuth2Success | OAuth2Failure | null
{
- $user = User::getUserByAuthData('twitter:' . $oauthUser->getId());
-
- if ($user->userID) {
- if (WCF::getUser()->userID) {
- // This account belongs to an existing user, but we are already logged in.
- // This can't be handled.
-
- throw new NamedUserException(
- WCF::getLanguage()->getDynamicVariable('wcf.user.3rdparty.twitter.connect.error.inuse')
- );
- } else {
- // This account belongs to an existing user, we are not logged in.
- // Perform the login.
-
- WCF::getSession()->changeUser($user);
- WCF::getSession()->update();
- EventHandler::getInstance()->fire(
- new UserLoggedIn($user)
- );
-
- return new RedirectResponse(
- LinkHandler::getInstance()->getLink()
- );
- }
- } else {
- WCF::getSession()->register('__3rdPartyProvider', 'twitter');
-
- if (WCF::getUser()->userID) {
- // This account does not belong to anyone and we are already logged in.
- // Thus we want to connect this account.
-
- WCF::getSession()->register('__oauthUser', $oauthUser);
-
- return new RedirectResponse(
- LinkHandler::getInstance()->getControllerLink(
- AccountManagementForm::class,
- [],
- '#3rdParty'
- )
- );
- } else {
- // This account does not belong to anyone and we are not logged in.
- // Thus we want to connect this account to a newly registered user.
-
- WCF::getSession()->register('__oauthUser', $oauthUser);
- WCF::getSession()->register('__username', $oauthUser->getUsername());
- WCF::getSession()->register('__email', $oauthUser->getEmail());
-
- // We assume that bots won't register an external account first, so
- // we skip the captcha.
- WCF::getSession()->register('noRegistrationCaptcha', true);
-
- WCF::getSession()->update();
-
- return new RedirectResponse(
- LinkHandler::getInstance()->getControllerLink(RegisterForm::class)
- );
- }
+ try {
+ $mapper = (new MapperBuilder())
+ ->allowSuperfluousKeys()
+ ->enableFlexibleCasting()
+ ->mapper();
+
+ return $mapper->map(
+ \sprintf("%s|%s", OAuth2TwitterSuccess::class, OAuth2TwitterFailure::class),
+ $request->getQueryParams()
+ );
+ } catch (\Throwable) {
+ return null;
}
}
}
if ($parser->getOutputType() == 'text/html') {
+ $float = (!empty($openingTag['attributes'][2])) ? $openingTag['attributes'][2] : 'none';
+
if ($media->isImage) {
$thumbnailSize = (!empty($openingTag['attributes'][1])) ? $openingTag['attributes'][1] : 'original';
- $float = (!empty($openingTag['attributes'][2])) ? $openingTag['attributes'][2] : 'none';
$width = (!empty($openingTag['attributes'][3])) ? $openingTag['attributes'][3] : 'auto';
- return WCF::getTPL()->fetch('mediaBBCodeTag', 'wcf', [
+ return WCF::getTPL()->fetch('shared_bbcode_wsm', 'wcf', [
'mediaLink' => $this->getLink($media),
'removeLinks' => $removeLinks,
'thumbnailLink' => $thumbnailSize !== 'original' ? $this->getThumbnailLink(
'width' => $width,
]);
} elseif ($media->isVideo() || $media->isAudio()) {
- return WCF::getTPL()->fetch('mediaBBCodeTag', 'wcf', [
+ return WCF::getTPL()->fetch('shared_bbcode_wsm', 'wcf', [
'mediaLink' => $this->getLink($media),
'removeLinks' => $removeLinks,
- 'float' => 'none',
+ 'float' => $float,
'media' => $media->getLocalizedVersion(MessageEmbeddedObjectManager::getInstance()->getActiveMessageLanguageID()),
'width' => 'auto',
]);