mirror of
https://github.com/Bubka/2FAuth.git
synced 2025-08-09 05:54:34 +02:00
Move token generation from dedicated class to TwoFAccount model class
This commit is contained in:
@ -114,31 +114,45 @@ class TwoFAccountController extends Controller
|
||||
*/
|
||||
public function generateOTP(Request $request)
|
||||
{
|
||||
$isPreview = false;
|
||||
|
||||
if( $request->id ) {
|
||||
// The request data is the Id of the account
|
||||
|
||||
// The request data is the Id of an existing account
|
||||
$twofaccount = TwoFAccount::FindOrFail($request->id);
|
||||
}
|
||||
else if( $request->otp['uri'] ) {
|
||||
|
||||
// The request data contain an uri
|
||||
$twofaccount = new TwoFAccount;
|
||||
$twofaccount->populateFromUri($request->otp['uri']);
|
||||
|
||||
$isPreview = true; // HOTP generated for preview (in the Create form) will not have its counter updated
|
||||
}
|
||||
else {
|
||||
|
||||
// The request data should contain all otp parameter
|
||||
$twofaccount = new TwoFAccount;
|
||||
$twofaccount->populate($request->otp);
|
||||
|
||||
$isPreview = true; // HOTP generated for preview (in the Create form) will not have its counter updated
|
||||
}
|
||||
|
||||
return response()->json(OTP::generate($twofaccount, $isPreview ? true : false), 200);
|
||||
if( $twofaccount->otpType === 'hotp' ) {
|
||||
|
||||
// returned counter & uri will be updated
|
||||
$twofaccount->increaseHotpCounter();
|
||||
|
||||
// and the db too
|
||||
if( $request->id ) {
|
||||
$twofaccount->save();
|
||||
}
|
||||
}
|
||||
|
||||
if( $request->id ) {
|
||||
return response()->json($twofaccount, 200);
|
||||
}
|
||||
|
||||
return response()->json($twofaccount->makeVisible(['uri', 'secret', 'algorithm']), 200);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
|
Reference in New Issue
Block a user