From 230eda1974501e143989d2fe2c527d47d81c22ac Mon Sep 17 00:00:00 2001 From: Miguel N Date: Fri, 29 Oct 2021 20:23:45 +0100 Subject: [PATCH] Removed API key feature Removed API key generation feature in preparation for JWT authentication --- app/ApiKey.php | 25 ---- app/Http/Controllers/ApiKeyController.php | 95 ------------- app/Http/Requests/CreateApiKeyRequest.php | 30 ---- config/adminlte.php | 6 - .../migrations/2021_03_29_224932_api_keys.php | 42 ------ ...10_26_000036_add_linked_accounts_table.php | 28 ++++ .../dashboard/administration/keys.blade.php | 134 ------------------ routes/web.php | 2 - 8 files changed, 28 insertions(+), 334 deletions(-) delete mode 100644 app/ApiKey.php delete mode 100644 app/Http/Controllers/ApiKeyController.php delete mode 100644 app/Http/Requests/CreateApiKeyRequest.php delete mode 100644 database/migrations/2021_03_29_224932_api_keys.php create mode 100644 database/migrations/2021_10_26_000036_add_linked_accounts_table.php delete mode 100644 resources/views/dashboard/administration/keys.blade.php diff --git a/app/ApiKey.php b/app/ApiKey.php deleted file mode 100644 index 0ed33ff..0000000 --- a/app/ApiKey.php +++ /dev/null @@ -1,25 +0,0 @@ -belongsTo('App\User', 'owner_user_id', 'id'); - } -} diff --git a/app/Http/Controllers/ApiKeyController.php b/app/Http/Controllers/ApiKeyController.php deleted file mode 100644 index f725ec4..0000000 --- a/app/Http/Controllers/ApiKeyController.php +++ /dev/null @@ -1,95 +0,0 @@ -authorize('viewAny', ApiKey::class); - - return view('dashboard.administration.keys') - ->with('keys', ApiKey::all()); - } - - /** - * Store a newly created resource in storage. - * - * @param \Illuminate\Http\Request $request - */ - public function store(CreateApiKeyRequest $request) - { - $this->authorize('create', ApiKey::class); - - $discriminator = "#" . bin2hex(random_bytes(7)); - $secret = bin2hex(random_bytes(32)); - - $key = ApiKey::create([ - 'name' => $request->keyName, - 'discriminator' => $discriminator, - 'secret' => Hash::make($secret), - 'status' => 'active', - 'owner_user_id' => Auth::user()->id - ]); - - if ($key) - { - $request->session()->flash('success', __('Key successfully registered!')); - $request->session()->flash('finalKey', $discriminator . '.' . $secret); - - return redirect() - ->back(); - } - - return redirect() - ->back() - ->with('error', __('An error occurred whilst trying to create an API key.')); - } - - - public function revokeKey(Request $request, ApiKey $key) - { - $this->authorize('update', $key); - - if ($key->status == 'active') - { - $key->status = 'disabled'; - $key->save(); - } - else - { - return redirect() - ->back() - ->with('error', __('Key already revoked.')); - } - - return redirect() - ->back() - ->with('success', __('Key revoked. Apps using this key will stop working.')); - - } - - /** - * Remove the specified resource from storage. - */ - public function destroy($id) - { - $key = ApiKey::findOrFail($id); - $this->authorize('delete', $key); - - $key->delete(); - - return redirect() - ->back() - ->with('success', __('Key deleted successfully. Apps using this key will stop working.')); - - } -} diff --git a/app/Http/Requests/CreateApiKeyRequest.php b/app/Http/Requests/CreateApiKeyRequest.php deleted file mode 100644 index d7bf22c..0000000 --- a/app/Http/Requests/CreateApiKeyRequest.php +++ /dev/null @@ -1,30 +0,0 @@ - 'required|string' - ]; - } -} diff --git a/config/adminlte.php b/config/adminlte.php index 23d0b2c..32b0b5e 100755 --- a/config/adminlte.php +++ b/config/adminlte.php @@ -365,12 +365,6 @@ return [ 'url' => '/admin/devtools', 'can' => 'admin.developertools.use', ], - [ - 'text' => 'API Keys', - 'icon' => 'fas fa-user-shield', - 'can' => 'admin.settings.view', - 'route' => 'keys.index' - ] ], ], [ diff --git a/database/migrations/2021_03_29_224932_api_keys.php b/database/migrations/2021_03_29_224932_api_keys.php deleted file mode 100644 index 43a990d..0000000 --- a/database/migrations/2021_03_29_224932_api_keys.php +++ /dev/null @@ -1,42 +0,0 @@ -id(); - $table->string('discriminator'); - $table->string('secret'); - $table->enum('status', ['disabled', 'active']); - $table->bigInteger('owner_user_id')->unsigned(); - - $table->foreign('owner_user_id') - ->references('id') - ->on('users') - ->cascadeOnDelete() - ->cascadeOnUpdate(); - }); - } - - /** - * Reverse the migrations. - * - * @return void - */ - public function down() - { - // - } -} diff --git a/database/migrations/2021_10_26_000036_add_linked_accounts_table.php b/database/migrations/2021_10_26_000036_add_linked_accounts_table.php new file mode 100644 index 0000000..712b9fb --- /dev/null +++ b/database/migrations/2021_10_26_000036_add_linked_accounts_table.php @@ -0,0 +1,28 @@ +{{__('messages.adm')}} / API Key Administration - -@stop - -@section('js') - - - -@stop - -@section('content') - - - -
- @csrf - -
- - -
- -
- - - - - -
- -
-
-
-

You can use the key discriminator to identify it's API calls in the logs.

-
-
-
- - @if (session()->has('finalKey')) -
-
-
-

This is your API key: {{ session('finalKey') }}

-

Please copy it now as it'll only appear once.

-
-
-
- @endif - -
-
- - - - -

Here, you can view and manage all API keys created by users in the app. You can't, however, use this page to access someone else's account.

-
- - - @if(!$keys->isEmpty()) - - - - - - - - - - - - - - - - @foreach($keys as $key) - - - - - - - - - - @endforeach - - -
NameDiscriminatorOwnerStatusLast UsedLast ModifiedActions
{{ $key->name }}{{ $key->discriminator }}{{ $key->user->name }}{{ ($key->status == 'disabled') ? 'Revoked' : 'Active' }}{{ ($key->last_used == null) ? 'No recent activity' : $key->last_used }}{{ $key->updated_at }} - @if ($key->status == 'active') -
- @csrf - @method('PATCH') - -
- @else - - @endif -
- @csrf - @method('DELETE') - -
- -
- @else -
-

No API keys have been registered yet.

-
- @endif - - - - - - - -
- -
-
- -@stop - - -@section('footer') - @include('breadcrumbs.dashboard.footer') -@stop diff --git a/routes/web.php b/routes/web.php index 5caaae9..f4e938e 100755 --- a/routes/web.php +++ b/routes/web.php @@ -241,8 +241,6 @@ Route::group(['prefix' => LaravelLocalization::setLocale(), 'middleware' => ['lo Route::get('settings', [OptionsController::class, 'index']) ->name('showSettings'); - Route::resource('keys', ApiKeyController::class); - Route::patch('keys/revoke/{key}', [ApiKeyController::class, 'revokeKey']) ->name('revokeKey');