MediaWiki master
ImmutableSessionProviderWithCookie.php
Go to the documentation of this file.
1<?php
24namespace MediaWiki\Session;
25
26use InvalidArgumentException;
29
44
46 protected $sessionCookieName = null;
48 protected $sessionCookieOptions = [];
49
57 public function __construct( $params = [] ) {
58 parent::__construct();
59
60 if ( isset( $params['sessionCookieName'] ) ) {
61 if ( !is_string( $params['sessionCookieName'] ) ) {
62 throw new InvalidArgumentException( 'sessionCookieName must be a string' );
63 }
64 $this->sessionCookieName = $params['sessionCookieName'];
65 }
66 if ( isset( $params['sessionCookieOptions'] ) ) {
67 if ( !is_array( $params['sessionCookieOptions'] ) ) {
68 throw new InvalidArgumentException( 'sessionCookieOptions must be an array' );
69 }
70 $this->sessionCookieOptions = $params['sessionCookieOptions'];
71 }
72 }
73
85 protected function getSessionIdFromCookie( WebRequest $request ) {
86 if ( $this->sessionCookieName === null ) {
87 throw new \BadMethodCallException(
88 __METHOD__ . ' may not be called when $this->sessionCookieName === null'
89 );
90 }
91
92 $prefix = $this->sessionCookieOptions['prefix']
93 ?? $this->getConfig()->get( MainConfigNames::CookiePrefix );
94 $id = $request->getCookie( $this->sessionCookieName, $prefix );
95 return SessionManager::validateSessionId( $id ) ? $id : null;
96 }
97
102 public function persistsSessionId() {
103 return $this->sessionCookieName !== null;
104 }
105
110 public function canChangeUser() {
111 return false;
112 }
113
118 public function persistSession( SessionBackend $session, WebRequest $request ) {
119 if ( $this->sessionCookieName === null ) {
120 return;
121 }
122
123 $response = $request->response();
124 if ( $response->headersSent() ) {
125 // Can't do anything now
126 $this->logger->debug( __METHOD__ . ': Headers already sent' );
127 return;
128 }
129
131 if ( $session->shouldForceHTTPS() || $session->getUser()->requiresHTTPS() ) {
132 // Send a cookie unless $wgForceHTTPS is set (T256095)
133 if ( !$this->getConfig()->get( MainConfigNames::ForceHTTPS ) ) {
134 $response->setCookie( 'forceHTTPS', 'true', null,
135 [ 'prefix' => '', 'secure' => false ] + $options );
136 }
137 $options['secure'] = true;
138 }
139
140 $response->setCookie( $this->sessionCookieName, $session->getId(), null, $options );
141 }
142
147 public function unpersistSession( WebRequest $request ) {
148 if ( $this->sessionCookieName === null ) {
149 return;
150 }
151
152 $response = $request->response();
153 if ( $response->headersSent() ) {
154 // Can't do anything now
155 $this->logger->debug( __METHOD__ . ': Headers already sent' );
156 return;
157 }
158
159 $response->clearCookie( $this->sessionCookieName, $this->sessionCookieOptions );
160 }
161
166 public function getVaryCookies() {
167 if ( $this->sessionCookieName === null ) {
168 return [];
169 }
170
171 $prefix = $this->sessionCookieOptions['prefix'] ??
173 return [ $prefix . $this->sessionCookieName ];
174 }
175
176 public function whyNoSession() {
177 return wfMessage( 'sessionprovider-nocookies' );
178 }
179}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
array $params
The job parameters.
A class containing constants representing the names of configuration variables.
const ForceHTTPS
Name constant for the ForceHTTPS setting, for use with Config::get()
const CookiePrefix
Name constant for the CookiePrefix setting, for use with Config::get()
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form,...
response()
Return a handle to WebResponse style object, for setting cookies, headers and other stuff,...
getCookie( $key, $prefix=null, $default=null)
Get a cookie from the $_COOKIE jar.
An ImmutableSessionProviderWithCookie doesn't persist the user, but optionally can use a cookie to su...
whyNoSession()
Return a Message for why sessions might not be being persisted.For example, "check whether you're blo...
canChangeUser()
Indicate whether the user associated with the request can be changed.If false, any session passed to ...
persistsSessionId()
Indicate whether self::persistSession() can save arbitrary session IDs.If false, any session passed t...
unpersistSession(WebRequest $request)
Remove any persisted session from a request/response.For example, blank and expire any cookies set by...
persistSession(SessionBackend $session, WebRequest $request)
Persist a session into a request/response.For example, you might set cookies for the session's ID,...
getVaryCookies()
Return the list of cookies that need varying on.to override For use by \MediaWiki\Session\SessionMana...
getSessionIdFromCookie(WebRequest $request)
Get the session ID from the cookie, if any.
This is the actual workhorse for Session.
shouldForceHTTPS()
Whether HTTPS should be forced.
getId()
Returns the session ID.
getUser()
Returns the authenticated user for this session.
static validateSessionId( $id)
Validate a session ID.
A SessionProvider provides SessionInfo and support for Session.