Page MenuHomeWickedGov Phorge

CentralAuthApiTokenGenerator.php
No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None

CentralAuthApiTokenGenerator.php

<?php
namespace MediaWiki\Extension\CentralAuth;
use InvalidArgumentException;
use MediaWiki\Extension\CentralAuth\User\CentralAuthUser;
use MediaWiki\User\UserIdentity;
use MWCryptRand;
/**
* Generate an authentication token to be used for API calls.
*/
class CentralAuthApiTokenGenerator {
private CentralAuthTokenManager $tokenManager;
public function __construct(
CentralAuthTokenManager $tokenManager
) {
$this->tokenManager = $tokenManager;
}
/**
* Get a CentralAuth token for making authenticated API requests to an attached wiki.
*
* @param UserIdentity $user The registered user
* @param string $sessionId The global session ID
* @param string $wikiId The wiki ID
* @return string Token in hexadecimal, with a random part and the central user ID
* @throws InvalidArgumentException
*/
public function getToken( UserIdentity $user, string $sessionId, string $wikiId ) {
if ( !$user->isRegistered() ) {
throw new InvalidArgumentException( 'Cannot get a token for an unregistered user' );
}
$centralUser = CentralAuthUser::getInstance( $user );
if ( !$centralUser->exists() || !$centralUser->isAttached() ) {
throw new InvalidArgumentException( 'Cannot get a token without an attached global user' );
}
$data = [
'userName' => $user->getName(),
'token' => $centralUser->getAuthToken(),
'origin' => $wikiId,
'originSessionId' => $sessionId,
];
$loginToken = MWCryptRand::generateHex( 32 ) . dechex( $centralUser->getId() );
$this->tokenManager->tokenize( $data, 'api-token', [ 'token' => $loginToken ] );
return $loginToken;
}
}

File Metadata

Mime Type
text/x-php
Expires
Sat, Oct 3, 04:01 (3 w, 4 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
e8/43/b10f34b2b9ff27c57e8e36dbaff7
Default Alt Text
CentralAuthApiTokenGenerator.php (1 KB)

Event Timeline