BotPasswordSessionProvider.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * Session provider for bot passwords
  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. * @ingroup Session
  22. */
  23. namespace MediaWiki\Session;
  24. use BotPassword;
  25. use User;
  26. use WebRequest;
  27. /**
  28. * Session provider for bot passwords
  29. * @since 1.27
  30. */
  31. class BotPasswordSessionProvider extends ImmutableSessionProviderWithCookie {
  32. /**
  33. * @param array $params Keys include:
  34. * - priority: (required) Set the priority
  35. * - sessionCookieName: Session cookie name. Default is '_BPsession'.
  36. * - sessionCookieOptions: Options to pass to WebResponse::setCookie().
  37. */
  38. public function __construct( array $params = [] ) {
  39. if ( !isset( $params['sessionCookieName'] ) ) {
  40. $params['sessionCookieName'] = '_BPsession';
  41. }
  42. parent::__construct( $params );
  43. if ( !isset( $params['priority'] ) ) {
  44. throw new \InvalidArgumentException( __METHOD__ . ': priority must be specified' );
  45. }
  46. if ( $params['priority'] < SessionInfo::MIN_PRIORITY ||
  47. $params['priority'] > SessionInfo::MAX_PRIORITY
  48. ) {
  49. throw new \InvalidArgumentException( __METHOD__ . ': Invalid priority' );
  50. }
  51. $this->priority = $params['priority'];
  52. }
  53. public function provideSessionInfo( WebRequest $request ) {
  54. // Only relevant for the API
  55. if ( !defined( 'MW_API' ) ) {
  56. return null;
  57. }
  58. // Enabled?
  59. if ( !$this->config->get( 'EnableBotPasswords' ) ) {
  60. return null;
  61. }
  62. // Have a session ID?
  63. $id = $this->getSessionIdFromCookie( $request );
  64. if ( $id === null ) {
  65. return null;
  66. }
  67. return new SessionInfo( $this->priority, [
  68. 'provider' => $this,
  69. 'id' => $id,
  70. 'persisted' => true
  71. ] );
  72. }
  73. public function newSessionInfo( $id = null ) {
  74. // We don't activate by default
  75. return null;
  76. }
  77. /**
  78. * Create a new session for a request
  79. * @param User $user
  80. * @param BotPassword $bp
  81. * @param WebRequest $request
  82. * @return Session
  83. */
  84. public function newSessionForRequest( User $user, BotPassword $bp, WebRequest $request ) {
  85. $id = $this->getSessionIdFromCookie( $request );
  86. $info = new SessionInfo( SessionInfo::MAX_PRIORITY, [
  87. 'provider' => $this,
  88. 'id' => $id,
  89. 'userInfo' => UserInfo::newFromUser( $user, true ),
  90. 'persisted' => $id !== null,
  91. 'metadata' => [
  92. 'centralId' => $bp->getUserCentralId(),
  93. 'appId' => $bp->getAppId(),
  94. 'token' => $bp->getToken(),
  95. 'rights' => \MWGrants::getGrantRights( $bp->getGrants() ),
  96. ],
  97. ] );
  98. $session = $this->getManager()->getSessionFromInfo( $info, $request );
  99. $session->persist();
  100. return $session;
  101. }
  102. public function refreshSessionInfo( SessionInfo $info, WebRequest $request, &$metadata ) {
  103. $missingKeys = array_diff(
  104. [ 'centralId', 'appId', 'token' ],
  105. array_keys( $metadata )
  106. );
  107. if ( $missingKeys ) {
  108. $this->logger->info( 'Session "{session}": Missing metadata: {missing}', [
  109. 'session' => $info,
  110. 'missing' => implode( ', ', $missingKeys ),
  111. ] );
  112. return false;
  113. }
  114. $bp = BotPassword::newFromCentralId( $metadata['centralId'], $metadata['appId'] );
  115. if ( !$bp ) {
  116. $this->logger->info(
  117. 'Session "{session}": No BotPassword for {centralId} {appId}',
  118. [
  119. 'session' => $info,
  120. 'centralId' => $metadata['centralId'],
  121. 'appId' => $metadata['appId'],
  122. ] );
  123. return false;
  124. }
  125. if ( !hash_equals( $metadata['token'], $bp->getToken() ) ) {
  126. $this->logger->info( 'Session "{session}": BotPassword token check failed', [
  127. 'session' => $info,
  128. 'centralId' => $metadata['centralId'],
  129. 'appId' => $metadata['appId'],
  130. ] );
  131. return false;
  132. }
  133. $status = $bp->getRestrictions()->check( $request );
  134. if ( !$status->isOK() ) {
  135. $this->logger->info(
  136. 'Session "{session}": Restrictions check failed',
  137. [
  138. 'session' => $info,
  139. 'restrictions' => $status->getValue(),
  140. 'centralId' => $metadata['centralId'],
  141. 'appId' => $metadata['appId'],
  142. ] );
  143. return false;
  144. }
  145. // Update saved rights
  146. $metadata['rights'] = \MWGrants::getGrantRights( $bp->getGrants() );
  147. return true;
  148. }
  149. /**
  150. * @codeCoverageIgnore
  151. * @inheritDoc
  152. */
  153. public function preventSessionsForUser( $username ) {
  154. BotPassword::removeAllPasswordsForUser( $username );
  155. }
  156. public function getAllowedUserRights( SessionBackend $backend ) {
  157. if ( $backend->getProvider() !== $this ) {
  158. throw new \InvalidArgumentException( 'Backend\'s provider isn\'t $this' );
  159. }
  160. $data = $backend->getProviderMetadata();
  161. if ( $data && isset( $data['rights'] ) && is_array( $data['rights'] ) ) {
  162. return $data['rights'];
  163. }
  164. // Should never happen
  165. $this->logger->debug( __METHOD__ . ': No provider metadata, returning no rights allowed' );
  166. return [];
  167. }
  168. }