Skip to content

Commit 9234b41

Browse files
feat: gestione login OAuth2 con Keycloak
1 parent 23d3b34 commit 9234b41

4 files changed

Lines changed: 81 additions & 8 deletions

File tree

index.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@
2828
$token = filter('token');
2929

3030
$microsoft = null;
31-
31+
$keycloack = null;
3232
if ($dbo->isConnected()) {
3333
try {
3434
$microsoft = $dbo->selectOne('zz_oauth2', '*', ['name' => 'Microsoft', 'enabled' => 1, 'is_login' => 1]);
35+
$keycloack = $dbo->selectOne('zz_oauth2', '*', ['name' => 'Keycloak', 'enabled' => 1, 'is_login' => 1]);
3536
} catch (QueryException $e) {
3637
}
3738
}
@@ -253,14 +254,23 @@ function brute() {
253254
<i class="fa fa-question-circle mr-1"></i>'.tr('Password dimenticata?').'
254255
</a>
255256
</div>';
256-
if ($microsoft) {
257+
if ($microsoft || $keycloack) {
257258
echo '
258259
<div class="social-auth-links text-center mt-4 pt-3 border-top">
259-
<p class="text-muted">'.tr('- oppure -').'</p>
260-
260+
<p class="text-muted">'.tr('- oppure -').'</p>';
261+
if ($microsoft) {
262+
echo '
261263
<a href="'.base_path_osm().'/oauth2_login.php?id='.$microsoft['id'].'" class="btn btn-block btn-social btn-primary btn-flat shadow-sm">
262264
<i class="fa fa-windows mr-2"></i>'.tr('Accedi con Microsoft').'
263-
</a>
265+
</a>';
266+
}
267+
if ($keycloack) {
268+
echo '
269+
<a href="'.base_path_osm().'/oauth2_login.php?id='.$keycloack['id'].'" class="btn btn-block btn-social btn-info btn-flat shadow-sm">
270+
<i class="fa fa-key mr-2"></i>'.tr('Accedi con Keycloack').'
271+
</a>';
272+
}
273+
echo '
264274
</div>';
265275
}
266276
echo '
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Modules\Emails\OAuth2;
4+
5+
use League\OAuth2\Client\Provider\GenericProvider;
6+
7+
class KeycloakLogin extends GenericProvider implements ProviderInterface
8+
{
9+
/**
10+
* Impostazioni native per la connessione.
11+
*
12+
* @var string[][]
13+
*/
14+
protected static $options = [];
15+
16+
public function __construct(array $options = [], array $collaborators = [])
17+
{
18+
// Configurazioni specifiche per il provider Keycloak
19+
$config = array_merge($options, [
20+
'urlAuthorize' => $options['auth_server_url'].'/realms/'.$options['realm'].'/protocol/openid-connect/auth',
21+
'urlAccessToken' => $options['auth_server_url'].'/realms/'.$options['realm'].'/protocol/openid-connect/token',
22+
'urlResourceOwnerDetails' => $options['auth_server_url'].'/realms/'.$options['realm'].'/protocol/openid-connect/userinfo',
23+
'redirectUri' => base_url().'/oauth2_login.php',
24+
]);
25+
26+
parent::__construct($config, $collaborators);
27+
}
28+
29+
public function getOptions()
30+
{
31+
return self::$options;
32+
}
33+
34+
public static function getConfigInputs()
35+
{
36+
return [
37+
'auth_server_url' => [
38+
'label' => 'Auth Server URL',
39+
'type' => 'text',
40+
],
41+
'realm' => [
42+
'label' => 'Realm',
43+
'type' => 'text',
44+
]
45+
];
46+
}
47+
48+
public function getUser($access_token)
49+
{
50+
$response = $this->getAuthenticatedRequest(
51+
'GET',
52+
$this->getResourceOwnerDetailsUrl($access_token),
53+
$access_token
54+
);
55+
56+
$user = $this->getParsedResponse($response);
57+
58+
return $user['email'] ?? null;
59+
}
60+
}

oauth2_login.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@
2929

3030
// Account individuato via state
3131
if (!empty($state)) {
32-
$account = OAuth2::find($_SESSION['oauth2_id'])
33-
->first();
32+
$account = OAuth2::find($_SESSION['oauth2_id']);
3433
} else {
3534
$account = OAuth2::find(get('id'));
3635

update/2_10.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -506,4 +506,8 @@ CREATE TABLE IF NOT EXISTS `an_automezzi_danni` (
506506

507507
-- Aggiunta colonne minimo e massimo alla tabella mg_listini_articoli per gestire i prezzi per range
508508
ALTER TABLE `mg_listini_articoli` ADD `minimo` decimal(15,6) DEFAULT NULL;
509-
ALTER TABLE `mg_listini_articoli` ADD `massimo` decimal(15,6) DEFAULT NULL;
509+
ALTER TABLE `mg_listini_articoli` ADD `massimo` decimal(15,6) DEFAULT NULL;
510+
511+
-- Aggiunta provider OAuth2 Keycloak
512+
INSERT INTO `zz_oauth2` (`name`, `class`, `client_id`, `client_secret`, `config`, `state`, `access_token`, `refresh_token`, `after_configuration`, `is_login`, `enabled`) VALUES
513+
('Keycloak', 'Modules\\Emails\\OAuth2\\KeycloakLogin', '', '', '{\"auth_server_url\":\"\",\"realm\":\"\"}', '', NULL, NULL, '', 1, 0);

0 commit comments

Comments
 (0)