Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F7403948
CentralAuthApiSessionProvider.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
CentralAuthApiSessionProvider.php
View Options
<?php
use
MediaWiki\Api\ApiUsageException
;
use
MediaWiki\Request\WebRequest
;
use
MediaWiki\Session\SessionInfo
;
/**
* Session provider for CentralAuth API centralauthtoken
*
* This session provider looks for the 'centralauthtoken' request parameter,
* and checks that it corresponds to an existing token set up by
* ApiCentralAuthToken. If the parameter is present but invalid, it returns a
* bogus SessionInfo and hooks ApiBeforeMain to throw an appropriate exception
* later when MediaWiki is ready to handle it.
*
* @see \MediaWiki\Extension\CentralAuth\Api\ApiCentralAuthToken
*/
class
CentralAuthApiSessionProvider
extends
CentralAuthTokenSessionProvider
{
/** @inheritDoc */
protected
function
getTokenDataFromRequest
(
WebRequest
$request
)
{
// Only applied to api.php and unit tests
if
(
!
defined
(
'MW_API'
)
&&
!
defined
(
'MW_PHPUNIT_TEST'
)
)
{
return
null
;
}
$oneTimeToken
=
$request
->
getVal
(
'centralauthtoken'
);
if
(
$oneTimeToken
===
null
)
{
return
null
;
}
if
(
$request
->
getMethod
()
===
'OPTIONS'
)
{
// Do not delete the tokenized data on OPTIONS requests, as they are generated automatically
// by the browser as part of the CORS preflight mechanism, with the same URL as the real
// GET/POST request (including the 'centralauthtoken' parameter). Deleting it here would cause
// the subsequent real request to fail. There is no way to avoid that.
// https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#preflighted_requests
return
$this
->
tokenManager
->
detokenize
(
$oneTimeToken
,
'api-token'
);
}
else
{
return
$this
->
tokenManager
->
detokenizeAndDelete
(
$oneTimeToken
,
'api-token'
);
}
}
/**
* Throw an exception, later
*
* @param string $code Error code
* @param string|array $error Error message key, or key+parameters
* @return SessionInfo
*/
protected
function
makeBogusSessionInfo
(
$code
,
$error
)
{
// Schedule the throwing of the exception for later when the API
// is ready to catch it.
$exception
=
ApiUsageException
::
newWithMessage
(
null
,
$error
,
$code
);
/** @return never */
$excepClosure
=
static
function
()
use
(
$exception
)
{
throw
$exception
;
};
$this
->
getHookContainer
()->
register
(
'ApiBeforeMain'
,
$excepClosure
);
return
parent
::
makeBogusSessionInfo
(
$code
,
$error
);
}
/** @inheritDoc */
public
function
provideSessionInfo
(
WebRequest
$request
)
{
// Only applied to api.php and unit tests
if
(
!
defined
(
'MW_API'
)
&&
!
defined
(
'MW_PHPUNIT_TEST'
)
)
{
return
null
;
}
return
parent
::
provideSessionInfo
(
$request
);
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, Nov 21, 18:53 (23 h, 27 m)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
ae/c6/34110c833c5f8ccccca7e9b27405
Default Alt Text
CentralAuthApiSessionProvider.php (2 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment