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;
|
|
|
|
|
|
2021-05-28 13:38:53 +00:00
|
|
|
use MediaWiki\User\UserNameUtils;
|
2016-02-01 20:44:03 +00:00
|
|
|
use User;
|
|
|
|
|
use WebRequest;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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
|
|
|
|
2020-07-01 00:01:00 +00:00
|
|
|
/** @var bool */
|
|
|
|
|
protected $useCrossSiteCookies;
|
|
|
|
|
|
2016-02-01 20:44:03 +00:00
|
|
|
/**
|
|
|
|
|
* @param array $params Keys include:
|
|
|
|
|
* - priority: (required) Priority of the returned sessions
|
|
|
|
|
* - callUserSetCookiesHook: Whether to call the deprecated hook
|
|
|
|
|
* - sessionName: Session cookie name. Doesn't honor 'prefix'. Defaults to
|
|
|
|
|
* $wgSessionName, or $wgCookiePrefix . '_session' if that is unset.
|
|
|
|
|
* - cookieOptions: Options to pass to WebRequest::setCookie():
|
|
|
|
|
* - 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'] ) ) {
|
|
|
|
|
throw new \InvalidArgumentException( __METHOD__ . ': priority must be specified' );
|
|
|
|
|
}
|
|
|
|
|
if ( $params['priority'] < SessionInfo::MIN_PRIORITY ||
|
|
|
|
|
$params['priority'] > SessionInfo::MAX_PRIORITY
|
|
|
|
|
) {
|
|
|
|
|
throw new \InvalidArgumentException( __METHOD__ . ': Invalid priority' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !is_array( $params['cookieOptions'] ) ) {
|
|
|
|
|
throw new \InvalidArgumentException( __METHOD__ . ': cookieOptions must be an array' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$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-01 20:44:03 +00:00
|
|
|
// @codeCoverageIgnoreStart
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->params += [
|
2016-02-01 20:44:03 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
|
'callUserSetCookiesHook' => false,
|
|
|
|
|
'sessionName' =>
|
2021-04-29 20:44:45 +00:00
|
|
|
$this->getConfig()->get( 'SessionName' ) ?: $this->getConfig()->get( 'CookiePrefix' ) . '_session',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2016-02-01 20:44:03 +00:00
|
|
|
|
2021-04-29 20:44:45 +00:00
|
|
|
$this->useCrossSiteCookies = strcasecmp( $this->getConfig()->get( 'CookieSameSite' ), 'none' ) === 0;
|
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
|
2021-04-29 20:44:45 +00:00
|
|
|
'prefix' => $this->getConfig()->get( 'CookiePrefix' ),
|
|
|
|
|
'path' => $this->getConfig()->get( 'CookiePath' ),
|
|
|
|
|
'domain' => $this->getConfig()->get( 'CookieDomain' ),
|
|
|
|
|
'secure' => $this->getConfig()->get( 'CookieSecure' ) || $this->getConfig()->get( 'ForceHTTPS' ),
|
|
|
|
|
'httpOnly' => $this->getConfig()->get( 'CookieHttpOnly' ),
|
|
|
|
|
'sameSite' => $this->getConfig()->get( 'CookieSameSite' ),
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
list( $userId, $userName, $token ) = $this->getUserInfoFromCookies( $request );
|
|
|
|
|
if ( $userId !== null ) {
|
|
|
|
|
try {
|
|
|
|
|
$userInfo = UserInfo::newFromId( $userId );
|
|
|
|
|
} catch ( \InvalidArgumentException $ex ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sanity check
|
|
|
|
|
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
|
|
|
],
|
|
|
|
|
] );
|
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(),
|
2016-02-17 09:09:32 +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'],
|
2016-02-17 09:09:32 +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 );
|
|
|
|
|
|
|
|
|
|
// Legacy hook
|
|
|
|
|
if ( $this->params['callUserSetCookiesHook'] && !$user->isAnon() ) {
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
$this->getHookRunner()->onUserSetCookies( $user, $sessionData, $cookies );
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$options = $this->cookieOptions;
|
|
|
|
|
|
|
|
|
|
$forceHTTPS = $session->shouldForceHTTPS() || $user->requiresHTTPS();
|
|
|
|
|
if ( $forceHTTPS ) {
|
2021-04-29 20:44:45 +00:00
|
|
|
$options['secure'] = $this->getConfig()->get( 'CookieSecure' )
|
|
|
|
|
|| $this->getConfig()->get( '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 ) {
|
2021-04-29 20:44:45 +00:00
|
|
|
if ( $this->getConfig()->get( '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'] )
|
|
|
|
|
) {
|
|
|
|
|
$request->response()->setCookie( 'LoggedOut', $loggedOut, $loggedOut + 86400,
|
|
|
|
|
$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 ) {
|
|
|
|
|
$name = $this->getCookie( $request, 'UserName', $this->cookieOptions['prefix'] );
|
|
|
|
|
if ( $name !== null ) {
|
2021-05-28 13:38:53 +00:00
|
|
|
$name = $this->userNameUtils->getCanonical( $name, UserNameUtils::RIGOR_USABLE );
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
return $name === false ? null : $name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch the user identity from cookies
|
|
|
|
|
* @param \WebRequest $request
|
|
|
|
|
* @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.
|
|
|
|
|
* @param \WebRequest $request
|
|
|
|
|
* @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 ) {
|
2020-07-01 00:01:00 +00:00
|
|
|
if ( $this->useCrossSiteCookies ) {
|
|
|
|
|
$value = $request->getCrossSiteCookie( $key, $prefix, $default );
|
|
|
|
|
} else {
|
|
|
|
|
$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 ) {
|
|
|
|
|
// If we're calling the legacy hook, we should populate $session
|
|
|
|
|
// like User::setCookies() did.
|
|
|
|
|
if ( !$user->isAnon() && $this->params['callUserSetCookiesHook'] ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2016-02-01 20:44:03 +00:00
|
|
|
'wsUserID' => $user->getId(),
|
|
|
|
|
'wsToken' => $user->getToken(),
|
|
|
|
|
'wsUserName' => $user->getName(),
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2016-02-01 20:44:03 +00:00
|
|
|
}
|
|
|
|
|
|
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();
|
2021-04-29 20:44:45 +00:00
|
|
|
$normalExpiration = $this->getConfig()->get( 'CookieExpiration' );
|
2016-05-13 00:03:20 +00:00
|
|
|
|
2016-06-22 16:36:16 +00:00
|
|
|
if ( $shouldRememberUser && in_array( $cookieName, $extendedCookies, true ) ) {
|
2021-04-29 20:44:45 +00:00
|
|
|
$extendedExpiration = $this->getConfig()->get( '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
|
|
|
}
|