11package dev .dsf .fhir .spring .config ;
22
3+ import java .io .IOException ;
34import java .net .MalformedURLException ;
45import java .net .URI ;
56import java .net .URISyntaxException ;
67import java .net .URL ;
8+ import java .nio .file .Path ;
79import java .nio .file .Paths ;
810import java .security .KeyStore ;
11+ import java .security .MessageDigest ;
12+ import java .security .NoSuchAlgorithmException ;
13+ import java .security .cert .CertificateEncodingException ;
914import java .security .cert .CertificateException ;
1015import java .security .cert .X509Certificate ;
1116import java .time .Duration ;
17+ import java .util .HexFormat ;
1218import java .util .List ;
1319import java .util .Properties ;
1420
@@ -169,6 +175,7 @@ public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderCon
169175 new DockerSecretsPropertySourceFactory (environment ).readDockerSecretsAndAddPropertiesToEnvironment ();
170176
171177 injectEndpointProperties (environment );
178+ computeOrganizationThumbprintPropertyIfPossible (environment );
172179
173180 return new PropertySourcesPlaceholderConfigurer ();
174181 }
@@ -193,6 +200,31 @@ private static void injectEndpointProperties(ConfigurableEnvironment environment
193200 }
194201 }
195202
203+ private static void computeOrganizationThumbprintPropertyIfPossible (ConfigurableEnvironment environment )
204+ {
205+ try
206+ {
207+ String organizationThumbprint = environment .getProperty ("dev.dsf.fhir.server.organization.thumbprint" );
208+
209+ if (organizationThumbprint == null )
210+ {
211+ Path clientCertPath = Paths .get (environment .getRequiredProperty ("dev.dsf.fhir.client.certificate" ));
212+ X509Certificate clientCert = PemReader .readCertificate (clientCertPath );
213+ MessageDigest md = MessageDigest .getInstance ("SHA-512" );
214+ HexFormat hexFormat = HexFormat .of ();
215+ String thumbprint = hexFormat .formatHex (md .digest (clientCert .getEncoded ())).toLowerCase ();
216+
217+ Properties properties = new Properties ();
218+ properties .put ("dev.dsf.fhir.server.organization.thumbprint" , thumbprint );
219+
220+ environment .getPropertySources ().addFirst (new PropertiesPropertySource ("organization-thumbprint-properties" , properties ));
221+ }
222+ } catch (IOException | NoSuchAlgorithmException | CertificateEncodingException e )
223+ {
224+ throw new RuntimeException (e );
225+ }
226+ }
227+
196228 @ Override
197229 public void afterPropertiesSet () throws Exception
198230 {
0 commit comments