2019-05-20 07:37:41 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2020-01-09 16:33:32 +01:00
|
|
|
use Validator;
|
2019-05-24 14:44:41 +02:00
|
|
|
use App\TwoFAccount;
|
2020-01-09 16:41:41 +01:00
|
|
|
use App\Classes\TimedTOTP;
|
2019-05-20 07:37:41 +02:00
|
|
|
use Illuminate\Http\Request;
|
2019-05-26 16:42:09 +02:00
|
|
|
use ParagonIE\ConstantTime\Base32;
|
2020-01-07 16:46:50 +01:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
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
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2019-05-24 14:44:41 +02:00
|
|
|
return response()->json(TwoFAccount::all()->toArray());
|
2019-05-20 07:37:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
2020-01-09 16:33:32 +01:00
|
|
|
|
|
|
|
// see https://github.com/google/google-authenticator/wiki/Key-Uri-Format
|
|
|
|
// for otpauth uri format validation
|
|
|
|
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
|
|
'service' => 'required',
|
|
|
|
'uri' => 'required|starts_with:otpauth://totp/',
|
2020-01-12 19:55:17 +01:00
|
|
|
]);
|
2020-01-09 16:33:32 +01:00
|
|
|
|
|
|
|
if ($validator->fails()) {
|
2020-01-14 12:23:31 +01:00
|
|
|
return response()->json(['validation' => $validator->errors()], 400);
|
2020-01-09 16:33:32 +01:00
|
|
|
}
|
|
|
|
|
2019-05-24 14:44:41 +02:00
|
|
|
$twofaccount = TwoFAccount::create([
|
2020-01-06 21:45:14 +01:00
|
|
|
'service' => $request->service,
|
|
|
|
'account' => $request->account,
|
2020-01-02 00:09:19 +01:00
|
|
|
'uri' => $request->uri,
|
|
|
|
'icon' => $request->icon
|
2019-05-20 07:37:41 +02:00
|
|
|
]);
|
|
|
|
|
2019-05-25 23:51:20 +02:00
|
|
|
return response()->json($twofaccount, 201);
|
2019-05-20 07:37:41 +02:00
|
|
|
}
|
|
|
|
|
2019-05-24 14:44:41 +02:00
|
|
|
|
2019-05-20 07:37:41 +02:00
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
2019-05-24 14:44:41 +02:00
|
|
|
* @param \App\TwoFAccount $twofaccount
|
2019-05-20 07:37:41 +02:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2020-01-10 00:07:37 +01:00
|
|
|
public function show($id)
|
2019-05-20 07:37:41 +02:00
|
|
|
{
|
2020-01-10 00:07:37 +01:00
|
|
|
try {
|
|
|
|
$twofaccount = TwoFAccount::FindOrFail($id);
|
|
|
|
return response()->json($twofaccount, 200);
|
|
|
|
} catch (\Exception $e) {
|
2020-01-14 12:23:31 +01:00
|
|
|
return response()->json( ['message' => 'not found' ], 404);
|
2020-01-10 00:07:37 +01:00
|
|
|
}
|
2019-05-20 07:37:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-26 16:42:09 +02:00
|
|
|
/**
|
|
|
|
* Generate a TOTP
|
|
|
|
*
|
|
|
|
* @param \App\TwoFAccount $twofaccount
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function generateTOTP(TwoFAccount $twofaccount)
|
|
|
|
{
|
2019-07-02 00:48:48 +02:00
|
|
|
|
2020-01-09 16:41:41 +01:00
|
|
|
return response()->json(TimedTOTP::get($twofaccount->uri), 200);
|
2019-05-26 16:42:09 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-20 07:37:41 +02:00
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
2019-05-24 14:44:41 +02:00
|
|
|
* @param \App\TwoFAccount $twofaccount
|
2019-05-20 07:37:41 +02:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2020-01-10 08:35:15 +01:00
|
|
|
public function update(Request $request, $id)
|
2019-05-20 07:37:41 +02:00
|
|
|
{
|
2020-01-09 21:04:30 +01:00
|
|
|
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
|
|
'service' => 'required',
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
2020-01-14 12:23:31 +01:00
|
|
|
return response()->json( ['validation' => $validator->errors() ], 400);
|
2020-01-09 21:04:30 +01:00
|
|
|
}
|
|
|
|
|
2019-05-20 07:37:41 +02:00
|
|
|
|
2020-01-10 08:35:15 +01:00
|
|
|
try {
|
|
|
|
|
|
|
|
$twofaccount = TwoFAccount::FindOrFail($id);
|
|
|
|
$twofaccount->update($request->all());
|
|
|
|
|
|
|
|
return response()->json($twofaccount, 200);
|
|
|
|
|
|
|
|
}
|
|
|
|
catch (\Exception $e) {
|
|
|
|
|
2020-01-14 12:23:31 +01:00
|
|
|
return response()->json( ['message' => 'not found' ] , 404);
|
2020-01-10 08:35:15 +01:00
|
|
|
|
|
|
|
}
|
2019-05-20 07:37:41 +02:00
|
|
|
}
|
|
|
|
|
2019-05-24 14:44:41 +02:00
|
|
|
|
2019-05-20 07:37:41 +02:00
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
2019-05-24 14:44:41 +02:00
|
|
|
* @param \App\TwoFAccount $twofaccount
|
2019-05-20 07:37:41 +02:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2020-01-10 00:22:45 +01:00
|
|
|
public function destroy($id)
|
2019-05-20 07:37:41 +02:00
|
|
|
{
|
2020-01-10 00:22:45 +01:00
|
|
|
try {
|
|
|
|
|
|
|
|
$twofaccount = TwoFAccount::FindOrFail($id);
|
|
|
|
|
|
|
|
// delete icon
|
|
|
|
$storedIcon = 'public/icons/' . $twofaccount->icon;
|
|
|
|
|
|
|
|
if( Storage::exists($storedIcon) ) {
|
|
|
|
Storage::delete($storedIcon);
|
|
|
|
}
|
|
|
|
|
|
|
|
$twofaccount->delete();
|
|
|
|
|
|
|
|
return response()->json(null, 204);
|
2019-05-22 00:49:27 +02:00
|
|
|
|
2020-01-07 16:46:50 +01:00
|
|
|
}
|
2020-01-10 00:22:45 +01:00
|
|
|
catch (\Exception $e) {
|
2019-05-24 14:44:41 +02:00
|
|
|
|
2020-01-14 12:23:31 +01:00
|
|
|
return response()->json(['message' => 'already gone'], 404);
|
2019-05-24 14:44:41 +02:00
|
|
|
|
2020-01-10 00:22:45 +01:00
|
|
|
}
|
2019-05-24 14:44:41 +02:00
|
|
|
}
|
|
|
|
|
2019-05-20 07:37:41 +02:00
|
|
|
}
|