1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-13 22:54:25 +01:00
invoiceninja/app/Http/Controllers/TwilioController.php

201 lines
6.2 KiB
PHP
Raw Normal View History

2022-07-27 03:21:12 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Http\Controllers;
2022-10-27 07:11:55 +02:00
use App\Http\Requests\Twilio\Confirm2faRequest;
2022-07-27 03:21:12 +02:00
use App\Http\Requests\Twilio\ConfirmSmsRequest;
2022-10-27 07:11:55 +02:00
use App\Http\Requests\Twilio\Generate2faRequest;
2022-07-27 03:21:12 +02:00
use App\Http\Requests\Twilio\GenerateSmsRequest;
use App\Libraries\MultiDB;
2022-10-27 07:11:55 +02:00
use App\Models\User;
2022-07-27 03:21:12 +02:00
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Http\Response;
use Twilio\Rest\Client;
class TwilioController extends BaseController
{
public function __construct()
{
parent::__construct();
}
/**
* Display a listing of the resource.
*
* @return void
*/
public function generate(GenerateSmsRequest $request)
{
$account = auth()->user()->company()->account;
if(MultiDB::hasPhoneNumber($request->phone))
return response()->json(['message' => 'This phone number has already been verified with another account'], 400);
$sid = config('ninja.twilio_account_sid');
$token = config('ninja.twilio_auth_token');
$twilio = new Client($sid, $token);
2022-07-27 15:12:36 +02:00
try {
$verification = $twilio->verify
->v2
->services(config('ninja.twilio_verify_sid'))
->verifications
->create($request->phone, "sms");
}
catch(\Exception $e) {
2022-07-28 03:24:50 +02:00
return response()->json(['message' => 'Invalid phone number please use + country code + number ie. +15552343334'], 400);
2022-07-27 15:12:36 +02:00
}
2022-07-27 03:21:12 +02:00
$account->account_sms_verification_code = $verification->sid;
$account->account_sms_verification_number = $request->phone;
$account->save();
return response()->json(['message' => 'Code sent.'], 200);
}
/**
* Show the form for creating a new resource.
*
* @return void
*/
public function confirm(ConfirmSmsRequest $request)
{
$account = auth()->user()->company()->account;
$sid = config('ninja.twilio_account_sid');
$token = config('ninja.twilio_auth_token');
$twilio = new Client($sid, $token);
$verification_check = $twilio->verify
->v2
->services(config('ninja.twilio_verify_sid'))
->verificationChecks
->create([
"to" => $account->account_sms_verification_number,
"code" => $request->code
]);
if($verification_check->status == 'approved'){
$account->account_sms_verified = true;
$account->save();
2022-11-02 07:36:17 +01:00
//on confirmation we set the users phone number.
$user = auth()->user();
$user->phone = $account->account_sms_verification_number;
2022-11-09 12:22:52 +01:00
$user->verified_phone_number = true;
2022-11-02 07:36:17 +01:00
$user->save();
2022-07-27 03:21:12 +02:00
return response()->json(['message' => 'SMS verified'], 200);
}
return response()->json(['message' => 'SMS not verified'], 400);
2022-10-27 07:11:55 +02:00
}
public function generate2faResetCode(Generate2faRequest $request)
{
$user = User::where('email', $request->email)->first();
if(!$user)
return response()->json(['message' => 'Unable to retrieve user.'], 400);
$sid = config('ninja.twilio_account_sid');
$token = config('ninja.twilio_auth_token');
$twilio = new Client($sid, $token);
try {
$verification = $twilio->verify
->v2
->services(config('ninja.twilio_verify_sid'))
->verifications
->create($user->phone, "sms");
}
catch(\Exception $e) {
2022-07-27 03:21:12 +02:00
2022-10-27 07:11:55 +02:00
return response()->json(['message' => 'Invalid phone number on file, we are unable to reset. Please contact support.'], 400);
}
$user->sms_verification_code = $verification->sid;
$user->save();
return response()->json(['message' => 'Code sent.'], 200);
2022-07-27 03:21:12 +02:00
}
2022-10-27 07:11:55 +02:00
public function confirm2faResetCode(Confirm2faRequest $request)
{
$user = User::where('email', $request->email)->first();
if(!$user)
return response()->json(['message' => 'Unable to retrieve user.'], 400);
$sid = config('ninja.twilio_account_sid');
$token = config('ninja.twilio_auth_token');
$twilio = new Client($sid, $token);
$verification_check = $twilio->verify
->v2
->services(config('ninja.twilio_verify_sid'))
->verificationChecks
->create([
"to" => $user->phone,
"code" => $request->code
]);
if($verification_check->status == 'approved'){
2022-11-15 03:35:24 +01:00
if($request->query('validate_only') == 'true'){
$user->verified_phone_number = true;
$user->save();
2022-11-09 12:22:52 +01:00
return response()->json(['message' => 'SMS verified'], 200);
2022-11-15 03:35:24 +01:00
}
2022-11-09 12:22:52 +01:00
2022-10-27 07:11:55 +02:00
$user->google_2fa_secret = '';
$user->sms_verification_code = '';
$user->save();
return response()->json(['message' => 'SMS verified, 2FA disabled.'], 200);
}
return response()->json(['message' => 'SMS not verified.'], 400);
}
2022-10-27 08:24:49 +02:00
public function validatePhoneNumber()
{
$sid = config('ninja.twilio_account_sid');
$token = config('ninja.twilio_auth_token');
$twilio = new Client($sid, $token);
$phone_number = $twilio->lookups->v1->phoneNumbers("0417918829")
->fetch(["countryCode" => "AU"]);
print($phone_number);
}
2022-07-27 03:21:12 +02:00
}