2727class JsonWebKeySetService
2828{
2929 /** @var JWKSet JWKS for OIDC protocol. */
30- private readonly JWKSet $ protocolJwkSet ;
30+ protected JWKSet $ protocolJwkSet ;
3131 /** @var JWKSet|null JWKS for OpenID Federation. */
32- private ?JWKSet $ federationJwkSet = null ;
32+ protected ?JWKSet $ federationJwkSet = null ;
3333
3434 /**
3535 * @throws \SimpleSAML\Error\Exception
3636 * @throws \Exception
3737 */
38- public function __construct (ModuleConfig $ moduleConfig )
39- {
40- $ publicKeyPath = $ moduleConfig ->getProtocolCertPath ();
41- if (!file_exists ($ publicKeyPath )) {
42- throw new Error \Exception ("OIDC protocol public key file does not exists: $ publicKeyPath. " );
43- }
38+ public function __construct (
39+ protected readonly ModuleConfig $ moduleConfig ,
40+ ) {
41+ $ this ->prepareProtocolJwkSet ();
4442
45- $ jwk = JWKFactory::createFromKeyFile ($ publicKeyPath , null , [
46- ClaimsEnum::Kid->value => FingerprintGenerator::forFile ($ publicKeyPath ),
47- ClaimsEnum::Use->value => PublicKeyUseEnum::Signature->value ,
48- ClaimsEnum::Alg->value => $ moduleConfig ->getProtocolSigner ()->algorithmId (),
49- ]);
50-
51- $ this ->protocolJwkSet = new JWKSet ([$ jwk ]);
52-
53- if (
54- ($ federationPublicKeyPath = $ moduleConfig ->getFederationCertPath ()) &&
55- file_exists ($ federationPublicKeyPath ) &&
56- ($ federationSigner = $ moduleConfig ->getFederationSigner ())
57- ) {
58- $ federationJwk = JWKFactory::createFromKeyFile ($ federationPublicKeyPath , null , [
59- ClaimsEnum::Kid->value => FingerprintGenerator::forFile ($ federationPublicKeyPath ),
60- ClaimsEnum::Use->value => PublicKeyUseEnum::Signature->value ,
61- ClaimsEnum::Alg->value => $ federationSigner ->algorithmId (),
62- ]);
63-
64- $ this ->federationJwkSet = new JWKSet ([$ federationJwk ]);
65- }
43+ $ this ->prepareFederationJwkSet ();
6644 }
6745
6846 /**
@@ -84,4 +62,72 @@ public function federationKeys(): array
8462
8563 return $ this ->federationJwkSet ->all ();
8664 }
65+
66+ /**
67+ * @throws \ReflectionException
68+ * @throws \SimpleSAML\Error\Exception
69+ */
70+ protected function prepareProtocolJwkSet (): void
71+ {
72+ $ protocolPublicKeyPath = $ this ->moduleConfig ->getProtocolCertPath ();
73+
74+ if (!file_exists ($ protocolPublicKeyPath )) {
75+ throw new Error \Exception ("OIDC protocol public key file does not exists: $ protocolPublicKeyPath. " );
76+ }
77+
78+ $ jwk = JWKFactory::createFromKeyFile ($ protocolPublicKeyPath , null , [
79+ ClaimsEnum::Kid->value => FingerprintGenerator::forFile ($ protocolPublicKeyPath ),
80+ ClaimsEnum::Use->value => PublicKeyUseEnum::Signature->value ,
81+ ClaimsEnum::Alg->value => $ this ->moduleConfig ->getProtocolSigner ()->algorithmId (),
82+ ]);
83+
84+ $ keys = [$ jwk ];
85+
86+ if (
87+ ($ protocolNewPublicKeyPath = $ this ->moduleConfig ->getProtocolNewCertPath ()) &&
88+ file_exists ($ protocolNewPublicKeyPath )
89+ ) {
90+ $ newJwk = JWKFactory::createFromKeyFile ($ protocolNewPublicKeyPath , null , [
91+ ClaimsEnum::Kid->value => FingerprintGenerator::forFile ($ protocolNewPublicKeyPath ),
92+ ClaimsEnum::Use->value => PublicKeyUseEnum::Signature->value ,
93+ ClaimsEnum::Alg->value => $ this ->moduleConfig ->getProtocolSigner ()->algorithmId (),
94+ ]);
95+
96+ $ keys [] = $ newJwk ;
97+ }
98+
99+ $ this ->protocolJwkSet = new JWKSet ($ keys );
100+ }
101+
102+ protected function prepareFederationJwkSet (): void
103+ {
104+ $ federationPublicKeyPath = $ this ->moduleConfig ->getFederationCertPath ();
105+
106+ if (!file_exists ($ federationPublicKeyPath )) {
107+ return ;
108+ }
109+
110+ $ federationJwk = JWKFactory::createFromKeyFile ($ federationPublicKeyPath , null , [
111+ ClaimsEnum::Kid->value => FingerprintGenerator::forFile ($ federationPublicKeyPath ),
112+ ClaimsEnum::Use->value => PublicKeyUseEnum::Signature->value ,
113+ ClaimsEnum::Alg->value => $ this ->moduleConfig ->getFederationSigner ()->algorithmId (),
114+ ]);
115+
116+ $ keys = [$ federationJwk ];
117+
118+ if (
119+ ($ federationNewPublicKeyPath = $ this ->moduleConfig ->getFederationNewCertPath ()) &&
120+ file_exists ($ federationNewPublicKeyPath )
121+ ) {
122+ $ federationNewJwk = JWKFactory::createFromKeyFile ($ federationNewPublicKeyPath , null , [
123+ ClaimsEnum::Kid->value => FingerprintGenerator::forFile ($ federationNewPublicKeyPath ),
124+ ClaimsEnum::Use->value => PublicKeyUseEnum::Signature->value ,
125+ ClaimsEnum::Alg->value => $ this ->moduleConfig ->getFederationSigner ()->algorithmId (),
126+ ]);
127+
128+ $ keys [] = $ federationNewJwk ;
129+ }
130+
131+ $ this ->federationJwkSet = new JWKSet ($ keys );
132+ }
87133}
0 commit comments