chore: create migrations & access token storage

This commit is contained in:
FoskyM 2023-09-28 22:45:28 +08:00
parent d9458ba3ef
commit 0d16e11871
No known key found for this signature in database
GPG key ID: 42C0ED6994AD7E9C
9 changed files with 192 additions and 1 deletions

View file

@ -11,7 +11,7 @@
"license": "MIT",
"require": {
"flarum/core": "^1.2.0",
"league/oauth2-server": "*",
"bshaffer/oauth2-server-php": "*",
"ext-openssl": "*",
"ext-json": "*"
},

View 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');
},
];

View file

@ -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');
},
];

View 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');
},
];

View 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');
},
];

View 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');
},
];

View 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');
},
];

View 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
View 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();
}
}