Added Apps and OAuth2 migrations.

This commit is contained in:
flash 2024-07-20 19:17:56 +00:00
parent ce57936c3a
commit ff4b1ba4e2
2 changed files with 165 additions and 0 deletions

View file

@ -0,0 +1,46 @@
<?php
use Index\Data\IDbConnection;
use Index\Data\Migration\IDbMigration;
final class AppsTables_20240720_185612 implements IDbMigration {
public function migrate(IDbConnection $conn): void {
$conn->execute(<<<SQL
CREATE TABLE hau_apps (
app_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
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_trusted TINYINT(3) UNSIGNED NOT NULL DEFAULT '0',
app_type ENUM('public','confidential') NOT NULL COLLATE 'ascii_general_ci',
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 hau_app_client_id_unique (app_client_id),
UNIQUE INDEX hau_app_name_unique (app_name),
INDEX hau_app_created_index (app_created),
INDEX hau_app_deleted_index (app_deleted)
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
SQL);
$conn->execute(<<<SQL
CREATE TABLE hau_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 hau_apps_uris_app_id_foreign (app_id),
INDEX hau_apps_uris_lookup_index (uri_id, uri_string),
INDEX hau_apps_uri_created_index (uri_created),
CONSTRAINT hau_apps_uris_app_id_foreign
FOREIGN KEY (app_id)
REFERENCES hau_apps (app_id)
ON UPDATE CASCADE
ON DELETE CASCADE
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
SQL);
}
}

View file

@ -0,0 +1,119 @@
<?php
use Index\Data\IDbConnection;
use Index\Data\Migration\IDbMigration;
final class OauthTables_20240720_185921 implements IDbMigration {
public function migrate(IDbConnection $conn): void {
$conn->execute(<<<SQL
CREATE TABLE hau_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_state VARBINARY(255) 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_approval ENUM('pending','approved','denied') NOT NULL DEFAULT 'pending' COLLATE 'ascii_general_ci',
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 hau_oauth2_authorise_code_unique (auth_code),
INDEX hau_oauth2_authorise_app_foreign (app_id),
INDEX hau_oauth2_authorise_uri_foreign (uri_id),
INDEX hau_oauth2_authorise_user_index (user_id),
INDEX hau_oauth2_authorise_expires_index (auth_expires),
CONSTRAINT hau_oauth2_authorise_app_foreign
FOREIGN KEY (app_id)
REFERENCES hau_apps (app_id)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT hau_oauth2_authorise_uri_foreign
FOREIGN KEY (uri_id)
REFERENCES hau_apps_uris (uri_id)
ON UPDATE CASCADE
ON DELETE CASCADE
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
SQL);
$conn->execute(<<<SQL
CREATE TABLE hau_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(8) NOT NULL COLLATE 'ascii_general_ci',
dev_attempts TINYINT(3) UNSIGNED NOT NULL DEFAULT '5',
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 hau_oauth2_device_user_code_unique (dev_user_code),
UNIQUE INDEX hau_oauth2_device_code_unique (dev_code),
INDEX hau_oauth2_device_expires_index (dev_expires),
INDEX hau_oauth2_device_app_foreign (app_id),
INDEX hau_oauth2_device_user_index (user_id),
CONSTRAINT hau_oauth2_device_app_foreign
FOREIGN KEY (app_id)
REFERENCES hau_apps (app_id)
ON UPDATE CASCADE
ON DELETE CASCADE
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
SQL);
$conn->execute(<<<SQL
CREATE TABLE hau_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 hau_oauth2_access_token_unique (acc_token),
INDEX hau_oauth2_access_user_index (user_id),
INDEX hau_oauth2_access_app_foreign (app_id),
INDEX hau_oauth2_access_expires_index (acc_expires),
CONSTRAINT hau_oauth2_access_app_foreign
FOREIGN KEY (app_id)
REFERENCES hau_apps (app_id)
ON UPDATE CASCADE
ON DELETE CASCADE
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
SQL);
$conn->execute(<<<SQL
CREATE TABLE hau_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 hau_oauth2_refresh_token_unique (ref_token),
UNIQUE INDEX hau_oauth2_refresh_access_foreign (acc_id),
INDEX hau_oauth2_refresh_expires_index (ref_expires),
INDEX hau_oauth2_refresh_app_foreign (app_id),
INDEX hau_oauth2_refresh_user_index (user_id),
CONSTRAINT hau_oauth2_refresh_access_foreign
FOREIGN KEY (acc_id)
REFERENCES hau_oauth2_access (acc_id)
ON UPDATE CASCADE
ON DELETE SET NULL,
CONSTRAINT hau_oauth2_refresh_app_foreign
FOREIGN KEY (app_id)
REFERENCES hau_apps (app_id)
ON UPDATE CASCADE
ON DELETE CASCADE
) COLLATE='utf8mb4_bin' ENGINE=InnoDB;
SQL);
}
}