MediaWiki  1.33.0
ImmutableSessionProviderWithCookie.php
Go to the documentation of this file.
1 <?php
24 namespace MediaWiki\Session;
25 
27 
41 
43  protected $sessionCookieName = null;
45  protected $sessionCookieOptions = [];
46 
53  public function __construct( $params = [] ) {
54  parent::__construct();
55 
56  if ( isset( $params['sessionCookieName'] ) ) {
57  if ( !is_string( $params['sessionCookieName'] ) ) {
58  throw new \InvalidArgumentException( 'sessionCookieName must be a string' );
59  }
60  $this->sessionCookieName = $params['sessionCookieName'];
61  }
62  if ( isset( $params['sessionCookieOptions'] ) ) {
63  if ( !is_array( $params['sessionCookieOptions'] ) ) {
64  throw new \InvalidArgumentException( 'sessionCookieOptions must be an array' );
65  }
66  $this->sessionCookieOptions = $params['sessionCookieOptions'];
67  }
68  }
69 
81  protected function getSessionIdFromCookie( WebRequest $request ) {
82  if ( $this->sessionCookieName === null ) {
83  throw new \BadMethodCallException(
84  __METHOD__ . ' may not be called when $this->sessionCookieName === null'
85  );
86  }
87 
88  $prefix = $this->sessionCookieOptions['prefix'] ?? $this->config->get( 'CookiePrefix' );
89  $id = $request->getCookie( $this->sessionCookieName, $prefix );
90  return SessionManager::validateSessionId( $id ) ? $id : null;
91  }
92 
93  public function persistsSessionId() {
94  return $this->sessionCookieName !== null;
95  }
96 
97  public function canChangeUser() {
98  return false;
99  }
100 
101  public function persistSession( SessionBackend $session, WebRequest $request ) {
102  if ( $this->sessionCookieName === null ) {
103  return;
104  }
105 
106  $response = $request->response();
107  if ( $response->headersSent() ) {
108  // Can't do anything now
109  $this->logger->debug( __METHOD__ . ': Headers already sent' );
110  return;
111  }
112 
114  if ( $session->shouldForceHTTPS() || $session->getUser()->requiresHTTPS() ) {
115  $response->setCookie( 'forceHTTPS', 'true', null,
116  [ 'prefix' => '', 'secure' => false ] + $options );
117  $options['secure'] = true;
118  }
119 
120  $response->setCookie( $this->sessionCookieName, $session->getId(), null, $options );
121  }
122 
123  public function unpersistSession( WebRequest $request ) {
124  if ( $this->sessionCookieName === null ) {
125  return;
126  }
127 
128  $response = $request->response();
129  if ( $response->headersSent() ) {
130  // Can't do anything now
131  $this->logger->debug( __METHOD__ . ': Headers already sent' );
132  return;
133  }
134 
135  $response->clearCookie( $this->sessionCookieName, $this->sessionCookieOptions );
136  }
137 
138  public function getVaryCookies() {
139  if ( $this->sessionCookieName === null ) {
140  return [];
141  }
142 
143  $prefix = $this->sessionCookieOptions['prefix'] ?? $this->config->get( 'CookiePrefix' );
144  return [ $prefix . $this->sessionCookieName ];
145  }
146 
147  public function whyNoSession() {
148  return wfMessage( 'sessionprovider-nocookies' );
149  }
150 }
MediaWiki\Session\ImmutableSessionProviderWithCookie\$sessionCookieName
string null $sessionCookieName
Definition: ImmutableSessionProviderWithCookie.php:43
MediaWiki\Session\ImmutableSessionProviderWithCookie\getSessionIdFromCookie
getSessionIdFromCookie(WebRequest $request)
Get the session ID from the cookie, if any.
Definition: ImmutableSessionProviderWithCookie.php:81
MediaWiki\Session\SessionBackend\getUser
getUser()
Returns the authenticated user for this session.
Definition: SessionBackend.php:390
MediaWiki\Session\SessionBackend\getId
getId()
Returns the session ID.
Definition: SessionBackend.php:224
$params
$params
Definition: styleTest.css.php:44
MediaWiki\Session\ImmutableSessionProviderWithCookie\unpersistSession
unpersistSession(WebRequest $request)
Remove any persisted session from a request/response.
Definition: ImmutableSessionProviderWithCookie.php:123
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\ImmutableSessionProviderWithCookie
An ImmutableSessionProviderWithCookie doesn't persist the user, but optionally can use a cookie to su...
Definition: ImmutableSessionProviderWithCookie.php:40
MediaWiki\Session\SessionManager\validateSessionId
static validateSessionId( $id)
Validate a session ID.
Definition: SessionManager.php:366
MediaWiki\Session\ImmutableSessionProviderWithCookie\canChangeUser
canChangeUser()
Indicate whether the user associated with the request can be changed.
Definition: ImmutableSessionProviderWithCookie.php:97
MediaWiki\Session\SessionProvider
A SessionProvider provides SessionInfo and support for Session.
Definition: SessionProvider.php:78
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
MediaWiki\Session\ImmutableSessionProviderWithCookie\$sessionCookieOptions
mixed[] $sessionCookieOptions
Definition: ImmutableSessionProviderWithCookie.php:45
MediaWiki\Session
Definition: BotPasswordSessionProvider.php:24
$request
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2636
MediaWiki\Session\SessionBackend\shouldForceHTTPS
shouldForceHTTPS()
Whether HTTPS should be forced.
Definition: SessionBackend.php:450
$response
this hook is for auditing only $response
Definition: hooks.txt:780
WebRequest
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
Definition: WebRequest.php:41
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:1985
MediaWiki\Session\ImmutableSessionProviderWithCookie\persistSession
persistSession(SessionBackend $session, WebRequest $request)
Persist a session into a request/response.
Definition: ImmutableSessionProviderWithCookie.php:101
MediaWiki\Session\ImmutableSessionProviderWithCookie\persistsSessionId
persistsSessionId()
Indicate whether self::persistSession() can save arbitrary session IDs.
Definition: ImmutableSessionProviderWithCookie.php:93
MediaWiki\Session\ImmutableSessionProviderWithCookie\whyNoSession
whyNoSession()
Return a Message for why sessions might not be being persisted.
Definition: ImmutableSessionProviderWithCookie.php:147
MediaWiki\Session\ImmutableSessionProviderWithCookie\getVaryCookies
getVaryCookies()
Return the list of cookies that need varying on.
Definition: ImmutableSessionProviderWithCookie.php:138
wfMessage
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
MediaWiki\Session\ImmutableSessionProviderWithCookie\__construct
__construct( $params=[])
Definition: ImmutableSessionProviderWithCookie.php:53
MediaWiki\Session\SessionBackend
This is the actual workhorse for Session.
Definition: SessionBackend.php:49