From 0d16e11871238668ccfec8998011d1af03b09246 Mon Sep 17 00:00:00 2001 From: FoskyM Date: Thu, 28 Sep 2023 22:45:28 +0800 Subject: [PATCH] chore: create migrations & access token storage --- composer.json | 2 +- ...09_28_create_oauth_access_tokens_table.php | 23 ++++++++++ ...create_oauth_authorization_codes_table.php | 25 +++++++++++ .../2023_09_28_create_oauth_clients_table.php | 24 ++++++++++ .../2023_09_28_create_oauth_jwt_table.php | 21 +++++++++ ...9_28_create_oauth_refresh_tokens_table.php | 23 ++++++++++ .../2023_09_28_create_oauth_scopes_table.php | 20 +++++++++ src/Models/AccessToken.php | 10 +++++ src/Storage.php | 45 +++++++++++++++++++ 9 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 migrations/2023_09_28_create_oauth_access_tokens_table.php create mode 100644 migrations/2023_09_28_create_oauth_authorization_codes_table.php create mode 100644 migrations/2023_09_28_create_oauth_clients_table.php create mode 100644 migrations/2023_09_28_create_oauth_jwt_table.php create mode 100644 migrations/2023_09_28_create_oauth_refresh_tokens_table.php create mode 100644 migrations/2023_09_28_create_oauth_scopes_table.php create mode 100644 src/Models/AccessToken.php create mode 100644 src/Storage.php diff --git a/composer.json b/composer.json index fcd1637..5951a76 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "license": "MIT", "require": { "flarum/core": "^1.2.0", - "league/oauth2-server": "*", + "bshaffer/oauth2-server-php": "*", "ext-openssl": "*", "ext-json": "*" }, diff --git a/migrations/2023_09_28_create_oauth_access_tokens_table.php b/migrations/2023_09_28_create_oauth_access_tokens_table.php new file mode 100644 index 0000000..c5f00ba --- /dev/null +++ b/migrations/2023_09_28_create_oauth_access_tokens_table.php @@ -0,0 +1,23 @@ + function (Builder $schema) { + if ($schema->hasTable('oauth_access_tokens')) { + return; + } + $schema->create('oauth_access_tokens', function (Blueprint $table) { + $table->increments('id'); + $table->string('access_token', 40); + $table->string('client_id', 80); + $table->string('user_id', 80)->nullable(); + $table->timestamp('expires'); + $table->string('scope', 4000)->nullable(); + }); + }, + 'down' => function (Builder $schema) { + $schema->dropIfExists('oauth_access_tokens'); + }, +]; diff --git a/migrations/2023_09_28_create_oauth_authorization_codes_table.php b/migrations/2023_09_28_create_oauth_authorization_codes_table.php new file mode 100644 index 0000000..cd7c3b3 --- /dev/null +++ b/migrations/2023_09_28_create_oauth_authorization_codes_table.php @@ -0,0 +1,25 @@ + function (Builder $schema) { + if ($schema->hasTable('oauth_authorization_codes')) { + return; + } + $schema->create('oauth_authorization_codes', function (Blueprint $table) { + $table->increments('id'); + $table->string('authorization_code', 40); + $table->string('client_id', 80); + $table->string('user_id', 80)->nullable(); + $table->string('redirect_uri', 2000)->nullable(); + $table->timestamp('expires'); + $table->string('scope', 4000)->nullable(); + $table->string('id_token', 1000)->nullable(); + }); + }, + 'down' => function (Builder $schema) { + $schema->dropIfExists('oauth_authorization_codes'); + }, +]; diff --git a/migrations/2023_09_28_create_oauth_clients_table.php b/migrations/2023_09_28_create_oauth_clients_table.php new file mode 100644 index 0000000..3ea101c --- /dev/null +++ b/migrations/2023_09_28_create_oauth_clients_table.php @@ -0,0 +1,24 @@ + function (Builder $schema) { + if ($schema->hasTable('oauth_clients')) { + return; + } + $schema->create('oauth_clients', function (Blueprint $table) { + $table->increments('id'); + $table->string('client_id', 80); + $table->string('client_secret', 80)->nullable(); + $table->string('redirect_uri', 2000)->nullable(); + $table->string('grant_types', 80)->nullable(); + $table->string('scope', 4000)->nullable(); + $table->string('user_id', 80)->nullable(); + }); + }, + 'down' => function (Builder $schema) { + $schema->dropIfExists('oauth_clients'); + }, +]; diff --git a/migrations/2023_09_28_create_oauth_jwt_table.php b/migrations/2023_09_28_create_oauth_jwt_table.php new file mode 100644 index 0000000..77451f1 --- /dev/null +++ b/migrations/2023_09_28_create_oauth_jwt_table.php @@ -0,0 +1,21 @@ + function (Builder $schema) { + if ($schema->hasTable('oauth_jwt')) { + return; + } + $schema->create('oauth_jwt', function (Blueprint $table) { + $table->increments('id'); + $table->string('client_id', 80); + $table->string('subject', 80)->nullable(); + $table->string('public_key', 2000); + }); + }, + 'down' => function (Builder $schema) { + $schema->dropIfExists('oauth_jwt'); + }, +]; diff --git a/migrations/2023_09_28_create_oauth_refresh_tokens_table.php b/migrations/2023_09_28_create_oauth_refresh_tokens_table.php new file mode 100644 index 0000000..649cd3f --- /dev/null +++ b/migrations/2023_09_28_create_oauth_refresh_tokens_table.php @@ -0,0 +1,23 @@ + function (Builder $schema) { + if ($schema->hasTable('oauth_refresh_tokens')) { + return; + } + $schema->create('oauth_refresh_tokens', function (Blueprint $table) { + $table->increments('id'); + $table->string('refresh_token', 40); + $table->string('client_id', 80); + $table->string('user_id', 80)->nullable(); + $table->timestamp('expires'); + $table->string('scope', 4000)->nullable(); + }); + }, + 'down' => function (Builder $schema) { + $schema->dropIfExists('oauth_refresh_tokens'); + }, +]; diff --git a/migrations/2023_09_28_create_oauth_scopes_table.php b/migrations/2023_09_28_create_oauth_scopes_table.php new file mode 100644 index 0000000..6ab8913 --- /dev/null +++ b/migrations/2023_09_28_create_oauth_scopes_table.php @@ -0,0 +1,20 @@ + function (Builder $schema) { + if ($schema->hasTable('oauth_scopes')) { + return; + } + $schema->create('oauth_scopes', function (Blueprint $table) { + $table->increments('id'); + $table->string('scope', 80); + $table->boolean('is_default')->nullable(); + }); + }, + 'down' => function (Builder $schema) { + $schema->dropIfExists('oauth_scopes'); + }, +]; diff --git a/src/Models/AccessToken.php b/src/Models/AccessToken.php new file mode 100644 index 0000000..ad6f439 --- /dev/null +++ b/src/Models/AccessToken.php @@ -0,0 +1,10 @@ +first()) { + $token['expires'] = strtotime($token['expires']); + return $token; + } + return false; + } + + public function setAccessToken($access_token, $client_id, $user_id, $expires, $scope = null) + { + $expires = date('Y-m-d H:i:s', $expires); + + if ($this->getAccessToken($access_token)) { + return Models\AccessToken::where('access_token', $access_token)->update([ + 'client_id' => $client_id, + 'user_id' => $user_id, + 'expires' => $expires, + 'scope' => $scope, + ]); + } else { + return Models\AccessToken::create([ + 'access_token' => $access_token, + 'client_id' => $client_id, + 'user_id' => $user_id, + 'expires' => $expires, + 'scope' => $scope, + ]); + } + } + + public function unsetAccessToken($access_token) + { + return Models\AccessToken::where('access_token', $access_token)->delete(); + } +}