Skip to content

Commit 9e6373e

Browse files
committed
Added Keycloak provider
1 parent 31116ec commit 9e6373e

File tree

4 files changed

+180
-8
lines changed

4 files changed

+180
-8
lines changed

composer.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,24 @@
1111
{
1212
"url": "https://github.com/bahuma20/oauth2-nextcloud.git",
1313
"type": "git"
14-
}
14+
},
15+
{
16+
"url": "https://github.com/stevenmaguire/oauth2-keycloak.git",
17+
"type": "git"
18+
}
1519
],
1620
"require": {
1721
"bahuma/oauth2-nextcloud": "=2.0.0",
1822
"jefferson49/webtrees-common": ">=1.2.8",
1923
"league/oauth2-client": "=2.8.0",
2024
"league/oauth2-github": "=3.1.1",
2125
"league/oauth2-google": "=4.0.1",
22-
"stevenmaguire/oauth2-dropbox": "dev-Remove-scope-from-getAccessToken"
26+
"stevenmaguire/oauth2-dropbox": "dev-Remove-scope-from-getAccessToken",
27+
"stevenmaguire/oauth2-keycloak": "=5.1.0"
2328
},
2429
"replace": {
2530
"guzzlehttp/guzzle": "*",
26-
"paragonie/random_compat": "*"
31+
"paragonie/random_compat": "*",
32+
"firebase/php-jwt": "*"
2733
}
2834
}

composer.lock

Lines changed: 62 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/LoginWithAuthorizationProviderAction.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,10 @@ public function handle(ServerRequestInterface $request): ResponseInterface
255255
$authorization_provider_id = $user_data_from_provider->getAuthorizationProviderUserId();
256256

257257
CustomModuleLog::addDebugLog($log_module, 'Adjusted user data from authorization provider to webtrees' . ': ' . json_encode([
258-
'authorization_provider_i' => $authorization_provider_id,
259-
'user_name' => $user_name,
260-
'real_name' => $real_name,
261-
'email' => $email,
258+
'authorization_provider_id' => $authorization_provider_id,
259+
'user_name' => $user_name,
260+
'real_name' => $real_name,
261+
'email' => $email,
262262
]));
263263

264264
$provider_to_connect = Session::get(OAuth2Client::activeModuleName() . OAuth2Client::SESSION_PROVIDER_TO_CONNECT, '');
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
/**
4+
* webtrees: online genealogy
5+
* Copyright (C) 2024 webtrees development team
6+
* <http://webtrees.net>
7+
*
8+
* OAuth2Client (webtrees custom module):
9+
* Copyright (C) 2024 Markus Hemprich
10+
* <http://www.familienforschung-hemprich.de>
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU General Public License as published by
14+
* the Free Software Foundation, either version 3 of the License, or
15+
* (at your option) any later version.
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU General Public License for more details.
20+
* You should have received a copy of the GNU General Public License
21+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*
23+
*
24+
* OAuth2-Client
25+
*
26+
* A weebtrees(https://webtrees.net) 2.1 custom module to implement an OAuth2 client
27+
*
28+
*/
29+
30+
declare(strict_types=1);
31+
32+
namespace Jefferson49\Webtrees\Module\OAuth2Client\Provider;
33+
34+
use Fisharebest\Webtrees\User;
35+
use Jefferson49\Webtrees\Module\OAuth2Client\AuthorizationProviderUser;
36+
use Jefferson49\Webtrees\Module\OAuth2Client\Contracts\AuthorizationProviderInterface;
37+
use League\OAuth2\Client\Provider\AbstractProvider;
38+
use Stevenmaguire\OAuth2\Client\Provider\Keycloak;
39+
use League\OAuth2\Client\Token\AccessToken;
40+
use League\OAuth2\Client\Tool\ArrayAccessorTrait;
41+
42+
43+
/**
44+
* An OAuth2 authorization client for Keycloak
45+
*/
46+
class KeycloakAuthorizationProvider extends AbstractAuthorizationProvider implements AuthorizationProviderInterface
47+
{
48+
use ArrayAccessorTrait;
49+
50+
//The authorization provider
51+
protected AbstractProvider $provider;
52+
53+
/**
54+
* @param string $redirectUri
55+
* @param array $options
56+
* @param array $collaborators
57+
*/
58+
public function __construct(string $redirectUri, array $options = [], array $collaborators = [])
59+
{
60+
$options = array_merge($options, [
61+
'redirectUri' => $redirectUri,
62+
]);
63+
64+
$this->provider = new Keycloak($options, $collaborators);
65+
66+
if (isset($options['signInButtonLabel'])) {
67+
$this->setSignInButtonLabel($options['signInButtonLabel']);
68+
}
69+
}
70+
71+
/**
72+
* Use access token to get user data from provider and return it as a webtrees User object
73+
*
74+
* @param AccessToken $token
75+
*
76+
* @return User
77+
*/
78+
public function getUserData(AccessToken $token) : AuthorizationProviderUser {
79+
80+
$user = parent::getUserData($token);
81+
$resource_owner = $user->getRessourceOwner();
82+
83+
//Apply specific user data provided by Keycloak
84+
$user->setUserName($resource_owner->getUserName() ?? '');
85+
$user->setRealName($resource_owner->getName() ?? '');
86+
$user->setEmail($resource_owner->getEmail() ?? '');
87+
88+
return $user;
89+
}
90+
91+
/**
92+
* Returns a list with options that can be passed to the provider
93+
*
94+
* @return array An array of option names, which can be set for this provider.
95+
* Options include `clientId`, `clientSecret`, `redirectUri`, etc.
96+
*/
97+
public static function getRequiredOptions() : array {
98+
return [
99+
'clientId',
100+
'clientSecret',
101+
'authServerUrl',
102+
'realm',
103+
];
104+
}
105+
}

0 commit comments

Comments
 (0)