Updated Index and RPCii.

This commit is contained in:
Pachira 2024-11-13 23:58:43 +00:00
parent bf40942e72
commit a53085e447
36 changed files with 307 additions and 454 deletions

View file

@ -1,15 +1,13 @@
{ {
"require": { "require": {
"flashwave/index": "^0.2408.40014", "flashwave/index": "^0.2410",
"flashwave/sasae": "^1.1", "flashii/rpcii": "^2.0",
"flashwave/syokuhou": "^1.2",
"chillerlan/php-qrcode": "^4.3", "chillerlan/php-qrcode": "^4.3",
"symfony/mailer": "^6.0", "symfony/mailer": "^6.0",
"matomo/device-detector": "^6.1", "matomo/device-detector": "^6.1",
"sentry/sdk": "^4.0", "sentry/sdk": "^4.0",
"phpseclib/phpseclib": "~3.0", "phpseclib/phpseclib": "~3.0",
"nesbot/carbon": "^3.7", "nesbot/carbon": "^3.7"
"flashwave/aiwass": "^1.0"
}, },
"autoload": { "autoload": {
"classmap": [ "classmap": [

475
composer.lock generated

File diff suppressed because it is too large Load diff

View file

@ -1,9 +1,9 @@
<?php <?php
use Index\Data\IDbConnection; use Index\Db\DbConnection;
use Index\Data\Migration\IDbMigration; use Index\Db\Migration\DbMigration;
final class AppsTables_20240720_185612 implements IDbMigration { final class AppsTables_20240720_185612 implements DbMigration {
public function migrate(IDbConnection $conn): void { public function migrate(DbConnection $conn): void {
$conn->execute(<<<SQL $conn->execute(<<<SQL
CREATE TABLE hau_apps ( CREATE TABLE hau_apps (
app_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, app_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,

View file

@ -1,9 +1,9 @@
<?php <?php
use Index\Data\IDbConnection; use Index\Db\DbConnection;
use Index\Data\Migration\IDbMigration; use Index\Db\Migration\DbMigration;
final class OauthTables_20240720_185921 implements IDbMigration { final class OauthTables_20240720_185921 implements DbMigration {
public function migrate(IDbConnection $conn): void { public function migrate(DbConnection $conn): void {
$conn->execute(<<<SQL $conn->execute(<<<SQL
CREATE TABLE hau_oauth2_authorise ( CREATE TABLE hau_oauth2_authorise (
auth_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, auth_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,

View file

@ -1,9 +1,9 @@
<?php <?php
use Index\Data\IDbConnection; use Index\Db\DbConnection;
use Index\Data\Migration\IDbMigration; use Index\Db\Migration\DbMigration;
final class ConfigurableRefreshLifetime_20240727_025524 implements IDbMigration { final class ConfigurableRefreshLifetime_20240727_025524 implements DbMigration {
public function migrate(IDbConnection $conn): void { public function migrate(DbConnection $conn): void {
$conn->execute('ALTER TABLE hau_apps ADD COLUMN app_refresh_lifetime INT UNSIGNED NULL DEFAULT NULL AFTER app_type'); $conn->execute('ALTER TABLE hau_apps ADD COLUMN app_refresh_lifetime INT UNSIGNED NULL DEFAULT NULL AFTER app_type');
} }
} }

View file

@ -1,9 +1,9 @@
<?php <?php
use Index\Data\IDbConnection; use Index\Db\DbConnection;
use Index\Data\Migration\IDbMigration; use Index\Db\Migration\DbMigration;
final class NineCharacterDeviceUserCode_20240727_201438 implements IDbMigration { final class NineCharacterDeviceUserCode_20240727_201438 implements DbMigration {
public function migrate(IDbConnection $conn): void { public function migrate(DbConnection $conn): void {
$conn->execute('ALTER TABLE hau_oauth2_device CHANGE COLUMN dev_user_code dev_user_code CHAR(9) NOT NULL COLLATE ascii_general_ci AFTER dev_code'); $conn->execute('ALTER TABLE hau_oauth2_device CHANGE COLUMN dev_user_code dev_user_code CHAR(9) NOT NULL COLLATE ascii_general_ci AFTER dev_code');
} }
} }

View file

@ -1,9 +1,9 @@
<?php <?php
use Index\Data\IDbConnection; use Index\Db\DbConnection;
use Index\Data\Migration\IDbMigration; use Index\Db\Migration\DbMigration;
final class RemovedDeviceAttemptsField_20240730_001441 implements IDbMigration { final class RemovedDeviceAttemptsField_20240730_001441 implements DbMigration {
public function migrate(IDbConnection $conn): void { public function migrate(DbConnection $conn): void {
$conn->execute('ALTER TABLE hau_oauth2_device DROP COLUMN dev_attempts'); $conn->execute('ALTER TABLE hau_oauth2_device DROP COLUMN dev_attempts');
} }
} }

View file

@ -1,9 +1,9 @@
<?php <?php
use Index\Data\IDbConnection; use Index\Db\DbConnection;
use Index\Data\Migration\IDbMigration; use Index\Db\Migration\DbMigration;
final class RemoveStateFieldsFromAuthorisationsDb_20240730_211859 implements IDbMigration { final class RemoveStateFieldsFromAuthorisationsDb_20240730_211859 implements DbMigration {
public function migrate(IDbConnection $conn): void { public function migrate(DbConnection $conn): void {
$conn->execute('ALTER TABLE hau_oauth2_authorise DROP COLUMN auth_state, DROP COLUMN auth_approval;'); $conn->execute('ALTER TABLE hau_oauth2_authorise DROP COLUMN auth_state, DROP COLUMN auth_approval;');
} }
} }

View file

@ -1,9 +1,9 @@
<?php <?php
use Index\Data\IDbConnection; use Index\Db\DbConnection;
use Index\Data\Migration\IDbMigration; use Index\Db\Migration\DbMigration;
final class ScopeTables_20240903_201657 implements IDbMigration { final class ScopeTables_20240903_201657 implements DbMigration {
public function migrate(IDbConnection $conn): void { public function migrate(DbConnection $conn): void {
$conn->execute(<<<SQL $conn->execute(<<<SQL
CREATE TABLE hau_scopes ( CREATE TABLE hau_scopes (
scope_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, scope_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,

View file

@ -1,9 +1,9 @@
<?php <?php
use Index\Data\IDbConnection; use Index\Db\DbConnection;
use Index\Data\Migration\IDbMigration; use Index\Db\Migration\DbMigration;
final class AccessLifetimeFieldForAppsTable_20240904_205008 implements IDbMigration { final class AccessLifetimeFieldForAppsTable_20240904_205008 implements DbMigration {
public function migrate(IDbConnection $conn): void { public function migrate(DbConnection $conn): void {
$conn->execute(<<<SQL $conn->execute(<<<SQL
ALTER TABLE hau_apps ALTER TABLE hau_apps
ADD COLUMN app_access_lifetime INT(10) UNSIGNED NULL DEFAULT NULL AFTER app_type; ADD COLUMN app_access_lifetime INT(10) UNSIGNED NULL DEFAULT NULL AFTER app_type;

View file

@ -1,8 +1,8 @@
<?php <?php
namespace Hanyuu; namespace Hanyuu;
use Index\Data\DbTools; use Index\Db\DbBackends;
use Syokuhou\SharpConfig; use Index\Config\Fs\FsConfig;
define('HAU_STARTUP', microtime(true)); define('HAU_STARTUP', microtime(true));
define('HAU_ROOT', __DIR__); define('HAU_ROOT', __DIR__);
@ -20,7 +20,7 @@ error_reporting(HAU_DEBUG ? -1 : 0);
mb_internal_encoding('UTF-8'); mb_internal_encoding('UTF-8');
date_default_timezone_set('GMT'); date_default_timezone_set('GMT');
$cfg = SharpConfig::fromFile(HAU_ROOT . '/hanyuu.cfg'); $cfg = FsConfig::fromFile(HAU_ROOT . '/hanyuu.cfg');
if($cfg->hasValues('sentry:dsn')) if($cfg->hasValues('sentry:dsn'))
(function($cfg) { (function($cfg) {
@ -35,7 +35,7 @@ if($cfg->hasValues('sentry:dsn'))
}); });
})($cfg->scopeTo('sentry')); })($cfg->scopeTo('sentry'));
$dbc = DbTools::create($cfg->getString('database:dsn', 'null')); $dbc = DbBackends::create($cfg->getString('database:dsn', 'null'));
$dbc->execute('SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\';'); $dbc->execute('SET SESSION time_zone = \'+00:00\', sql_mode = \'STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION\';');
$hau = new HanyuuContext($cfg, $dbc); $hau = new HanyuuContext($cfg, $dbc);

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Hanyuu\Apps; namespace Hanyuu\Apps;
use Index\Data\IDbResult; use Index\Db\DbResult;
class AppInfo { class AppInfo {
public function __construct( public function __construct(
@ -20,7 +20,7 @@ class AppInfo {
private ?int $deleted private ?int $deleted
) {} ) {}
public static function fromResult(IDbResult $result): AppInfo { public static function fromResult(DbResult $result): AppInfo {
return new AppInfo( return new AppInfo(
id: $result->getString(0), id: $result->getString(0),
name: $result->getString(1), name: $result->getString(1),

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Hanyuu\Apps; namespace Hanyuu\Apps;
use Index\Data\IDbResult; use Index\Db\DbResult;
class AppUriInfo { class AppUriInfo {
public function __construct( public function __construct(
@ -11,7 +11,7 @@ class AppUriInfo {
private int $created private int $created
) {} ) {}
public static function fromResult(IDbResult $result): AppUriInfo { public static function fromResult(DbResult $result): AppUriInfo {
return new AppUriInfo( return new AppUriInfo(
id: $result->getString(0), id: $result->getString(0),
appId: $result->getString(1), appId: $result->getString(1),

View file

@ -2,13 +2,13 @@
namespace Hanyuu\Apps; namespace Hanyuu\Apps;
use RuntimeException; use RuntimeException;
use Index\Data\IDbConnection; use Index\Db\DbConnection;
class AppsContext { class AppsContext {
private AppsData $apps; private AppsData $apps;
private ScopesData $scopes; private ScopesData $scopes;
public function __construct(IDbConnection $dbConn) { public function __construct(DbConnection $dbConn) {
$this->apps = new AppsData($dbConn); $this->apps = new AppsData($dbConn);
$this->scopes = new ScopesData($dbConn); $this->scopes = new ScopesData($dbConn);
} }

View file

@ -5,13 +5,13 @@ use stdClass;
use InvalidArgumentException; use InvalidArgumentException;
use RuntimeException; use RuntimeException;
use Index\XString; use Index\XString;
use Index\Data\{DbStatementCache,IDbConnection}; use Index\Db\{DbConnection,DbStatementCache};
class AppsData { class AppsData {
private DbStatementCache $cache; private DbStatementCache $cache;
public function __construct( public function __construct(
private IDbConnection $dbConn private DbConnection $dbConn
) { ) {
$this->cache = new DbStatementCache($dbConn); $this->cache = new DbStatementCache($dbConn);
} }

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Hanyuu\Apps; namespace Hanyuu\Apps;
use Index\Data\IDbResult; use Index\Db\DbResult;
class ScopeInfo { class ScopeInfo {
public function __construct( public function __construct(
@ -13,7 +13,7 @@ class ScopeInfo {
private ?int $deprecated private ?int $deprecated
) {} ) {}
public static function fromResult(IDbResult $result): ScopeInfo { public static function fromResult(DbResult $result): ScopeInfo {
return new ScopeInfo( return new ScopeInfo(
$result->getString(0), $result->getString(0),
$result->getString(1), $result->getString(1),

View file

@ -3,13 +3,13 @@ namespace Hanyuu\Apps;
use InvalidArgumentException; use InvalidArgumentException;
use RuntimeException; use RuntimeException;
use Index\Data\{DbStatementCache,IDbConnection}; use Index\Db\{DbConnection,DbStatementCache};
class ScopesData { class ScopesData {
private DbStatementCache $cache; private DbStatementCache $cache;
public function __construct( public function __construct(
private IDbConnection $dbConn private DbConnection $dbConn
) { ) {
$this->cache = new DbStatementCache($dbConn); $this->cache = new DbStatementCache($dbConn);
} }

View file

@ -1,19 +1,18 @@
<?php <?php
namespace Hanyuu; namespace Hanyuu;
use Aiwass\HmacVerificationProvider;
use Aiwass\Server\RpcServer;
use Index\Environment;
use Index\Colour\Colour; use Index\Colour\Colour;
use Index\Data\IDbConnection; use Index\Config\Config;
use Index\Data\Migration\{IDbMigrationRepo,DbMigrationManager,FsDbMigrationRepo}; use Index\Db\DbConnection;
use Sasae\SasaeEnvironment; use Index\Db\Migration\{DbMigrationRepo,DbMigrationManager,FsDbMigrationRepo};
use Syokuhou\IConfig; use Index\Templating\TplEnvironment;
use RPCii\HmacVerificationProvider;
use RPCii\Server\HttpRpcServer;
class HanyuuContext { class HanyuuContext {
private IConfig $config; private Config $config;
private IDbConnection $dbConn; private DbConnection $dbConn;
private SasaeEnvironment $templating; private TplEnvironment $templating;
private SiteInfo $siteInfo; private SiteInfo $siteInfo;
private MisuzuRpcClient $misuzuRpc; private MisuzuRpcClient $misuzuRpc;
@ -22,7 +21,7 @@ class HanyuuContext {
private Apps\AppsContext $appsCtx; private Apps\AppsContext $appsCtx;
private OAuth2\OAuth2Context $oauth2Ctx; private OAuth2\OAuth2Context $oauth2Ctx;
public function __construct(IConfig $config, IDbConnection $dbConn) { public function __construct(Config $config, DbConnection $dbConn) {
$this->config = $config; $this->config = $config;
$this->dbConn = $dbConn; $this->dbConn = $dbConn;
$this->siteInfo = new SiteInfo($config->scopeTo('site')); $this->siteInfo = new SiteInfo($config->scopeTo('site'));
@ -35,12 +34,12 @@ class HanyuuContext {
$this->appsCtx $this->appsCtx
); );
$this->templating = new SasaeEnvironment( $this->templating = new TplEnvironment(
HAU_DIR_TEMPLATES, HAU_DIR_TEMPLATES,
cache: null,//HAU_DEBUG ? null : ['Hanyuu', GitInfo::hash(true)], cache: null,//HAU_DEBUG ? null : ['Hanyuu', GitInfo::hash(true)],
debug: HAU_DEBUG, debug: HAU_DEBUG,
); );
$this->templating->addExtension(new HanyuuSasaeExtension($this)); $this->templating->addExtension(new TemplatingExtension($this));
$this->templating->addGlobal('globals', [ $this->templating->addGlobal('globals', [
'siteInfo' => $this->siteInfo, 'siteInfo' => $this->siteInfo,
@ -51,7 +50,7 @@ class HanyuuContext {
return $this->misuzuRpc; return $this->misuzuRpc;
} }
public function getDatabase(): IDbConnection { public function getDatabase(): DbConnection {
return $this->dbConn; return $this->dbConn;
} }
@ -64,7 +63,7 @@ class HanyuuContext {
return new DbMigrationManager($this->dbConn, 'hau_' . DbMigrationManager::DEFAULT_TABLE); return new DbMigrationManager($this->dbConn, 'hau_' . DbMigrationManager::DEFAULT_TABLE);
} }
public function createMigrationRepo(): IDbMigrationRepo { public function createMigrationRepo(): DbMigrationRepo {
return new FsDbMigrationRepo(HAU_DIR_MIGRATIONS); return new FsDbMigrationRepo(HAU_DIR_MIGRATIONS);
} }
@ -77,7 +76,7 @@ class HanyuuContext {
return $this->authInfo; return $this->authInfo;
} }
public function getCSRFPSecret(): string { public function getCsrfTokenSecret(): string {
return $this->config->getString('csrfp:secret', 'beans'); return $this->config->getString('csrfp:secret', 'beans');
} }
@ -86,7 +85,7 @@ class HanyuuContext {
return is_file($path) ? json_decode(file_get_contents($path)) : null; return is_file($path) ? json_decode(file_get_contents($path)) : null;
} }
public function getTemplating(): SasaeEnvironment { public function getTemplating(): TplEnvironment {
return $this->templating; return $this->templating;
} }
@ -111,15 +110,15 @@ class HanyuuContext {
$this->oauth2Ctx, $this->oauth2Ctx,
$this->templating, $this->templating,
$this->getAuthInfo(...), $this->getAuthInfo(...),
$this->getCSRFPSecret(...) $this->getCsrfTokenSecret(...)
)); ));
$rpcServer = new RpcServer; $rpcServer = new HttpRpcServer;
$routingCtx->register($rpcServer->createRouteHandler( $routingCtx->register($rpcServer->createRouteHandler(
new HmacVerificationProvider(fn() => $this->config->getString('aleister:secret')) new HmacVerificationProvider(fn() => $this->config->getString('aleister:secret'))
)); ));
$rpcServer->register(new OAuth2\OAuth2RpcActions($this->oauth2Ctx)); $rpcServer->register(new OAuth2\OAuth2RpcHandler($this->oauth2Ctx));
return $routingCtx; return $routingCtx;
} }

View file

@ -3,15 +3,15 @@ namespace Hanyuu;
use stdClass; use stdClass;
use RuntimeException; use RuntimeException;
use Aiwass\Client\RpcClient;
use Index\UriBase64; use Index\UriBase64;
use Syokuhou\IConfig; use Index\Config\Config;
use RPCii\Client\{HttpRpcClient,RpcClient};
class MisuzuRpcClient { class MisuzuRpcClient {
private RpcClient $client; private RpcClient $client;
public function __construct(IConfig $config) { public function __construct(Config $config) {
$this->client = RpcClient::createHmac( $this->client = HttpRpcClient::createHmac(
sprintf('%s/_hanyuu', $config->getString('endpoint')), sprintf('%s/_hanyuu', $config->getString('endpoint')),
fn() => $config->getString('secret') fn() => $config->getString('secret')
); );
@ -22,7 +22,7 @@ class MisuzuRpcClient {
} }
public function authCheck(string $method, string $token, string $remoteAddr, array $avatars = []): MisuzuAuthInfo { public function authCheck(string $method, string $token, string $remoteAddr, array $avatars = []): MisuzuAuthInfo {
$result = $this->client->procedure('mszhau:authCheck', [ $result = $this->client->action('mszhau:authCheck', [
'method' => $method, 'method' => $method,
'token' => $token, 'token' => $token,
'remoteAddr' => $remoteAddr, 'remoteAddr' => $remoteAddr,

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Hanyuu\OAuth2; namespace Hanyuu\OAuth2;
use Index\Data\IDbResult; use Index\Db\DbResult;
class OAuth2AccessInfo { class OAuth2AccessInfo {
public const DEFAULT_LIFETIME = 3600; public const DEFAULT_LIFETIME = 3600;
@ -16,7 +16,7 @@ class OAuth2AccessInfo {
private int $expires private int $expires
) {} ) {}
public static function fromResult(IDbResult $result): OAuth2AccessInfo { public static function fromResult(DbResult $result): OAuth2AccessInfo {
return new OAuth2AccessInfo( return new OAuth2AccessInfo(
id: $result->getString(0), id: $result->getString(0),
appId: $result->getString(1), appId: $result->getString(1),

View file

@ -2,10 +2,11 @@
namespace Hanyuu\OAuth2; namespace Hanyuu\OAuth2;
use RuntimeException; use RuntimeException;
use Index\Http\Routing\{HttpGet,HttpOptions,HttpPost,RouteHandler}; use Index\Http\Routing\{HttpGet,HttpOptions,HttpPost,RouteHandler,RouteHandlerTrait};
use Syokuhou\IConfig;
final class OAuth2ApiRoutes implements RouteHandler {
use RouteHandlerTrait;
final class OAuth2ApiRoutes extends RouteHandler {
public function __construct( public function __construct(
private OAuth2Context $oauth2Ctx private OAuth2Context $oauth2Ctx
) {} ) {}

View file

@ -4,16 +4,15 @@ namespace Hanyuu\OAuth2;
use InvalidArgumentException; use InvalidArgumentException;
use RuntimeException; use RuntimeException;
use Index\XString; use Index\XString;
use Index\Data\DbStatementCache; use Index\Db\{DbConnection,DbStatementCache};
use Index\Data\IDbConnection; use Hanyuu\Apps\{AppInfo,AppUriInfo};
use Hanyuu\Apps\AppInfo;
use Hanyuu\Apps\AppUriInfo;
class OAuth2AuthorisationData { class OAuth2AuthorisationData {
private IDbConnection $dbConn;
private DbStatementCache $cache; private DbStatementCache $cache;
public function __construct(IDbConnection $dbConn) { public function __construct(
private DbConnection $dbConn
) {
$this->dbConn = $dbConn; $this->dbConn = $dbConn;
$this->cache = new DbStatementCache($dbConn); $this->cache = new DbStatementCache($dbConn);
} }

View file

@ -2,7 +2,7 @@
namespace Hanyuu\OAuth2; namespace Hanyuu\OAuth2;
use Index\UriBase64; use Index\UriBase64;
use Index\Data\IDbResult; use Index\Db\DbResult;
class OAuth2AuthorisationInfo { class OAuth2AuthorisationInfo {
public function __construct( public function __construct(
@ -18,7 +18,7 @@ class OAuth2AuthorisationInfo {
private int $expires private int $expires
) {} ) {}
public static function fromResult(IDbResult $result): OAuth2AuthorisationInfo { public static function fromResult(DbResult $result): OAuth2AuthorisationInfo {
return new OAuth2AuthorisationInfo( return new OAuth2AuthorisationInfo(
id: $result->getString(0), id: $result->getString(0),
appId: $result->getString(1), appId: $result->getString(1),

View file

@ -3,8 +3,8 @@ namespace Hanyuu\OAuth2;
use RuntimeException; use RuntimeException;
use Hanyuu\Apps\{AppsContext,AppInfo}; use Hanyuu\Apps\{AppsContext,AppInfo};
use Index\Data\IDbConnection; use Index\Config\Config;
use Syokuhou\IConfig; use Index\Db\DbConnection;
class OAuth2Context { class OAuth2Context {
private OAuth2AuthorisationData $authorisations; private OAuth2AuthorisationData $authorisations;
@ -12,8 +12,8 @@ class OAuth2Context {
private OAuth2DevicesData $devices; private OAuth2DevicesData $devices;
public function __construct( public function __construct(
private IConfig $config, private Config $config,
IDbConnection $dbConn, DbConnection $dbConn,
private AppsContext $appsCtx private AppsContext $appsCtx
) { ) {
$this->authorisations = new OAuth2AuthorisationData($dbConn); $this->authorisations = new OAuth2AuthorisationData($dbConn);
@ -21,7 +21,7 @@ class OAuth2Context {
$this->devices = new OAuth2DevicesData($dbConn); $this->devices = new OAuth2DevicesData($dbConn);
} }
public function getConfig(): IConfig { public function getConfig(): Config {
return $this->config; return $this->config;
} }

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Hanyuu\OAuth2; namespace Hanyuu\OAuth2;
use Index\Data\IDbResult; use Index\Db\DbResult;
class OAuth2DeviceInfo { class OAuth2DeviceInfo {
public const DEFAULT_LIFETIME = 600; public const DEFAULT_LIFETIME = 600;
@ -21,7 +21,7 @@ class OAuth2DeviceInfo {
private int $expires private int $expires
) {} ) {}
public static function fromResult(IDbResult $result): OAuth2DeviceInfo { public static function fromResult(DbResult $result): OAuth2DeviceInfo {
return new OAuth2DeviceInfo( return new OAuth2DeviceInfo(
id: $result->getString(0), id: $result->getString(0),
appId: $result->getString(1), appId: $result->getString(1),

View file

@ -4,8 +4,7 @@ namespace Hanyuu\OAuth2;
use InvalidArgumentException; use InvalidArgumentException;
use RuntimeException; use RuntimeException;
use Index\XString; use Index\XString;
use Index\Data\DbStatementCache; use Index\Db\{DbConnection,DbStatementCache};
use Index\Data\IDbConnection;
use Hanyuu\Apps\AppInfo; use Hanyuu\Apps\AppInfo;
class OAuth2DevicesData { class OAuth2DevicesData {
@ -14,7 +13,7 @@ class OAuth2DevicesData {
private DbStatementCache $cache; private DbStatementCache $cache;
public function __construct( public function __construct(
private IDbConnection $dbConn private DbConnection $dbConn
) { ) {
$this->cache = new DbStatementCache($dbConn); $this->cache = new DbStatementCache($dbConn);
} }

View file

@ -1,7 +1,7 @@
<?php <?php
namespace Hanyuu\OAuth2; namespace Hanyuu\OAuth2;
use Index\Data\IDbResult; use Index\Db\DbResult;
class OAuth2RefreshInfo { class OAuth2RefreshInfo {
public function __construct( public function __construct(
@ -15,7 +15,7 @@ class OAuth2RefreshInfo {
private int $expires private int $expires
) {} ) {}
public static function fromResult(IDbResult $result): OAuth2RefreshInfo { public static function fromResult(DbResult $result): OAuth2RefreshInfo {
return new OAuth2RefreshInfo( return new OAuth2RefreshInfo(
id: $result->getString(0), id: $result->getString(0),
appId: $result->getString(1), appId: $result->getString(1),

View file

@ -3,10 +3,11 @@ namespace Hanyuu\OAuth2;
use RuntimeException; use RuntimeException;
use Hanyuu\Apps\AppsContext; use Hanyuu\Apps\AppsContext;
use Aiwass\Server\{RpcActionHandler,RpcProcedure}; use RPCii\Server\{RpcHandler,RpcHandlerCommon,RpcAction};
use Syokuhou\IConfig;
final class OAuth2RpcHandler implements RpcHandler {
use RpcHandlerCommon;
final class OAuth2RpcActions extends RpcActionHandler {
public function __construct( public function __construct(
private OAuth2Context $oauth2Ctx private OAuth2Context $oauth2Ctx
) {} ) {}
@ -26,7 +27,7 @@ final class OAuth2RpcActions extends RpcActionHandler {
return $scopes; return $scopes;
} }
#[RpcProcedure('hanyuu:oauth2:attemptAppAuth')] #[RpcAction('hanyuu:oauth2:attemptAppAuth')]
public function procAttemptAppAuth(string $remoteAddr, string $clientId, string $clientSecret = ''): array { public function procAttemptAppAuth(string $remoteAddr, string $clientId, string $clientSecret = ''): array {
try { try {
$appInfo = $this->oauth2Ctx->getApps()->getAppsData()->getAppInfo(clientId: $clientId, deleted: false); $appInfo = $this->oauth2Ctx->getApps()->getAppsData()->getAppInfo(clientId: $clientId, deleted: false);
@ -52,7 +53,7 @@ final class OAuth2RpcActions extends RpcActionHandler {
]; ];
} }
#[RpcProcedure('hanyuu:oauth2:attemptBearerAuth')] #[RpcAction('hanyuu:oauth2:attemptBearerAuth')]
public function procAttemptBearerAuth(string $remoteAddr, string $token): array { public function procAttemptBearerAuth(string $remoteAddr, string $token): array {
try { try {
$tokenInfo = $this->oauth2Ctx->getTokensData()->getAccessInfo($token, OAuth2TokensData::ACCESS_BY_TOKEN); $tokenInfo = $this->oauth2Ctx->getTokensData()->getAccessInfo($token, OAuth2TokensData::ACCESS_BY_TOKEN);
@ -73,7 +74,7 @@ final class OAuth2RpcActions extends RpcActionHandler {
]; ];
} }
#[RpcProcedure('hanyuu:oauth2:createAuthoriseRequest')] #[RpcAction('hanyuu:oauth2:createAuthoriseRequest')]
public function procCreateAuthoriseRequest(string $appId, ?string $scope = null): array { public function procCreateAuthoriseRequest(string $appId, ?string $scope = null): array {
try { try {
$appInfo = $this->oauth2Ctx->getApps()->getAppsData()->getAppInfo(appId: $appId, deleted: false); $appInfo = $this->oauth2Ctx->getApps()->getAppsData()->getAppInfo(appId: $appId, deleted: false);
@ -87,7 +88,7 @@ final class OAuth2RpcActions extends RpcActionHandler {
return $this->oauth2Ctx->createDeviceAuthorisationRequest($appInfo, $scope); return $this->oauth2Ctx->createDeviceAuthorisationRequest($appInfo, $scope);
} }
#[RpcProcedure('hanyuu:oauth2:createBearerToken:authorisationCode')] #[RpcAction('hanyuu:oauth2:createBearerToken:authorisationCode')]
public function procCreateBearerTokenAuthzCode(string $appId, bool $isAuthed, string $code, string $codeVerifier): array { public function procCreateBearerTokenAuthzCode(string $appId, bool $isAuthed, string $code, string $codeVerifier): array {
try { try {
$appInfo = $this->oauth2Ctx->getApps()->getAppsData()->getAppInfo(appId: $appId, deleted: false); $appInfo = $this->oauth2Ctx->getApps()->getAppsData()->getAppInfo(appId: $appId, deleted: false);
@ -101,7 +102,7 @@ final class OAuth2RpcActions extends RpcActionHandler {
return $this->oauth2Ctx->redeemAuthorisationCode($appInfo, $isAuthed, $code, $codeVerifier); return $this->oauth2Ctx->redeemAuthorisationCode($appInfo, $isAuthed, $code, $codeVerifier);
} }
#[RpcProcedure('hanyuu:oauth2:createBearerToken:refreshToken')] #[RpcAction('hanyuu:oauth2:createBearerToken:refreshToken')]
public function procCreateBearerTokenRefreshToken(string $appId, bool $isAuthed, string $refreshToken, ?string $scope = null): array { public function procCreateBearerTokenRefreshToken(string $appId, bool $isAuthed, string $refreshToken, ?string $scope = null): array {
try { try {
$appInfo = $this->oauth2Ctx->getApps()->getAppsData()->getAppInfo(appId: $appId, deleted: false); $appInfo = $this->oauth2Ctx->getApps()->getAppsData()->getAppInfo(appId: $appId, deleted: false);
@ -115,7 +116,7 @@ final class OAuth2RpcActions extends RpcActionHandler {
return $this->oauth2Ctx->redeemRefreshToken($appInfo, $isAuthed, $refreshToken, $scope); return $this->oauth2Ctx->redeemRefreshToken($appInfo, $isAuthed, $refreshToken, $scope);
} }
#[RpcProcedure('hanyuu:oauth2:createBearerToken:clientCredentials')] #[RpcAction('hanyuu:oauth2:createBearerToken:clientCredentials')]
public function procCreateBearerTokenClientCreds(string $appId, bool $isAuthed, ?string $scope = null): array { public function procCreateBearerTokenClientCreds(string $appId, bool $isAuthed, ?string $scope = null): array {
try { try {
$appInfo = $this->oauth2Ctx->getApps()->getAppsData()->getAppInfo(appId: $appId, deleted: false); $appInfo = $this->oauth2Ctx->getApps()->getAppsData()->getAppInfo(appId: $appId, deleted: false);
@ -129,7 +130,7 @@ final class OAuth2RpcActions extends RpcActionHandler {
return $this->oauth2Ctx->redeemClientCredentials($appInfo, $isAuthed, $scope); return $this->oauth2Ctx->redeemClientCredentials($appInfo, $isAuthed, $scope);
} }
#[RpcProcedure('hanyuu:oauth2:createBearerToken:deviceCode')] #[RpcAction('hanyuu:oauth2:createBearerToken:deviceCode')]
public function procCreateBearerTokenDeviceCode(string $appId, bool $isAuthed, string $deviceCode): array { public function procCreateBearerTokenDeviceCode(string $appId, bool $isAuthed, string $deviceCode): array {
try { try {
$appInfo = $this->oauth2Ctx->getApps()->getAppsData()->getAppInfo(appId: $appId, deleted: false); $appInfo = $this->oauth2Ctx->getApps()->getAppsData()->getAppInfo(appId: $appId, deleted: false);

View file

@ -4,15 +4,14 @@ namespace Hanyuu\OAuth2;
use InvalidArgumentException; use InvalidArgumentException;
use RuntimeException; use RuntimeException;
use Index\XString; use Index\XString;
use Index\Data\DbStatementCache; use Index\Db\{DbConnection,DbStatementCache};
use Index\Data\IDbConnection;
use Hanyuu\Apps\AppInfo; use Hanyuu\Apps\AppInfo;
class OAuth2TokensData { class OAuth2TokensData {
private DbStatementCache $cache; private DbStatementCache $cache;
public function __construct( public function __construct(
private IDbConnection $dbConn private DbConnection $dbConn
) { ) {
$this->cache = new DbStatementCache($dbConn); $this->cache = new DbStatementCache($dbConn);
} }

View file

@ -3,22 +3,23 @@ namespace Hanyuu\OAuth2;
use InvalidArgumentException; use InvalidArgumentException;
use RuntimeException; use RuntimeException;
use Index\CSRFP; use Index\CsrfToken;
use Index\Http\Routing\{HttpGet,HttpPost,RouteHandler}; use Index\Http\Routing\{HttpGet,HttpPost,RouteHandler,RouteHandlerTrait};
use Sasae\SasaeEnvironment; use Index\Templating\TplEnvironment;
use Syokuhou\IConfig;
final class OAuth2WebRoutes implements RouteHandler {
use RouteHandlerTrait;
final class OAuth2WebRoutes extends RouteHandler {
public function __construct( public function __construct(
private OAuth2Context $oauth2Ctx, private OAuth2Context $oauth2Ctx,
private SasaeEnvironment $templating, private TplEnvironment $templating,
private $getAuthInfo, private $getAuthInfo,
private $getCSRFPSecret private $getCsrfTokenSecret
) { ) {
if(!is_callable($getAuthInfo)) if(!is_callable($getAuthInfo))
throw new InvalidArgumentException('$getAuthInfo must be callable'); throw new InvalidArgumentException('$getAuthInfo must be callable');
if(!is_callable($getCSRFPSecret)) if(!is_callable($getCsrfTokenSecret))
throw new InvalidArgumentException('$getCSRFPSecret must be callable'); throw new InvalidArgumentException('$getCsrfTokenSecret must be callable');
} }
#[HttpGet('/oauth2/authorise')] #[HttpGet('/oauth2/authorise')]
@ -30,7 +31,7 @@ final class OAuth2WebRoutes extends RouteHandler {
'register_url' => $authInfo->getRegisterUrl(), 'register_url' => $authInfo->getRegisterUrl(),
]); ]);
$csrfp = new CSRFP(($this->getCSRFPSecret)(), $authInfo->getSessionInfo()->getToken()); $csrfp = new CsrfToken(($this->getCsrfTokenSecret)(), $authInfo->getSessionInfo()->getToken());
return $this->templating->render('oauth2/authorise', [ return $this->templating->render('oauth2/authorise', [
'csrfp_token' => $csrfp->createToken(), 'csrfp_token' => $csrfp->createToken(),
@ -50,7 +51,7 @@ final class OAuth2WebRoutes extends RouteHandler {
$content = $request->getContent(); $content = $request->getContent();
$csrfp = new CSRFP(($this->getCSRFPSecret)(), $authInfo->getSessionInfo()->getToken()); $csrfp = new CsrfToken(($this->getCsrfTokenSecret)(), $authInfo->getSessionInfo()->getToken());
if(!$csrfp->verifyToken((string)$content->getParam('_csrfp'))) if(!$csrfp->verifyToken((string)$content->getParam('_csrfp')))
return ['error' => 'csrf']; return ['error' => 'csrf'];
@ -137,7 +138,7 @@ final class OAuth2WebRoutes extends RouteHandler {
return ['error' => 'auth']; return ['error' => 'auth'];
$sessionInfo = $authInfo->getSessionInfo(); $sessionInfo = $authInfo->getSessionInfo();
$csrfp = new CSRFP(($this->getCSRFPSecret)(), $sessionInfo->getToken()); $csrfp = new CsrfToken(($this->getCsrfTokenSecret)(), $sessionInfo->getToken());
if(!$csrfp->verifyToken((string)$request->getParam('csrfp'))) if(!$csrfp->verifyToken((string)$request->getParam('csrfp')))
return ['error' => 'csrf']; return ['error' => 'csrf'];
@ -212,7 +213,7 @@ final class OAuth2WebRoutes extends RouteHandler {
'register_url' => $authInfo->getRegisterUrl(), 'register_url' => $authInfo->getRegisterUrl(),
]); ]);
$csrfp = new CSRFP(($this->getCSRFPSecret)(), $authInfo->getSessionInfo()->getToken()); $csrfp = new CsrfToken(($this->getCsrfTokenSecret)(), $authInfo->getSessionInfo()->getToken());
return $this->templating->render('oauth2/verify', [ return $this->templating->render('oauth2/verify', [
'csrfp_token' => $csrfp->createToken(), 'csrfp_token' => $csrfp->createToken(),
@ -232,7 +233,7 @@ final class OAuth2WebRoutes extends RouteHandler {
$content = $request->getContent(); $content = $request->getContent();
$csrfp = new CSRFP(($this->getCSRFPSecret)(), $authInfo->getSessionInfo()->getToken()); $csrfp = new CsrfToken(($this->getCsrfTokenSecret)(), $authInfo->getSessionInfo()->getToken());
if(!$csrfp->verifyToken((string)$content->getParam('_csrfp'))) if(!$csrfp->verifyToken((string)$content->getParam('_csrfp')))
return ['error' => 'csrf']; return ['error' => 'csrf'];
@ -294,7 +295,7 @@ final class OAuth2WebRoutes extends RouteHandler {
if($authInfo->isFailure()) if($authInfo->isFailure())
return ['error' => 'auth']; return ['error' => 'auth'];
$csrfp = new CSRFP(($this->getCSRFPSecret)(), $authInfo->getSessionInfo()->getToken()); $csrfp = new CsrfToken(($this->getCsrfTokenSecret)(), $authInfo->getSessionInfo()->getToken());
if(!$csrfp->verifyToken((string)$request->getParam('csrfp'))) if(!$csrfp->verifyToken((string)$request->getParam('csrfp')))
return ['error' => 'csrf']; return ['error' => 'csrf'];

View file

@ -1,24 +1,24 @@
<?php <?php
namespace Hanyuu; namespace Hanyuu;
use Index\Http\Routing\{HttpRouter,IRouter,IRouteHandler}; use Index\Http\Routing\{HttpRouter,Router,RouteHandler};
use Sasae\SasaeEnvironment; use Index\Templating\TplEnvironment;
class RoutingContext { class RoutingContext {
private HttpRouter $router; private HttpRouter $router;
private SasaeEnvironment $templating; private TplEnvironment $templating;
public function __construct(SasaeEnvironment $templating) { public function __construct(TplEnvironment $templating) {
$this->templating = $templating; $this->templating = $templating;
$this->router = new HttpRouter(errorHandler: new RoutingErrorHandler); $this->router = new HttpRouter(errorHandler: new RoutingErrorHandler);
$this->router->use('/', fn($resp) => $resp->setPoweredBy('Hanyuu')); $this->router->use('/', fn($resp) => $resp->setPoweredBy('Hanyuu'));
} }
public function getRouter(): IRouter { public function getRouter(): Router {
return $this->router; return $this->router;
} }
public function register(IRouteHandler $handler): void { public function register(RouteHandler $handler): void {
$this->router->register($handler); $this->router->register($handler);
} }

View file

@ -1,10 +1,9 @@
<?php <?php
namespace Hanyuu; namespace Hanyuu;
use Index\Http\{HttpResponseBuilder,HttpRequest}; use Index\Http\{HtmlHttpErrorHandler,HttpResponseBuilder,HttpRequest};
use Index\Http\ErrorHandling\HtmlErrorHandler;
class RoutingErrorHandler extends HtmlErrorHandler { class RoutingErrorHandler extends HtmlHttpErrorHandler {
#[\Override] #[\Override]
public function handle(HttpResponseBuilder $response, HttpRequest $request, int $code, string $message): void { public function handle(HttpResponseBuilder $response, HttpRequest $request, int $code, string $message): void {
$path = HAU_DIR_PUBLIC . sprintf('/error-%03d.html', $code); $path = HAU_DIR_PUBLIC . sprintf('/error-%03d.html', $code);

View file

@ -1,11 +1,11 @@
<?php <?php
namespace Hanyuu; namespace Hanyuu;
use Syokuhou\IConfig; use Index\Config\Config;
class SiteInfo { class SiteInfo {
public function __construct( public function __construct(
private IConfig $config private Config $config
) {} ) {}
public function getName(): string { public function getName(): string {

View file

@ -4,7 +4,7 @@ namespace Hanyuu;
use Twig\Extension\AbstractExtension; use Twig\Extension\AbstractExtension;
use Twig\TwigFunction; use Twig\TwigFunction;
final class HanyuuSasaeExtension extends AbstractExtension { final class TemplatingExtension extends AbstractExtension {
private HanyuuContext $ctx; private HanyuuContext $ctx;
private ?object $assets; private ?object $assets;

View file

@ -1,6 +1,6 @@
#!/usr/bin/env php #!/usr/bin/env php
<?php <?php
use Index\Data\Migration\FsDbMigrationRepo; use Index\Db\Migration\FsDbMigrationRepo;
require_once __DIR__ . '/../hanyuu.php'; require_once __DIR__ . '/../hanyuu.php';

View file

@ -1,6 +1,6 @@
#!/usr/bin/env php #!/usr/bin/env php
<?php <?php
use Index\Data\Migration\FsDbMigrationRepo; use Index\Db\Migration\FsDbMigrationRepo;
require_once __DIR__ . '/../hanyuu.php'; require_once __DIR__ . '/../hanyuu.php';