Switched EEPROM from private chat auth to public API.

This commit is contained in:
flash 2024-11-19 22:11:14 +00:00
parent c3827334a1
commit a409410ea0
5 changed files with 54 additions and 64 deletions

View file

@ -1,6 +1,7 @@
{ {
"require": { "require": {
"flashwave/index": "^0.2410", "flashwave/index": "^0.2410",
"flashii/apii": "^0.2",
"sentry/sdk": "^4.0", "sentry/sdk": "^4.0",
"nesbot/carbon": "^3.7" "nesbot/carbon": "^3.7"
}, },

39
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "0bd5a41ea92a75983cfabd9b9052a8a2", "content-hash": "5bb4c1111eb08d57ef2ef77239a4ddc8",
"packages": [ "packages": [
{ {
"name": "carbonphp/carbon-doctrine-types", "name": "carbonphp/carbon-doctrine-types",
@ -75,6 +75,43 @@
], ],
"time": "2024-02-09T16:56:22+00:00" "time": "2024-02-09T16:56:22+00:00"
}, },
{
"name": "flashii/apii",
"version": "v0.2.1",
"source": {
"type": "git",
"url": "https://patchii.net/flashii/apii-php.git",
"reference": "6a93d31375dd7e75ff9264f3024f2208ce602f49"
},
"require": {
"php": ">=8.1"
},
"require-dev": {
"phpstan/phpstan": "^1.12",
"phpunit/phpunit": "^10.5"
},
"type": "library",
"autoload": {
"psr-4": {
"Flashii\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"bsd-3-clause-clear"
],
"authors": [
{
"name": "flashwave",
"email": "packagist@flash.moe",
"homepage": "https://flash.moe",
"role": "mom"
}
],
"description": "Client library for the Flashii.net API.",
"homepage": "https://api.flashii.net",
"time": "2024-11-16T16:03:42+00:00"
},
{ {
"name": "flashwave/index", "name": "flashwave/index",
"version": "v0.2410.191603", "version": "v0.2410.191603",

View file

@ -2,6 +2,8 @@
namespace EEPROM\Auth; namespace EEPROM\Auth;
use EEPROM\Users\UsersContext; use EEPROM\Users\UsersContext;
use Flashii\{FlashiiClient,FlashiiUrls};
use Flashii\Credentials\{BearerCredentials,MisuzuCredentials};
use Index\Config\Config; use Index\Config\Config;
use Index\Http\Routing\{HttpMiddleware,RouteHandler,RouteHandlerTrait}; use Index\Http\Routing\{HttpMiddleware,RouteHandler,RouteHandlerTrait};
@ -28,16 +30,19 @@ class AuthRoutes implements RouteHandler {
$authMethod = strval($authParts[0] ?? ''); $authMethod = strval($authParts[0] ?? '');
$authToken = strval($authParts[1] ?? ''); $authToken = strval($authParts[1] ?? '');
if(strcasecmp($authMethod, 'misuzu') === 0 || strcasecmp($authMethod, 'bearer') === 0) { $isMisuzu = strcasecmp($authMethod, 'misuzu') === 0;
$authResult = ChatAuth::attempt( $isBearer = strcasecmp($authMethod, 'bearer') === 0;
$this->config->getString('endpoint'),
$authMethod,
$this->config->getString('secret'),
$authToken
);
if(!empty($authResult->success)) if($isMisuzu || $isBearer) {
$this->authInfo->setInfo($this->usersCtx->getUser($authResult->user_id)); $credentials = $isMisuzu ? new MisuzuCredentials($authToken) : new BearerCredentials($authToken);
$flashii = new FlashiiClient('EEPROM', $credentials, new FlashiiUrls(
$this->config->getString('api', FlashiiUrls::PROD_API_URL),
$this->config->getString('id', FlashiiUrls::PROD_ID_URL)
));
$authInfo = $flashii->v1()->me();
if($authInfo !== null)
$this->authInfo->setInfo($this->usersCtx->getUser($authInfo->getId()));
} }
} }
} }

View file

@ -1,53 +0,0 @@
<?php
namespace EEPROM\Auth;
use stdClass;
final class ChatAuth {
public static function attempt(string $endPoint, string $method, string $secret, string $cookie): stdClass {
if(!empty($cookie)) {
$signature = sprintf('verify#%s#%s#%s', $method, $cookie, $_SERVER['REMOTE_ADDR']);
$signature = hash_hmac('sha256', $signature, $secret);
$login = curl_init($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' => $cookie,
'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 => 'EEPROM',
CURLOPT_HTTPHEADER => [
'Content-Type: application/x-www-form-urlencoded',
'X-SharpChat-Signature: ' . $signature,
],
]);
$userInfo = json_decode(curl_exec($login));
curl_close($login);
}
if(empty($userInfo->success)) {
$userInfo = new stdClass;
$userInfo->success = false;
$userInfo->user_id = 0;
$userInfo->username = 'Anonymous';
$userInfo->colour_raw = 0x40000000;
$userInfo->rank = 0;
$userInfo->hierarchy = 0;
$userInfo->perms = 0;
}
return $userInfo;
}
}

View file

@ -61,7 +61,7 @@ class EEPROMContext {
if($isApiDomain) { if($isApiDomain) {
$routingCtx->register(new Auth\AuthRoutes( $routingCtx->register(new Auth\AuthRoutes(
$this->config->scopeTo('misuzu'), $this->config->scopeTo('apii'),
$this->authInfo, $this->authInfo,
$this->usersCtx $this->usersCtx
)); ));