Skip to content

Commit c94217f

Browse files
committed
Add basic auth via Laravel Middlewares
1 parent d8dc1a4 commit c94217f

6 files changed

Lines changed: 201 additions & 3 deletions

File tree

src/Auth.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ protected function saveToSession()
863863
*
864864
* @param int $user_id
865865
*/
866-
protected function identifyUser($user_id)
866+
public function identifyUser($user_id)
867867
{
868868
$database = database();
869869

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Middlewares;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Support\Facades\Auth;
7+
use Models\User;
8+
use Models\UserTokens;
9+
use Symfony\Component\HttpFoundation\Response;
10+
11+
class APIAuthMiddleware
12+
{
13+
/**
14+
* Handle an incoming request.
15+
*
16+
* @param \Closure(Request): (Response) $next
17+
*/
18+
public function handle(Request $request, \Closure $next): Response
19+
{
20+
if (!Auth::user()) {
21+
$token = $request->headers->get('X-API-Key');
22+
23+
$user = null;
24+
if (!empty($token)) {
25+
$user_match = UserTokens::where('enabled', 1)->find($token);
26+
27+
if ($user_match) {
28+
$user = User::with('group')->find($user_match->id_utente);
29+
}
30+
}
31+
32+
if ($user) {
33+
Auth::once($user);
34+
35+
return $next($request);
36+
}
37+
}
38+
39+
// Disabilita autenticazione su base delle opzioni
40+
if (config('osm.api_development', false)) {
41+
return $next($request);
42+
}
43+
44+
return response()->json(['error' => 'Unauthenticated.'], 401);
45+
}
46+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Middlewares;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Support\Facades\Auth;
7+
use Models\User;
8+
use Symfony\Component\HttpFoundation\Response;
9+
10+
class OSMAuthMiddleware
11+
{
12+
/**
13+
* Handle an incoming request.
14+
*
15+
* @param \Closure(Request): (Response) $next
16+
*/
17+
public function handle(Request $request, \Closure $next): Response
18+
{
19+
$base_path = $request->url();
20+
21+
$base_path = substr($base_path, stripos($base_path, $request->host()) + strlen($request->host()));
22+
if (stripos($base_path, '/public/') !== false) {
23+
$base_path = substr($base_path, 0, stripos($base_path, '/public/'));
24+
}
25+
26+
// Sicurezza della sessioni
27+
ini_set('session.cookie_samesite', 'lax');
28+
ini_set('session.use_trans_sid', '0');
29+
ini_set('session.use_only_cookies', '1');
30+
31+
session_set_cookie_params(0, $base_path, null, isHTTPS(true));
32+
session_start();
33+
34+
$user = null;
35+
if (isset($_SESSION['id_utente'])) {
36+
$user = User::with('group')->find($_SESSION['id_utente']);
37+
}
38+
39+
if ($user && !Auth::user()) {
40+
Auth::login($user);
41+
auth_osm()->identifyUser($user->id);
42+
}
43+
if (!$user && Auth::user()) {
44+
Auth::logout();
45+
auth_osm()->destroy();
46+
}
47+
48+
return $next($request);
49+
}
50+
}

src/Models/User.php

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@
2323
use Common\SimpleModelTrait;
2424
use Illuminate\Database\Eloquent\Model;
2525
use Modules\Anagrafiche\Anagrafica;
26+
use Illuminate\Contracts\Auth\Authenticatable;
2627

27-
class User extends Model
28+
class User extends Model implements Authenticatable
2829
{
2930
use SimpleModelTrait;
3031

@@ -250,4 +251,38 @@ public function modules()
250251
{
251252
return $this->group->modules();
252253
}
254+
255+
public function getAuthIdentifierName() : string
256+
{
257+
return $this->username;
258+
}
259+
260+
public function getAuthIdentifier() : mixed
261+
{
262+
return $this->id;
263+
}
264+
265+
public function getAuthPassword() : string
266+
{
267+
return $this->password;
268+
}
269+
270+
public function getAuthPasswordName() : string
271+
{
272+
return 'password';
273+
}
274+
275+
public function getRememberToken() : string
276+
{
277+
return '';
278+
}
279+
280+
public function setRememberToken($value)
281+
{
282+
}
283+
284+
public function getRememberTokenName() : string
285+
{
286+
return '';
287+
}
253288
}

src/Models/UserTokens.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
5+
* Copyright (C) DevCode s.r.l.
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
19+
*/
20+
21+
namespace Models;
22+
23+
use Common\SimpleModelTrait;
24+
use Illuminate\Database\Eloquent\Model;
25+
use Modules\Anagrafiche\Anagrafica;
26+
use Illuminate\Contracts\Auth\Authenticatable;
27+
28+
class UserTokens extends Model
29+
{
30+
use SimpleModelTrait;
31+
32+
protected $table = 'zz_tokens';
33+
34+
public static function build(?Group $gruppo = null, $username = null, $email = null, $password = null)
35+
{
36+
$model = new static();
37+
$model->save();
38+
39+
return $model;
40+
}
41+
42+
/* Relazioni Eloquent */
43+
44+
public function user()
45+
{
46+
return $this->belongsTo(User::class, 'id_utente');
47+
}
48+
}

src/Providers/AppServiceProvider.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use ApiPlatform\State\ProcessorInterface;
66
use Illuminate\Support\ServiceProvider;
7+
use Illuminate\Http\Request;
8+
use Models\Locale;
79

810
class AppServiceProvider extends ServiceProvider
911
{
@@ -20,6 +22,23 @@ public function register(): void
2022
*/
2123
public function boot(): void
2224
{
23-
//
25+
// Connect to database at boot
26+
database();
27+
28+
$translator = trans_osm();
29+
$translator->addLocalePath(base_dir().'/locale');
30+
$translator->addLocalePath(base_dir().'/modules/*/locale');
31+
$formatter = !empty(config()->get('osm.formatter')) ? config()->get('osm.formatter') : [];
32+
33+
// Inizializzazione traduzioni
34+
if (database()->tableExists('zz_settings') && database()->tableExists('zz_langs')) {
35+
$id_lang = setting('Lingua');
36+
Locale::setDefault($id_lang);
37+
Locale::setPredefined();
38+
39+
$lang = Locale::find($id_lang)->language_code;
40+
$translator->setLocale($lang, $formatter);
41+
}
42+
2443
}
2544
}

0 commit comments

Comments
 (0)