MediaWiki master
|
A SessionProvider provides SessionInfo and support for Session. More...
Inherits Stringable, and MediaWiki\Session\SessionProviderInterface.
Inherited by MediaWiki\Installer\InstallerSessionProvider, MediaWiki\Session\CookieSessionProvider, and MediaWiki\Session\ImmutableSessionProviderWithCookie.
Public Member Functions | |
__construct () | |
__toString () | |
canAlwaysAutocreate () | |
Returns true if this provider is exempt from autocreate user permissions check. | |
canChangeUser () | |
Indicate whether the user associated with the request can be changed. | |
describe (Language $lang) | |
Return an identifier for this session type.
| |
getAllowedUserRights (SessionBackend $backend) | |
Fetch the rights allowed the user when the specified session is active. | |
getManager () | |
Get the session manager. | |
getRememberUserDuration () | |
Returns the duration (in seconds) for which users will be remembered when Session::setRememberUser() is set. | |
getRestrictions (?array $providerMetadata) | |
Fetch any restrictions imposed on logins or actions when this session is active. | |
getVaryCookies () | |
Return the list of cookies that need varying on. | |
getVaryHeaders () | |
Return the HTTP headers that need varying on. | |
init (LoggerInterface $logger, Config $config, SessionManager $manager, HookContainer $hookContainer, UserNameUtils $userNameUtils) | |
Initialise with dependencies of a SessionProvider. | |
invalidateSessionsForUser (User $user) | |
Invalidate existing sessions for a user. | |
mergeMetadata (array $savedMetadata, array $providedMetadata) | |
Merge saved session provider metadata. | |
newSessionInfo ( $id=null) | |
Provide session info for a new, empty session. | |
persistSession (SessionBackend $session, WebRequest $request) | |
Persist a session into a request/response. | |
persistsSessionId () | |
Indicate whether self::persistSession() can save arbitrary session IDs. | |
preventSessionsForUser ( $username) | |
Prevent future sessions for the user. | |
provideSessionInfo (WebRequest $request) | |
Provide session info for a request. | |
refreshSessionInfo (SessionInfo $info, WebRequest $request, &$metadata) | |
Validate a loaded SessionInfo and refresh provider metadata. | |
safeAgainstCsrf () | |
Most session providers require protection against CSRF attacks (usually via CSRF tokens) | |
sessionIdWasReset (SessionBackend $session, $oldId) | |
Notification that the session ID was reset. | |
setConfig (Config $config) | |
Set configuration. | |
setHookContainer ( $hookContainer) | |
setLogger (LoggerInterface $logger) | |
Sets a logger instance on the object. | |
setManager (SessionManager $manager) | |
Set the session manager. | |
suggestLoginUsername (WebRequest $request) | |
Get a suggested username for the login form. | |
unpersistSession (WebRequest $request) | |
Remove any persisted session from a request/response. | |
whyNoSession () | |
Return a Message for why sessions might not be being persisted.For example, "check whether you're blocking our cookies".
| |
Protected Member Functions | |
describeMessage () | |
Return a Message identifying this session type. | |
getConfig () | |
Get the config. | |
getHookContainer () | |
Get the HookContainer. | |
getHookRunner () | |
Get the HookRunner. | |
hashToSessionId ( $data, $key=null) | |
Hash data as a session ID. | |
makeException ( $key,... $params) | |
Throw an exception, later. | |
postInitSetup () | |
A provider can override this to do any necessary setup after init() is called. | |
Protected Attributes | |
Config | $config |
LoggerInterface | $logger |
SessionManager | $manager |
int | $priority |
Session priority. | |
UserNameUtils | $userNameUtils |
A SessionProvider provides SessionInfo and support for Session.
A SessionProvider is responsible for taking a WebRequest and determining the authenticated session that it's a part of. It does this by returning an SessionInfo object with basic information about the session it thinks is associated with the request, namely the session ID and possibly the authenticated user the session belongs to.
The SessionProvider also provides for updating the WebResponse with information necessary to provide the client with data that the client will send with later requests, and for populating the Vary and Key headers with the data necessary to correctly vary the cache on these client requests.
An important part of the latter is indicating whether it even can tell the client to include such data in future requests, via the persistsSessionId() and canChangeUser() methods. The cases are (in order of decreasing commonness):
Note that many methods that are technically "cannot persist ID" could be turned into "can persist ID but not change User" using a session cookie, as implemented by ImmutableSessionProviderWithCookie. If doing so, different session cookie names should be used for different providers to avoid collisions.
Definition at line 89 of file SessionProvider.php.
MediaWiki\Session\SessionProvider::__construct | ( | ) |
Definition at line 117 of file SessionProvider.php.
References MediaWiki\Session\SessionInfo\MIN_PRIORITY.
MediaWiki\Session\SessionProvider::__toString | ( | ) |
Definition at line 611 of file SessionProvider.php.
MediaWiki\Session\SessionProvider::canAlwaysAutocreate | ( | ) |
Returns true if this provider is exempt from autocreate user permissions check.
By default returns false, meaning this provider respects the normal rights of anonymous user creation. When true the permission checks will be bypassed and the user will always be created (subject to other limitations, like read only db and such).
Implements MediaWiki\Session\SessionProviderInterface.
Definition at line 679 of file SessionProvider.php.
|
abstract |
Indicate whether the user associated with the request can be changed.
If false, any session passed to self::persistSession() will have a user that was originally provided by self::provideSessionInfo(). Further, self::provideSessionInfo() may only provide sessions that have a user already set.
If true, the provider may be passed sessions with arbitrary users, and will be expected to manipulate the request in such a way that future requests will cause self::provideSessionInfo() to provide a SessionInfo with that ID. This can be as simple as not passing any 'userInfo' into SessionInfo's constructor, in which case SessionInfo will load the user from the saved session's metadata.
For example, a session provider for OAuth or SSL client certificates would function by matching the OAuth headers or certificate to a particular user, and thus would return false here since it can't arbitrarily assign those OAuth credentials or that certificate to a different user. A session provider that shoves information into cookies, on the other hand, could easily do so.
Reimplemented in MediaWiki\Installer\InstallerSessionProvider, MediaWiki\Session\CookieSessionProvider, and MediaWiki\Session\ImmutableSessionProviderWithCookie.
MediaWiki\Session\SessionProvider::describe | ( | Language | $lang | ) |
Return an identifier for this session type.
Implements MediaWiki\Session\SessionProviderInterface.
Definition at line 641 of file SessionProvider.php.
References wfMessage().
|
protected |
Return a Message identifying this session type.
This default implementation takes the class name, lowercases it, replaces backslashes with dashes, and prefixes 'sessionprovider-' to determine the message key. For example, MediaWiki\Session\CookieSessionProvider produces 'sessionprovider-mediawiki-session-cookiesessionprovider'.
Definition at line 631 of file SessionProvider.php.
References wfMessage().
MediaWiki\Session\SessionProvider::getAllowedUserRights | ( | SessionBackend | $backend | ) |
Fetch the rights allowed the user when the specified session is active.
This is mainly meant for allowing the user to restrict access to the account by certain methods; you probably want to use this with GrantsInfo. The returned rights will be intersected with the user's actual rights.
SessionBackend | $backend |
Reimplemented in MediaWiki\Session\BotPasswordSessionProvider.
Definition at line 583 of file SessionProvider.php.
References MediaWiki\Session\SessionBackend\getProvider().
|
protected |
Get the config.
Definition at line 198 of file SessionProvider.php.
References MediaWiki\Session\SessionProvider\$config.
Referenced by MediaWiki\Session\CookieSessionProvider\getLoginCookieExpiration(), MediaWiki\Session\ImmutableSessionProviderWithCookie\getSessionIdFromCookie(), MediaWiki\Session\ImmutableSessionProviderWithCookie\getVaryCookies(), MediaWiki\Session\CookieSessionProvider\persistSession(), MediaWiki\Session\ImmutableSessionProviderWithCookie\persistSession(), MediaWiki\Session\CookieSessionProvider\postInitSetup(), MediaWiki\Session\BotPasswordSessionProvider\provideSessionInfo(), and MediaWiki\Session\CookieSessionProvider\setForceHTTPSCookie().
|
protected |
|
protected |
Get the HookRunner.
Definition at line 259 of file SessionProvider.php.
MediaWiki\Session\SessionProvider::getManager | ( | ) |
Get the session manager.
Definition at line 222 of file SessionProvider.php.
References MediaWiki\Session\SessionProvider\$manager.
Referenced by MediaWiki\Session\BotPasswordSessionProvider\newSessionForRequest().
MediaWiki\Session\SessionProvider::getRememberUserDuration | ( | ) |
Returns the duration (in seconds) for which users will be remembered when Session::setRememberUser() is set.
Null means setting the remember flag will have no effect (and endpoints should not offer that option).
Reimplemented in MediaWiki\Session\CookieSessionProvider.
Definition at line 429 of file SessionProvider.php.
MediaWiki\Session\SessionProvider::getRestrictions | ( | ?array | $providerMetadata | ) |
Fetch any restrictions imposed on logins or actions when this session is active.
Reimplemented in MediaWiki\Session\BotPasswordSessionProvider.
Definition at line 600 of file SessionProvider.php.
MediaWiki\Session\SessionProvider::getVaryCookies | ( | ) |
Return the list of cookies that need varying on.
Reimplemented in MediaWiki\Session\CookieSessionProvider, and MediaWiki\Session\ImmutableSessionProviderWithCookie.
Definition at line 557 of file SessionProvider.php.
MediaWiki\Session\SessionProvider::getVaryHeaders | ( | ) |
Return the HTTP headers that need varying on.
The return value is such that someone could theoretically do this:
Definition at line 547 of file SessionProvider.php.
|
finalprotected |
Hash data as a session ID.
Generally this will only be used when self::persistsSessionId() is false and the provider has to base the session ID on the verified user's identity or other static data. The SessionInfo should then typically have the 'forceUse' flag set to avoid persistent session failure if validation of the stored data fails.
string | $data | |
string | null | $key | Defaults to $this->getConfig()->get( MainConfigNames::SecretKey ) |
Definition at line 696 of file SessionProvider.php.
MediaWiki\Session\SessionProvider::init | ( | LoggerInterface | $logger, |
Config | $config, | ||
SessionManager | $manager, | ||
HookContainer | $hookContainer, | ||
UserNameUtils | $userNameUtils ) |
Initialise with dependencies of a SessionProvider.
LoggerInterface | $logger | |
Config | $config | |
SessionManager | $manager | |
HookContainer | $hookContainer | |
UserNameUtils | $userNameUtils |
Definition at line 134 of file SessionProvider.php.
References MediaWiki\Session\SessionProvider\$config, MediaWiki\Session\SessionProvider\$logger, MediaWiki\Session\SessionProvider\$manager, MediaWiki\Session\SessionProvider\$userNameUtils, and MediaWiki\Session\SessionProvider\postInitSetup().
MediaWiki\Session\SessionProvider::invalidateSessionsForUser | ( | User | $user | ) |
Invalidate existing sessions for a user.
If the provider has its own equivalent of CookieSessionProvider's Token cookie (and doesn't use User::getToken() to implement it), it should reset whatever token it does use here.
User | $user |
Definition at line 530 of file SessionProvider.php.
|
protected |
Throw an exception, later.
Needed because during session initialization the framework isn't quite ready to handle an exception.
This should be called from provideSessionInfo() to fail in a user-friendly way when a session mechanism is used in a way it's not supposed to be used (e.g. invalid credentials or a non-API request when the session provider only supports API requests), and the returned SessionInfo should be returned by provideSessionInfo().
string | $key | Key for the error message |
mixed | ...$params Parameters as strings. |
Definition at line 736 of file SessionProvider.php.
MediaWiki\Session\SessionProvider::mergeMetadata | ( | array | $savedMetadata, |
array | $providedMetadata ) |
Merge saved session provider metadata.
This method will be used to compare the metadata returned by provideSessionInfo() with the saved metadata (which has been returned by provideSessionInfo() the last time the session was saved), and merge the two into the new saved metadata, or abort if the current request is not a valid continuation of the session.
The default implementation checks that anything in both arrays is identical, then returns $providedMetadata.
array | $savedMetadata | Saved provider metadata |
array | $providedMetadata | Provided provider metadata (from the SessionInfo) |
MetadataMergeException | If the metadata cannot be merged. Such exceptions will be handled by SessionManager and are a safe way of rejecting a suspicious or incompatible session. The provider is expected to write an appropriate message to its logger. |
Definition at line 335 of file SessionProvider.php.
MediaWiki\Session\SessionProvider::newSessionInfo | ( | $id = null | ) |
Provide session info for a new, empty session.
Return null if such a session cannot be created. This base implementation assumes that it only makes sense if a session ID can be persisted and changing users is allowed.
string | null | $id | ID to force for the new session |
Reimplemented in MediaWiki\Session\BotPasswordSessionProvider.
Definition at line 301 of file SessionProvider.php.
|
abstract |
Persist a session into a request/response.
For example, you might set cookies for the session's ID, user ID, user name, and user token on the passed request.
To correctly persist a user independently of the session ID, the provider should persist both the user ID (or name, but preferably the ID) and the user token. When reading the data from the request, it should construct a User object from the ID/name and then verify that the User object's token matches the token included in the request. Should the tokens not match, an anonymous user must be passed to SessionInfo::__construct().
When persisting a user independently of the session ID, $session->shouldRememberUser() should be checked first. If this returns false, the user token must not be saved to cookies. The user name and/or ID may be persisted, and should be used to construct an unverified UserInfo to pass to SessionInfo::__construct().
A backend that cannot persist session ID or user info should implement this as a no-op.
SessionBackend | $session | Session to persist |
WebRequest | $request | Request into which to persist the session |
Reimplemented in MediaWiki\Installer\InstallerSessionProvider, MediaWiki\Session\CookieSessionProvider, and MediaWiki\Session\ImmutableSessionProviderWithCookie.
|
abstract |
Indicate whether self::persistSession() can save arbitrary session IDs.
If false, any session passed to self::persistSession() will have an ID that was originally provided by self::provideSessionInfo().
If true, the provider may be passed sessions with arbitrary session IDs, and will be expected to manipulate the request in such a way that future requests will cause self::provideSessionInfo() to provide a SessionInfo with that ID.
For example, a session provider for OAuth would function by matching the OAuth headers to a particular user, and then would use self::hashToSessionId() to turn the user and OAuth client ID (and maybe also the user token and client secret) into a session ID, and therefore can't easily assign that user+client a different ID. Similarly, a session provider for SSL client certificates would function by matching the certificate to a particular user, and then would use self::hashToSessionId() to turn the user and certificate fingerprint into a session ID, and therefore can't easily assign a different ID either. On the other hand, a provider that saves the session ID into a cookie can easily just set the cookie to a different value.
Reimplemented in MediaWiki\Installer\InstallerSessionProvider, MediaWiki\Session\CookieSessionProvider, and MediaWiki\Session\ImmutableSessionProviderWithCookie.
|
protected |
A provider can override this to do any necessary setup after init() is called.
Reimplemented in MediaWiki\Session\CookieSessionProvider.
Definition at line 157 of file SessionProvider.php.
Referenced by MediaWiki\Session\SessionProvider\init().
MediaWiki\Session\SessionProvider::preventSessionsForUser | ( | $username | ) |
Prevent future sessions for the user.
If the provider is capable of returning a SessionInfo with a verified UserInfo for the named user in some manner other than by validating against $user->getToken(), steps must be taken to prevent that from occurring in the future. This might add the username to a list, or it might just delete whatever authentication credentials would allow such a session in the first place (e.g. remove all OAuth grants or delete record of the SSL client certificate).
The intention is that the named account will never again be usable for normal login (i.e. there is no way to undo the prevention of access).
Note that the passed user name might not exist locally (i.e. UserIdentity::getId() === 0); the name should still be prevented, if applicable.
string | $username |
Reimplemented in MediaWiki\Session\BotPasswordSessionProvider.
Definition at line 511 of file SessionProvider.php.
|
abstract |
Provide session info for a request.
If no session exists for the request, return null. Otherwise return an SessionInfo object identifying the session.
If multiple SessionProviders provide sessions, the one with highest priority wins. In case of a tie, an exception is thrown. SessionProviders are encouraged to make priorities user-configurable unless only max-priority makes sense.
WebRequest | $request |
Reimplemented in MediaWiki\Installer\InstallerSessionProvider, MediaWiki\Session\BotPasswordSessionProvider, and MediaWiki\Session\CookieSessionProvider.
MediaWiki\Session\SessionProvider::refreshSessionInfo | ( | SessionInfo | $info, |
WebRequest | $request, | ||
& | $metadata ) |
Validate a loaded SessionInfo and refresh provider metadata.
This is similar in purpose to the 'SessionCheckInfo' hook, and also allows for updating the provider metadata. On failure, the provider is expected to write an appropriate message to its logger.
SessionInfo | $info | Any changes by mergeMetadata() will already be reflected here. |
WebRequest | $request | |
array | null | &$metadata | Provider metadata, may be altered. |
Reimplemented in MediaWiki\Session\BotPasswordSessionProvider.
Definition at line 363 of file SessionProvider.php.
MediaWiki\Session\SessionProvider::safeAgainstCsrf | ( | ) |
Most session providers require protection against CSRF attacks (usually via CSRF tokens)
Implements MediaWiki\Session\SessionProviderInterface.
Definition at line 664 of file SessionProvider.php.
MediaWiki\Session\SessionProvider::sessionIdWasReset | ( | SessionBackend | $session, |
$oldId ) |
Notification that the session ID was reset.
No need to persist here, persistSession() will be called if appropriate.
SessionBackend | $session | Session to persist |
string | $oldId | Old session ID |
Definition at line 444 of file SessionProvider.php.
MediaWiki\Session\SessionProvider::setConfig | ( | Config | $config | ) |
Set configuration.
Config | $config |
Definition at line 187 of file SessionProvider.php.
References MediaWiki\Session\SessionProvider\$config, and wfDeprecated().
MediaWiki\Session\SessionProvider::setHookContainer | ( | $hookContainer | ) |
HookContainer | $hookContainer |
Definition at line 236 of file SessionProvider.php.
References wfDeprecated().
MediaWiki\Session\SessionProvider::setLogger | ( | LoggerInterface | $logger | ) |
Sets a logger instance on the object.
LoggerInterface | $logger |
Definition at line 171 of file SessionProvider.php.
References MediaWiki\Session\SessionProvider\$logger, and wfDeprecated().
MediaWiki\Session\SessionProvider::setManager | ( | SessionManager | $manager | ) |
Set the session manager.
SessionManager | $manager |
Definition at line 213 of file SessionProvider.php.
References MediaWiki\Session\SessionProvider\$manager, and wfDeprecated().
MediaWiki\Session\SessionProvider::suggestLoginUsername | ( | WebRequest | $request | ) |
Get a suggested username for the login form.
WebRequest | $request |
Reimplemented in MediaWiki\Session\CookieSessionProvider.
Definition at line 568 of file SessionProvider.php.
|
abstract |
Remove any persisted session from a request/response.
For example, blank and expire any cookies set by self::persistSession().
A backend that cannot persist session ID or user info should implement this as a no-op.
WebRequest | $request | Request from which to remove any session data |
Reimplemented in MediaWiki\Installer\InstallerSessionProvider, MediaWiki\Session\CookieSessionProvider, and MediaWiki\Session\ImmutableSessionProviderWithCookie.
MediaWiki\Session\SessionProvider::whyNoSession | ( | ) |
Return a Message for why sessions might not be being persisted.For example, "check whether you're blocking our cookies".
Implements MediaWiki\Session\SessionProviderInterface.
Reimplemented in MediaWiki\Session\CookieSessionProvider, and MediaWiki\Session\ImmutableSessionProviderWithCookie.
Definition at line 654 of file SessionProvider.php.
|
protected |
Definition at line 95 of file SessionProvider.php.
Referenced by MediaWiki\Session\SessionProvider\getConfig(), MediaWiki\Session\SessionProvider\init(), and MediaWiki\Session\SessionProvider\setConfig().
|
protected |
Definition at line 92 of file SessionProvider.php.
Referenced by MediaWiki\Session\SessionProvider\init(), and MediaWiki\Session\SessionProvider\setLogger().
|
protected |
Definition at line 98 of file SessionProvider.php.
Referenced by MediaWiki\Session\SessionProvider\getManager(), MediaWiki\Session\SessionProvider\init(), and MediaWiki\Session\SessionProvider\setManager().
|
protected |
Session priority.
Used for the default newSessionInfo(), but could be used by subclasses too.
Definition at line 112 of file SessionProvider.php.
|
protected |
Definition at line 107 of file SessionProvider.php.
Referenced by MediaWiki\Session\SessionProvider\init().