Merged OAuth2 handling into Misuzu.

This commit is contained in:
flash 2025-02-02 02:09:56 +00:00
parent 1994a9892d
commit 534e947522
115 changed files with 4556 additions and 77 deletions

View file

@ -0,0 +1,54 @@
<?php
use Index\Db\DbConnection;
use Index\Db\Migration\DbMigration;
final class CreateAppsTables_20250201_181944 implements DbMigration {
public function migrate(DbConnection $conn): void {
$conn->execute(<<<SQL
CREATE TABLE msz_apps (
app_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
user_id INT(10) UNSIGNED NULL DEFAULT NULL,
app_name VARCHAR(64) NOT NULL COLLATE 'utf8mb4_unicode_520_ci',
app_summary VARCHAR(255) NOT NULL COLLATE 'utf8mb4_bin',
app_website VARCHAR(255) NOT NULL COLLATE 'utf8mb4_bin',
app_type ENUM('public','confidential','trusted') NOT NULL COLLATE 'ascii_general_ci',
app_access_lifetime INT(10) UNSIGNED NULL DEFAULT NULL,
app_refresh_lifetime INT(10) UNSIGNED NULL DEFAULT NULL,
app_client_id CHAR(20) NOT NULL COLLATE 'ascii_bin',
app_client_secret VARCHAR(255) NOT NULL COLLATE 'ascii_bin',
app_created TIMESTAMP NOT NULL DEFAULT current_timestamp(),
app_updated TIMESTAMP NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
app_deleted TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (app_id),
UNIQUE INDEX apps_client_id_unique (app_client_id),
UNIQUE INDEX apps_name_unique (app_name),
INDEX apps_user_foreign (user_id),
INDEX apps_created_index (app_created),
INDEX apps_deleted_index (app_deleted),
CONSTRAINT apps_user_foreign
FOREIGN KEY (user_id)
REFERENCES msz_users (user_id)
ON UPDATE CASCADE
ON DELETE CASCADE
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
SQL);
$conn->execute(<<<SQL
CREATE TABLE msz_apps_uris (
uri_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
app_id INT(10) UNSIGNED NOT NULL,
uri_string VARCHAR(255) NOT NULL COLLATE 'ascii_bin',
uri_created TIMESTAMP NOT NULL DEFAULT current_timestamp(),
PRIMARY KEY (uri_id),
INDEX apps_uris_app_foreign (app_id),
INDEX apps_uris_lookup_index (uri_id, uri_string),
INDEX apps_uri_created_index (uri_created),
CONSTRAINT apps_uris_app_foreign
FOREIGN KEY (app_id)
REFERENCES msz_apps (app_id)
ON UPDATE CASCADE
ON DELETE CASCADE
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
SQL);
}
}

View file

@ -0,0 +1,43 @@
<?php
use Index\Db\DbConnection;
use Index\Db\Migration\DbMigration;
final class CreateScopesTables_20250201_182753 implements DbMigration {
public function migrate(DbConnection $conn): void {
$conn->execute(<<<SQL
CREATE TABLE msz_scopes (
scope_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
scope_string VARCHAR(50) NOT NULL COLLATE 'ascii_bin',
scope_restricted TINYINT(3) UNSIGNED NOT NULL,
scope_summary VARCHAR(255) NOT NULL DEFAULT '' COLLATE 'utf8mb4_unicode_520_ci',
scope_created TIMESTAMP NOT NULL DEFAULT current_timestamp(),
scope_deprecated TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (scope_id),
UNIQUE INDEX scopes_string_unique (scope_string),
INDEX scopes_created_index (scope_created),
INDEX scopes_deprecated_index (scope_deprecated)
) COLLATE=utf8mb4_bin ENGINE=InnoDB;
SQL);
$conn->execute(<<<SQL
CREATE TABLE msz_apps_scopes (
app_id INT(10) UNSIGNED NOT NULL,
scope_id INT(10) UNSIGNED NOT NULL,
scope_allowed TINYINT(3) UNSIGNED NOT NULL,
PRIMARY KEY (app_id, scope_id),
INDEX apps_scopes_app_foreign (app_id),
INDEX apps_scopes_scope_foreign (scope_id),
CONSTRAINT apps_scopes_app_foreign
FOREIGN KEY (app_id)
REFERENCES msz_apps (app_id)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT apps_scopes_scope_foreign
FOREIGN KEY (scope_id)
REFERENCES msz_scopes (scope_id)
ON UPDATE CASCADE
ON DELETE CASCADE
) COLLATE=utf8mb4_bin ENGINE=InnoDB;
SQL);
}
}

View file

@ -0,0 +1,136 @@
<?php
use Index\Db\DbConnection;
use Index\Db\Migration\DbMigration;
final class CreateOauthTables_20250201_183150 implements DbMigration {
public function migrate(DbConnection $conn): void {
$conn->execute(<<<SQL
CREATE TABLE msz_oauth2_authorise (
auth_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
app_id INT(10) UNSIGNED NOT NULL,
user_id INT(10) UNSIGNED NOT NULL,
uri_id INT(10) UNSIGNED NOT NULL,
auth_challenge_code VARCHAR(128) NOT NULL COLLATE 'ascii_bin',
auth_challenge_method ENUM('plain','S256') NOT NULL DEFAULT 'plain' COLLATE 'ascii_bin',
auth_scope TEXT NOT NULL COLLATE 'ascii_bin',
auth_code CHAR(60) NOT NULL COLLATE 'ascii_bin',
auth_created TIMESTAMP NOT NULL DEFAULT current_timestamp(),
auth_expires TIMESTAMP NOT NULL DEFAULT (current_timestamp() + interval 10 minute),
PRIMARY KEY (auth_id),
UNIQUE INDEX oauth2_authorise_code_unique (auth_code),
INDEX oauth2_authorise_app_foreign (app_id),
INDEX oauth2_authorise_uri_foreign (uri_id),
INDEX oauth2_authorise_user_foreign (user_id),
INDEX oauth2_authorise_expires_index (auth_expires),
CONSTRAINT oauth2_authorise_app_foreign
FOREIGN KEY (app_id)
REFERENCES msz_apps (app_id)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT oauth2_authorise_uri_foreign
FOREIGN KEY (uri_id)
REFERENCES msz_apps_uris (uri_id)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT oauth2_authorise_user_foreign
FOREIGN KEY (user_id)
REFERENCES msz_users (user_id)
ON UPDATE CASCADE
ON DELETE CASCADE
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
SQL);
$conn->execute(<<<SQL
CREATE TABLE msz_oauth2_device (
dev_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
app_id INT(10) UNSIGNED NOT NULL,
user_id INT(10) UNSIGNED NULL DEFAULT NULL,
dev_code CHAR(60) NOT NULL COLLATE 'ascii_bin',
dev_user_code CHAR(9) NOT NULL COLLATE 'ascii_general_ci',
dev_interval TINYINT(3) UNSIGNED NOT NULL DEFAULT '5',
dev_polled TIMESTAMP NOT NULL DEFAULT current_timestamp(),
dev_scope TEXT NOT NULL COLLATE 'ascii_bin',
dev_approval ENUM('pending','approved','denied') NOT NULL DEFAULT 'pending' COLLATE 'ascii_general_ci',
dev_created TIMESTAMP NOT NULL DEFAULT current_timestamp(),
dev_expires TIMESTAMP NOT NULL DEFAULT (current_timestamp() + interval 10 minute),
PRIMARY KEY (dev_id),
UNIQUE INDEX oauth2_device_user_code_unique (dev_user_code),
UNIQUE INDEX oauth2_device_code_unique (dev_code),
INDEX oauth2_device_expires_index (dev_expires),
INDEX oauth2_device_app_foreign (app_id),
INDEX oauth2_device_user_foreign (user_id),
CONSTRAINT oauth2_device_app_foreign
FOREIGN KEY (app_id)
REFERENCES msz_apps (app_id)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT oauth2_device_user_foreign
FOREIGN KEY (user_id)
REFERENCES msz_users (user_id)
ON UPDATE CASCADE
ON DELETE CASCADE
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
SQL);
$conn->execute(<<<SQL
CREATE TABLE msz_oauth2_access (
acc_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
app_id INT(10) UNSIGNED NOT NULL,
user_id INT(10) UNSIGNED NULL DEFAULT NULL,
acc_token VARCHAR(255) NOT NULL COLLATE 'ascii_bin',
acc_scope TEXT NOT NULL COLLATE 'ascii_bin',
acc_created TIMESTAMP NOT NULL DEFAULT current_timestamp(),
acc_expires TIMESTAMP NOT NULL DEFAULT (current_timestamp() + interval 1 hour),
PRIMARY KEY (acc_id),
UNIQUE INDEX oauth2_access_token_unique (acc_token),
INDEX oauth2_access_user_foreign (user_id),
INDEX oauth2_access_app_foreign (app_id),
INDEX oauth2_access_expires_index (acc_expires),
CONSTRAINT oauth2_access_app_foreign
FOREIGN KEY (app_id)
REFERENCES msz_apps (app_id)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT oauth2_access_user_foreign
FOREIGN KEY (user_id)
REFERENCES msz_users (user_id)
ON UPDATE CASCADE
ON DELETE CASCADE
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
SQL);
$conn->execute(<<<SQL
CREATE TABLE msz_oauth2_refresh (
ref_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
app_id INT(10) UNSIGNED NOT NULL,
user_id INT(10) UNSIGNED NULL DEFAULT NULL,
acc_id INT(10) UNSIGNED NULL DEFAULT NULL,
ref_token VARCHAR(255) NOT NULL COLLATE 'ascii_bin',
ref_scope TEXT NOT NULL COLLATE 'ascii_bin',
ref_created TIMESTAMP NOT NULL DEFAULT current_timestamp(),
ref_expires TIMESTAMP NOT NULL DEFAULT (current_timestamp() + interval 1 month),
PRIMARY KEY (ref_id),
UNIQUE INDEX oauth2_refresh_token_unique (ref_token),
UNIQUE INDEX oauth2_refresh_access_foreign (acc_id),
INDEX oauth2_refresh_expires_index (ref_expires),
INDEX oauth2_refresh_app_foreign (app_id),
INDEX oauth2_refresh_user_foreign (user_id),
CONSTRAINT oauth2_refresh_access_foreign
FOREIGN KEY (acc_id)
REFERENCES msz_oauth2_access (acc_id)
ON UPDATE CASCADE
ON DELETE SET NULL,
CONSTRAINT oauth2_refresh_app_foreign
FOREIGN KEY (app_id)
REFERENCES msz_apps (app_id)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT oauth2_refresh_user_foreign
FOREIGN KEY (user_id)
REFERENCES msz_users (user_id)
ON UPDATE CASCADE
ON DELETE CASCADE
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
SQL);
}
}