2016-02-01 20:44:03 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* MediaWiki cookie-based session provider interface
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Session
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Session;
|
|
|
|
|
|
2024-05-19 17:29:11 +00:00
|
|
|
use InvalidArgumentException;
|
2022-04-26 15:48:03 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2023-09-07 11:46:15 +00:00
|
|
|
use MediaWiki\Request\WebRequest;
|
2023-09-19 12:13:45 +00:00
|
|
|
use MediaWiki\User\User;
|
2022-03-16 04:08:00 +00:00
|
|
|
use MediaWiki\User\UserRigorOptions;
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A CookieSessionProvider persists sessions using cookies
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Session
|
|
|
|
|
* @since 1.27
|
|
|
|
|
*/
|
|
|
|
|
class CookieSessionProvider extends SessionProvider {
|
|
|
|
|
|
2019-02-16 20:21:03 +00:00
|
|
|
/** @var mixed[] */
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $params = [];
|
2019-02-16 20:21:03 +00:00
|
|
|
|
|
|
|
|
/** @var mixed[] */
|
2016-02-17 09:09:32 +00:00
|
|
|
protected $cookieOptions = [];
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $params Keys include:
|
|
|
|
|
* - priority: (required) Priority of the returned sessions
|
|
|
|
|
* - sessionName: Session cookie name. Doesn't honor 'prefix'. Defaults to
|
|
|
|
|
* $wgSessionName, or $wgCookiePrefix . '_session' if that is unset.
|
2022-10-27 13:14:16 +00:00
|
|
|
* - cookieOptions: Options to pass to WebRequest::setCookie():
|
2016-02-01 20:44:03 +00:00
|
|
|
* - prefix: Cookie prefix, defaults to $wgCookiePrefix
|
|
|
|
|
* - path: Cookie path, defaults to $wgCookiePath
|
|
|
|
|
* - domain: Cookie domain, defaults to $wgCookieDomain
|
|
|
|
|
* - secure: Cookie secure flag, defaults to $wgCookieSecure
|
|
|
|
|
* - httpOnly: Cookie httpOnly flag, defaults to $wgCookieHttpOnly
|
2020-07-01 00:01:00 +00:00
|
|
|
* - sameSite: Cookie SameSite attribute, defaults to $wgCookieSameSite
|
2016-02-01 20:44:03 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
public function __construct( $params = [] ) {
|
2016-02-01 20:44:03 +00:00
|
|
|
parent::__construct();
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$params += [
|
|
|
|
|
'cookieOptions' => [],
|
2016-02-01 20:44:03 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2016-02-01 20:44:03 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
|
|
|
|
|
if ( !isset( $params['priority'] ) ) {
|
2024-05-19 17:29:11 +00:00
|
|
|
throw new InvalidArgumentException( __METHOD__ . ': priority must be specified' );
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
if ( $params['priority'] < SessionInfo::MIN_PRIORITY ||
|
|
|
|
|
$params['priority'] > SessionInfo::MAX_PRIORITY
|
|
|
|
|
) {
|
2024-05-19 17:29:11 +00:00
|
|
|
throw new InvalidArgumentException( __METHOD__ . ': Invalid priority' );
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !is_array( $params['cookieOptions'] ) ) {
|
2024-05-19 17:29:11 +00:00
|
|
|
throw new InvalidArgumentException( __METHOD__ . ': cookieOptions must be an array' );
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->priority = $params['priority'];
|
|
|
|
|
$this->cookieOptions = $params['cookieOptions'];
|
|
|
|
|
$this->params = $params;
|
|
|
|
|
unset( $this->params['priority'] );
|
|
|
|
|
unset( $this->params['cookieOptions'] );
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-29 20:44:45 +00:00
|
|
|
protected function postInitSetup() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->params += [
|
2016-02-01 20:44:03 +00:00
|
|
|
'sessionName' =>
|
2022-04-26 15:48:03 +00:00
|
|
|
$this->getConfig()->get( MainConfigNames::SessionName )
|
|
|
|
|
?: $this->getConfig()->get( MainConfigNames::CookiePrefix ) . '_session',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2022-06-11 09:35:37 +00:00
|
|
|
$sameSite = $this->getConfig()->get( MainConfigNames::CookieSameSite );
|
2020-07-01 00:01:00 +00:00
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->cookieOptions += [
|
2016-02-01 20:44:03 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2022-04-26 15:48:03 +00:00
|
|
|
'prefix' => $this->getConfig()->get( MainConfigNames::CookiePrefix ),
|
|
|
|
|
'path' => $this->getConfig()->get( MainConfigNames::CookiePath ),
|
|
|
|
|
'domain' => $this->getConfig()->get( MainConfigNames::CookieDomain ),
|
|
|
|
|
'secure' => $this->getConfig()->get( MainConfigNames::CookieSecure )
|
|
|
|
|
|| $this->getConfig()->get( MainConfigNames::ForceHTTPS ),
|
|
|
|
|
'httpOnly' => $this->getConfig()->get( MainConfigNames::CookieHttpOnly ),
|
2022-06-11 09:35:37 +00:00
|
|
|
'sameSite' => $sameSite,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function provideSessionInfo( WebRequest $request ) {
|
2016-02-10 21:55:35 +00:00
|
|
|
$sessionId = $this->getCookie( $request, $this->params['sessionName'], '' );
|
2016-02-17 09:09:32 +00:00
|
|
|
$info = [
|
2016-02-01 20:44:03 +00:00
|
|
|
'provider' => $this,
|
|
|
|
|
'forceHTTPS' => $this->getCookie( $request, 'forceHTTPS', '', false )
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2016-02-10 21:55:35 +00:00
|
|
|
if ( SessionManager::validateSessionId( $sessionId ) ) {
|
|
|
|
|
$info['id'] = $sessionId;
|
|
|
|
|
$info['persisted'] = true;
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ $userId, $userName, $token ] = $this->getUserInfoFromCookies( $request );
|
|
|
|
|
if ( $userId !== null ) {
|
|
|
|
|
try {
|
|
|
|
|
$userInfo = UserInfo::newFromId( $userId );
|
2024-05-19 17:29:11 +00:00
|
|
|
} catch ( InvalidArgumentException $ex ) {
|
2016-02-01 20:44:03 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $userName !== null && $userInfo->getName() !== $userName ) {
|
2016-02-02 16:37:27 +00:00
|
|
|
$this->logger->warning(
|
|
|
|
|
'Session "{session}" requested with mismatched UserID and UserName cookies.',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2016-02-10 21:55:35 +00:00
|
|
|
'session' => $sessionId,
|
2016-02-17 09:09:32 +00:00
|
|
|
'mismatch' => [
|
2016-02-02 16:37:27 +00:00
|
|
|
'userid' => $userId,
|
|
|
|
|
'cookie_username' => $userName,
|
|
|
|
|
'username' => $userInfo->getName(),
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2024-03-10 22:26:24 +00:00
|
|
|
] );
|
2016-02-01 20:44:03 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $token !== null ) {
|
|
|
|
|
if ( !hash_equals( $userInfo->getToken(), $token ) ) {
|
2016-02-02 16:37:27 +00:00
|
|
|
$this->logger->warning(
|
|
|
|
|
'Session "{session}" requested with invalid Token cookie.',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2016-02-10 21:55:35 +00:00
|
|
|
'session' => $sessionId,
|
2016-02-02 16:37:27 +00:00
|
|
|
'userid' => $userId,
|
|
|
|
|
'username' => $userInfo->getName(),
|
2024-03-10 22:26:24 +00:00
|
|
|
] );
|
2016-02-01 20:44:03 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
$info['userInfo'] = $userInfo->verified();
|
2016-02-16 17:13:32 +00:00
|
|
|
$info['persisted'] = true; // If we have user+token, it should be
|
2016-02-01 20:44:03 +00:00
|
|
|
} elseif ( isset( $info['id'] ) ) {
|
|
|
|
|
$info['userInfo'] = $userInfo;
|
|
|
|
|
} else {
|
|
|
|
|
// No point in returning, loadSessionInfoFromStore() will
|
|
|
|
|
// reject it anyway.
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
} elseif ( isset( $info['id'] ) ) {
|
|
|
|
|
// No UserID cookie, so insist that the session is anonymous.
|
2016-02-02 16:37:27 +00:00
|
|
|
// Note: this event occurs for several normal activities:
|
|
|
|
|
// * anon visits Special:UserLogin
|
|
|
|
|
// * anon browsing after seeing Special:UserLogin
|
|
|
|
|
// * anon browsing after edit or preview
|
|
|
|
|
$this->logger->debug(
|
|
|
|
|
'Session "{session}" requested without UserID cookie',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2016-02-02 16:37:27 +00:00
|
|
|
'session' => $info['id'],
|
2024-03-10 22:26:24 +00:00
|
|
|
] );
|
2016-02-01 20:44:03 +00:00
|
|
|
$info['userInfo'] = UserInfo::newAnonymous();
|
|
|
|
|
} else {
|
|
|
|
|
// No session ID and no user is the same as an empty session, so
|
|
|
|
|
// there's no point.
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new SessionInfo( $this->priority, $info );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function persistsSessionId() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function canChangeUser() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function persistSession( SessionBackend $session, WebRequest $request ) {
|
|
|
|
|
$response = $request->response();
|
|
|
|
|
if ( $response->headersSent() ) {
|
|
|
|
|
// Can't do anything now
|
|
|
|
|
$this->logger->debug( __METHOD__ . ': Headers already sent' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$user = $session->getUser();
|
|
|
|
|
|
|
|
|
|
$cookies = $this->cookieDataToExport( $user, $session->shouldRememberUser() );
|
|
|
|
|
$sessionData = $this->sessionDataToExport( $user );
|
|
|
|
|
|
|
|
|
|
$options = $this->cookieOptions;
|
|
|
|
|
|
|
|
|
|
$forceHTTPS = $session->shouldForceHTTPS() || $user->requiresHTTPS();
|
|
|
|
|
if ( $forceHTTPS ) {
|
2022-04-26 15:48:03 +00:00
|
|
|
$options['secure'] = $this->getConfig()->get( MainConfigNames::CookieSecure )
|
|
|
|
|
|| $this->getConfig()->get( MainConfigNames::ForceHTTPS );
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$response->setCookie( $this->params['sessionName'], $session->getId(), null,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'prefix' => '' ] + $options
|
2016-02-01 20:44:03 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
foreach ( $cookies as $key => $value ) {
|
|
|
|
|
if ( $value === false ) {
|
|
|
|
|
$response->clearCookie( $key, $options );
|
|
|
|
|
} else {
|
2016-06-22 16:36:16 +00:00
|
|
|
$expirationDuration = $this->getLoginCookieExpiration( $key, $session->shouldRememberUser() );
|
2016-05-13 00:03:20 +00:00
|
|
|
$expiration = $expirationDuration ? $expirationDuration + time() : null;
|
|
|
|
|
$response->setCookie( $key, (string)$value, $expiration, $options );
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->setForceHTTPSCookie( $forceHTTPS, $session, $request );
|
|
|
|
|
$this->setLoggedOutCookie( $session->getLoggedOutTimestamp(), $request );
|
|
|
|
|
|
|
|
|
|
if ( $sessionData ) {
|
|
|
|
|
$session->addData( $sessionData );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function unpersistSession( WebRequest $request ) {
|
|
|
|
|
$response = $request->response();
|
|
|
|
|
if ( $response->headersSent() ) {
|
|
|
|
|
// Can't do anything now
|
|
|
|
|
$this->logger->debug( __METHOD__ . ': Headers already sent' );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$cookies = [
|
2016-02-01 20:44:03 +00:00
|
|
|
'UserID' => false,
|
|
|
|
|
'Token' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2016-02-01 20:44:03 +00:00
|
|
|
|
|
|
|
|
$response->clearCookie(
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->params['sessionName'], [ 'prefix' => '' ] + $this->cookieOptions
|
2016-02-01 20:44:03 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
foreach ( $cookies as $key => $value ) {
|
|
|
|
|
$response->clearCookie( $key, $this->cookieOptions );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->setForceHTTPSCookie( false, null, $request );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
Introduce $wgForceHTTPS
Add $wgForceHTTPS. When set to true:
* It makes the HTTP to HTTPS redirect unconditional and suppresses the
forceHTTPS cookie.
* It makes session cookies be secure.
* In the Action API, it triggers the existing deprecation warning and
avoids more expensive user/session checks.
* In login and signup, it suppresses the old hidden form fields for
protocol switching.
* It hides the prefershttps user preference.
Other changes:
* Factor out the HTTPS redirect in MediaWiki::main() into
maybeDoHttpsRedirect() and shouldDoHttpRedirect(). Improve
documentation.
* User::requiresHTTPS() reflects $wgForceHTTPS whereas the Session
concept of "force HTTPS" does not. The documentation of
User::requiresHTTPS() says that it includes configuration, and
retaining this definition was beneficial for some callers. Whereas
Session::shouldForceHTTPS() was used fairly narrowly as the value
of the forceHTTPS cookie, and injecting configuration into it is not
so easy or beneficial, so I left it as it was, except for clarifying
the documentation.
* Deprecate the following hooks: BeforeHttpsRedirect, UserRequiresHTTPS,
CanIPUseHTTPS. No known extension uses them, and they're not compatible
with the long-term goal of ending support for mixed-protocol wikis.
BeforeHttpsRedirect was documented as unstable from its inception.
CanIPUseHTTPS was a WMF config hack now superseded by GFOC's SNI
sniffing.
* For tests which failed with $wgForceHTTPS=true, I mostly split the
tests, testing each configuration value separately.
* Add ArrayUtils::cartesianProduct() as a helper for generating
combinations of boolean options in the session tests.
Bug: T256095
Change-Id: Iefb5ba55af35350dfc7c050f9fb8f4e8a79751cb
2020-06-24 00:56:46 +00:00
|
|
|
* Set the "forceHTTPS" cookie, unless $wgForceHTTPS prevents it.
|
|
|
|
|
*
|
2016-02-01 20:44:03 +00:00
|
|
|
* @param bool $set Whether the cookie should be set or not
|
|
|
|
|
* @param SessionBackend|null $backend
|
|
|
|
|
* @param WebRequest $request
|
|
|
|
|
*/
|
2019-10-10 15:37:58 +00:00
|
|
|
protected function setForceHTTPSCookie( $set, ?SessionBackend $backend, WebRequest $request ) {
|
2022-04-26 15:48:03 +00:00
|
|
|
if ( $this->getConfig()->get( MainConfigNames::ForceHTTPS ) ) {
|
Introduce $wgForceHTTPS
Add $wgForceHTTPS. When set to true:
* It makes the HTTP to HTTPS redirect unconditional and suppresses the
forceHTTPS cookie.
* It makes session cookies be secure.
* In the Action API, it triggers the existing deprecation warning and
avoids more expensive user/session checks.
* In login and signup, it suppresses the old hidden form fields for
protocol switching.
* It hides the prefershttps user preference.
Other changes:
* Factor out the HTTPS redirect in MediaWiki::main() into
maybeDoHttpsRedirect() and shouldDoHttpRedirect(). Improve
documentation.
* User::requiresHTTPS() reflects $wgForceHTTPS whereas the Session
concept of "force HTTPS" does not. The documentation of
User::requiresHTTPS() says that it includes configuration, and
retaining this definition was beneficial for some callers. Whereas
Session::shouldForceHTTPS() was used fairly narrowly as the value
of the forceHTTPS cookie, and injecting configuration into it is not
so easy or beneficial, so I left it as it was, except for clarifying
the documentation.
* Deprecate the following hooks: BeforeHttpsRedirect, UserRequiresHTTPS,
CanIPUseHTTPS. No known extension uses them, and they're not compatible
with the long-term goal of ending support for mixed-protocol wikis.
BeforeHttpsRedirect was documented as unstable from its inception.
CanIPUseHTTPS was a WMF config hack now superseded by GFOC's SNI
sniffing.
* For tests which failed with $wgForceHTTPS=true, I mostly split the
tests, testing each configuration value separately.
* Add ArrayUtils::cartesianProduct() as a helper for generating
combinations of boolean options in the session tests.
Bug: T256095
Change-Id: Iefb5ba55af35350dfc7c050f9fb8f4e8a79751cb
2020-06-24 00:56:46 +00:00
|
|
|
// No need to send a cookie if the wiki is always HTTPS (T256095)
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-02-01 20:44:03 +00:00
|
|
|
$response = $request->response();
|
|
|
|
|
if ( $set ) {
|
2016-05-13 00:03:20 +00:00
|
|
|
if ( $backend->shouldRememberUser() ) {
|
2016-06-22 16:36:16 +00:00
|
|
|
$expirationDuration = $this->getLoginCookieExpiration(
|
|
|
|
|
'forceHTTPS',
|
|
|
|
|
true
|
|
|
|
|
);
|
2016-05-13 00:03:20 +00:00
|
|
|
$expiration = $expirationDuration ? $expirationDuration + time() : null;
|
|
|
|
|
} else {
|
|
|
|
|
$expiration = null;
|
|
|
|
|
}
|
|
|
|
|
$response->setCookie( 'forceHTTPS', 'true', $expiration,
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'prefix' => '', 'secure' => false ] + $this->cookieOptions );
|
2016-02-01 20:44:03 +00:00
|
|
|
} else {
|
|
|
|
|
$response->clearCookie( 'forceHTTPS',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'prefix' => '', 'secure' => false ] + $this->cookieOptions );
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $loggedOut timestamp
|
|
|
|
|
* @param WebRequest $request
|
|
|
|
|
*/
|
|
|
|
|
protected function setLoggedOutCookie( $loggedOut, WebRequest $request ) {
|
|
|
|
|
if ( $loggedOut + 86400 > time() &&
|
|
|
|
|
$loggedOut !== (int)$this->getCookie( $request, 'LoggedOut', $this->cookieOptions['prefix'] )
|
|
|
|
|
) {
|
2022-02-26 16:28:48 +00:00
|
|
|
$request->response()->setCookie( 'LoggedOut', (string)$loggedOut, $loggedOut + 86400,
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->cookieOptions );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getVaryCookies() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2016-02-01 20:44:03 +00:00
|
|
|
// Vary on token and session because those are the real authn
|
|
|
|
|
// determiners. UserID and UserName don't matter without those.
|
|
|
|
|
$this->cookieOptions['prefix'] . 'Token',
|
|
|
|
|
$this->cookieOptions['prefix'] . 'LoggedOut',
|
|
|
|
|
$this->params['sessionName'],
|
|
|
|
|
'forceHTTPS',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function suggestLoginUsername( WebRequest $request ) {
|
2021-09-03 22:52:31 +00:00
|
|
|
$name = $this->getCookie( $request, 'UserName', $this->cookieOptions['prefix'] );
|
|
|
|
|
if ( $name !== null ) {
|
2022-04-11 01:39:33 +00:00
|
|
|
if ( $this->userNameUtils->isTemp( $name ) ) {
|
|
|
|
|
$name = false;
|
|
|
|
|
} else {
|
|
|
|
|
$name = $this->userNameUtils->getCanonical( $name, UserRigorOptions::RIGOR_USABLE );
|
|
|
|
|
}
|
2021-09-03 22:52:31 +00:00
|
|
|
}
|
|
|
|
|
return $name === false ? null : $name;
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch the user identity from cookies
|
2024-01-31 00:05:22 +00:00
|
|
|
* @param WebRequest $request
|
2016-02-01 20:44:03 +00:00
|
|
|
* @return array (string|null $id, string|null $username, string|null $token)
|
|
|
|
|
*/
|
|
|
|
|
protected function getUserInfoFromCookies( $request ) {
|
|
|
|
|
$prefix = $this->cookieOptions['prefix'];
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2016-02-01 20:44:03 +00:00
|
|
|
$this->getCookie( $request, 'UserID', $prefix ),
|
|
|
|
|
$this->getCookie( $request, 'UserName', $prefix ),
|
|
|
|
|
$this->getCookie( $request, 'Token', $prefix ),
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get a cookie. Contains an auth-specific hack.
|
2024-01-31 00:05:22 +00:00
|
|
|
* @param WebRequest $request
|
2016-02-01 20:44:03 +00:00
|
|
|
* @param string $key
|
|
|
|
|
* @param string $prefix
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param mixed|null $default
|
2016-02-01 20:44:03 +00:00
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
protected function getCookie( $request, $key, $prefix, $default = null ) {
|
2024-01-17 00:45:34 +00:00
|
|
|
$value = $request->getCookie( $key, $prefix, $default );
|
2016-02-01 20:44:03 +00:00
|
|
|
if ( $value === 'deleted' ) {
|
|
|
|
|
// PHP uses this value when deleting cookies. A legitimate cookie will never have
|
|
|
|
|
// this value (usernames start with uppercase, token is longer, other auth cookies
|
|
|
|
|
// are booleans or integers). Seeing this means that in a previous request we told the
|
|
|
|
|
// client to delete the cookie, but it has poor cookie handling. Pretend the cookie is
|
|
|
|
|
// not there to avoid invalidating the session.
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the data to store in cookies
|
|
|
|
|
* @param User $user
|
|
|
|
|
* @param bool $remember
|
|
|
|
|
* @return array $cookies Set value false to unset the cookie
|
|
|
|
|
*/
|
|
|
|
|
protected function cookieDataToExport( $user, $remember ) {
|
|
|
|
|
if ( $user->isAnon() ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2016-02-01 20:44:03 +00:00
|
|
|
'UserID' => false,
|
|
|
|
|
'Token' => false,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2016-02-01 20:44:03 +00:00
|
|
|
} else {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2016-02-01 20:44:03 +00:00
|
|
|
'UserID' => $user->getId(),
|
|
|
|
|
'UserName' => $user->getName(),
|
|
|
|
|
'Token' => $remember ? (string)$user->getToken() : false,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return extra data to store in the session
|
|
|
|
|
* @param User $user
|
2021-06-17 14:32:05 +00:00
|
|
|
* @return array
|
2016-02-01 20:44:03 +00:00
|
|
|
*/
|
|
|
|
|
protected function sessionDataToExport( $user ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [];
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function whyNoSession() {
|
|
|
|
|
return wfMessage( 'sessionprovider-nocookies' );
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-13 00:03:20 +00:00
|
|
|
public function getRememberUserDuration() {
|
2016-06-22 16:36:16 +00:00
|
|
|
return min( $this->getLoginCookieExpiration( 'UserID', true ),
|
|
|
|
|
$this->getLoginCookieExpiration( 'Token', true ) ) ?: null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the list of cookies that must be set to the 'remember me' duration,
|
|
|
|
|
* if $wgExtendedLoginCookieExpiration is in use.
|
|
|
|
|
*
|
|
|
|
|
* @return string[] Array of unprefixed cookie keys
|
|
|
|
|
*/
|
|
|
|
|
protected function getExtendedLoginCookies() {
|
|
|
|
|
return [ 'UserID', 'UserName', 'Token' ];
|
2016-05-13 00:03:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the lifespan of the login cookies, in seconds. 0 means until the end of the session.
|
2016-06-22 16:36:16 +00:00
|
|
|
*
|
|
|
|
|
* Cookies that are session-length do not call this function.
|
|
|
|
|
*
|
2016-05-13 00:03:20 +00:00
|
|
|
* @param string $cookieName
|
2017-08-20 11:20:59 +00:00
|
|
|
* @param bool $shouldRememberUser Whether the user should be remembered
|
2016-06-22 16:36:16 +00:00
|
|
|
* long-term
|
2016-05-13 00:03:20 +00:00
|
|
|
* @return int Cookie expiration time in seconds; 0 for session cookies
|
|
|
|
|
*/
|
2016-06-22 16:36:16 +00:00
|
|
|
protected function getLoginCookieExpiration( $cookieName, $shouldRememberUser ) {
|
|
|
|
|
$extendedCookies = $this->getExtendedLoginCookies();
|
2022-04-26 15:48:03 +00:00
|
|
|
$normalExpiration = $this->getConfig()->get( MainConfigNames::CookieExpiration );
|
2016-05-13 00:03:20 +00:00
|
|
|
|
2016-06-22 16:36:16 +00:00
|
|
|
if ( $shouldRememberUser && in_array( $cookieName, $extendedCookies, true ) ) {
|
2022-04-26 15:48:03 +00:00
|
|
|
$extendedExpiration = $this->getConfig()->get( MainConfigNames::ExtendedLoginCookieExpiration );
|
2016-06-22 16:36:16 +00:00
|
|
|
|
|
|
|
|
return ( $extendedExpiration !== null ) ? (int)$extendedExpiration : (int)$normalExpiration;
|
|
|
|
|
} else {
|
2016-05-13 00:03:20 +00:00
|
|
|
return (int)$normalExpiration;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|