chore: create migrations & access token storage
This commit is contained in:
parent
d9458ba3ef
commit
0d16e11871
9 changed files with 192 additions and 1 deletions
|
@ -11,7 +11,7 @@
|
|||
"license": "MIT",
|
||||
"require": {
|
||||
"flarum/core": "^1.2.0",
|
||||
"league/oauth2-server": "*",
|
||||
"bshaffer/oauth2-server-php": "*",
|
||||
"ext-openssl": "*",
|
||||
"ext-json": "*"
|
||||
},
|
||||
|
|
23
migrations/2023_09_28_create_oauth_access_tokens_table.php
Normal file
23
migrations/2023_09_28_create_oauth_access_tokens_table.php
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Schema\Builder;
|
||||
|
||||
return [
|
||||
'up' => 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');
|
||||
},
|
||||
];
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Schema\Builder;
|
||||
|
||||
return [
|
||||
'up' => 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');
|
||||
},
|
||||
];
|
24
migrations/2023_09_28_create_oauth_clients_table.php
Normal file
24
migrations/2023_09_28_create_oauth_clients_table.php
Normal file
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Schema\Builder;
|
||||
|
||||
return [
|
||||
'up' => 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');
|
||||
},
|
||||
];
|
21
migrations/2023_09_28_create_oauth_jwt_table.php
Normal file
21
migrations/2023_09_28_create_oauth_jwt_table.php
Normal file
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Schema\Builder;
|
||||
|
||||
return [
|
||||
'up' => 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');
|
||||
},
|
||||
];
|
23
migrations/2023_09_28_create_oauth_refresh_tokens_table.php
Normal file
23
migrations/2023_09_28_create_oauth_refresh_tokens_table.php
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Schema\Builder;
|
||||
|
||||
return [
|
||||
'up' => 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');
|
||||
},
|
||||
];
|
20
migrations/2023_09_28_create_oauth_scopes_table.php
Normal file
20
migrations/2023_09_28_create_oauth_scopes_table.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Database\Schema\Builder;
|
||||
|
||||
return [
|
||||
'up' => 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');
|
||||
},
|
||||
];
|
10
src/Models/AccessToken.php
Normal file
10
src/Models/AccessToken.php
Normal file
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace FoskyM\OAuthCenter\Models;
|
||||
|
||||
use Flarum\Database\AbstractModel;
|
||||
|
||||
class AccessToken extends AbstractModel
|
||||
{
|
||||
protected $table = 'oauth_access_tokens';
|
||||
}
|
45
src/Storage.php
Normal file
45
src/Storage.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
namespace FoskyM\OAuthCenter;
|
||||
use OAuth2\Storage\AccessTokenInterface;
|
||||
use OAuth2\Storage\ClientCredentialsInterface;
|
||||
use OAuth2\Storage\AuthorizationCodeInterface;
|
||||
abstract class Storage implements AccessTokenInterface,
|
||||
ClientCredentialsInterface, AuthorizationCodeInterface
|
||||
{
|
||||
public function getAccessToken($access_token)
|
||||
{
|
||||
if ($token = Models\AccessToken::where('access_token', $access_token)->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();
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue