22
33namespace Drupal \os2web_key ;
44
5+ use Drupal \Core \DependencyInjection \DependencySerializationTrait ;
56use Drupal \Core \Logger \LoggerChannelInterface ;
67use Drupal \key \KeyInterface ;
78use Drupal \os2web_key \Exception \RuntimeException ;
89use Drupal \os2web_key \Plugin \KeyType \CertificateKeyType ;
10+ use Drupal \os2web_key \Plugin \KeyType \OidcKeyType ;
911use Psr \Log \LoggerAwareTrait ;
1012
1113/**
12- * Certificate helper.
14+ * Key helper.
1315 */
14- class CertificateHelper {
16+ class KeyHelper {
17+ use DependencySerializationTrait;
1518 use LoggerAwareTrait;
1619
17- protected const FORMAT_PEM = 'pem ' ;
18- protected const FORMAT_PFX = 'pfx ' ;
19- protected const CERT = 'cert ' ;
20- protected const PKEY = 'pkey ' ;
21-
2220 public function __construct (
2321 LoggerChannelInterface $ logger ,
2422 ) {
@@ -31,15 +29,15 @@ public function __construct(
3129 * @param \Drupal\key\KeyInterface $key
3230 * The key.
3331 *
34- * @return array< string, string>
32+ * @return array{cert: string, pkey: string}
3533 * The certificates.
3634 */
3735 public function getCertificates (KeyInterface $ key ): array {
38- $ contents = $ key ->getKeyValue ();
3936 $ type = $ key ->getKeyType ();
4037 if (!($ type instanceof CertificateKeyType)) {
41- throw new RuntimeException (sprintf ('Invalid key type: %s ' , $ type ::class));
38+ throw $ this -> createSslRuntimeException (sprintf ('Invalid key type: %s ' , $ type ::class), $ key );
4239 }
40+ $ contents = $ key ->getKeyValue ();
4341
4442 return $ this ->parseCertificates (
4543 $ contents ,
@@ -50,9 +48,43 @@ public function getCertificates(KeyInterface $key): array {
5048 }
5149
5250 /**
53- * Read a certificate.
51+ * Get OIDC values from a key.
52+ *
53+ * @param \Drupal\key\KeyInterface $key
54+ * The key.
5455 *
55- * @return array<string, string>
56+ * @return array{discovery_url: string, client_id: string, client_secret: string}
57+ * The OIDC values.
58+ */
59+ public function getOidcValues (KeyInterface $ key ): array {
60+ $ type = $ key ->getKeyType ();
61+ if (!($ type instanceof OidcKeyType)) {
62+ throw $ this ->createSslRuntimeException (sprintf ('Invalid key type: %s ' , $ type ::class), $ key );
63+ }
64+ $ contents = $ key ->getKeyValue ();
65+
66+ try {
67+ $ values = json_decode ($ contents , TRUE , 512 , JSON_THROW_ON_ERROR );
68+ foreach ([
69+ OidcKeyType::DISCOVERY_URL ,
70+ OidcKeyType::CLIENT_ID ,
71+ OidcKeyType::CLIENT_SECRET ,
72+ ] as $ name ) {
73+ if (!isset ($ values [$ name ])) {
74+ throw $ this ->createRuntimeException (sprintf ("Missing OIDC value: %s " , $ name ), $ key );
75+ }
76+ }
77+ return $ values ;
78+ }
79+ catch (\JsonException $ e ) {
80+ throw $ this ->createRuntimeException (sprintf ("Cannot get OIDC values: %s " , $ e ->getMessage ()), $ key );
81+ }
82+ }
83+
84+ /**
85+ * Parse certificates.
86+ *
87+ * @return array{cert: string, pkey: string}
5688 * The certificates.
5789 */
5890 public function parseCertificates (
@@ -62,17 +94,17 @@ public function parseCertificates(
6294 ?KeyInterface $ key ,
6395 ): array {
6496 $ certificates = [
65- self ::CERT => NULL ,
66- self ::PKEY => NULL ,
97+ CertificateKeyType ::CERT => NULL ,
98+ CertificateKeyType ::PKEY => NULL ,
6799 ];
68100 switch ($ format ) {
69- case self ::FORMAT_PFX :
101+ case CertificateKeyType ::FORMAT_PFX :
70102 if (!openssl_pkcs12_read ($ contents , $ certificates , $ passphrase )) {
71103 throw $ this ->createSslRuntimeException ('Error reading certificate ' , $ key );
72104 }
73105 break ;
74106
75- case self ::FORMAT_PEM :
107+ case CertificateKeyType ::FORMAT_PEM :
76108 $ certificate = @openssl_x509_read ($ contents );
77109 if (FALSE === $ certificate ) {
78110 throw $ this ->createSslRuntimeException ('Error reading certificate ' , $ key );
@@ -90,7 +122,7 @@ public function parseCertificates(
90122 break ;
91123 }
92124
93- if (!isset ($ certificates [self ::CERT ], $ certificates [self ::PKEY ])) {
125+ if (!isset ($ certificates [CertificateKeyType ::CERT ], $ certificates [CertificateKeyType ::PKEY ])) {
94126 throw $ this ->createRuntimeException ("Cannot read certificate parts 'cert' and 'pkey' " , $ key );
95127 }
96128
@@ -101,40 +133,30 @@ public function parseCertificates(
101133 * Create a passwordless certificate.
102134 */
103135 public function createPasswordlessCertificate (array $ certificates , string $ format , ?KeyInterface $ key ): string {
104- $ cert = $ certificates [self ::CERT ] ?? NULL ;
136+ $ cert = $ certificates [CertificateKeyType ::CERT ] ?? NULL ;
105137 if (!isset ($ cert )) {
106138 throw $ this ->createRuntimeException ('Certificate part "cert" not found ' , $ key );
107139 }
108140
109- $ pkey = $ certificates [self ::PKEY ] ?? NULL ;
141+ $ pkey = $ certificates [CertificateKeyType ::PKEY ] ?? NULL ;
110142 if (!isset ($ pkey )) {
111143 throw $ this ->createRuntimeException ('Certificate part "pkey" not found ' , $ key );
112144 }
113145
114146 $ output = '' ;
115147 switch ($ format ) {
116- case self ::FORMAT_PEM :
148+ case CertificateKeyType ::FORMAT_PEM :
117149 $ parts = ['' , '' ];
118150 if (!@openssl_x509_export ($ cert , $ parts [0 ])) {
119151 throw $ this ->createSslRuntimeException ('Cannot export certificate ' , $ key );
120152 }
121153 if (!@openssl_pkey_export ($ pkey , $ parts [1 ])) {
122154 throw $ this ->createSslRuntimeException ('Cannot export private key ' , $ key );
123155 }
124- $ extracerts = $ certificates ['extracerts ' ] ?? NULL ;
125- if (is_array ($ extracerts )) {
126- foreach ($ extracerts as $ extracert ) {
127- $ part = '' ;
128- if (!@openssl_x509_export ($ extracert , $ part )) {
129- throw $ this ->createSslRuntimeException ('Cannot export certificate ' , $ key );
130- }
131- // $parts[] = $part;
132- }
133- }
134156 $ output = implode ('' , $ parts );
135157 break ;
136158
137- case self ::FORMAT_PFX :
159+ case CertificateKeyType ::FORMAT_PFX :
138160 if (!@openssl_pkcs12_export ($ cert , $ output , $ pkey , '' )) {
139161 throw $ this ->createSslRuntimeException ('Cannot export certificate ' , $ key );
140162 }
0 commit comments