ApiLinkAccount.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Copyright © 2016 Wikimedia Foundation and contributors
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. use MediaWiki\Auth\AuthManager;
  23. use MediaWiki\Auth\AuthenticationResponse;
  24. /**
  25. * Link an account with AuthManager
  26. *
  27. * @ingroup API
  28. */
  29. class ApiLinkAccount extends ApiBase {
  30. public function __construct( ApiMain $main, $action ) {
  31. parent::__construct( $main, $action, 'link' );
  32. }
  33. public function getFinalDescription() {
  34. // A bit of a hack to append 'api-help-authmanager-general-usage'
  35. $msgs = parent::getFinalDescription();
  36. $msgs[] = ApiBase::makeMessage( 'api-help-authmanager-general-usage', $this->getContext(), [
  37. $this->getModulePrefix(),
  38. $this->getModuleName(),
  39. $this->getModulePath(),
  40. AuthManager::ACTION_LINK,
  41. self::needsToken(),
  42. ] );
  43. return $msgs;
  44. }
  45. public function execute() {
  46. if ( !$this->getUser()->isLoggedIn() ) {
  47. $this->dieWithError( 'apierror-mustbeloggedin-linkaccounts', 'notloggedin' );
  48. }
  49. $params = $this->extractRequestParams();
  50. $this->requireAtLeastOneParameter( $params, 'continue', 'returnurl' );
  51. if ( $params['returnurl'] !== null ) {
  52. $bits = wfParseUrl( $params['returnurl'] );
  53. if ( !$bits || $bits['scheme'] === '' ) {
  54. $encParamName = $this->encodeParamName( 'returnurl' );
  55. $this->dieWithError(
  56. [ 'apierror-badurl', $encParamName, wfEscapeWikiText( $params['returnurl'] ) ],
  57. "badurl_{$encParamName}"
  58. );
  59. }
  60. }
  61. $helper = new ApiAuthManagerHelper( $this );
  62. $manager = AuthManager::singleton();
  63. // Check security-sensitive operation status
  64. $helper->securitySensitiveOperation( 'LinkAccounts' );
  65. // Make sure it's possible to link accounts
  66. if ( !$manager->canLinkAccounts() ) {
  67. $this->getResult()->addValue( null, 'linkaccount', $helper->formatAuthenticationResponse(
  68. AuthenticationResponse::newFail( $this->msg( 'userlogin-cannot-' . AuthManager::ACTION_LINK ) )
  69. ) );
  70. return;
  71. }
  72. // Perform the link step
  73. if ( $params['continue'] ) {
  74. $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LINK_CONTINUE );
  75. $res = $manager->continueAccountLink( $reqs );
  76. } else {
  77. $reqs = $helper->loadAuthenticationRequests( AuthManager::ACTION_LINK );
  78. $res = $manager->beginAccountLink( $this->getUser(), $reqs, $params['returnurl'] );
  79. }
  80. $this->getResult()->addValue( null, 'linkaccount',
  81. $helper->formatAuthenticationResponse( $res ) );
  82. }
  83. public function isReadMode() {
  84. return false;
  85. }
  86. public function isWriteMode() {
  87. return true;
  88. }
  89. public function needsToken() {
  90. return 'csrf';
  91. }
  92. public function getAllowedParams() {
  93. return ApiAuthManagerHelper::getStandardParams( AuthManager::ACTION_LINK,
  94. 'requests', 'messageformat', 'mergerequestfields', 'returnurl', 'continue'
  95. );
  96. }
  97. public function dynamicParameterDocumentation() {
  98. return [ 'api-help-authmanagerhelper-additional-params', AuthManager::ACTION_LINK ];
  99. }
  100. protected function getExamplesMessages() {
  101. return [
  102. 'action=linkaccount&provider=Example&linkreturnurl=http://example.org/&linktoken=123ABC'
  103. => 'apihelp-linkaccount-example-link',
  104. ];
  105. }
  106. public function getHelpUrls() {
  107. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Linkaccount';
  108. }
  109. }