2FAuth/app/Http/Controllers/TwoFAccountController.php

233 lines
6.5 KiB
PHP
Raw Normal View History

2019-05-20 07:37:41 +02:00
<?php
namespace App\Http\Controllers;
2020-11-02 22:41:40 +01:00
use App\Group;
use App\TwoFAccount;
2020-10-25 23:51:50 +01:00
use App\Classes\Options;
use App\Http\Requests\TwoFAccountReorderRequest;
use App\Http\Requests\TwoFAccountStoreRequest;
use App\Http\Requests\TwoFAccountUpdateRequest;
use App\Http\Resources\TwoFAccountReadResource;
use App\Http\Resources\TwoFAccountStoreResource;
use App\Http\Requests\TwoFAccountDeleteRequest;
use App\Http\Requests\TwoFAccountUriRequest;
use App\Http\Requests\TwoFAccountDynamicRequest;
use App\Services\TwoFAccountService;
use Illuminate\Support\Arr;
2019-05-20 07:37:41 +02:00
use Illuminate\Http\Request;
class TwoFAccountController extends Controller
2019-05-20 07:37:41 +02:00
{
/**
* The TwoFAccount Service instance.
*/
protected $twofaccountService;
/**
* Create a new controller instance.
2019-05-20 07:37:41 +02:00
*
* @param TwoFAccountService $twofaccountService
* @return void
2019-05-20 07:37:41 +02:00
*/
public function __construct(TwoFAccountService $twofaccountService)
2019-05-20 07:37:41 +02:00
{
$this->twofaccountService = $twofaccountService;
2019-05-20 07:37:41 +02:00
}
/**
* List all resources
2019-05-20 07:37:41 +02:00
*
* @return \App\Http\Resources\TwoFAccountReadResource
2019-05-20 07:37:41 +02:00
*/
public function index(Request $request)
2019-05-20 07:37:41 +02:00
{
$request->merge(['hideSecret' => true]);
return TwoFAccountReadResource::collection(TwoFAccount::ordered()->get());
}
/**
* Display a resource
*
* @param \App\TwoFAccount $twofaccount
*
* @return \App\Http\Resources\TwoFAccountReadResource
*/
public function show(TwoFAccount $twofaccount)
{
return new TwoFAccountReadResource($twofaccount);
}
/**
* Store a newly created resource in storage.
*
* @param \App\Http\Requests\TwoFAccountDynamicRequest $request
*
* @return \App\Http\Resources\TwoFAccountReadResource
*/
public function store(TwoFAccountDynamicRequest $request)
{
// Two possible cases :
// - 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
$validated = $request->validated();
$twofaccount = Arr::has($validated, 'uri')
? $this->twofaccountService->createFromUri($validated['uri'])
: $this->twofaccountService->createFromParameters($validated);
2019-05-20 07:37:41 +02:00
2020-11-02 22:41:40 +01:00
// Possible group association
$groupId = Options::get('defaultGroup') === '-1' ? (int) Options::get('activeGroup') : (int) Options::get('defaultGroup');
// 0 is the pseudo group 'All', only groups with id > 0 are true user groups
if( $groupId > 0 ) {
$group = Group::find($groupId);
if($group) {
$group->twofaccounts()->save($twofaccount);
}
}
return (new TwoFAccountReadResource($twofaccount))
->response()
->setStatusCode(201);
2019-05-20 07:37:41 +02:00
}
2019-05-20 07:37:41 +02:00
/**
* Update the specified resource in storage.
2019-05-20 07:37:41 +02:00
*
* @param \App\Http\TwoFAccountUpdateRequest $request
* @param \App\TwoFAccount $twofaccount
2019-05-20 07:37:41 +02:00
* @return \Illuminate\Http\Response
*/
public function update(TwoFAccountUpdateRequest $request, TwoFAccount $twofaccount)
2019-05-20 07:37:41 +02:00
{
$validated = $request->validated();
2019-05-20 07:37:41 +02:00
$this->twofaccountService->update($twofaccount, $validated);
return response()->json($twofaccount, 200);
2019-05-20 07:37:41 +02:00
}
/**
* Set new order.
*
* @param App\Http\Requests\TwoFAccountReorderRequest $request
* @return \Illuminate\Http\Response
*/
public function reorder(TwoFAccountReorderRequest $request)
{
$validated = $request->validated();
$this->twofaccountService->saveOrder($validated['orderedIds']);
return response()->json(['message' => 'order saved'], 200);
}
/**
* Preview account using an uri, without any db moves
*
* @param \App\Http\Requests\TwoFAccountUriRequest $request
*
* @return \App\Http\Resources\TwoFAccountStoreResource
*/
public function preview(TwoFAccountUriRequest $request)
{
$twofaccount = $this->twofaccountService->createFromUri($request->uri, $saveToDB = false);
return new TwoFAccountStoreResource($twofaccount);
}
2019-05-26 16:42:09 +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
2019-05-26 16:42:09 +02:00
* @return \Illuminate\Http\Response
*/
public function otp(Request $request, $id = null)
2019-05-26 16:42:09 +02:00
{
$inputs = $request->all();
// The request input is the ID of an existing account
if ( $id ) {
$otp = $this->twofaccountService->getOTP((int) $id);
2020-02-04 17:06:11 +01:00
}
// The request input is an uri
else if ( count($inputs) === 1 && $request->has('uri') ) {
$validatedData = $request->validate((new TwoFAccountUriRequest)->rules());
$otp = $this->twofaccountService->getOTP($validatedData['uri']);
}
else if ( count($inputs) > 1 && $request->has('uri')) {
return response()->json([
'message' => 'bad request',
'reason' => ['uri' => __('validation.single', ['attribute' => 'uri'])]
], 400);
}
// The request inputs should define an account
else {
$validatedData = $request->validate((new TwoFAccountStoreRequest)->rules());
$otp = $this->twofaccountService->getOTP($validatedData);
2020-02-04 17:06:11 +01:00
}
return response()->json($otp, 200);
2019-05-20 07:37:41 +02:00
}
/**
* A simple and light method to get the account count.
*
* @param \App\TwoFAccount $twofaccount
* @return \Illuminate\Http\Response
*/
public function count(Request $request)
{
return response()->json([ 'count' => TwoFAccount::count() ], 200);
}
2019-05-20 07:37:41 +02:00
/**
* Remove the specified resource from storage.
*
* @param \App\TwoFAccount $twofaccount
2019-05-20 07:37:41 +02:00
* @return \Illuminate\Http\Response
*/
public function destroy(TwoFAccount $twofaccount)
2019-05-20 07:37:41 +02:00
{
$this->twofaccountService->delete($twofaccount->id);
2020-01-10 00:22:45 +01:00
return response()->json(null, 204);
}
2020-01-10 00:22:45 +01:00
/**
* Remove the specified resources from storage.
*
* @param \App\Http\Requests\TwoFAccountDeleteRequest $request
* @return \Illuminate\Http\Response
*/
public function batchDestroy(TwoFAccountDeleteRequest $request)
{
$this->twofaccountService->delete($request->ids);
2020-01-26 21:49:39 +01:00
return response()->json(null, 204);
}
2019-05-20 07:37:41 +02:00
}