Skip to content

Commit 3f9c57a

Browse files
committed
feat: add cert download functionality #13186
Signed-off-by: jgomer2001 <bonustrack310@gmail.com>
1 parent a4ef916 commit 3f9c57a

File tree

5 files changed

+33
-8
lines changed

5 files changed

+33
-8
lines changed

jans-casa/plugins/cert-authn/src/main/java/io/jans/casa/plugins/certauthn/model/Certificate.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class Certificate {
99
private long expirationDate;
1010
private boolean expired;
1111
private String fingerPrint;
12+
private String pemContent;
1213

1314
public String getCommonName() {
1415
return commonName;
@@ -66,4 +67,12 @@ public void setFormattedName(String formattedName) {
6667
this.formattedName = formattedName;
6768
}
6869

70+
public String getPemContent() {
71+
return pemContent;
72+
}
73+
74+
public void setPemContent(String pemContent) {
75+
this.pemContent = pemContent;
76+
}
77+
6978
}

jans-casa/plugins/cert-authn/src/main/java/io/jans/casa/plugins/certauthn/service/CertService.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,24 +78,23 @@ public int getDevicesTotal(String userId) {
7878

7979
}
8080

81-
public boolean removeFromUser(String fingerPrint, String userId) throws Exception {
81+
public boolean removeFromUser(Certificate certificate, String userId) throws Exception {
8282

8383
CertPerson person = persistenceService.get(CertPerson.class, persistenceService.getPersonDn(userId));
84-
8584
List<String> stringCerts = Optional.ofNullable(person.getX509Certificates()).orElse(new ArrayList<>());
8685
List<io.jans.scim.model.scim2.user.X509Certificate> scimCerts = getScimX509Certificates(stringCerts);
8786

8887
boolean found = false;
8988
int i;
9089
for (i = 0; i < scimCerts.size() && !found; i++) {
9190
String val = scimCerts.get(i).getValue();
92-
found = getFingerPrint(CertUtils.x509CertificateFromPem(val)).equals(fingerPrint);
91+
found = val != null && val.equals(certificate.getPemContent());
9392
}
9493
if (found) {
9594
logger.info("Removing cert from SCIM profile data");
9695
person.getX509Certificates().remove(i - 1);
9796
}
98-
person.getJansExtUid().remove(CERT_PREFIX + fingerPrint);
97+
person.getJansExtUid().remove(CERT_PREFIX + certificate.getFingerPrint());
9998

10099
logger.info("Removing cert reference from user");
101100
return persistenceService.modify(person);
@@ -141,6 +140,7 @@ private Certificate getExtraCertsInfo(String externalUid, List<io.jans.scim.mode
141140
long date = x509Certificate.getNotAfter().getTime();
142141
cert.setExpirationDate(date);
143142
cert.setExpired(date < System.currentTimeMillis());
143+
cert.setPemContent(sc.getValue());
144144

145145
break;
146146
}

jans-casa/plugins/cert-authn/src/main/java/io/jans/casa/plugins/certauthn/vm/CertAuthenticationSummaryVM.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.zkoss.util.resource.Labels;
2323
import org.zkoss.zk.ui.Executions;
2424
import org.zkoss.zk.ui.select.annotation.WireVariable;
25-
import org.zkoss.zul.Messagebox;
25+
import org.zkoss.zul.*;
2626

2727
import static java.nio.charset.StandardCharsets.UTF_8;
2828

@@ -96,6 +96,15 @@ public void redirect() throws URISyntaxException, StringEncrypter.EncryptionExce
9696

9797
}
9898

99+
public void download(Certificate certificate) {
100+
101+
String fileName = Optional.ofNullable(certificate.getCommonName())
102+
.map(s -> s.replaceAll("[^\\w ]+", "_")).orElse("");
103+
fileName = fileName.length() == 0 ? "cert" : fileName;
104+
Filedownload.save(certificate.getPemContent(), "application/x-pem-file", fileName + ".pem");
105+
106+
}
107+
99108
public void delete(Certificate certificate) {
100109

101110
String resetMessages = sndFactorUtils.removalConflict(CertService.AGAMA_FLOW, certificates.size(), user).getY();
@@ -108,7 +117,7 @@ public void delete(Certificate certificate) {
108117
if (Messagebox.ON_YES.equals(event.getName())) {
109118
try {
110119
String fingerprint = certificate.getFingerPrint();
111-
boolean success = certService.removeFromUser(fingerprint, userId);
120+
boolean success = certService.removeFromUser(certificate, userId);
112121

113122
if (success) {
114123
logger.info("Certificate {} removed from user account", fingerprint);

jans-casa/plugins/cert-authn/src/main/resources/assets/cert-detail.zul

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<z:include src="/back-home.zul" />
1111

1212
<div class="ph4 mb2">
13-
<div class="alert alert-success dn" id="feedback-cert-edit" role="alert" />
13+
<div class="alert alert-success dn" id="feedback-cert-delete" role="alert" />
1414
</div>
1515

1616
<div class="${css['sectionsWrapper']}">
@@ -61,7 +61,12 @@
6161
</p>
6262
</div>
6363
<div class="pl2 pt2">
64-
<h:button class="${css.deleteButton} mb2" w:onClick="alertRef = $('#feedback-cert-edit')"
64+
<h:button class="${css.editButton} mb2 mr2" visible="${each.pemContent ne null}"
65+
onClick="@('download', each)"
66+
data-original-title="${labels.usercert.download}" data-toggle="tooltip" data-placement="top">
67+
<i class="fas fa-download" />
68+
</h:button>
69+
<h:button class="${css.deleteButton} mb2" w:onClick="alertRef = $('#feedback-cert-delete')"
6570
onClick="@('delete', each)"
6671
data-original-title="${labels.general.delete}" data-toggle="tooltip" data-placement="top">
6772
<i class="fas fa-trash-alt" />

jans-casa/plugins/cert-authn/src/main/resources/labels/zk-label.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,5 @@ You are about to remove this certificate.
4141

4242
Proceed?
4343
}
44+
45+
usercert.download=Download this certificate

0 commit comments

Comments
 (0)