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

130 lines
3.1 KiB
PHP
Raw Normal View History

2019-05-20 07:37:41 +02:00
<?php
namespace App\Http\Controllers;
use App\TwoFAccount;
2019-05-26 16:42:09 +02:00
use OTPHP\TOTP;
use OTPHP\Factory;
2019-05-20 07:37:41 +02:00
use Illuminate\Http\Request;
2019-05-26 16:42:09 +02:00
use ParagonIE\ConstantTime\Base32;
2019-05-20 07:37: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()
{
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)
{
$twofaccount = TwoFAccount::create([
2019-05-20 07:37:41 +02:00
'name' => $request->name,
2019-06-10 23:42:13 +02:00
'email' => $request->email,
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-20 07:37:41 +02:00
/**
* Display the specified resource.
*
* @param \App\TwoFAccount $twofaccount
2019-05-20 07:37:41 +02:00
* @return \Illuminate\Http\Response
*/
public function show(TwoFAccount $twofaccount)
2019-05-20 07:37:41 +02:00
{
2019-05-25 23:51:20 +02:00
return response()->json($twofaccount, 200);
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)
{
try {
$otp = Factory::loadFromProvisioningUri($twofaccount->uri);
2019-05-26 16:42:09 +02:00
} catch (InvalidArgumentException $exception) {
return response()->json([
'message' => 'Error generating TOTP',
], 500);
}
2019-07-02 00:48:48 +02:00
$currentPosition = time();
$PeriodCount = floor($currentPosition / 30); //nombre de période de 30s depuis T0
$currentPeriodStartAt = $PeriodCount * 30;
$currentPeriodendAt = $currentPeriodStartAt + 30;
$positionInCurrentPeriod = $currentPosition - $currentPeriodStartAt;
2019-05-26 16:42:09 +02:00
return response()->json([
'totp' => $otp->now(),
2019-07-02 00:48:48 +02:00
'position' => $positionInCurrentPeriod
2019-05-26 16:42:09 +02:00
], 200);
}
2019-05-20 07:37:41 +02:00
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\TwoFAccount $twofaccount
2019-05-20 07:37:41 +02:00
* @return \Illuminate\Http\Response
*/
public function update(Request $request, TwoFAccount $twofaccount)
2019-05-20 07:37:41 +02:00
{
2019-05-25 23:51:20 +02:00
$twofaccount->update($request->all());
2019-05-20 07:37:41 +02:00
2019-05-25 23:51:20 +02:00
return response()->json($twofaccount, 200);
2019-05-20 07:37:41 +02:00
}
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
{
2019-05-25 23:51:20 +02:00
$twofaccount->delete();
2019-05-20 07:37:41 +02:00
2019-05-25 23:51:20 +02:00
return response()->json(null, 204);
2019-05-20 07:37:41 +02:00
}
2019-05-22 00:49:27 +02:00
/**
* Remove the specified soft deleted resource from storage.
*
* @param \App\TwoFAccount $twofaccount
* @return \Illuminate\Http\Response
*/
public function forceDestroy($id)
{
$twofaccount = TwoFAccount::onlyTrashed()->findOrFail($id);
2019-05-25 23:51:20 +02:00
$twofaccount->forceDelete();
2019-05-25 23:51:20 +02:00
return response()->json(null, 204);
}
2019-05-20 07:37:41 +02:00
}