MediaWiki  1.29.2
BotPasswordSessionProvider.php
Go to the documentation of this file.
1 <?php
25 
27 use User;
29 
35 
42  public function __construct( array $params = [] ) {
43  if ( !isset( $params['sessionCookieName'] ) ) {
44  $params['sessionCookieName'] = '_BPsession';
45  }
46  parent::__construct( $params );
47 
48  if ( !isset( $params['priority'] ) ) {
49  throw new \InvalidArgumentException( __METHOD__ . ': priority must be specified' );
50  }
51  if ( $params['priority'] < SessionInfo::MIN_PRIORITY ||
53  ) {
54  throw new \InvalidArgumentException( __METHOD__ . ': Invalid priority' );
55  }
56 
57  $this->priority = $params['priority'];
58  }
59 
60  public function provideSessionInfo( WebRequest $request ) {
61  // Only relevant for the API
62  if ( !defined( 'MW_API' ) ) {
63  return null;
64  }
65 
66  // Enabled?
67  if ( !$this->config->get( 'EnableBotPasswords' ) ) {
68  return null;
69  }
70 
71  // Have a session ID?
72  $id = $this->getSessionIdFromCookie( $request );
73  if ( $id === null ) {
74  return null;
75  }
76 
77  return new SessionInfo( $this->priority, [
78  'provider' => $this,
79  'id' => $id,
80  'persisted' => true
81  ] );
82  }
83 
84  public function newSessionInfo( $id = null ) {
85  // We don't activate by default
86  return null;
87  }
88 
97  $id = $this->getSessionIdFromCookie( $request );
99  'provider' => $this,
100  'id' => $id,
101  'userInfo' => UserInfo::newFromUser( $user, true ),
102  'persisted' => $id !== null,
103  'metadata' => [
104  'centralId' => $bp->getUserCentralId(),
105  'appId' => $bp->getAppId(),
106  'token' => $bp->getToken(),
107  'rights' => \MWGrants::getGrantRights( $bp->getGrants() ),
108  ],
109  ] );
110  $session = $this->getManager()->getSessionFromInfo( $info, $request );
111  $session->persist();
112  return $session;
113  }
114 
115  public function refreshSessionInfo( SessionInfo $info, WebRequest $request, &$metadata ) {
116  $missingKeys = array_diff(
117  [ 'centralId', 'appId', 'token' ],
118  array_keys( $metadata )
119  );
120  if ( $missingKeys ) {
121  $this->logger->info( 'Session "{session}": Missing metadata: {missing}', [
122  'session' => $info,
123  'missing' => implode( ', ', $missingKeys ),
124  ] );
125  return false;
126  }
127 
128  $bp = BotPassword::newFromCentralId( $metadata['centralId'], $metadata['appId'] );
129  if ( !$bp ) {
130  $this->logger->info(
131  'Session "{session}": No BotPassword for {centralId} {appId}',
132  [
133  'session' => $info,
134  'centralId' => $metadata['centralId'],
135  'appId' => $metadata['appId'],
136  ] );
137  return false;
138  }
139 
140  if ( !hash_equals( $metadata['token'], $bp->getToken() ) ) {
141  $this->logger->info( 'Session "{session}": BotPassword token check failed', [
142  'session' => $info,
143  'centralId' => $metadata['centralId'],
144  'appId' => $metadata['appId'],
145  ] );
146  return false;
147  }
148 
149  $status = $bp->getRestrictions()->check( $request );
150  if ( !$status->isOK() ) {
151  $this->logger->info(
152  'Session "{session}": Restrictions check failed',
153  [
154  'session' => $info,
155  'restrictions' => $status->getValue(),
156  'centralId' => $metadata['centralId'],
157  'appId' => $metadata['appId'],
158  ] );
159  return false;
160  }
161 
162  // Update saved rights
163  $metadata['rights'] = \MWGrants::getGrantRights( $bp->getGrants() );
164 
165  return true;
166  }
167 
171  public function preventSessionsForUser( $username ) {
173  }
174 
175  public function getAllowedUserRights( SessionBackend $backend ) {
176  if ( $backend->getProvider() !== $this ) {
177  throw new \InvalidArgumentException( 'Backend\'s provider isn\'t $this' );
178  }
179  $data = $backend->getProviderMetadata();
180  if ( $data && isset( $data['rights'] ) && is_array( $data['rights'] ) ) {
181  return $data['rights'];
182  }
183 
184  // Should never happen
185  $this->logger->debug( __METHOD__ . ': No provider metadata, returning no rights allowed' );
186  return [];
187  }
188 }
MediaWiki\Session\Session\BotPasswordSessionProvider\provideSessionInfo
provideSessionInfo(WebRequest $request)
Provide session info for a request.
Definition: BotPasswordSessionProvider.php:60
MediaWiki\Session\SessionProvider\getManager
getManager()
Get the session manager.
Definition: SessionProvider.php:128
$request
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2612
BotPassword\getUserCentralId
getUserCentralId()
Get the central user ID.
Definition: BotPassword.php:190
MediaWiki\Session\ImmutableSessionProviderWithCookie\getSessionIdFromCookie
getSessionIdFromCookie(WebRequest $request)
Get the session ID from the cookie, if any.
Definition: ImmutableSessionProviderWithCookie.php:80
MediaWiki\Session\Session\BotPasswordSessionProvider\getAllowedUserRights
getAllowedUserRights(SessionBackend $backend)
Fetch the rights allowed the user when the specified session is active.
Definition: BotPasswordSessionProvider.php:175
MediaWiki\Session\Session\BotPasswordSessionProvider
Session provider for bot passwords.
Definition: BotPasswordSessionProvider.php:34
MediaWiki\Session\SessionBackend\getProviderMetadata
getProviderMetadata()
Fetch provider metadata.
Definition: SessionBackend.php:481
MWGrants\getGrantRights
static getGrantRights( $grants)
Fetch the rights allowed by a set of grants.
Definition: MWGrants.php:107
BotPassword
Utility class for bot passwords.
Definition: BotPassword.php:28
MediaWiki\Session\Session\BotPasswordSessionProvider\refreshSessionInfo
refreshSessionInfo(SessionInfo $info, WebRequest $request, &$metadata)
Validate a loaded SessionInfo and refresh provider metadata.
Definition: BotPasswordSessionProvider.php:115
$status
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1049
MediaWiki\Session\SessionBackend\getProvider
getProvider()
Fetch the SessionProvider for this session.
Definition: SessionBackend.php:260
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:246
$params
$params
Definition: styleTest.css.php:40
BotPassword\getAppId
getAppId()
Get the app ID.
Definition: BotPassword.php:198
BotPassword\getToken
getToken()
Get the token.
Definition: BotPassword.php:206
User
User
Definition: All_system_messages.txt:425
MediaWiki\Session\UserInfo\newFromUser
static newFromUser(User $user, $verified=false)
Create an instance from an existing User object.
Definition: UserInfo.php:116
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
MediaWiki\Session\Session\BotPasswordSessionProvider\newSessionForRequest
newSessionForRequest(User $user, BotPassword $bp, WebRequest $request)
Create a new session for a request.
Definition: BotPasswordSessionProvider.php:96
MediaWiki\Session\Session\BotPasswordSessionProvider\preventSessionsForUser
preventSessionsForUser( $username)
Prevent future sessions for the user.
Definition: BotPasswordSessionProvider.php:171
MediaWiki\Session\ImmutableSessionProviderWithCookie
An ImmutableSessionProviderWithCookie doesn't persist the user, but optionally can use a cookie to su...
Definition: ImmutableSessionProviderWithCookie.php:40
MediaWiki\Session\Session\BotPasswordSessionProvider\__construct
__construct(array $params=[])
Definition: BotPasswordSessionProvider.php:42
MediaWiki\Session
Definition: BotPasswordSessionProvider.php:24
MediaWiki\Session\SessionInfo\MAX_PRIORITY
const MAX_PRIORITY
Maximum allowed priority.
Definition: SessionInfo.php:39
MediaWiki\Session\Session\BotPasswordSessionProvider\newSessionInfo
newSessionInfo( $id=null)
Provide session info for a new, empty session.
Definition: BotPasswordSessionProvider.php:84
BotPassword\newFromCentralId
static newFromCentralId( $centralId, $appId, $flags=self::READ_NORMAL)
Load a BotPassword from the database.
Definition: BotPassword.php:104
WebRequest
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
Definition: WebRequest.php:38
MediaWiki\Session\SessionInfo
Value object returned by SessionProvider.
Definition: SessionInfo.php:34
BotPassword\getGrants
getGrants()
Get the grants.
Definition: BotPassword.php:222
BotPassword\removeAllPasswordsForUser
static removeAllPasswordsForUser( $username)
Remove all passwords for a user, by name.
Definition: BotPassword.php:364
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:50
MediaWiki\Session\SessionInfo\MIN_PRIORITY
const MIN_PRIORITY
Minimum allowed priority.
Definition: SessionInfo.php:36
$username
this hook is for auditing only or null if authentication failed before getting that far $username
Definition: hooks.txt:783
array
the array() calling protocol came about after MediaWiki 1.4rc1.
MediaWiki\Session\SessionBackend
This is the actual workhorse for Session.
Definition: SessionBackend.php:49