1212use Drupal \Core \Site \Settings ;
1313use Drupal \Core \StringTranslation \StringTranslationTrait ;
1414use Drupal \Core \Url ;
15+ use Drupal \key \KeyRepositoryInterface ;
1516use Drupal \os2forms_nemlogin_openid_connect \Exception \AuthenticationException ;
1617use Drupal \os2forms_nemlogin_openid_connect \Plugin \os2web \NemloginAuthProvider \OpenIDConnect ;
18+ use Drupal \os2web_key \KeyHelper ;
19+ use Drupal \os2web_key \Plugin \KeyType \OidcKeyType ;
1720use Drupal \os2web_nemlogin \Service \AuthProviderService ;
1821use ItkDev \OpenIdConnect \Security \OpenIdConfigurationProvider ;
1922use Psr \Cache \CacheItemPoolInterface ;
@@ -77,8 +80,10 @@ public function __construct(
7780 private readonly SessionInterface $ session ,
7881 private readonly CacheItemPoolInterface $ cacheItemPool ,
7982 private readonly LanguageManagerInterface $ languageManager ,
80- LoggerInterface $ logger ,
8183 private readonly RendererInterface $ renderer ,
84+ private readonly KeyRepositoryInterface $ keyRepository ,
85+ private readonly KeyHelper $ keyHelper ,
86+ LoggerInterface $ logger ,
8287 ) {
8388 $ this ->setLogger ($ logger );
8489 }
@@ -93,8 +98,10 @@ public static function create(ContainerInterface $container): self {
9398 $ container ->get ('session ' ),
9499 $ container ->get ('drupal_psr6_cache.cache_item_pool ' ),
95100 $ container ->get ('language_manager ' ),
96- $ container ->get ('logger.channel.os2forms_nemlogin_openid_connect ' ),
97101 $ container ->get ('renderer ' ),
102+ $ container ->get ('key.repository ' ),
103+ $ container ->get (KeyHelper::class),
104+ $ container ->get ('logger.channel.os2forms_nemlogin_openid_connect ' ),
98105 );
99106 }
100107
@@ -140,12 +147,35 @@ public function main(string $id) {
140147 private function getOpenIdConfigurationProvider (): OpenIdConfigurationProvider {
141148 $ pluginConfiguration = $ this ->plugin ->getConfiguration ();
142149
150+ if (OpenIDConnect::PROVIDER_TYPE_KEY === $ pluginConfiguration [OpenIDConnect::PROVIDER_TYPE_KEY ]) {
151+ $ discoveryUrl = $ pluginConfiguration [OpenIDConnect::DISCOVERY_URL ];
152+ $ clientId = $ pluginConfiguration [OpenIDConnect::CLIENT_ID ];
153+ $ clientSecret = $ pluginConfiguration [OpenIDConnect::CLIENT_SECRET ];
154+ }
155+ else {
156+ try {
157+ $ keyId = $ pluginConfiguration [OpenIDConnect::KEY ] ?? '' ;
158+ $ key = $ this ->keyRepository ->getKey ($ keyId );
159+ if (NULL === $ key ) {
160+ throw new \RuntimeException (sprintf ('Cannot get key %s ' , $ keyId ));
161+ }
162+ [
163+ OidcKeyType::DISCOVERY_URL => $ discoveryUrl ,
164+ OidcKeyType::CLIENT_ID => $ clientId ,
165+ OidcKeyType::CLIENT_SECRET => $ clientSecret ,
166+ ] = $ this ->keyHelper ->getOidcValues ($ key );
167+ }
168+ catch (\Exception $ e ) {
169+ throw new AuthenticationException ('Cannot get client id and secret ' , $ e ->getCode (), $ e );
170+ }
171+ }
172+
143173 $ providerOptions = [
144174 'redirectUri ' => $ this ->getRedirectUri (),
145- 'openIDConnectMetadataUrl ' => $ pluginConfiguration [ ' nemlogin_openid_connect_discovery_url ' ] ,
175+ 'openIDConnectMetadataUrl ' => $ discoveryUrl ,
146176 'cacheItemPool ' => $ this ->cacheItemPool ,
147- 'clientId ' => $ pluginConfiguration [ ' nemlogin_openid_connect_client_id ' ] ,
148- 'clientSecret ' => $ pluginConfiguration [ ' nemlogin_openid_connect_client_secret ' ] ,
177+ 'clientId ' => $ clientId ,
178+ 'clientSecret ' => $ clientSecret ,
149179 'localTestMode ' => FALSE ,
150180 'allowHttp ' => (bool ) ($ this ->getSettings ()['allow_http ' ] ?? FALSE ),
151181 ];
@@ -273,7 +303,7 @@ public function endSession(string $id): Response {
273303 private function getPostLogoutRedirectUri (): string {
274304 try {
275305 $ pluginConfiguration = $ this ->plugin ->getConfiguration ();
276- $ url = $ pluginConfiguration [' nemlogin_openid_connect_post_logout_redirect_uri ' ] ?? '/ ' ;
306+ $ url = $ pluginConfiguration [OpenIDConnect:: POST_LOGOUT_REDIRECT_URI ] ?? '/ ' ;
277307 $ options = [
278308 'absolute ' => TRUE ,
279309 'path_processing ' => FALSE ,
0 commit comments