1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.openehealth.ipf.platform.camel.ihe.fhir.core.intercept.consumer;
18
19 import org.apache.camel.Exchange;
20 import org.openehealth.ipf.commons.ihe.core.atna.AuditDataset;
21 import org.openehealth.ipf.commons.ihe.fhir.Constants;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 import javax.naming.ldap.LdapName;
26 import javax.naming.ldap.Rdn;
27 import javax.security.cert.X509Certificate;
28 import java.security.Principal;
29
30
31
32
33 public abstract class AuditInterceptorUtils {
34
35 private static final Logger LOG = LoggerFactory.getLogger(AuditInterceptorUtils.class);
36
37 public static void extractClientCertificateCommonName(Exchange exchange, AuditDataset auditDataset) {
38 X509Certificate[] certificates = (X509Certificate[]) exchange.getIn().getHeader(Constants.HTTP_X509_CERTIFICATES);
39 if (certificates != null && certificates.length > 0) {
40 try {
41 X509Certificate certificate = certificates[0];
42 Principal principal = certificate.getSubjectDN();
43 String dn = principal.getName();
44 LdapName ldapDN = new LdapName(dn);
45 for (Rdn rdn : ldapDN.getRdns()) {
46 if (rdn.getType().equalsIgnoreCase("CN")) {
47 auditDataset.setSourceUserName((String) rdn.getValue());
48 break;
49 }
50 }
51 } catch (Exception e) {
52 LOG.info("Could not extract CN from client certificate", e);
53 }
54 }
55 }
56 }