Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like CookieSessionProvider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CookieSessionProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class CookieSessionProvider extends SessionProvider { |
||
| 37 | |||
| 38 | protected $params = []; |
||
| 39 | protected $cookieOptions = []; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param array $params Keys include: |
||
| 43 | * - priority: (required) Priority of the returned sessions |
||
| 44 | * - callUserSetCookiesHook: Whether to call the deprecated hook |
||
| 45 | * - sessionName: Session cookie name. Doesn't honor 'prefix'. Defaults to |
||
| 46 | * $wgSessionName, or $wgCookiePrefix . '_session' if that is unset. |
||
| 47 | * - cookieOptions: Options to pass to WebRequest::setCookie(): |
||
| 48 | * - prefix: Cookie prefix, defaults to $wgCookiePrefix |
||
| 49 | * - path: Cookie path, defaults to $wgCookiePath |
||
| 50 | * - domain: Cookie domain, defaults to $wgCookieDomain |
||
| 51 | * - secure: Cookie secure flag, defaults to $wgCookieSecure |
||
| 52 | * - httpOnly: Cookie httpOnly flag, defaults to $wgCookieHttpOnly |
||
| 53 | */ |
||
| 54 | public function __construct( $params = [] ) { |
||
| 82 | |||
| 83 | public function setConfig( Config $config ) { |
||
| 104 | |||
| 105 | public function provideSessionInfo( WebRequest $request ) { |
||
| 106 | $sessionId = $this->getCookie( $request, $this->params['sessionName'], '' ); |
||
| 107 | $info = [ |
||
| 108 | 'provider' => $this, |
||
| 109 | 'forceHTTPS' => $this->getCookie( $request, 'forceHTTPS', '', false ) |
||
| 110 | ]; |
||
| 111 | if ( SessionManager::validateSessionId( $sessionId ) ) { |
||
| 112 | $info['id'] = $sessionId; |
||
| 113 | $info['persisted'] = true; |
||
| 114 | } |
||
| 115 | |||
| 116 | list( $userId, $userName, $token ) = $this->getUserInfoFromCookies( $request ); |
||
| 117 | if ( $userId !== null ) { |
||
| 118 | try { |
||
| 119 | $userInfo = UserInfo::newFromId( $userId ); |
||
| 120 | } catch ( \InvalidArgumentException $ex ) { |
||
| 121 | return null; |
||
| 122 | } |
||
| 123 | |||
| 124 | // Sanity check |
||
| 125 | if ( $userName !== null && $userInfo->getName() !== $userName ) { |
||
| 126 | $this->logger->warning( |
||
| 127 | 'Session "{session}" requested with mismatched UserID and UserName cookies.', |
||
| 128 | [ |
||
| 129 | 'session' => $sessionId, |
||
| 130 | 'mismatch' => [ |
||
| 131 | 'userid' => $userId, |
||
| 132 | 'cookie_username' => $userName, |
||
| 133 | 'username' => $userInfo->getName(), |
||
| 134 | ], |
||
| 135 | ] ); |
||
| 136 | return null; |
||
| 137 | } |
||
| 138 | |||
| 139 | if ( $token !== null ) { |
||
| 140 | if ( !hash_equals( $userInfo->getToken(), $token ) ) { |
||
| 141 | $this->logger->warning( |
||
| 142 | 'Session "{session}" requested with invalid Token cookie.', |
||
| 143 | [ |
||
| 144 | 'session' => $sessionId, |
||
| 145 | 'userid' => $userId, |
||
| 146 | 'username' => $userInfo->getName(), |
||
| 147 | ] ); |
||
| 148 | return null; |
||
| 149 | } |
||
| 150 | $info['userInfo'] = $userInfo->verified(); |
||
| 151 | $info['persisted'] = true; // If we have user+token, it should be |
||
| 152 | } elseif ( isset( $info['id'] ) ) { |
||
| 153 | $info['userInfo'] = $userInfo; |
||
| 154 | } else { |
||
| 155 | // No point in returning, loadSessionInfoFromStore() will |
||
| 156 | // reject it anyway. |
||
| 157 | return null; |
||
| 158 | } |
||
| 159 | } elseif ( isset( $info['id'] ) ) { |
||
| 160 | // No UserID cookie, so insist that the session is anonymous. |
||
| 161 | // Note: this event occurs for several normal activities: |
||
| 162 | // * anon visits Special:UserLogin |
||
| 163 | // * anon browsing after seeing Special:UserLogin |
||
| 164 | // * anon browsing after edit or preview |
||
| 165 | $this->logger->debug( |
||
| 166 | 'Session "{session}" requested without UserID cookie', |
||
| 167 | [ |
||
| 168 | 'session' => $info['id'], |
||
| 169 | ] ); |
||
| 170 | $info['userInfo'] = UserInfo::newAnonymous(); |
||
| 171 | } else { |
||
| 172 | // No session ID and no user is the same as an empty session, so |
||
| 173 | // there's no point. |
||
| 174 | return null; |
||
| 175 | } |
||
| 176 | |||
| 177 | return new SessionInfo( $this->priority, $info ); |
||
| 178 | } |
||
| 179 | |||
| 180 | public function persistsSessionId() { |
||
| 183 | |||
| 184 | public function canChangeUser() { |
||
| 187 | |||
| 188 | public function persistSession( SessionBackend $session, WebRequest $request ) { |
||
| 189 | $response = $request->response(); |
||
| 190 | if ( $response->headersSent() ) { |
||
| 191 | // Can't do anything now |
||
| 192 | $this->logger->debug( __METHOD__ . ': Headers already sent' ); |
||
| 193 | return; |
||
| 194 | } |
||
| 195 | |||
| 196 | $user = $session->getUser(); |
||
| 197 | |||
| 198 | $cookies = $this->cookieDataToExport( $user, $session->shouldRememberUser() ); |
||
| 199 | $sessionData = $this->sessionDataToExport( $user ); |
||
| 200 | |||
| 201 | // Legacy hook |
||
| 202 | if ( $this->params['callUserSetCookiesHook'] && !$user->isAnon() ) { |
||
| 203 | \Hooks::run( 'UserSetCookies', [ $user, &$sessionData, &$cookies ] ); |
||
| 204 | } |
||
| 205 | |||
| 206 | $options = $this->cookieOptions; |
||
| 207 | |||
| 208 | $forceHTTPS = $session->shouldForceHTTPS() || $user->requiresHTTPS(); |
||
| 209 | if ( $forceHTTPS ) { |
||
| 210 | // Don't set the secure flag if the request came in |
||
| 211 | // over "http", for backwards compat. |
||
| 212 | // @todo Break that backwards compat properly. |
||
| 213 | $options['secure'] = $this->config->get( 'CookieSecure' ); |
||
| 214 | } |
||
| 215 | |||
| 216 | $response->setCookie( $this->params['sessionName'], $session->getId(), null, |
||
| 217 | [ 'prefix' => '' ] + $options |
||
| 218 | ); |
||
| 219 | |||
| 220 | foreach ( $cookies as $key => $value ) { |
||
| 221 | if ( $value === false ) { |
||
| 222 | $response->clearCookie( $key, $options ); |
||
| 223 | } else { |
||
| 224 | $expirationDuration = $this->getLoginCookieExpiration( $key ); |
||
| 225 | $expiration = $expirationDuration ? $expirationDuration + time() : null; |
||
| 226 | $response->setCookie( $key, (string)$value, $expiration, $options ); |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | $this->setForceHTTPSCookie( $forceHTTPS, $session, $request ); |
||
| 231 | $this->setLoggedOutCookie( $session->getLoggedOutTimestamp(), $request ); |
||
| 232 | |||
| 233 | if ( $sessionData ) { |
||
| 234 | $session->addData( $sessionData ); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | public function unpersistSession( WebRequest $request ) { |
||
| 239 | $response = $request->response(); |
||
| 240 | if ( $response->headersSent() ) { |
||
| 241 | // Can't do anything now |
||
| 242 | $this->logger->debug( __METHOD__ . ': Headers already sent' ); |
||
| 243 | return; |
||
| 244 | } |
||
| 245 | |||
| 246 | $cookies = [ |
||
| 247 | 'UserID' => false, |
||
| 248 | 'Token' => false, |
||
| 249 | ]; |
||
| 250 | |||
| 251 | $response->clearCookie( |
||
| 252 | $this->params['sessionName'], [ 'prefix' => '' ] + $this->cookieOptions |
||
| 253 | ); |
||
| 254 | |||
| 255 | foreach ( $cookies as $key => $value ) { |
||
| 256 | $response->clearCookie( $key, $this->cookieOptions ); |
||
| 257 | } |
||
| 258 | |||
| 259 | $this->setForceHTTPSCookie( false, null, $request ); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Set the "forceHTTPS" cookie |
||
| 264 | * @param bool $set Whether the cookie should be set or not |
||
| 265 | * @param SessionBackend|null $backend |
||
| 266 | * @param WebRequest $request |
||
| 267 | */ |
||
| 268 | protected function setForceHTTPSCookie( |
||
| 269 | $set, SessionBackend $backend = null, WebRequest $request |
||
| 270 | ) { |
||
| 271 | $response = $request->response(); |
||
| 272 | if ( $set ) { |
||
| 273 | if ( $backend->shouldRememberUser() ) { |
||
|
|
|||
| 274 | $expirationDuration = $this->getLoginCookieExpiration( 'forceHTTPS' ); |
||
| 275 | $expiration = $expirationDuration ? $expirationDuration + time() : null; |
||
| 276 | } else { |
||
| 277 | $expiration = null; |
||
| 278 | } |
||
| 279 | $response->setCookie( 'forceHTTPS', 'true', $expiration, |
||
| 280 | [ 'prefix' => '', 'secure' => false ] + $this->cookieOptions ); |
||
| 281 | } else { |
||
| 282 | $response->clearCookie( 'forceHTTPS', |
||
| 283 | [ 'prefix' => '', 'secure' => false ] + $this->cookieOptions ); |
||
| 284 | } |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Set the "logged out" cookie |
||
| 289 | * @param int $loggedOut timestamp |
||
| 290 | * @param WebRequest $request |
||
| 291 | */ |
||
| 292 | protected function setLoggedOutCookie( $loggedOut, WebRequest $request ) { |
||
| 300 | |||
| 301 | public function getVaryCookies() { |
||
| 302 | return [ |
||
| 303 | // Vary on token and session because those are the real authn |
||
| 304 | // determiners. UserID and UserName don't matter without those. |
||
| 305 | $this->cookieOptions['prefix'] . 'Token', |
||
| 306 | $this->cookieOptions['prefix'] . 'LoggedOut', |
||
| 307 | $this->params['sessionName'], |
||
| 308 | 'forceHTTPS', |
||
| 309 | ]; |
||
| 310 | } |
||
| 311 | |||
| 312 | public function suggestLoginUsername( WebRequest $request ) { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Fetch the user identity from cookies |
||
| 322 | * @param \WebRequest $request |
||
| 323 | * @return array (string|null $id, string|null $username, string|null $token) |
||
| 324 | */ |
||
| 325 | protected function getUserInfoFromCookies( $request ) { |
||
| 326 | $prefix = $this->cookieOptions['prefix']; |
||
| 327 | return [ |
||
| 328 | $this->getCookie( $request, 'UserID', $prefix ), |
||
| 329 | $this->getCookie( $request, 'UserName', $prefix ), |
||
| 330 | $this->getCookie( $request, 'Token', $prefix ), |
||
| 331 | ]; |
||
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Get a cookie. Contains an auth-specific hack. |
||
| 336 | * @param \WebRequest $request |
||
| 337 | * @param string $key |
||
| 338 | * @param string $prefix |
||
| 339 | * @param mixed $default |
||
| 340 | * @return mixed |
||
| 341 | */ |
||
| 342 | protected function getCookie( $request, $key, $prefix, $default = null ) { |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Return the data to store in cookies |
||
| 357 | * @param User $user |
||
| 358 | * @param bool $remember |
||
| 359 | * @return array $cookies Set value false to unset the cookie |
||
| 360 | */ |
||
| 361 | protected function cookieDataToExport( $user, $remember ) { |
||
| 362 | if ( $user->isAnon() ) { |
||
| 363 | return [ |
||
| 364 | 'UserID' => false, |
||
| 365 | 'Token' => false, |
||
| 366 | ]; |
||
| 367 | } else { |
||
| 368 | return [ |
||
| 369 | 'UserID' => $user->getId(), |
||
| 370 | 'UserName' => $user->getName(), |
||
| 371 | 'Token' => $remember ? (string)$user->getToken() : false, |
||
| 372 | ]; |
||
| 373 | } |
||
| 374 | } |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Return extra data to store in the session |
||
| 378 | * @param User $user |
||
| 379 | * @return array $session |
||
| 380 | */ |
||
| 381 | protected function sessionDataToExport( $user ) { |
||
| 382 | // If we're calling the legacy hook, we should populate $session |
||
| 383 | // like User::setCookies() did. |
||
| 384 | if ( !$user->isAnon() && $this->params['callUserSetCookiesHook'] ) { |
||
| 385 | return [ |
||
| 386 | 'wsUserID' => $user->getId(), |
||
| 387 | 'wsToken' => $user->getToken(), |
||
| 388 | 'wsUserName' => $user->getName(), |
||
| 389 | ]; |
||
| 390 | } |
||
| 391 | |||
| 392 | return []; |
||
| 393 | } |
||
| 394 | |||
| 395 | public function whyNoSession() { |
||
| 398 | |||
| 399 | public function getRememberUserDuration() { |
||
| 400 | return min( $this->getLoginCookieExpiration( 'UserID' ), |
||
| 401 | $this->getLoginCookieExpiration( 'Token' ) ) ?: null; |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Returns the lifespan of the login cookies, in seconds. 0 means until the end of the session. |
||
| 406 | * @param string $cookieName |
||
| 407 | * @return int Cookie expiration time in seconds; 0 for session cookies |
||
| 408 | */ |
||
| 409 | protected function getLoginCookieExpiration( $cookieName ) { |
||
| 419 | } |
||
| 420 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: