Skip to content

Commit c681a63

Browse files
jgomer2001moabu
andauthored
feat: add download cert functionality to plugin (#13200)
* chore: minor flow refactoring #13186 Signed-off-by: jgomer2001 <bonustrack310@gmail.com> * feat: add cert download functionality #13186 Signed-off-by: jgomer2001 <bonustrack310@gmail.com> --------- Signed-off-by: jgomer2001 <bonustrack310@gmail.com> Co-authored-by: Mohammad Abudayyeh <47318409+moabu@users.noreply.github.com>
1 parent ab9e405 commit c681a63

File tree

9 files changed

+63
-29
lines changed

9 files changed

+63
-29
lines changed

jans-casa/plugins/cert-authn/agama/project/code/io.jans.casa.cert.oneStepAuthn.flow

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,7 @@ Otherwise
2323

2424
When obj.aborted is null //User took usual Casa authentication, terminate as such
2525
Finish obj
26-
27-
Log "Smart card/User cert option selected"
28-
obj = Trigger io.jans.casa.cert.promptAndValidate
29-
30-
When obj.success is true
31-
fingerPrint = obj.data.cert.fingerPrint
32-
x509 = obj.data.cert.x509
33-
uid = Call io.jans.casa.certauthn.CertUtil#findOwner fingerPrint
34-
35-
When uid is not null
36-
Finish uid
3726

38-
uid = Call io.jans.casa.certauthn.CertUtil#register x509 conf.mappingClassField
39-
status = Call io.jans.casa.certauthn.CertUtil#enroll uid x509 fingerPrint
40-
status = Call status toString
41-
42-
When status is "SUCCESS"
43-
Finish uid
44-
45-
obj = { success: false }
46-
27+
Log "Smart card/User cert option selected"
28+
obj = Trigger io.jans.casa.cert.standaloneOneStepAuthn
4729
Finish obj
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Flow io.jans.casa.cert.standaloneOneStepAuthn
2+
Basepath ""
3+
Configs conf
4+
5+
obj = Trigger io.jans.casa.cert.promptAndValidate
6+
7+
When obj.success is true
8+
fingerPrint = obj.data.cert.fingerPrint
9+
x509 = obj.data.cert.x509
10+
uid = Call io.jans.casa.certauthn.CertUtil#findOwner fingerPrint
11+
12+
When uid is not null
13+
Finish uid
14+
15+
uid = Call io.jans.casa.certauthn.CertUtil#register x509 conf.mappingClassField
16+
status = Call io.jans.casa.certauthn.CertUtil#enroll uid x509 fingerPrint
17+
status = Call status toString
18+
19+
When status is "SUCCESS"
20+
Finish uid
21+
22+
obj = { success: false }
23+
24+
Finish obj

jans-casa/plugins/cert-authn/agama/project/lib/io/jans/casa/certauthn/CertUtil.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ public static String findOwner(String fingerPrint) {
144144

145145
}
146146

147+
@SuppressWarnings("unchecked")
147148
public static String register(X509Certificate certificate, String classField)
148149
throws IllegalAccessException, NoSuchFieldException {
149150

jans-casa/plugins/cert-authn/agama/project/project.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
"roundTripMaxTime": 45
1212
},
1313
"io.jans.casa.cert.oneStepAuthn": {
14-
"useAcctLinking": false,
14+
"useAcctLinking": false
15+
},
16+
"io.jans.casa.cert.standaloneOneStepAuthn": {
1517
"mappingClassField": "STRAIGHT"
1618
}
1719
}

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)