You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.1 KiB
PHP
80 lines
2.1 KiB
PHP
<?php
|
|
namespace Isekai\ChatComplete\Api\ChatComplete;
|
|
|
|
use ApiBase;
|
|
use Config;
|
|
use Isekai\ChatComplete\Api\ApiChatComplete;
|
|
use Isekai\ChatComplete\Api\ApiChatCompleteBot;
|
|
use MediaWiki\MediaWikiServices;
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
|
use Firebase\JWT\JWT;
|
|
|
|
class ApiCreateToken extends ApiBase {
|
|
/** @var MediaWikiServices */
|
|
private $services;
|
|
|
|
/** @var Config */
|
|
private $config;
|
|
|
|
/** @var ApiChatComplete */
|
|
private $mParent;
|
|
|
|
public function __construct(ApiChatComplete $main, $method) {
|
|
parent::__construct($main->getMain(), $method);
|
|
|
|
$this->mParent = $main;
|
|
$this->services = MediaWikiServices::getInstance();
|
|
$this->config = $this->getConfig();
|
|
}
|
|
|
|
public function execute() {
|
|
$user = $this->getUser();
|
|
|
|
if (!$user->isRegistered()) {
|
|
$this->addError('apierror-isekai-chatcomplete-nopermission', 'nopermission');
|
|
return false;
|
|
}
|
|
|
|
$result = $this->getResult();
|
|
|
|
$permissionMap = ApiChatComplete::$permissionMap;
|
|
|
|
if (!$user->isAllowedAny(...array_values($permissionMap))) {
|
|
$this->addError('apierror-isekai-chatcomplete-nopermission', 'nopermission');
|
|
return false;
|
|
}
|
|
|
|
// Create JWT
|
|
$tokenId = $this->config->get('IsekaiChatCompleteTokenId');
|
|
$token = $this->config->get('IsekaiChatCompleteToken');
|
|
|
|
$currentTime = time();
|
|
$expireTime = $currentTime + 86400; // 1 day
|
|
|
|
$payload = [
|
|
'iss' => 'mwchatcomplete',
|
|
'sub' => $user->getId(),
|
|
'userName' => $user->getName(),
|
|
'iat' => $currentTime,
|
|
'exp' => $expireTime,
|
|
];
|
|
|
|
$jwt = JWT::encode($payload, $token, 'HS256', $tokenId);
|
|
|
|
$result->addValue(['chatcomplete', $this->getModuleName()], 'token', $jwt);
|
|
|
|
return true;
|
|
}
|
|
|
|
public function getCacheMode($params) {
|
|
return 'private';
|
|
}
|
|
|
|
public function getParent() {
|
|
return $this->mParent;
|
|
}
|
|
|
|
public function needsToken() {
|
|
return 'csrf';
|
|
}
|
|
} |