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.
104 lines
3.3 KiB
PHP
104 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace Isekai\OIDC;
|
|
|
|
use ApiBase;
|
|
use MediaWiki\MediaWikiServices;
|
|
use Wikimedia\ParamValidator\ParamValidator;
|
|
|
|
class ApiOIDCWebhook extends ApiBase {
|
|
public function __construct( $main, $method ) {
|
|
parent::__construct( $main->getMain(), $method );
|
|
}
|
|
|
|
public function execute() {
|
|
$services = MediaWikiServices::getInstance();
|
|
$config = $services->getMainConfig();
|
|
$wgIsekaiOIDC = $config->get('IsekaiOIDC');
|
|
|
|
$queryValues = $this->getRequest()->getQueryValues();
|
|
$provider = '';
|
|
if (isset($queryValues['provider'])) {
|
|
$provider = $queryValues['provider'];
|
|
}
|
|
|
|
if (isset($wgIsekaiOIDC['webhookKey'])) {
|
|
if (!isset($queryValues['key']) || $queryValues['key'] !== $wgIsekaiOIDC['webhookKey']) {
|
|
$this->addError('isekaioidc-api-key error', '-1', [ 'provider' => $provider ]);
|
|
return;
|
|
}
|
|
}
|
|
|
|
switch ($provider) {
|
|
case 'keycloak':
|
|
$this->keycloakCallback();
|
|
break;
|
|
default:
|
|
$this->addError('isekaioidc-api-provider-not-supported', '-1', [ 'provider' => $provider ]);
|
|
}
|
|
}
|
|
|
|
private function keycloakCallback() {
|
|
$services = MediaWikiServices::getInstance();
|
|
$config = $services->getMainConfig();
|
|
$wgIsekaiOIDC = $config->get('IsekaiOIDC');
|
|
|
|
if (!$this->getRequest()->wasPosted()) {
|
|
$this->addError('isekaioidc-api-post-body-invalid');
|
|
return;
|
|
}
|
|
|
|
$postBody = $this->getRequest()->getRawPostString();
|
|
$postData = json_decode($postBody);
|
|
if (!$this->getRequest()->wasPosted() || !$postData) {
|
|
$this->addError('isekaioidc-api-post-body-invalid');
|
|
return;
|
|
}
|
|
$realm = $wgIsekaiOIDC['realm'];
|
|
$apiMode = $wgIsekaiOIDC['apiMode'] ?? 'oauth';
|
|
|
|
$eventType = $postData->type;
|
|
$subject = $postData->userId;
|
|
$eventRealm = $postData->realmId;
|
|
if ($eventRealm !== $realm) {
|
|
//return $this->addError('isekaioidc-api-realm-not-match');
|
|
$this->getResult()->addValue(null, 'webhook', 0);
|
|
return;
|
|
}
|
|
|
|
if (!in_array($eventType, ['UPDATE_PROFILE', 'UPDATE_EMAIL'])) {
|
|
//return $this->addError('isekaioidc-api-unsupported-event');
|
|
$this->getResult()->addValue(null, 'webhook', 0);
|
|
return;
|
|
}
|
|
|
|
list($userId, $userName, $accessToken, $refreshToken) = IsekaiOIDCAuth::findUser($subject);
|
|
|
|
if ($userId) {
|
|
$userInfo = $postData->userInfo;
|
|
$newProfile = [
|
|
'realname' => $userInfo->name,
|
|
'email' => $userInfo->email,
|
|
'phone' => $userInfo->phone_number,
|
|
];
|
|
|
|
$user = $services->getUserFactory()->newFromId($userId);
|
|
IsekaiOIDCAuth::updateUserInfo($user, $newProfile);
|
|
}
|
|
$this->getResult()->addValue(null, 'webhook', 1);
|
|
}
|
|
|
|
public function getAllowedParams() {
|
|
return [
|
|
'provider' => [
|
|
ParamValidator::PARAM_DEFAULT => null,
|
|
ParamValidator::PARAM_TYPE => 'text',
|
|
],
|
|
'key' => [
|
|
ParamValidator::PARAM_DEFAULT => null,
|
|
ParamValidator::PARAM_TYPE => 'text',
|
|
]
|
|
];
|
|
}
|
|
}
|