Skip to content

Commit ddba9d9

Browse files
authored
Merge pull request #125 from comepasto/build_settings_dynamically
Build settings dynamically
2 parents e78e650 + 24b428c commit ddba9d9

File tree

5 files changed

+441
-140
lines changed

5 files changed

+441
-140
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ At *java-saml-tookit-jspsample/src/main/resources* folder is the *onelogin.saml.
178178
#### Settings
179179
First of all we need to configure the toolkit. The SP's info, the IdP's info, and in some cases, configuration for advanced security issues, such as signatures and encryption.
180180

181+
##### Properties File
181182
All the settings are defined in one unique file; by default, the Auth class loads a *onelogin.saml.properties* file with the Auth() method, but if we named it in a different way, we can use Auth(filename);
182183

183184
Here are the list of properties to be defined on the settings file:
@@ -337,7 +338,27 @@ onelogin.saml2.organization.lang = en
337338
onelogin.saml2.contacts.technical.given_name = Technical Guy
338339
onelogin.saml2.contacts.technical.email_address = technical@example.com
339340
onelogin.saml2.contacts.support.given_name = Support Guy
340-
onelogin.saml2.contacts.support.email_address = support@@example.com
341+
onelogin.saml2.contacts.support.email_address = support@example.com
342+
```
343+
344+
##### Dynamic Settings
345+
It is possible to build settings programatically. You can load your values from different sources such as files, databases, or generated values.
346+
347+
The `SettingsBuilder` class exposes the method `fromValues(Map<String, Object> samlData)` which let you build your settings dynamically. The `key` strings are the same from the *Properties file*
348+
```java
349+
Map<String, Object> samlData = new HashMap<>();
350+
samlData.put("onelogin.saml2.sp.entityid", "http://localhost:8080/java-saml-tookit-jspsample/metadata.jsp");
351+
samlData.put("onelogin.saml2.sp.assertion_consumer_service.url", new URL("http://localhost:8080/java-saml-tookit-jspsample/acs.jsp"));
352+
samlData.put("onelogin.saml2.security.want_xml_validation",true);
353+
samlData.put("onelogin.saml2.sp.x509cert", myX509CertInstance);
354+
355+
SettingsBuilder builder = new SettingsBuilder();
356+
Saml2Settings settings = builder.fromValues(samlData).build();
357+
```
358+
359+
To instantiate the `Auth` class you write
360+
```java
361+
Auth auth = new Auth(settings, request, response);
341362
```
342363

343364
#### The HttpRequest

core/src/main/java/com/onelogin/saml2/settings/Saml2Settings.java

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -56,31 +56,31 @@ public class Saml2Settings {
5656
private String idpCertFingerprintAlgorithm = "sha1";
5757

5858
// Security
59-
private Boolean nameIdEncrypted = false;
60-
private Boolean authnRequestsSigned = false;
61-
private Boolean logoutRequestSigned = false;
62-
private Boolean logoutResponseSigned = false;
63-
private Boolean wantMessagesSigned = false;
64-
private Boolean wantAssertionsSigned = false;
65-
private Boolean wantAssertionsEncrypted = false;
66-
private Boolean wantNameId = true;
67-
private Boolean wantNameIdEncrypted = false;
68-
private Boolean signMetadata = false;
69-
private List<String> requestedAuthnContext = new ArrayList<String>();
59+
private boolean nameIdEncrypted = false;
60+
private boolean authnRequestsSigned = false;
61+
private boolean logoutRequestSigned = false;
62+
private boolean logoutResponseSigned = false;
63+
private boolean wantMessagesSigned = false;
64+
private boolean wantAssertionsSigned = false;
65+
private boolean wantAssertionsEncrypted = false;
66+
private boolean wantNameId = true;
67+
private boolean wantNameIdEncrypted = false;
68+
private boolean signMetadata = false;
69+
private List<String> requestedAuthnContext = new ArrayList<>();
7070
private String requestedAuthnContextComparison = "exact";
71-
private Boolean wantXMLValidation = true;
71+
private boolean wantXMLValidation = true;
7272
private String signatureAlgorithm = Constants.RSA_SHA1;
73-
private Boolean rejectUnsolicitedResponsesWithInResponseTo = false;
73+
private boolean rejectUnsolicitedResponsesWithInResponseTo = false;
7474

7575
// Compress
76-
private Boolean compressRequest = true;
77-
private Boolean compressResponse = true;
76+
private boolean compressRequest = true;
77+
private boolean compressResponse = true;
7878

7979
// Misc
80-
private List<Contact> contacts = new LinkedList<Contact>();
80+
private List<Contact> contacts = new LinkedList<>();
8181
private Organization organization = null;
8282

83-
private boolean spValidationOnly = false;
83+
private boolean spValidationOnly = false;
8484

8585
/**
8686
* @return the strict setting value
@@ -215,70 +215,70 @@ public final String getIdpCertFingerprintAlgorithm() {
215215
/**
216216
* @return the nameIdEncrypted setting value
217217
*/
218-
public Boolean getNameIdEncrypted() {
218+
public boolean getNameIdEncrypted() {
219219
return nameIdEncrypted;
220220
}
221221

222222
/**
223223
* @return the authnRequestsSigned setting value
224224
*/
225-
public Boolean getAuthnRequestsSigned() {
225+
public boolean getAuthnRequestsSigned() {
226226
return authnRequestsSigned;
227227
}
228228

229229
/**
230230
* @return the logoutRequestSigned setting value
231231
*/
232-
public Boolean getLogoutRequestSigned() {
232+
public boolean getLogoutRequestSigned() {
233233
return logoutRequestSigned;
234234
}
235235

236236
/**
237237
* @return the logoutResponseSigned setting value
238238
*/
239-
public Boolean getLogoutResponseSigned() {
239+
public boolean getLogoutResponseSigned() {
240240
return logoutResponseSigned;
241241
}
242242

243243
/**
244244
* @return the wantMessagesSigned setting value
245245
*/
246-
public Boolean getWantMessagesSigned() {
246+
public boolean getWantMessagesSigned() {
247247
return wantMessagesSigned;
248248
}
249249

250250
/**
251251
* @return the wantAssertionsSigned setting value
252252
*/
253-
public Boolean getWantAssertionsSigned() {
253+
public boolean getWantAssertionsSigned() {
254254
return wantAssertionsSigned;
255255
}
256256

257257
/**
258258
* @return the wantAssertionsEncrypted setting value
259259
*/
260-
public Boolean getWantAssertionsEncrypted() {
260+
public boolean getWantAssertionsEncrypted() {
261261
return wantAssertionsEncrypted;
262262
}
263263

264264
/**
265265
* @return the wantNameId setting value
266266
*/
267-
public Boolean getWantNameId() {
267+
public boolean getWantNameId() {
268268
return wantNameId;
269269
}
270270

271271
/**
272272
* @return the wantNameIdEncrypted setting value
273273
*/
274-
public Boolean getWantNameIdEncrypted() {
274+
public boolean getWantNameIdEncrypted() {
275275
return wantNameIdEncrypted;
276276
}
277277

278278
/**
279279
* @return the signMetadata setting value
280280
*/
281-
public Boolean getSignMetadata() {
281+
public boolean getSignMetadata() {
282282
return signMetadata;
283283
}
284284

@@ -299,7 +299,7 @@ public String getRequestedAuthnContextComparison() {
299299
/**
300300
* @return the wantXMLValidation setting value
301301
*/
302-
public Boolean getWantXMLValidation() {
302+
public boolean getWantXMLValidation() {
303303
return wantXMLValidation;
304304
}
305305

@@ -327,7 +327,7 @@ public Organization getOrganization() {
327327
/**
328328
* @return if the debug is active or not
329329
*/
330-
public Boolean isDebugActive() {
330+
public boolean isDebugActive() {
331331
return this.debug;
332332
}
333333

@@ -528,7 +528,7 @@ protected final void setIdpCertFingerprintAlgorithm(String idpCertFingerprintAlg
528528
* @param nameIdEncrypted
529529
* the nameIdEncrypted value to be set. Based on it the SP will encrypt the NameID or not
530530
*/
531-
public void setNameIdEncrypted(Boolean nameIdEncrypted) {
531+
public void setNameIdEncrypted(boolean nameIdEncrypted) {
532532
this.nameIdEncrypted = nameIdEncrypted;
533533
}
534534

@@ -538,7 +538,7 @@ public void setNameIdEncrypted(Boolean nameIdEncrypted) {
538538
* @param authnRequestsSigned
539539
* the authnRequestsSigned value to be set. Based on it the SP will sign Logout Request or not
540540
*/
541-
public void setAuthnRequestsSigned(Boolean authnRequestsSigned) {
541+
public void setAuthnRequestsSigned(boolean authnRequestsSigned) {
542542
this.authnRequestsSigned = authnRequestsSigned;
543543
}
544544

@@ -548,7 +548,7 @@ public void setAuthnRequestsSigned(Boolean authnRequestsSigned) {
548548
* @param logoutRequestSigned
549549
* the logoutRequestSigned value to be set. Based on it the SP will sign Logout Request or not
550550
*/
551-
public void setLogoutRequestSigned(Boolean logoutRequestSigned) {
551+
public void setLogoutRequestSigned(boolean logoutRequestSigned) {
552552
this.logoutRequestSigned = logoutRequestSigned;
553553
}
554554

@@ -558,7 +558,7 @@ public void setLogoutRequestSigned(Boolean logoutRequestSigned) {
558558
* @param logoutResponseSigned
559559
* the logoutResponseSigned value to be set. Based on it the SP will sign Logout Response or not
560560
*/
561-
public void setLogoutResponseSigned(Boolean logoutResponseSigned) {
561+
public void setLogoutResponseSigned(boolean logoutResponseSigned) {
562562
this.logoutResponseSigned = logoutResponseSigned;
563563
}
564564

@@ -568,7 +568,7 @@ public void setLogoutResponseSigned(Boolean logoutResponseSigned) {
568568
* @param wantMessagesSigned
569569
* the wantMessagesSigned value to be set. Based on it the SP expects the SAML Messages to be signed or not
570570
*/
571-
public void setWantMessagesSigned(Boolean wantMessagesSigned) {
571+
public void setWantMessagesSigned(boolean wantMessagesSigned) {
572572
this.wantMessagesSigned = wantMessagesSigned;
573573
}
574574

@@ -578,7 +578,7 @@ public void setWantMessagesSigned(Boolean wantMessagesSigned) {
578578
* @param wantAssertionsSigned
579579
* the wantAssertionsSigned value to be set. Based on it the SP expects the SAML Assertions to be signed or not
580580
*/
581-
public void setWantAssertionsSigned(Boolean wantAssertionsSigned) {
581+
public void setWantAssertionsSigned(boolean wantAssertionsSigned) {
582582
this.wantAssertionsSigned = wantAssertionsSigned;
583583
}
584584

@@ -588,7 +588,7 @@ public void setWantAssertionsSigned(Boolean wantAssertionsSigned) {
588588
* @param wantAssertionsEncrypted
589589
* the wantAssertionsEncrypted value to be set. Based on it the SP expects the SAML Assertions to be encrypted or not
590590
*/
591-
public void setWantAssertionsEncrypted(Boolean wantAssertionsEncrypted) {
591+
public void setWantAssertionsEncrypted(boolean wantAssertionsEncrypted) {
592592
this.wantAssertionsEncrypted = wantAssertionsEncrypted;
593593
}
594594

@@ -598,7 +598,7 @@ public void setWantAssertionsEncrypted(Boolean wantAssertionsEncrypted) {
598598
* @param wantNameId
599599
* the wantNameId value to be set. Based on it the SP expects a NameID
600600
*/
601-
public void setWantNameId(Boolean wantNameId) {
601+
public void setWantNameId(boolean wantNameId) {
602602
this.wantNameId = wantNameId;
603603
}
604604

@@ -608,7 +608,7 @@ public void setWantNameId(Boolean wantNameId) {
608608
* @param wantNameIdEncrypted
609609
* the wantNameIdEncrypted value to be set. Based on it the SP expects the NameID to be encrypted or not
610610
*/
611-
public void setWantNameIdEncrypted(Boolean wantNameIdEncrypted) {
611+
public void setWantNameIdEncrypted(boolean wantNameIdEncrypted) {
612612
this.wantNameIdEncrypted = wantNameIdEncrypted;
613613
}
614614

@@ -618,7 +618,7 @@ public void setWantNameIdEncrypted(Boolean wantNameIdEncrypted) {
618618
* @param signMetadata
619619
* the signMetadata value to be set. Based on it the SP will sign or not the metadata with the SP PrivateKey/Certificate
620620
*/
621-
public void setSignMetadata(Boolean signMetadata) {
621+
public void setSignMetadata(boolean signMetadata) {
622622
this.signMetadata = signMetadata;
623623
}
624624

@@ -629,7 +629,9 @@ public void setSignMetadata(Boolean signMetadata) {
629629
* the requestedAuthnContext value to be set on the AuthNRequest.
630630
*/
631631
public void setRequestedAuthnContext(List<String> requestedAuthnContext) {
632-
this.requestedAuthnContext = requestedAuthnContext;
632+
if (requestedAuthnContext != null) {
633+
this.requestedAuthnContext = requestedAuthnContext;
634+
}
633635
}
634636

635637
/**
@@ -648,7 +650,7 @@ public void setRequestedAuthnContextComparison(String requestedAuthnContextCompa
648650
* @param wantXMLValidation
649651
* the wantXMLValidation value to be set. Based on it the SP will validate SAML messages against the XML scheme
650652
*/
651-
public void setWantXMLValidation(Boolean wantXMLValidation) {
653+
public void setWantXMLValidation(boolean wantXMLValidation) {
652654
this.wantXMLValidation = wantXMLValidation;
653655
}
654656

@@ -744,7 +746,7 @@ protected final void setOrganization(Organization organization) {
744746
* @return errors found on the settings data
745747
*/
746748
public List<String> checkSettings() {
747-
List<String> errors = new ArrayList<String>(this.checkSPSettings());
749+
List<String> errors = new ArrayList<>(this.checkSPSettings());
748750
if (!spValidationOnly) {
749751
errors.addAll(this.checkIdPSettings());
750752
}
@@ -758,7 +760,7 @@ public List<String> checkSettings() {
758760
* @return errors found on the IdP settings data
759761
*/
760762
public List<String> checkIdPSettings() {
761-
List<String> errors = new ArrayList<String>();
763+
List<String> errors = new ArrayList<>();
762764
String errorMsg;
763765

764766
if (!checkRequired(getIdpEntityId())) {
@@ -794,7 +796,7 @@ public List<String> checkIdPSettings() {
794796
* @return errors found on the SP settings data
795797
*/
796798
public List<String> checkSPSettings() {
797-
List<String> errors = new ArrayList<String>();
799+
List<String> errors = new ArrayList<>();
798800
String errorMsg;
799801

800802
if (!checkRequired(getSpEntityId())) {
@@ -862,7 +864,7 @@ public List<String> checkSPSettings() {
862864
*
863865
* @return true if the SP settings are valid
864866
*/
865-
public Boolean checkSPCerts() {
867+
public boolean checkSPCerts() {
866868
X509Certificate cert = getSPcert();
867869
PrivateKey key = getSPkey();
868870

@@ -899,7 +901,7 @@ private boolean checkRequired(Object value) {
899901
* @param spValidationOnly
900902
* the spValidationOnly value to be set
901903
*/
902-
public void setSPValidationOnly(Boolean spValidationOnly)
904+
public void setSPValidationOnly(boolean spValidationOnly)
903905
{
904906
this.spValidationOnly = spValidationOnly;
905907
}
@@ -924,7 +926,7 @@ public String getSPMetadata() throws CertificateEncodingException {
924926
String metadataString = metadataObj.getMetadataString();
925927

926928
// Check if must be signed
927-
Boolean signMetadata = this.getSignMetadata();
929+
boolean signMetadata = this.getSignMetadata();
928930
if (signMetadata) {
929931
// TODO Extend this in order to be able to read not only SP privateKey/certificate
930932
try {
@@ -957,7 +959,7 @@ public static List<String> validateMetadata(String metadataString) throws Except
957959

958960
Document metadataDocument = Util.loadXML(metadataString);
959961

960-
List<String> errors = new ArrayList<String>();
962+
List<String> errors = new ArrayList<>();
961963

962964
if (!Util.validateXML(metadataDocument, SchemaFactory.SAML_SCHEMA_METADATA_2_0)) {
963965
errors.add("Invalid SAML Metadata. Not match the saml-schema-metadata-2.0.xsd");

0 commit comments

Comments
 (0)