2019-05-20 07:37:41 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2020-11-02 22:41:40 +01:00
|
|
|
use App\Group;
|
2019-05-24 14:44:41 +02:00
|
|
|
use App\TwoFAccount;
|
2020-10-25 23:51:50 +01:00
|
|
|
use App\Classes\Options;
|
2020-11-20 14:11:32 +01:00
|
|
|
use Illuminate\Support\Str;
|
2019-05-20 07:37:41 +02:00
|
|
|
use Illuminate\Http\Request;
|
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()
|
|
|
|
{
|
2020-11-17 21:39:02 +01:00
|
|
|
return response()->json(TwoFAccount::ofGroup(Options::get('activeGroup'))->ordered()->get()->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
|
2020-11-13 15:45:17 +01:00
|
|
|
|
2020-01-19 23:02:20 +01:00
|
|
|
$this->validate($request, [
|
2020-11-13 15:45:17 +01:00
|
|
|
'service' => 'required|string',
|
2020-11-14 18:48:27 +01:00
|
|
|
'account' => 'required_without:uri|nullable|string|regex:/^[^:]+$/i',
|
2020-11-13 15:45:17 +01:00
|
|
|
'icon' => 'nullable|string',
|
|
|
|
'uri' => 'nullable|string|regex:/^otpauth:\/\/[h,t]otp\//i',
|
2020-11-16 14:45:24 +01:00
|
|
|
'otpType' => 'required_without:uri|in:totp,hotp',
|
2020-11-13 15:45:17 +01:00
|
|
|
'secret' => 'required_without:uri|string',
|
|
|
|
'digits' => 'nullable|integer|between:6,10',
|
|
|
|
'algorithm' => 'nullable|in:sha1,sha256,sha512,md5',
|
|
|
|
'totpPeriod' => 'nullable|integer|min:1',
|
|
|
|
'hotpCounter' => 'nullable|integer|min:0',
|
2020-01-12 19:55:17 +01:00
|
|
|
]);
|
2020-01-09 16:33:32 +01:00
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
// Two possible cases :
|
|
|
|
// - The most common case, the uri is provided thanks to a QR code live scan or file upload
|
|
|
|
// -> We use this uri to populate the account
|
|
|
|
// - The advanced form has been used and provide no uri but all individual parameters
|
|
|
|
// -> We use the parameters collection to populate the account
|
|
|
|
$twofaccount = new TwoFAccount;
|
2020-11-17 15:45:01 +01:00
|
|
|
$twofaccount->service = $request->service;
|
|
|
|
$twofaccount->account = $request->account;
|
|
|
|
$twofaccount->icon = $request->icon;
|
2020-01-25 00:01:30 +01:00
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
if( $request->uri ) {
|
2020-11-17 15:30:50 +01:00
|
|
|
$twofaccount->uri = $request->uri;
|
2020-11-13 15:45:17 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
$twofaccount->populate($request->all());
|
|
|
|
}
|
|
|
|
|
|
|
|
$twofaccount->save();
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-02-02 20:46:41 +01:00
|
|
|
public function show(TwoFAccount $twofaccount)
|
2019-05-20 07:37:41 +02:00
|
|
|
{
|
2020-01-26 21:49:39 +01:00
|
|
|
return response()->json($twofaccount, 200);
|
2019-05-20 07:37:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-16 14:10:54 +01:00
|
|
|
/**
|
|
|
|
* Display the specified resource with all attributes.
|
|
|
|
*
|
|
|
|
* @param \App\TwoFAccount $twofaccount
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function showWithSensitive(TwoFAccount $twofaccount)
|
|
|
|
{
|
|
|
|
return response()->json($twofaccount->makeVisible(['uri', 'secret', 'algorithm']), 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-27 22:36:01 +01:00
|
|
|
/**
|
|
|
|
* Save new order.
|
|
|
|
*
|
|
|
|
* @param \App\TwoFAccount $twofaccount
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function reorder(Request $request)
|
|
|
|
{
|
|
|
|
TwoFAccount::setNewOrder($request->orderedIds);
|
|
|
|
return response()->json('order saved', 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-20 14:11:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Preview account using an uri, without any db moves
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function preview(Request $request)
|
|
|
|
{
|
|
|
|
|
|
|
|
$this->validate($request, [
|
|
|
|
'uri' => 'required|string|regex:/^otpauth:\/\/[h,t]otp\//i',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$twofaccount = new TwoFAccount;
|
|
|
|
$twofaccount->uri = $request->uri;
|
|
|
|
|
|
|
|
// If present, use the imageLink parameter to prefill the icon field
|
|
|
|
if( $twofaccount->imageLink ) {
|
|
|
|
|
|
|
|
$chunks = explode('.', $twofaccount->imageLink);
|
|
|
|
$hashFilename = Str::random(40) . '.' . end($chunks);
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
Storage::disk('local')->put('imagesLink/' . $hashFilename, file_get_contents($twofaccount->imageLink));
|
|
|
|
|
|
|
|
if( in_array(Storage::mimeType('imagesLink/' . $hashFilename), ['image/png', 'image/jpeg', 'image/webp', 'image/bmp']) ) {
|
|
|
|
if( getimagesize(storage_path() . '/app/imagesLink/' . $hashFilename) ) {
|
|
|
|
|
|
|
|
Storage::move('imagesLink/' . $hashFilename, 'public/icons/' . $hashFilename);
|
|
|
|
$twofaccount->icon = $hashFilename;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-22 10:56:27 +01:00
|
|
|
catch( \Exception $e ) {
|
2020-11-20 14:11:32 +01:00
|
|
|
$twofaccount->imageLink = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json($twofaccount->makeVisible(['uri', 'secret', 'algorithm']), 200);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-26 16:42:09 +02:00
|
|
|
/**
|
2020-11-21 21:46:31 +01:00
|
|
|
* Generate an OTP token
|
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
|
|
|
|
*/
|
2020-11-21 21:46:31 +01:00
|
|
|
public function token(Request $request)
|
2019-05-26 16:42:09 +02:00
|
|
|
{
|
2020-11-21 21:46:31 +01:00
|
|
|
// When the method is called during the process of creating/editing an HOTP account the
|
|
|
|
// sensitive data have to be returned, because of the hotpCounter increment
|
|
|
|
$shouldResponseWithSensitiveData = false;
|
2020-02-06 12:24:18 +01:00
|
|
|
|
2020-11-12 00:15:55 +01:00
|
|
|
if( $request->id ) {
|
2020-11-14 18:55:10 +01:00
|
|
|
|
|
|
|
// The request data is the Id of an existing account
|
2020-11-12 00:15:55 +01:00
|
|
|
$twofaccount = TwoFAccount::FindOrFail($request->id);
|
2020-02-04 17:06:11 +01:00
|
|
|
}
|
2020-11-13 15:45:17 +01:00
|
|
|
else if( $request->otp['uri'] ) {
|
2020-11-14 18:55:10 +01:00
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
// The request data contain an uri
|
|
|
|
$twofaccount = new TwoFAccount;
|
2020-11-17 15:30:50 +01:00
|
|
|
$twofaccount->uri = $request->otp['uri'];
|
2020-11-21 21:46:31 +01:00
|
|
|
$shouldResponseWithSensitiveData = true;
|
2020-11-13 15:45:17 +01:00
|
|
|
}
|
2020-02-04 17:06:11 +01:00
|
|
|
else {
|
2020-11-14 18:55:10 +01:00
|
|
|
|
2020-11-13 15:45:17 +01:00
|
|
|
// The request data should contain all otp parameter
|
2020-11-12 00:15:55 +01:00
|
|
|
$twofaccount = new TwoFAccount;
|
2020-11-13 15:45:17 +01:00
|
|
|
$twofaccount->populate($request->otp);
|
2020-11-21 21:46:31 +01:00
|
|
|
$shouldResponseWithSensitiveData = true;
|
2020-11-14 18:55:10 +01:00
|
|
|
}
|
|
|
|
|
2020-11-21 21:46:31 +01:00
|
|
|
$response = [
|
|
|
|
'token' => $twofaccount->token,
|
|
|
|
];
|
|
|
|
|
2020-11-14 18:55:10 +01:00
|
|
|
if( $twofaccount->otpType === 'hotp' ) {
|
2020-11-12 00:15:55 +01:00
|
|
|
|
2020-11-14 18:55:10 +01:00
|
|
|
// returned counter & uri will be updated
|
|
|
|
$twofaccount->increaseHotpCounter();
|
|
|
|
|
|
|
|
// and the db too
|
|
|
|
if( $request->id ) {
|
|
|
|
$twofaccount->save();
|
|
|
|
}
|
2020-11-21 21:46:31 +01:00
|
|
|
|
|
|
|
if( $shouldResponseWithSensitiveData ) {
|
|
|
|
$response['hotpCounter'] = $twofaccount->hotpCounter;
|
|
|
|
$response['uri'] = $twofaccount->uri;
|
|
|
|
}
|
2020-11-14 18:55:10 +01:00
|
|
|
}
|
2020-11-21 21:46:31 +01:00
|
|
|
else {
|
2020-11-14 18:55:10 +01:00
|
|
|
|
2020-11-21 21:46:31 +01:00
|
|
|
$response['totpPeriod'] = $twofaccount->totpPeriod;
|
|
|
|
$response['totpTimestamp'] = $twofaccount->totpTimestamp;
|
2020-02-04 17:06:11 +01:00
|
|
|
}
|
|
|
|
|
2020-11-21 21:46:31 +01:00
|
|
|
return response()->json($response, 200);
|
2019-05-26 16:42:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-14 18:55:10 +01: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
|
|
|
|
2020-01-19 23:02:20 +01:00
|
|
|
$this->validate($request, [
|
2020-11-16 14:45:24 +01:00
|
|
|
'service' => 'required|string',
|
|
|
|
'account' => 'required_without:uri|nullable|string|regex:/^[^:]+$/i',
|
|
|
|
'icon' => 'nullable|string',
|
|
|
|
'uri' => 'nullable|string|regex:/^otpauth:\/\/[h,t]otp\//i',
|
|
|
|
'otpType' => 'required_without:uri|in:totp,hotp',
|
|
|
|
'secret' => 'required_without:uri|string',
|
|
|
|
'digits' => 'nullable|integer|between:6,10',
|
|
|
|
'algorithm' => 'nullable|in:sha1,sha256,sha512,md5',
|
|
|
|
'totpPeriod' => 'required_if:otpType,totp|nullable|integer|min:1',
|
|
|
|
'hotpCounter' => 'required_if:otpType,hotp|nullable|integer|min:0',
|
2020-01-09 21:04:30 +01:00
|
|
|
]);
|
|
|
|
|
2020-01-27 13:56:19 +01:00
|
|
|
// Here we catch a possible missing model exception in order to
|
|
|
|
// delete orphan submited icon
|
|
|
|
try {
|
|
|
|
|
|
|
|
$twofaccount = TwoFAccount::FindOrFail($id);
|
|
|
|
|
|
|
|
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
|
|
|
|
|
|
|
|
if( $request->icon ) {
|
|
|
|
Storage::delete('public/icons/' . $request->icon);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw $e;
|
|
|
|
}
|
2020-01-10 08:35:15 +01:00
|
|
|
|
2020-11-16 14:45:24 +01:00
|
|
|
$twofaccount->populate($request->all());
|
|
|
|
$twofaccount->save();
|
2020-01-26 21:49:39 +01:00
|
|
|
|
|
|
|
return response()->json($twofaccount, 200);
|
2020-01-10 08:35:15 +01:00
|
|
|
|
2019-05-20 07:37:41 +02:00
|
|
|
}
|
|
|
|
|
2019-05-24 14:44:41 +02:00
|
|
|
|
2020-11-21 19:41:36 +01: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.
|
|
|
|
*
|
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-31 23:05:06 +01:00
|
|
|
public function destroy(TwoFAccount $twofaccount)
|
2019-05-20 07:37:41 +02:00
|
|
|
{
|
2020-01-31 23:05:06 +01:00
|
|
|
$twofaccount->delete();
|
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
|
|
|
|
2019-05-24 14:44:41 +02:00
|
|
|
|
2020-01-31 23:05:06 +01:00
|
|
|
/**
|
|
|
|
* Remove the specified resources from storage.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function batchDestroy(Request $request)
|
|
|
|
{
|
|
|
|
$ids = $request->all();
|
|
|
|
|
|
|
|
TwoFAccount::destroy($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
|
|
|
}
|
|
|
|
|
2019-05-20 07:37:41 +02:00
|
|
|
}
|