mirror of
https://github.com/henrywhitaker3/Speedtest-Tracker.git
synced 2025-12-25 23:03:36 +01:00
Added optional authentication
This commit is contained in:
59
app/Console/Commands/AuthenticationCommand.php
Normal file
59
app/Console/Commands/AuthenticationCommand.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Helpers\SettingsHelper;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class AuthenticationCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'speedtest:auth {--enable} {--disable}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Toggle authentication for the app';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$opts = $this->options();
|
||||
|
||||
if($opts['enable'] === true && $opts['disable'] === true) {
|
||||
$this->warn('Please specify only ONE of --enable and --disable');
|
||||
} else if($opts['enable'] === false && $opts['disable'] === false) {
|
||||
$this->warn('You need to specify either --enable OR --disable');
|
||||
} else {
|
||||
if($opts['enable'] === true) {
|
||||
$this->info('Enabling authentication');
|
||||
SettingsHelper::set('auth', true);
|
||||
}
|
||||
|
||||
if($opts['disable'] === true) {
|
||||
$this->info('Disabling authentication');
|
||||
SettingsHelper::set('auth', false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
48
app/Console/Commands/ClearOldSessionsCommand.php
Normal file
48
app/Console/Commands/ClearOldSessionsCommand.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Auth\LoginSession;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Console\Command;
|
||||
use Log;
|
||||
|
||||
class ClearOldSessionsCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'speedtest:clear-sessions';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Clear expired sessions from database';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$now = Carbon::now()->timestamp;
|
||||
$sessions = LoginSession::where('expires', '<=', $now)
|
||||
->delete();
|
||||
$this->info('Invalidated expired sessions');
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,7 @@ class Kernel extends ConsoleKernel
|
||||
{
|
||||
$schedule->job(new SpeedtestJob(true, config('integrations')))->cron(SettingsHelper::get('schedule')['value']);
|
||||
$schedule->command('speedtest:overview')->cron('0 ' . SettingsHelper::get('speedtest_overview_time')->value . ' * * *');
|
||||
$schedule->command('speedtest:clear-sessions')->everyMinute();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -145,7 +145,8 @@ class SettingsHelper {
|
||||
'slack_webhook' => SettingsHelper::settingIsEditable('slack_webhook'),
|
||||
'telegram_bot_token' => SettingsHelper::settingIsEditable('telegram_bot_token'),
|
||||
'telegram_chat_id' => SettingsHelper::settingIsEditable('telegram_chat_id'),
|
||||
]
|
||||
],
|
||||
'auth' => (bool)SettingsHelper::get('auth')->value
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -227,7 +227,7 @@ class SpeedtestHelper {
|
||||
$range = [
|
||||
Carbon::today()
|
||||
];
|
||||
for($i = 0; $i < $days; $i++) {
|
||||
for($i = 0; $i < ($days - 1); $i++) {
|
||||
$prev = end($range);
|
||||
$new = $prev->copy()->subDays(1);
|
||||
array_push($range, $new);
|
||||
|
||||
@@ -8,11 +8,14 @@ use App\Helpers\EmailVerificationHelper;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\LoginSession;
|
||||
use App\Rules\CurrentPasswordMatches;
|
||||
use App\User;
|
||||
use DateTime;
|
||||
use Hash;
|
||||
use Illuminate\Support\Facades\Request as RequestFacade;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Log;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
class AuthController extends Controller
|
||||
@@ -164,6 +167,10 @@ class AuthController extends Controller
|
||||
[ 'expires', '>', time() ]
|
||||
])->get();
|
||||
|
||||
$sessions = $sessions->map(function ($item) {
|
||||
return collect($item)->forget(['token']);
|
||||
});
|
||||
|
||||
return response()->json([
|
||||
'method' => 'get auth sessions',
|
||||
'response' => $sessions
|
||||
@@ -211,4 +218,36 @@ class AuthController extends Controller
|
||||
'success' => true,
|
||||
], 200);
|
||||
}
|
||||
|
||||
public function changePassword(Request $request)
|
||||
{
|
||||
$rules = [
|
||||
'currentPassword' => [ 'string', 'required', new CurrentPasswordMatches() ],
|
||||
'newPassword' => [ 'required', 'string', 'confirmed', 'min:8' ],
|
||||
'logoutDevices' => [ 'required', 'bool' ]
|
||||
];
|
||||
|
||||
$validator = Validator::make($request->all(), $rules);
|
||||
if($validator->fails()) {
|
||||
return response()->json([
|
||||
'method' => 'reset password',
|
||||
'success' => false,
|
||||
'error' => $validator->errors()
|
||||
], 403);
|
||||
}
|
||||
|
||||
$user = Auth::user();
|
||||
|
||||
$user->password = $request->newPassword;
|
||||
$user->save();
|
||||
|
||||
if($request->logoutDevices == true) {
|
||||
AuthLoginSession::where('user_id', $user->id)->update([ 'active' => false ]);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'method' => 'reset password',
|
||||
'success' => true
|
||||
], 200);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Helpers\BackupHelper;
|
||||
use App\Helpers\SettingsHelper;
|
||||
use DateTime;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
@@ -12,6 +13,13 @@ use Illuminate\Http\JsonResponse;
|
||||
class BackupController extends Controller
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
if((bool)SettingsHelper::get('auth')->value === true) {
|
||||
$this->middleware('auth:api');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get backup of speedtests
|
||||
*
|
||||
|
||||
@@ -13,6 +13,13 @@ use Ramsey\Uuid\Exception\InvalidUuidStringException;
|
||||
|
||||
class IntegrationsController extends Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
if((bool)SettingsHelper::get('auth')->value === true) {
|
||||
$this->middleware('auth:api');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the healthchecks config
|
||||
*
|
||||
|
||||
@@ -13,6 +13,13 @@ use Illuminate\Support\Collection;
|
||||
|
||||
class SettingsController extends Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
if((bool)SettingsHelper::get('auth')->value === true) {
|
||||
$this->middleware('auth:api')
|
||||
->except([ 'config' ]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all settings
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Helpers\SettingsHelper;
|
||||
use App\Helpers\SpeedtestHelper;
|
||||
use App\Jobs\SpeedtestJob;
|
||||
use App\Speedtest;
|
||||
@@ -15,6 +16,13 @@ use Illuminate\Http\JsonResponse;
|
||||
|
||||
class SpeedtestController extends Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
if((bool)SettingsHelper::get('auth')->value === true) {
|
||||
$this->middleware('auth:api')
|
||||
->only([ 'run', 'delete', 'deleteAll' ]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns paginated list of speedtests
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Helpers\SettingsHelper;
|
||||
use Exception;
|
||||
use Updater;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -9,6 +10,12 @@ use Illuminate\Http\JsonResponse;
|
||||
|
||||
class UpdateController extends Controller
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
if((bool)SettingsHelper::get('auth')->value === true) {
|
||||
$this->middleware('auth:api');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check for new update
|
||||
|
||||
41
app/Rules/CurrentPasswordMatches.php
Normal file
41
app/Rules/CurrentPasswordMatches.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rules;
|
||||
|
||||
use Hash;
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
|
||||
class CurrentPasswordMatches implements Rule
|
||||
{
|
||||
/**
|
||||
* Create a new rule instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the validation rule passes.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function passes($attribute, $value)
|
||||
{
|
||||
return Hash::check($value, auth()->user()->password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation error message.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function message()
|
||||
{
|
||||
return 'The current password doesn\'t match.';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user