MediaWiki master
ImmutableSessionProviderWithCookie.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Session;
8
9use InvalidArgumentException;
12
27
29 protected $sessionCookieName = null;
31 protected $sessionCookieOptions = [];
32
40 public function __construct( $params = [] ) {
41 parent::__construct();
42
43 if ( isset( $params['sessionCookieName'] ) ) {
44 if ( !is_string( $params['sessionCookieName'] ) ) {
45 throw new InvalidArgumentException( 'sessionCookieName must be a string' );
46 }
47 $this->sessionCookieName = $params['sessionCookieName'];
48 }
49 if ( isset( $params['sessionCookieOptions'] ) ) {
50 if ( !is_array( $params['sessionCookieOptions'] ) ) {
51 throw new InvalidArgumentException( 'sessionCookieOptions must be an array' );
52 }
53 $this->sessionCookieOptions = $params['sessionCookieOptions'];
54 }
55 }
56
68 protected function getSessionIdFromCookie( WebRequest $request ) {
69 if ( $this->sessionCookieName === null ) {
70 throw new \BadMethodCallException(
71 __METHOD__ . ' may not be called when $this->sessionCookieName === null'
72 );
73 }
74
75 $prefix = $this->sessionCookieOptions['prefix']
76 ?? $this->getConfig()->get( MainConfigNames::CookiePrefix );
77 $id = $request->getCookie( $this->sessionCookieName, $prefix );
78 return SessionManager::validateSessionId( $id ) ? $id : null;
79 }
80
85 public function persistsSessionId() {
86 return $this->sessionCookieName !== null;
87 }
88
93 public function canChangeUser() {
94 return false;
95 }
96
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 // Send a cookie unless $wgForceHTTPS is set (T256095)
116 if ( !$this->getConfig()->get( MainConfigNames::ForceHTTPS ) ) {
117 $response->setCookie( 'forceHTTPS', 'true', null,
118 [ 'prefix' => '', 'secure' => false ] + $options );
119 }
120 $options['secure'] = true;
121 }
122
123 $response->setCookie( $this->sessionCookieName, $session->getId(), null, $options );
124 }
125
130 public function unpersistSession( WebRequest $request ) {
131 if ( $this->sessionCookieName === null ) {
132 return;
133 }
134
135 $response = $request->response();
136 if ( $response->headersSent() ) {
137 // Can't do anything now
138 $this->logger->debug( __METHOD__ . ': Headers already sent' );
139 return;
140 }
141
142 $response->clearCookie( $this->sessionCookieName, $this->sessionCookieOptions );
143 }
144
149 public function getVaryCookies() {
150 if ( $this->sessionCookieName === null ) {
151 return [];
152 }
153
154 $prefix = $this->sessionCookieOptions['prefix'] ??
156 return [ $prefix . $this->sessionCookieName ];
157 }
158
160 public function whyNoSession() {
161 return wfMessage( 'sessionprovider-nocookies' );
162 }
163}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
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()
Return the session ID.
getUser()
Return the authenticated user for this session.
static validateSessionId( $id)
Validate a session ID.
A SessionProvider provides SessionInfo and support for Session.