Skip to content

Commit 2afea4f

Browse files
committed
Close #97 Add fromProperties method to the SettingsBuilder class
1 parent da82b7d commit 2afea4f

2 files changed

Lines changed: 103 additions & 24 deletions

File tree

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.onelogin.saml2.settings;
22

3-
import java.io.FileNotFoundException;
43
import java.io.IOException;
54
import java.io.InputStream;
65
import java.io.UnsupportedEncodingException;
@@ -18,7 +17,6 @@
1817
import org.slf4j.LoggerFactory;
1918

2019
import com.onelogin.saml2.exception.Error;
21-
import com.onelogin.saml2.exception.SettingsException;
2220
import com.onelogin.saml2.model.Contact;
2321
import com.onelogin.saml2.model.Organization;
2422
import com.onelogin.saml2.util.Util;
@@ -35,7 +33,7 @@ public class SettingsBuilder {
3533
private static final Logger LOGGER = LoggerFactory.getLogger(SettingsBuilder.class);
3634

3735
/**
38-
* Private property that contain the SAML settings
36+
* Private property that contains the SAML settings
3937
*/
4038
private Properties prop = new Properties();
4139

@@ -102,7 +100,7 @@ public class SettingsBuilder {
102100
public final static String ORGANIZATION_URL = "onelogin.saml2.organization.url";
103101

104102
/**
105-
* Load settings from the file.
103+
* Load settings from the file
106104
*
107105
* @param propFileName
108106
* OneLogin_Saml2_Settings
@@ -151,6 +149,19 @@ private void loadPropFile(String propFileName) throws IOException, Error {
151149
}
152150
}
153151

152+
/**
153+
* Loads the settings from a properties object
154+
*
155+
* @param prop
156+
* contains the properties
157+
*
158+
* @return the SettingsBuilder object with the settings loaded from the prop object
159+
*/
160+
public SettingsBuilder fromProperties(Properties prop) {
161+
this.prop = prop;
162+
return this;
163+
}
164+
154165
/**
155166
* Builds the Saml2Settings object. Read the Properties object and set all the SAML settings
156167
*

core/src/test/java/com/onelogin/saml2/test/settings/SettingBuilderTest.java

Lines changed: 88 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
import java.security.cert.CertificateException;
1212
import java.util.ArrayList;
1313
import java.util.List;
14+
import java.util.Properties;
1415

16+
import org.apache.commons.codec.binary.Base64;
1517
import org.junit.Rule;
1618
import org.junit.Test;
1719
import org.junit.rules.ExpectedException;
@@ -34,14 +36,14 @@ public class SettingBuilderTest {
3436
public ExpectedException expectedEx = ExpectedException.none();
3537

3638
/**
37-
* Tests SettingsBuilder constructor
39+
* Tests SettingsBuilder fromFile method
3840
* Case: config file not found
3941
*
4042
* @throws IOException
4143
* @throws SettingsException
4244
* @throws Error
4345
*
44-
* @see com.onelogin.saml2.settings.SettingsBuilder
46+
* @see com.onelogin.saml2.settings.SettingsBuilder#fromFile
4547
*/
4648
@Test
4749
public void testLoadFromFileNotExist() throws IOException, SettingsException, Error {
@@ -53,7 +55,7 @@ public void testLoadFromFileNotExist() throws IOException, SettingsException, Er
5355
}
5456

5557
/**
56-
* Tests SettingsBuilder constructor
58+
* Tests SettingsBuilder fromFile method
5759
* Case: empty config file
5860
*
5961
* @throws IOException
@@ -62,7 +64,7 @@ public void testLoadFromFileNotExist() throws IOException, SettingsException, Er
6264
* @throws SettingsException
6365
* @throws Error
6466
*
65-
* @see com.onelogin.saml2.settings.SettingsBuilder
67+
* @see com.onelogin.saml2.settings.SettingsBuilder#fromFile
6668
*/
6769
@Test
6870
public void testLoadFromFileEmpty() throws IOException, CertificateException, URISyntaxException, SettingsException, Error {
@@ -107,7 +109,7 @@ public void testLoadFromFileEmpty() throws IOException, CertificateException, UR
107109
}
108110

109111
/**
110-
* Tests SettingsBuilder constructor
112+
* Tests SettingsBuilder fromFile method
111113
* Case: minimum settings config file
112114
*
113115
* @throws IOException
@@ -116,7 +118,7 @@ public void testLoadFromFileEmpty() throws IOException, CertificateException, UR
116118
* @throws SettingsException
117119
* @throws Error
118120
*
119-
* @see com.onelogin.saml2.settings.SettingsBuilder
121+
* @see com.onelogin.saml2.settings.SettingsBuilder#fromFile
120122
*/
121123
@Test
122124
public void testLoadFromFileMinProp() throws IOException, CertificateException, URISyntaxException, SettingsException, Error {
@@ -162,7 +164,7 @@ public void testLoadFromFileMinProp() throws IOException, CertificateException,
162164
}
163165

164166
/**
165-
* Tests SettingsBuilder constructor
167+
* Tests SettingsBuilder fromFile method
166168
* Case: all settings config file
167169
*
168170
* @throws IOException
@@ -171,7 +173,7 @@ public void testLoadFromFileMinProp() throws IOException, CertificateException,
171173
* @throws SettingsException
172174
* @throws Error
173175
*
174-
* @see com.onelogin.saml2.settings.SettingsBuilder
176+
* @see com.onelogin.saml2.settings.SettingsBuilder#fromFile
175177
*/
176178
@Test
177179
public void testLoadFromFileAllProp() throws IOException, CertificateException, URISyntaxException, SettingsException, Error {
@@ -231,7 +233,7 @@ public void testLoadFromFileAllProp() throws IOException, CertificateException,
231233
}
232234

233235
/**
234-
* Tests SettingsBuilder constructor
236+
* Tests SettingsBuilder fromFile method
235237
* Case: settings config file with certificate string
236238
*
237239
* @throws IOException
@@ -240,7 +242,7 @@ public void testLoadFromFileAllProp() throws IOException, CertificateException,
240242
* @throws SettingsException
241243
* @throws Error
242244
*
243-
* @see com.onelogin.saml2.settings.SettingsBuilder
245+
* @see com.onelogin.saml2.settings.SettingsBuilder#fromFile
244246
*/
245247
@Test
246248
public void testLoadFromFileCertString() throws IOException, CertificateException, URISyntaxException, SettingsException, Error {
@@ -285,7 +287,7 @@ public void testLoadFromFileCertString() throws IOException, CertificateExceptio
285287
}
286288

287289
/**
288-
* Tests SettingsBuilder constructor
290+
* Tests SettingsBuilder fromFile method
289291
* Case: settings config file with invalid contact info (not all required fields)
290292
*
291293
* @throws IOException
@@ -294,7 +296,7 @@ public void testLoadFromFileCertString() throws IOException, CertificateExceptio
294296
* @throws SettingsException
295297
* @throws Error
296298
*
297-
* @see com.onelogin.saml2.settings.SettingsBuilder
299+
* @see com.onelogin.saml2.settings.SettingsBuilder#fromFile
298300
*/
299301
@Test
300302
public void testLoadFromFileContactString() throws IOException, CertificateException, URISyntaxException, SettingsException, Error {
@@ -348,7 +350,7 @@ public void testLoadFromFileContactString() throws IOException, CertificateExcep
348350
}
349351

350352
/**
351-
* Tests SettingsBuilder constructor
353+
* Tests SettingsBuilder fromFile method
352354
* Case: settings config file with invalids SP cert/private key
353355
*
354356
* @throws IOException
@@ -357,7 +359,7 @@ public void testLoadFromFileContactString() throws IOException, CertificateExcep
357359
* @throws SettingsException
358360
* @throws Error
359361
*
360-
* @see com.onelogin.saml2.settings.SettingsBuilder
362+
* @see com.onelogin.saml2.settings.SettingsBuilder#fromFile
361363
*/
362364
@Test
363365
public void testLoadFromFileInvalidSPCerts() throws IOException, CertificateException, URISyntaxException, SettingsException, Error {
@@ -368,7 +370,7 @@ public void testLoadFromFileInvalidSPCerts() throws IOException, CertificateExce
368370
}
369371

370372
/**
371-
* Tests SettingsBuilder constructor
373+
* Tests SettingsBuilder fromFile method
372374
* Case: Compress
373375
*
374376
* @throws IOException
@@ -377,7 +379,7 @@ public void testLoadFromFileInvalidSPCerts() throws IOException, CertificateExce
377379
* @throws SettingsException
378380
* @throws Error
379381
*
380-
* @see com.onelogin.saml2.settings.SettingsBuilder
382+
* @see com.onelogin.saml2.settings.SettingsBuilder#fromFile
381383
*/
382384
@Test
383385
public void testCompression() throws IOException, CertificateException, URISyntaxException, SettingsException, Error {
@@ -396,15 +398,15 @@ public void testCompression() throws IOException, CertificateException, URISynta
396398
}
397399

398400
/**
399-
* Tests SettingsBuilder constructor
401+
* Tests SettingsBuilder fromFile method
400402
* Case: settings config file with some empty values
401403
*
402404
* @throws IOException
403405
* @throws CertificateException
404406
* @throws URISyntaxException
405407
* @throws Error
406408
*
407-
* @see com.onelogin.saml2.settings.SettingsBuilder
409+
* @see com.onelogin.saml2.settings.SettingsBuilder#fromFile
408410
*/
409411
@Test
410412
public void testLoadFromFileSomeEmptyProp() throws IOException, CertificateException, URISyntaxException, Error {
@@ -449,15 +451,15 @@ public void testLoadFromFileSomeEmptyProp() throws IOException, CertificateExcep
449451
}
450452

451453
/**
452-
* Tests SettingsBuilder constructor
454+
* Tests SettingsBuilder fromFile method
453455
* Case: settings config file with different values
454456
*
455457
* @throws IOException
456458
* @throws CertificateException
457459
* @throws URISyntaxException
458460
* @throws Error
459461
*
460-
* @see com.onelogin.saml2.settings.SettingsBuilder
462+
* @see com.onelogin.saml2.settings.SettingsBuilder#fromFile
461463
*/
462464
@Test
463465
public void testLoadFromFileDifferentProp() throws IOException, CertificateException, URISyntaxException, Error {
@@ -514,6 +516,72 @@ public void testLoadFromFileDifferentProp() throws IOException, CertificateExcep
514516
assertEquals("support@example.com", c2.getEmailAddress());
515517
assertTrue(c2.getGivenName().isEmpty());
516518
}
519+
520+
/**
521+
* Tests SettingsBuilder fromProperties method
522+
*
523+
* @throws Error
524+
* @throws IOException
525+
* @throws CertificateException
526+
*
527+
* @see com.onelogin.saml2.settings.SettingsBuilder#fromProperties
528+
*/
529+
@Test
530+
public void testFromProperties() throws IOException, Error, CertificateException {
531+
Saml2Settings setting = new SettingsBuilder().fromFile("config/config.min.properties").build();
532+
533+
Base64 encoder = new Base64(64);
534+
String x509cert = new String(encoder.encode(setting.getIdpx509cert().getEncoded()));
535+
536+
Properties prop = new Properties();
537+
prop.setProperty(SettingsBuilder.IDP_ENTITYID_PROPERTY_KEY, setting.getIdpEntityId());
538+
prop.setProperty(SettingsBuilder.IDP_SINGLE_SIGN_ON_SERVICE_URL_PROPERTY_KEY, setting.getIdpSingleSignOnServiceUrl().toString());
539+
prop.setProperty(SettingsBuilder.IDP_SINGLE_LOGOUT_SERVICE_URL_PROPERTY_KEY, setting.getIdpSingleLogoutServiceUrl().toString());
540+
prop.setProperty(SettingsBuilder.IDP_X509CERT_PROPERTY_KEY , x509cert);
541+
prop.setProperty(SettingsBuilder.SP_ENTITYID_PROPERTY_KEY, setting.getSpEntityId());
542+
prop.setProperty(SettingsBuilder.SP_ASSERTION_CONSUMER_SERVICE_URL_PROPERTY_KEY, setting.getSpAssertionConsumerServiceUrl().toString());
543+
prop.setProperty(SettingsBuilder.SP_SINGLE_LOGOUT_SERVICE_URL_PROPERTY_KEY, setting.getSpSingleLogoutServiceUrl().toString());
544+
545+
Saml2Settings setting2 = new SettingsBuilder().fromProperties(prop).build();
546+
547+
assertFalse(setting2.isDebugActive());
548+
assertFalse(setting2.isStrict());
549+
550+
assertEquals("http://localhost:8080/java-saml-jspsample/metadata.jsp", setting2.getSpEntityId());
551+
assertEquals("http://localhost:8080/java-saml-jspsample/acs.jsp", setting2.getSpAssertionConsumerServiceUrl().toString());
552+
assertEquals(setting2.getSpAssertionConsumerServiceBinding(), "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST");
553+
assertEquals("http://localhost:8080/java-saml-jspsample/sls.jsp", setting2.getSpSingleLogoutServiceUrl().toString());
554+
assertEquals(setting2.getSpSingleLogoutServiceBinding(), "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect");
555+
assertEquals(setting2.getSpNameIDFormat(), "urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified");
556+
557+
assertEquals("http://idp.example.com/", setting2.getIdpEntityId());
558+
assertEquals("http://idp.example.com/simplesaml/saml2/idp/SSOService.php", setting2.getIdpSingleSignOnServiceUrl().toString());
559+
assertEquals(setting2.getIdpSingleSignOnServiceBinding(), "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect");
560+
assertEquals("http://idp.example.com/simplesaml/saml2/idp/SingleLogoutService.php", setting2.getIdpSingleLogoutServiceUrl().toString());
561+
assertEquals("http://idp.example.com/simplesaml/saml2/idp/SingleLogoutService.php", setting2.getIdpSingleLogoutServiceResponseUrl().toString());
562+
assertEquals(setting2.getIdpSingleLogoutServiceBinding(), "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect");
563+
assertNotNull(setting2.getIdpx509cert());
564+
assertEquals(Util.loadCert(Util.getFileAsString("certs/certificate1")), setting2.getIdpx509cert());
565+
assertNull(setting2.getIdpCertFingerprint());
566+
assertEquals("sha1", setting2.getIdpCertFingerprintAlgorithm());
567+
568+
assertFalse(setting2.getNameIdEncrypted());
569+
assertFalse(setting2.getAuthnRequestsSigned());
570+
assertFalse(setting2.getLogoutRequestSigned());
571+
assertFalse(setting2.getLogoutResponseSigned());
572+
assertFalse(setting2.getWantMessagesSigned());
573+
assertFalse(setting2.getWantAssertionsSigned());
574+
assertFalse(setting2.getWantAssertionsEncrypted());
575+
assertFalse(setting2.getWantNameIdEncrypted());
576+
assertTrue(setting2.getRequestedAuthnContext().isEmpty());
577+
assertEquals("exact", setting2.getRequestedAuthnContextComparison());
578+
assertTrue(setting2.getWantXMLValidation());
579+
assertEquals(Constants.RSA_SHA1, setting2.getSignatureAlgorithm());
580+
assertFalse(setting2.getSignMetadata());
581+
582+
assertNull(setting2.getOrganization());
583+
assertTrue(setting2.getContacts().isEmpty());
584+
}
517585

518586
/**
519587
* Tests SettingsBuilder constructor

0 commit comments

Comments
 (0)