forked from flashii/eeprom
59 lines
1.9 KiB
PHP
59 lines
1.9 KiB
PHP
<?php
|
|
namespace EEPROM\Auth;
|
|
|
|
use RuntimeException;
|
|
use Index\Serialisation\Serialiser;
|
|
|
|
class MisuzuAuth implements IAuth {
|
|
private $endPoint = '';
|
|
private $secretKey = '';
|
|
|
|
public function __construct() {
|
|
global $cfg;
|
|
|
|
$this->endPoint = $cfg->getString('misuzu:endpoint');
|
|
$this->secretKey = $cfg->getString('misuzu:secret');
|
|
}
|
|
|
|
public function getName(): string { return 'Misuzu'; }
|
|
|
|
public function verifyToken(string $token): int {
|
|
if(empty($token))
|
|
return 0;
|
|
|
|
$method = 'Misuzu';
|
|
$signature = sprintf('verify#%s#%s#%s', $method, $token, $_SERVER['REMOTE_ADDR']);
|
|
$signature = hash_hmac('sha256', $signature, $this->secretKey);
|
|
|
|
$login = curl_init($this->endPoint);
|
|
curl_setopt_array($login, [
|
|
CURLOPT_AUTOREFERER => false,
|
|
CURLOPT_FAILONERROR => false,
|
|
CURLOPT_FOLLOWLOCATION => true,
|
|
CURLOPT_HEADER => false,
|
|
CURLOPT_POST => true,
|
|
CURLOPT_POSTFIELDS => http_build_query([
|
|
'method' => $method,
|
|
'token' => $token,
|
|
'ipaddr' => $_SERVER['REMOTE_ADDR'],
|
|
], '', '&', PHP_QUERY_RFC3986),
|
|
CURLOPT_RETURNTRANSFER => true,
|
|
CURLOPT_TCP_FASTOPEN => true,
|
|
CURLOPT_CONNECTTIMEOUT => 2,
|
|
CURLOPT_MAXREDIRS => 2,
|
|
CURLOPT_PROTOCOLS => CURLPROTO_HTTPS,
|
|
CURLOPT_TIMEOUT => 5,
|
|
CURLOPT_USERAGENT => 'Flashii EEPROM',
|
|
CURLOPT_HTTPHEADER => [
|
|
'Content-Type: application/x-www-form-urlencoded',
|
|
'X-SharpChat-Signature: ' . $signature,
|
|
],
|
|
]);
|
|
$rawUserInfo = curl_exec($login);
|
|
$userInfo = json_decode($rawUserInfo);
|
|
curl_close($login);
|
|
|
|
return empty($userInfo->success) || empty($userInfo->user_id) ? 0 : $userInfo->user_id;
|
|
}
|
|
}
|