2021-03-30 00:27:49 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use App\ApiKey;
|
|
|
|
use App\Http\Requests\CreateApiKeyRequest;
|
|
|
|
use App\User;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
|
|
|
|
class ApiKeyController extends Controller
|
|
|
|
{
|
2021-03-31 02:55:09 +00:00
|
|
|
|
2021-03-30 00:27:49 +00:00
|
|
|
public function index()
|
|
|
|
{
|
2021-03-31 02:55:09 +00:00
|
|
|
$this->authorize('viewAny', ApiKey::class);
|
2021-03-30 00:27:49 +00:00
|
|
|
|
2021-03-31 02:55:09 +00:00
|
|
|
return view('dashboard.administration.keys')
|
|
|
|
->with('keys', ApiKey::all());
|
2021-03-30 17:16:01 +00:00
|
|
|
}
|
|
|
|
|
2021-03-30 00:27:49 +00:00
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
*/
|
|
|
|
public function store(CreateApiKeyRequest $request)
|
|
|
|
{
|
2021-03-31 02:55:09 +00:00
|
|
|
$this->authorize('create', ApiKey::class);
|
|
|
|
|
2021-07-19 23:35:03 +00:00
|
|
|
$discriminator = "#" . bin2hex(random_bytes(7));
|
|
|
|
$secret = bin2hex(random_bytes(32));
|
2021-03-30 00:27:49 +00:00
|
|
|
|
|
|
|
$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)
|
|
|
|
{
|
2021-03-31 02:55:09 +00:00
|
|
|
$this->authorize('update', $key);
|
2021-03-30 00:27:49 +00:00
|
|
|
|
2021-03-31 02:55:09 +00:00
|
|
|
if ($key->status == 'active')
|
|
|
|
{
|
|
|
|
$key->status = 'disabled';
|
|
|
|
$key->save();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-30 00:27:49 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
2021-03-31 02:55:09 +00:00
|
|
|
->with('error', 'Key already revoked.');
|
2021-03-30 00:27:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()
|
|
|
|
->back()
|
2021-03-31 02:55:09 +00:00
|
|
|
->with('success', 'Key revoked. Apps using this key will stop working.');
|
|
|
|
|
2021-03-30 00:27:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
|
|
|
$key = ApiKey::findOrFail($id);
|
2021-03-31 02:55:09 +00:00
|
|
|
$this->authorize('delete', $key);
|
2021-03-30 00:27:49 +00:00
|
|
|
|
2021-03-31 02:55:09 +00:00
|
|
|
$key->delete();
|
2021-03-30 00:27:49 +00:00
|
|
|
|
|
|
|
return redirect()
|
|
|
|
->back()
|
2021-03-31 02:55:09 +00:00
|
|
|
->with('success', 'Key deleted successfully. Apps using this key will stop working.');
|
|
|
|
|
2021-03-30 00:27:49 +00:00
|
|
|
}
|
|
|
|
}
|