58 parent::__construct();
61 'cookieOptions' => [],
66 if ( !isset(
$params[
'priority'] ) ) {
67 throw new \InvalidArgumentException( __METHOD__ .
': priority must be specified' );
72 throw new \InvalidArgumentException( __METHOD__ .
': Invalid priority' );
75 if ( !is_array(
$params[
'cookieOptions'] ) ) {
76 throw new \InvalidArgumentException( __METHOD__ .
': cookieOptions must be an array' );
79 $this->priority =
$params[
'priority'];
80 $this->cookieOptions =
$params[
'cookieOptions'];
82 unset( $this->params[
'priority'] );
83 unset( $this->params[
'cookieOptions'] );
92 'callUserSetCookiesHook' =>
false,
98 $this->cookieOptions += [
104 'httpOnly' =>
$config->
get(
'CookieHttpOnly' ),
109 $sessionId = $this->
getCookie( $request, $this->params[
'sessionName'],
'' );
112 'forceHTTPS' => $this->
getCookie( $request,
'forceHTTPS',
'',
false )
115 $info[
'id'] = $sessionId;
116 $info[
'persisted'] =
true;
120 if ( $userId !==
null ) {
123 }
catch ( \InvalidArgumentException $ex ) {
128 if ( $userName !==
null && $userInfo->getName() !== $userName ) {
129 $this->logger->warning(
130 'Session "{session}" requested with mismatched UserID and UserName cookies.',
132 'session' => $sessionId,
135 'cookie_username' => $userName,
136 'username' => $userInfo->getName(),
142 if ( $token !==
null ) {
143 if ( !hash_equals( $userInfo->getToken(), $token ) ) {
144 $this->logger->warning(
145 'Session "{session}" requested with invalid Token cookie.',
147 'session' => $sessionId,
149 'username' => $userInfo->getName(),
153 $info[
'userInfo'] = $userInfo->verified();
154 $info[
'persisted'] =
true;
155 } elseif ( isset( $info[
'id'] ) ) {
156 $info[
'userInfo'] = $userInfo;
162 } elseif ( isset( $info[
'id'] ) ) {
168 $this->logger->debug(
169 'Session "{session}" requested without UserID cookie',
171 'session' => $info[
'id'],
195 $this->logger->debug( __METHOD__ .
': Headers already sent' );
205 if ( $this->params[
'callUserSetCookiesHook'] && !$user->isAnon() ) {
206 \Hooks::run(
'UserSetCookies', [ $user, &$sessionData, &$cookies ] );
216 $options[
'secure'] = $this->config->get(
'CookieSecure' );
219 $response->setCookie( $this->params[
'sessionName'], $session->
getId(),
null,
220 [
'prefix' =>
'' ] + $options
223 foreach ( $cookies as $key => $value ) {
224 if ( $value ===
false ) {
225 $response->clearCookie( $key, $options );
228 $expiration = $expirationDuration ? $expirationDuration + time() :
null;
229 $response->setCookie( $key, (
string)$value, $expiration, $options );
236 if ( $sessionData ) {
237 $session->
addData( $sessionData );
245 $this->logger->debug( __METHOD__ .
': Headers already sent' );
255 $this->params[
'sessionName'], [
'prefix' =>
'' ] + $this->cookieOptions
258 foreach ( $cookies as $key => $value ) {
259 $response->clearCookie( $key, $this->cookieOptions );
276 if ( $backend->shouldRememberUser() ) {
281 $expiration = $expirationDuration ? $expirationDuration + time() :
null;
285 $response->setCookie(
'forceHTTPS',
'true', $expiration,
286 [
'prefix' =>
'',
'secure' =>
false ] + $this->cookieOptions );
289 [
'prefix' =>
'',
'secure' =>
false ] + $this->cookieOptions );
299 if ( $loggedOut + 86400 > time() &&
300 $loggedOut !== (
int)$this->
getCookie( $request,
'LoggedOut', $this->cookieOptions[
'prefix'] )
302 $request->
response()->setCookie(
'LoggedOut', $loggedOut, $loggedOut + 86400,
303 $this->cookieOptions );
311 $this->cookieOptions[
'prefix'] .
'Token',
312 $this->cookieOptions[
'prefix'] .
'LoggedOut',
313 $this->params[
'sessionName'],
319 $name = $this->
getCookie( $request,
'UserName', $this->cookieOptions[
'prefix'] );
320 if ( $name !==
null ) {
323 return $name ===
false ? null : $name;
332 $prefix = $this->cookieOptions[
'prefix'];
334 $this->
getCookie( $request,
'UserID', $prefix ),
335 $this->
getCookie( $request,
'UserName', $prefix ),
336 $this->
getCookie( $request,
'Token', $prefix ),
348 protected function getCookie( $request, $key, $prefix, $default =
null ) {
349 $value = $request->getCookie( $key, $prefix, $default );
350 if ( $value ===
'deleted' ) {
368 if ( $user->isAnon() ) {
375 'UserID' => $user->getId(),
376 'UserName' => $user->getName(),
377 'Token' => $remember ? (string)$user->getToken() :
false,
390 if ( !$user->isAnon() && $this->params[
'callUserSetCookiesHook'] ) {
392 'wsUserID' => $user->getId(),
393 'wsToken' => $user->getToken(),
394 'wsUserName' => $user->getName(),
402 return wfMessage(
'sessionprovider-nocookies' );
417 return [
'UserID',
'UserName',
'Token' ];
432 $normalExpiration = $this->config->get(
'CookieExpiration' );
434 if ( $shouldRememberUser && in_array( $cookieName, $extendedCookies,
true ) ) {
435 $extendedExpiration = $this->config->get(
'ExtendedLoginCookieExpiration' );
437 return ( $extendedExpiration !==
null ) ? (int)$extendedExpiration : (
int)$normalExpiration;
439 return (
int)$normalExpiration;