From dbbf8f5b9ce4d540fa264946c22650e9b74900d5 Mon Sep 17 00:00:00 2001 From: Gustavo pantoja <gustavopantoja.ap@gmail.com> Date: Tue, 22 Aug 2023 18:43:01 -0300 Subject: [PATCH 1/3] adicionado o campos de boqueio --- app/Http/Controllers/AuthController.php | 39 ++++++++--- app/Http/Controllers/ParamentroController.php | 65 +++++++++++++++++++ app/Http/Kernel.php | 2 + app/Http/Middleware/BlockIpMiddleware.php | 24 +++++++ app/Models/Paramentro.php | 11 ++++ ..._08_22_145013_create_paramentros_table.php | 29 +++++++++ routes/web.php | 7 ++ 7 files changed, 167 insertions(+), 10 deletions(-) create mode 100644 app/Http/Controllers/ParamentroController.php create mode 100644 app/Http/Middleware/BlockIpMiddleware.php create mode 100644 app/Models/Paramentro.php create mode 100644 database/migrations/2023_08_22_145013_create_paramentros_table.php diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index cac0fc7..802362d 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -11,7 +11,7 @@ class AuthController extends Controller { public function __construct() { - $this->middleware('auth:api', ['except' => ['login2','login', 'termoAceite', + $this->middleware('auth:api', ['except' => ['login2', 'login', 'termoAceite', 'grupo', 'grupoMenu']]); } @@ -79,35 +79,54 @@ public function login() $user = \App\Models\User::where('cpf', $cpf)->first(); if ($user) { - // Verifica se a conta está bloqueada - if ($user->blocked_until && now() < $user->blocked_until) { - return response()->json(['error' => 'Account is blocked'], 401); + if ($user->is_blocked) { + if ($user->blocked_until > now()) { + return response()->json(['error' => 'Unauthorized - Account is temporarily blocked'], 401); + } else { + $user->is_blocked = 0; + $user->blocked_until = null; + $user->login_attempts = 0; + $user->save(); + } } $credentials = ['cpf' => $cpf, 'password' => $password]; if (!Auth::attempt($credentials)) { - // Aumenta o contador de tentativas $user->login_attempts++; if ($user->login_attempts >= 5) { - // Bloqueia a conta por 1 hora após 5 tentativas malsucedidas - $user->is_blocked=1; + $user->is_blocked = 1; $user->blocked_until = now()->addHour(); } $user->save(); - return response()->json(['error' => 'Unauthorized'], 401); + return response()->json(['error' => 'Unauthorized - Invalid credentials'], 401); + } + + if ($this->hasActiveToken($user)) { + $user->tokens->each(function ($token) { + // $token->delete(); + }); + + return response()->json(['error' => 'Unauthorized - Session is active in another place. All sessions have been terminated.'], 401); } - // O login foi bem-sucedido, redefina o contador de tentativas $user->login_attempts = 0; $user->save(); + $token = $user->createToken('api-token')->plainTextToken; return $this->respondWithToken(Auth::attempt($credentials)); } + // ... } + + protected function hasActiveToken($user) + { + return $user->tokens->isNotEmpty(); + } + public function login2() { $ip = \request()->getClientIps(); @@ -131,7 +150,7 @@ public function login2() $user->login_attempts++; if ($user->login_attempts >= 5) { // Bloqueia a conta por 1 hora após 5 tentativas malsucedidas - $user->is_blocked=1; + $user->is_blocked = 1; $user->blocked_until = now()->addHour(); } $user->save(); diff --git a/app/Http/Controllers/ParamentroController.php b/app/Http/Controllers/ParamentroController.php new file mode 100644 index 0000000..a7c302d --- /dev/null +++ b/app/Http/Controllers/ParamentroController.php @@ -0,0 +1,65 @@ +<?php + +namespace App\Http\Controllers; + +use App\Models\Paramentro; +use Illuminate\Http\Request; + +class ParamentroController extends Controller +{ + /** + * Display a listing of the resource. + */ + public function index() + { + // + } + + /** + * Show the form for creating a new resource. + */ + public function create() + { + // + } + + /** + * Store a newly created resource in storage. + */ + public function store(Request $request) + { + // + } + + /** + * Display the specified resource. + */ + public function show(Paramentro $paramentro) + { + // + } + + /** + * Show the form for editing the specified resource. + */ + public function edit(Paramentro $paramentro) + { + // + } + + /** + * Update the specified resource in storage. + */ + public function update(Request $request, Paramentro $paramentro) + { + // + } + + /** + * Remove the specified resource from storage. + */ + public function destroy(Paramentro $paramentro) + { + // + } +} diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 8dca3d1..d21d9ec 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -2,6 +2,7 @@ namespace App\Http; +use App\Http\Middleware\BlockIpMiddleware; use App\Http\Middleware\JWTMiddleware; use Illuminate\Foundation\Http\Kernel as HttpKernel; @@ -68,6 +69,7 @@ class Kernel extends HttpKernel 'signed' => \App\Http\Middleware\ValidateSignature::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class, + // 'blockIP'=> BlockIpMiddleware::class // 'JWTMiddleware' => JWTMiddleware::class ]; } diff --git a/app/Http/Middleware/BlockIpMiddleware.php b/app/Http/Middleware/BlockIpMiddleware.php new file mode 100644 index 0000000..ea0c531 --- /dev/null +++ b/app/Http/Middleware/BlockIpMiddleware.php @@ -0,0 +1,24 @@ +<?php + +namespace App\Http\Middleware; + +use Closure; +use Illuminate\Http\Request; +use Symfony\Component\HttpFoundation\Response; + +class BlockIpMiddleware +{ + public $blockIps = ['whitelist-ip-1', 'whitelist-ip-2', '127.0.0.1']; + /** + * Handle an incoming request. + * + * @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next + */ + public function handle(Request $request, Closure $next): Response + { + if (in_array($request->ip(), $this->blockIps)) { + abort(403, "You are restricted to access the site."); + } + return $next($request); + } +} diff --git a/app/Models/Paramentro.php b/app/Models/Paramentro.php new file mode 100644 index 0000000..9b39fc5 --- /dev/null +++ b/app/Models/Paramentro.php @@ -0,0 +1,11 @@ +<?php + +namespace App\Models; + +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Model; + +class Paramentro extends Model +{ + use HasFactory; +} diff --git a/database/migrations/2023_08_22_145013_create_paramentros_table.php b/database/migrations/2023_08_22_145013_create_paramentros_table.php new file mode 100644 index 0000000..f7261da --- /dev/null +++ b/database/migrations/2023_08_22_145013_create_paramentros_table.php @@ -0,0 +1,29 @@ +<?php + +use Illuminate\Database\Migrations\Migration; +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Facades\Schema; + +return new class extends Migration +{ + /** + * Run the migrations. + */ + public function up(): void + { + Schema::create('paramentros', function (Blueprint $table) { + $table->id(); + $table->foreignIdFor(\App\Models\Consignante::class); + $table->json('parametros'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('paramentros'); + } +}; diff --git a/routes/web.php b/routes/web.php index 776ad71..e671607 100644 --- a/routes/web.php +++ b/routes/web.php @@ -254,3 +254,10 @@ return "Erro de conexão: " . $e->getMessage(); } }); + + +Route::get('testeuser/{id}',function ($id){ + $usuario = \App\Models\UserSistema::find($id); + dd($usuario->UsuarioAcesso->servidor); + +}); -- GitLab From 32ceb361919415720c3a1c60127ce22872a33dc7 Mon Sep 17 00:00:00 2001 From: Gustavo pantoja <gustavopantoja.ap@gmail.com> Date: Tue, 22 Aug 2023 18:43:22 -0300 Subject: [PATCH 2/3] adicionado o campos de boqueio --- app/Http/Controllers/AuthController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index 802362d..4af00dc 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -111,6 +111,7 @@ public function login() return response()->json(['error' => 'Unauthorized - Session is active in another place. All sessions have been terminated.'], 401); } + $user->login_attempts = 0; $user->save(); -- GitLab From a1c8a67f18e3696953e3fcd7343677221f801537 Mon Sep 17 00:00:00 2001 From: Gustavo pantoja <gustavopantoja.ap@gmail.com> Date: Wed, 23 Aug 2023 12:03:53 -0300 Subject: [PATCH 3/3] adicionado o campos de boqueio --- app/Http/Controllers/AuthController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index 4af00dc..4d20bd2 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -112,6 +112,7 @@ public function login() } + $user->login_attempts = 0; $user->save(); -- GitLab