2019-05-20 07:37:41 +02:00
|
|
|
<?php
|
|
|
|
|
2021-11-07 21:57:22 +01:00
|
|
|
namespace App\Api\v1\Controllers;
|
2019-05-20 07:37:41 +02:00
|
|
|
|
2022-11-22 15:15:52 +01:00
|
|
|
use App\Api\v1\Requests\TwoFAccountBatchRequest;
|
|
|
|
use App\Api\v1\Requests\TwoFAccountDynamicRequest;
|
|
|
|
use App\Api\v1\Requests\TwoFAccountImportRequest;
|
2021-11-07 21:57:22 +01:00
|
|
|
use App\Api\v1\Requests\TwoFAccountReorderRequest;
|
|
|
|
use App\Api\v1\Requests\TwoFAccountStoreRequest;
|
|
|
|
use App\Api\v1\Requests\TwoFAccountUpdateRequest;
|
|
|
|
use App\Api\v1\Requests\TwoFAccountUriRequest;
|
|
|
|
use App\Api\v1\Resources\TwoFAccountCollection;
|
2022-12-13 15:57:33 +01:00
|
|
|
use App\Api\v1\Resources\TwoFAccountExportCollection;
|
2021-11-07 21:57:22 +01:00
|
|
|
use App\Api\v1\Resources\TwoFAccountReadResource;
|
|
|
|
use App\Api\v1\Resources\TwoFAccountStoreResource;
|
2022-07-30 11:25:19 +02:00
|
|
|
use App\Facades\Groups;
|
2022-07-30 11:38:20 +02:00
|
|
|
use App\Facades\TwoFAccounts;
|
2021-11-07 21:57:22 +01:00
|
|
|
use App\Http\Controllers\Controller;
|
2022-11-22 15:15:52 +01:00
|
|
|
use App\Models\TwoFAccount;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Arr;
|
2019-05-20 07:37:41 +02:00
|
|
|
|
2019-05-24 14:44:41 +02:00
|
|
|
class TwoFAccountController extends Controller
|
2019-05-20 07:37:41 +02:00
|
|
|
{
|
|
|
|
/**
|
2021-09-18 00:00:39 +02:00
|
|
|
* List all resources
|
2019-05-20 07:37:41 +02:00
|
|
|
*
|
2021-11-26 11:18:58 +01:00
|
|
|
* @return \App\Api\v1\Resources\TwoFAccountCollection
|
2019-05-20 07:37:41 +02:00
|
|
|
*/
|
2021-09-18 00:00:39 +02:00
|
|
|
public function index(Request $request)
|
2019-05-20 07:37:41 +02:00
|
|
|
{
|
2021-09-22 22:50:45 +02:00
|
|
|
return new TwoFAccountCollection(TwoFAccount::ordered()->get());
|
2021-09-18 00:00:39 +02:00
|
|
|
}
|
2020-01-09 16:33:32 +01:00
|
|
|
|
2021-09-18 00:00:39 +02:00
|
|
|
/**
|
2021-11-26 11:18:58 +01:00
|
|
|
* Display a 2FA account
|
2021-09-18 00:00:39 +02:00
|
|
|
*
|
2021-12-02 13:15:53 +01:00
|
|
|
* @param \App\Models\TwoFAccount $twofaccount
|
2021-11-26 11:18:58 +01:00
|
|
|
* @return \App\Api\v1\Resources\TwoFAccountReadResource
|
2021-09-18 00:00:39 +02:00
|
|
|
*/
|
|
|
|
public function show(TwoFAccount $twofaccount)
|
|
|
|
{
|
|
|
|
return new TwoFAccountReadResource($twofaccount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-26 11:18:58 +01:00
|
|
|
* Store a new 2FA account
|
2021-09-18 00:00:39 +02:00
|
|
|
*
|
2021-11-26 11:18:58 +01:00
|
|
|
* @param \App\Api\v1\Requests\TwoFAccountDynamicRequest $request
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2021-09-18 00:00:39 +02:00
|
|
|
*/
|
|
|
|
public function store(TwoFAccountDynamicRequest $request)
|
|
|
|
{
|
2020-11-13 15:45:17 +01:00
|
|
|
// Two possible cases :
|
2021-09-18 00:00:39 +02:00
|
|
|
// - The most common case, an URI is provided by the QuickForm, thanks to a QR code live scan or file upload
|
|
|
|
// -> We use that URI to define the account
|
|
|
|
// - The advanced form has been used and all individual parameters
|
|
|
|
// -> We use the parameters array to define the account
|
2020-11-13 15:45:17 +01:00
|
|
|
|
2022-11-22 15:15:52 +01:00
|
|
|
$validated = $request->validated();
|
2022-07-05 10:10:24 +02:00
|
|
|
$twofaccount = new TwoFAccount;
|
2021-09-18 00:00:39 +02:00
|
|
|
|
2022-07-05 10:10:24 +02:00
|
|
|
if (Arr::has($validated, 'uri')) {
|
|
|
|
$twofaccount->fillWithURI($validated['uri'], Arr::get($validated, 'custom_otp') === TwoFAccount::STEAM_TOTP);
|
2022-11-22 15:15:52 +01:00
|
|
|
} else {
|
2022-07-05 10:10:24 +02:00
|
|
|
$twofaccount->fillWithOtpParameters($validated);
|
|
|
|
}
|
|
|
|
$twofaccount->save();
|
2019-05-20 07:37:41 +02:00
|
|
|
|
2020-11-02 22:41:40 +01:00
|
|
|
// Possible group association
|
2022-07-30 11:25:19 +02:00
|
|
|
Groups::assign($twofaccount->id);
|
2020-11-02 22:41:40 +01:00
|
|
|
|
2021-11-16 23:45:30 +01:00
|
|
|
return (new TwoFAccountReadResource($twofaccount->refresh()))
|
2022-12-13 15:57:33 +01:00
|
|
|
->response()
|
|
|
|
->setStatusCode(201);
|
2019-05-20 07:37:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-11-26 11:18:58 +01:00
|
|
|
* Update a 2FA account
|
2019-05-20 07:37:41 +02:00
|
|
|
*
|
2021-11-26 11:18:58 +01:00
|
|
|
* @param \App\Api\v1\Requests\TwoFAccountUpdateRequest $request
|
2021-12-02 13:15:53 +01:00
|
|
|
* @param \App\Models\TwoFAccount $twofaccount
|
2021-11-26 11:18:58 +01:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2019-05-20 07:37:41 +02:00
|
|
|
*/
|
2021-09-18 00:00:39 +02:00
|
|
|
public function update(TwoFAccountUpdateRequest $request, TwoFAccount $twofaccount)
|
2019-05-20 07:37:41 +02:00
|
|
|
{
|
2021-09-18 00:00:39 +02:00
|
|
|
$validated = $request->validated();
|
2019-05-20 07:37:41 +02:00
|
|
|
|
2022-07-05 10:10:24 +02:00
|
|
|
$twofaccount->fillWithOtpParameters($validated);
|
|
|
|
$twofaccount->save();
|
2021-09-18 00:00:39 +02:00
|
|
|
|
2021-10-08 13:54:30 +02:00
|
|
|
return (new TwoFAccountReadResource($twofaccount))
|
2022-12-13 15:57:33 +01:00
|
|
|
->response()
|
|
|
|
->setStatusCode(200);
|
2020-11-16 14:10:54 +01:00
|
|
|
}
|
|
|
|
|
2022-10-07 18:58:48 +02:00
|
|
|
/**
|
|
|
|
* Convert a migration resource to a valid TwoFAccounts collection
|
|
|
|
*
|
|
|
|
* @param \App\Api\v1\Requests\TwoFAccountImportRequest $request
|
2022-10-12 11:13:13 +02:00
|
|
|
* @return \Illuminate\Http\JsonResponse|\App\Api\v1\Resources\TwoFAccountCollection
|
2022-10-07 18:58:48 +02:00
|
|
|
*/
|
|
|
|
public function migrate(TwoFAccountImportRequest $request)
|
|
|
|
{
|
|
|
|
$validated = $request->validated();
|
|
|
|
|
|
|
|
if (Arr::has($validated, 'file')) {
|
|
|
|
$migrationResource = $request->file('file');
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2022-10-07 18:58:48 +02:00
|
|
|
return $migrationResource instanceof \Illuminate\Http\UploadedFile
|
|
|
|
? new TwoFAccountCollection(TwoFAccounts::migrate($migrationResource->get()))
|
|
|
|
: response()->json(['message' => __('errors.file_upload_failed')], 500);
|
2022-11-22 15:15:52 +01:00
|
|
|
} else {
|
2022-10-07 18:58:48 +02:00
|
|
|
return new TwoFAccountCollection(TwoFAccounts::migrate($request->payload));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-27 22:36:01 +01:00
|
|
|
/**
|
2021-11-26 11:18:58 +01:00
|
|
|
* Save 2FA accounts order
|
2020-03-27 22:36:01 +01:00
|
|
|
*
|
2021-11-26 11:18:58 +01:00
|
|
|
* @param \App\Api\v1\Requests\TwoFAccountReorderRequest $request
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2020-03-27 22:36:01 +01:00
|
|
|
*/
|
2021-09-07 23:10:17 +02:00
|
|
|
public function reorder(TwoFAccountReorderRequest $request)
|
2020-03-27 22:36:01 +01:00
|
|
|
{
|
2021-09-07 23:10:17 +02:00
|
|
|
$validated = $request->validated();
|
2020-03-27 22:36:01 +01:00
|
|
|
|
2021-09-21 22:46:35 +02:00
|
|
|
TwoFAccount::setNewOrder($validated['orderedIds']);
|
2021-09-18 00:00:39 +02:00
|
|
|
|
|
|
|
return response()->json(['message' => 'order saved'], 200);
|
|
|
|
}
|
2020-03-27 22:36:01 +01:00
|
|
|
|
2020-11-20 14:11:32 +01:00
|
|
|
/**
|
|
|
|
* Preview account using an uri, without any db moves
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
2021-11-26 11:18:58 +01:00
|
|
|
* @param \App\Api\v1\Requests\TwoFAccountUriRequest $request
|
|
|
|
* @return \App\Api\v1\Resources\TwoFAccountStoreResource
|
2020-11-20 14:11:32 +01:00
|
|
|
*/
|
2021-09-18 00:00:39 +02:00
|
|
|
public function preview(TwoFAccountUriRequest $request)
|
2020-11-20 14:11:32 +01:00
|
|
|
{
|
2022-07-05 10:10:24 +02:00
|
|
|
$twofaccount = new TwoFAccount;
|
2022-07-30 21:23:54 +02:00
|
|
|
$twofaccount->fillWithURI($request->uri, $request->custom_otp === TwoFAccount::STEAM_TOTP);
|
2020-11-20 14:11:32 +01:00
|
|
|
|
2021-09-18 00:00:39 +02:00
|
|
|
return new TwoFAccountStoreResource($twofaccount);
|
2020-11-20 14:11:32 +01:00
|
|
|
}
|
|
|
|
|
2022-12-13 15:57:33 +01:00
|
|
|
/**
|
|
|
|
* Export accounts
|
|
|
|
*
|
|
|
|
* @param \App\Api\v1\Requests\TwoFAccountBatchRequest $request
|
|
|
|
* @return TwoFAccountExportCollection|\Illuminate\Http\JsonResponse
|
|
|
|
*/
|
|
|
|
public function export(TwoFAccountBatchRequest $request)
|
|
|
|
{
|
|
|
|
$validated = $request->validated();
|
|
|
|
|
|
|
|
if ($this->tooManyIds($validated['ids'])) {
|
|
|
|
return response()->json([
|
|
|
|
'message' => 'bad request',
|
|
|
|
'reason' => [__('errors.too_many_ids')],
|
|
|
|
], 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new TwoFAccountExportCollection(TwoFAccounts::export($validated['ids']));
|
|
|
|
}
|
|
|
|
|
2019-05-26 16:42:09 +02:00
|
|
|
/**
|
2021-09-18 00:00:39 +02:00
|
|
|
* Get a One-Time Password
|
2019-05-26 16:42:09 +02:00
|
|
|
*
|
2020-02-04 17:06:11 +01:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2022-11-22 15:15:52 +01:00
|
|
|
* @param string|null $id
|
2021-11-26 11:18:58 +01:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2019-05-26 16:42:09 +02:00
|
|
|
*/
|
2021-09-18 00:00:39 +02:00
|
|
|
public function otp(Request $request, $id = null)
|
2019-05-26 16:42:09 +02:00
|
|
|
{
|
2021-09-18 00:00:39 +02:00
|
|
|
$inputs = $request->all();
|
2020-11-14 18:55:10 +01:00
|
|
|
|
2021-09-18 00:00:39 +02:00
|
|
|
// The request input is the ID of an existing account
|
2022-07-05 10:10:24 +02:00
|
|
|
if ($id) {
|
|
|
|
$twofaccount = TwoFAccount::findOrFail((int) $id);
|
2020-02-04 17:06:11 +01:00
|
|
|
}
|
2020-11-14 18:55:10 +01:00
|
|
|
|
2021-09-18 00:00:39 +02:00
|
|
|
// The request input is an uri
|
2022-11-22 15:15:52 +01:00
|
|
|
elseif ($request->has('uri')) {
|
2022-07-05 10:10:24 +02:00
|
|
|
// return 404 if uri is provided with any parameter other than otp_type
|
|
|
|
if ((count($inputs) == 2 && $request->missing('custom_otp')) || count($inputs) > 2) {
|
|
|
|
return response()->json([
|
|
|
|
'message' => 'bad request',
|
2022-11-22 15:15:52 +01:00
|
|
|
'reason' => ['uri' => __('validation.onlyCustomOtpWithUri')],
|
2022-07-05 10:10:24 +02:00
|
|
|
], 400);
|
2022-11-22 15:15:52 +01:00
|
|
|
} else {
|
2022-07-05 10:10:24 +02:00
|
|
|
$validatedData = $request->validate((new TwoFAccountUriRequest)->rules());
|
2022-11-22 15:15:52 +01:00
|
|
|
$twofaccount = new TwoFAccount;
|
2022-07-26 22:35:04 +02:00
|
|
|
$twofaccount->fillWithURI($validatedData['uri'], Arr::get($validatedData, 'custom_otp') === TwoFAccount::STEAM_TOTP, true);
|
2022-07-05 10:10:24 +02:00
|
|
|
}
|
2020-11-14 18:55:10 +01:00
|
|
|
}
|
|
|
|
|
2021-09-18 00:00:39 +02:00
|
|
|
// The request inputs should define an account
|
2020-11-21 21:46:31 +01:00
|
|
|
else {
|
2021-09-18 00:00:39 +02:00
|
|
|
$validatedData = $request->validate((new TwoFAccountStoreRequest)->rules());
|
2022-11-22 15:15:52 +01:00
|
|
|
$twofaccount = new TwoFAccount();
|
2022-07-26 22:35:04 +02:00
|
|
|
$twofaccount->fillWithOtpParameters($validatedData, true);
|
2020-02-04 17:06:11 +01:00
|
|
|
}
|
|
|
|
|
2022-07-05 10:10:24 +02:00
|
|
|
return response()->json($twofaccount->getOTP(), 200);
|
2019-05-20 07:37:41 +02:00
|
|
|
}
|
|
|
|
|
2020-11-21 19:41:36 +01:00
|
|
|
/**
|
|
|
|
* A simple and light method to get the account count.
|
|
|
|
*
|
2021-09-22 22:59:56 +02:00
|
|
|
* @param \Illuminate\Http\Request $request
|
2021-11-26 11:18:58 +01:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2020-11-21 19:41:36 +01:00
|
|
|
*/
|
|
|
|
public function count(Request $request)
|
|
|
|
{
|
2022-11-22 15:15:52 +01:00
|
|
|
return response()->json(['count' => TwoFAccount::count()], 200);
|
2020-11-21 19:41:36 +01:00
|
|
|
}
|
|
|
|
|
2021-09-21 22:46:35 +02:00
|
|
|
/**
|
|
|
|
* Withdraw one or more accounts from their group
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
|
|
|
* @param \App\Api\v1\Requests\TwoFAccountBatchRequest $request
|
2021-11-26 11:18:58 +01:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2021-09-21 22:46:35 +02:00
|
|
|
*/
|
|
|
|
public function withdraw(TwoFAccountBatchRequest $request)
|
2022-11-22 15:15:52 +01:00
|
|
|
{
|
2021-09-21 22:46:35 +02:00
|
|
|
$validated = $request->validated();
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2021-09-21 22:46:35 +02:00
|
|
|
if ($this->tooManyIds($validated['ids'])) {
|
|
|
|
return response()->json([
|
|
|
|
'message' => 'bad request',
|
2022-11-22 15:15:52 +01:00
|
|
|
'reason' => [__('errors.too_many_ids')],
|
2021-09-21 22:46:35 +02:00
|
|
|
], 400);
|
|
|
|
}
|
|
|
|
|
2022-07-30 11:38:20 +02:00
|
|
|
TwoFAccounts::withdraw($validated['ids']);
|
2021-09-21 22:46:35 +02:00
|
|
|
|
2022-11-22 15:15:52 +01:00
|
|
|
return response()->json(['message' => 'accounts withdrawn'], 200);
|
|
|
|
}
|
2021-09-21 22:46:35 +02:00
|
|
|
|
2019-05-20 07:37:41 +02:00
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
2021-12-02 13:15:53 +01:00
|
|
|
* @param \App\Models\TwoFAccount $twofaccount
|
2021-11-26 11:18:58 +01:00
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2019-05-20 07:37:41 +02:00
|
|
|
*/
|
2020-01-31 23:05:06 +01:00
|
|
|
public function destroy(TwoFAccount $twofaccount)
|
2019-05-20 07:37:41 +02:00
|
|
|
{
|
2022-07-30 11:38:20 +02:00
|
|
|
TwoFAccounts::delete($twofaccount->id);
|
2020-01-10 00:22:45 +01:00
|
|
|
|
2020-01-31 23:05:06 +01:00
|
|
|
return response()->json(null, 204);
|
|
|
|
}
|
2020-01-10 00:22:45 +01:00
|
|
|
|
2020-01-31 23:05:06 +01:00
|
|
|
/**
|
|
|
|
* Remove the specified resources from storage.
|
|
|
|
*
|
2021-11-26 11:18:58 +01:00
|
|
|
* @param \App\Api\v1\Requests\TwoFAccountBatchRequest $request
|
|
|
|
* @return \Illuminate\Http\JsonResponse
|
2020-01-31 23:05:06 +01:00
|
|
|
*/
|
2021-09-21 22:46:35 +02:00
|
|
|
public function batchDestroy(TwoFAccountBatchRequest $request)
|
2020-01-31 23:05:06 +01:00
|
|
|
{
|
2021-09-19 22:42:21 +02:00
|
|
|
$validated = $request->validated();
|
|
|
|
|
2021-09-21 22:46:35 +02:00
|
|
|
if ($this->tooManyIds($validated['ids'])) {
|
2021-09-19 22:42:21 +02:00
|
|
|
return response()->json([
|
|
|
|
'message' => 'bad request',
|
2022-11-22 15:15:52 +01:00
|
|
|
'reason' => [__('errors.too_many_ids')],
|
2021-09-19 22:42:21 +02:00
|
|
|
], 400);
|
|
|
|
}
|
|
|
|
|
2022-07-30 11:38:20 +02:00
|
|
|
TwoFAccounts::delete($validated['ids']);
|
2019-05-24 14:44:41 +02:00
|
|
|
|
2020-01-26 21:49:39 +01:00
|
|
|
return response()->json(null, 204);
|
2019-05-24 14:44:41 +02:00
|
|
|
}
|
|
|
|
|
2021-09-21 22:46:35 +02:00
|
|
|
/**
|
|
|
|
* Checks ids length
|
2022-11-22 15:15:52 +01:00
|
|
|
*
|
|
|
|
* @param string $ids comma-separated ids
|
2021-09-22 22:59:56 +02:00
|
|
|
* @return bool whether or not the number of ids is acceptable
|
2021-09-21 22:46:35 +02:00
|
|
|
*/
|
|
|
|
private function tooManyIds(string $ids) : bool
|
|
|
|
{
|
|
|
|
$arIds = explode(',', $ids, 100);
|
2022-11-22 15:15:52 +01:00
|
|
|
$nb = count($arIds);
|
2021-09-21 22:46:35 +02:00
|
|
|
|
|
|
|
return $nb > 99 ? true : false;
|
|
|
|
}
|
2019-05-20 07:37:41 +02:00
|
|
|
}
|