|
| 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