Skip to content

Commit 6912461

Browse files
committed
Add SettingsBuilder.fromValues for KeyStore settings
1 parent c6b2ca3 commit 6912461

5 files changed

Lines changed: 184 additions & 61 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.onelogin.saml2.model;
2+
3+
import java.security.KeyStore;
4+
5+
/**
6+
* KeyStore class of OneLogin's Java Toolkit.
7+
*
8+
* A class that stores KeyStore details for Certificates and Private Key
9+
*/
10+
public class KeyStoreSettings {
11+
/**
12+
* KeyStore which stores certificates and key
13+
*/
14+
private final KeyStore keyStore;
15+
16+
/**
17+
* Alias for SP key entry
18+
*/
19+
private final String spAlias;
20+
21+
/**
22+
* Password for KeyStore
23+
*/
24+
private final String storePass;
25+
26+
/**
27+
* Constructor
28+
*
29+
* @param keyStore
30+
* stores certificates and key
31+
*
32+
* @param spAlias
33+
* Alias for SP key entry
34+
*
35+
* @param storePass
36+
* password to access KeyStore
37+
*/
38+
public KeyStoreSettings(KeyStore keyStore, String spAlias, String storePass) {
39+
this.keyStore = keyStore;
40+
this.spAlias = spAlias;
41+
this.storePass = storePass;
42+
}
43+
44+
/**
45+
* @return the keyStore
46+
*/
47+
public final KeyStore getKeyStore() {
48+
return keyStore;
49+
}
50+
51+
/**
52+
* @return the spAlias
53+
*/
54+
public final String getSpAlias() {
55+
return spAlias;
56+
}
57+
58+
/**
59+
* @return the storePass
60+
*/
61+
public final String getStorePass() {
62+
return storePass;
63+
}
64+
65+
}

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

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import com.onelogin.saml2.exception.Error;
2929
import com.onelogin.saml2.model.Contact;
30+
import com.onelogin.saml2.model.KeyStoreSettings;
3031
import com.onelogin.saml2.model.Organization;
3132
import com.onelogin.saml2.util.Util;
3233

@@ -128,7 +129,7 @@ public class SettingsBuilder {
128129
* @throws Error
129130
*/
130131
public SettingsBuilder fromFile(String propFileName) throws Error {
131-
return fromFile(propFileName, null, null, null);
132+
return fromFile(propFileName, null);
132133
}
133134

134135
/**
@@ -144,7 +145,7 @@ public SettingsBuilder fromFile(String propFileName) throws Error {
144145
* @throws IOException
145146
* @throws Error
146147
*/
147-
public SettingsBuilder fromFile(String propFileName, KeyStore ks, String alias, String password) throws Error {
148+
public SettingsBuilder fromFile(String propFileName, KeyStoreSettings keyStoreSetting) throws Error {
148149

149150
ClassLoader classLoader = getClass().getClassLoader();
150151
try (InputStream inputStream = classLoader.getResourceAsStream(propFileName)) {
@@ -164,8 +165,8 @@ public SettingsBuilder fromFile(String propFileName, KeyStore ks, String alias,
164165
throw new Error(errorMsg, Error.SETTINGS_FILE_NOT_FOUND);
165166
}
166167
// Parse KeyStore and set the properties for SP Cert and Key
167-
if (ks != null && StringUtils.isNotBlank(alias) && StringUtils.isNotBlank(password)) {
168-
parseKeyStore(ks, alias, password);
168+
if (keyStoreSetting != null) {
169+
parseKeyStore(keyStoreSetting);
169170
}
170171

171172
return this;
@@ -193,8 +194,24 @@ public SettingsBuilder fromProperties(Properties prop) {
193194
* object
194195
*/
195196
public SettingsBuilder fromValues(Map<String, Object> samlData) {
197+
return this.fromValues(samlData, null);
198+
}
199+
200+
/**
201+
* Loads the settings from mapped values and KeyStore settings.
202+
*
203+
* @param samlData Mapped values.
204+
* @param keyStoreSetting KeyStore model
205+
*
206+
* @return the SettingsBuilder object with the settings loaded from the prop
207+
* object
208+
*/
209+
public SettingsBuilder fromValues(Map<String, Object> samlData, KeyStoreSettings keyStoreSetting) {
196210
if (samlData != null) {
197-
this.samlData.putAll(samlData);
211+
this.samlData.putAll(samlData);
212+
}
213+
if (keyStoreSetting != null) {
214+
parseKeyStore(keyStoreSetting);
198215
}
199216
return this;
200217
}
@@ -762,11 +779,11 @@ private void parseProperties(Properties properties) {
762779
}
763780
}
764781

765-
private void parseKeyStore(KeyStore keyStore, String alias, String password) {
766-
this.samlData.put(KEYSTORE_KEY, keyStore);
767-
this.samlData.put(KEYSTORE_ALIAS, alias);
768-
this.samlData.put(KEYSTORE_PASSWORD, password);
769-
}
782+
private void parseKeyStore(KeyStoreSettings setting) {
783+
this.samlData.put(KEYSTORE_KEY, setting.getKeyStore());
784+
this.samlData.put(KEYSTORE_ALIAS, setting.getSpAlias());
785+
this.samlData.put(KEYSTORE_PASSWORD, setting.getStorePass());
786+
}
770787

771788
private boolean isString(Object propValue) {
772789
return propValue instanceof String && StringUtils.isNotBlank((String) propValue);

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

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
package com.onelogin.saml2.test.settings;
22

33
import static com.onelogin.saml2.settings.SettingsBuilder.*;
4-
54
import static org.junit.Assert.assertEquals;
65
import static org.junit.Assert.assertFalse;
76
import static org.junit.Assert.assertNotNull;
87
import static org.junit.Assert.assertNull;
98
import static org.junit.Assert.assertTrue;
109

1110
import java.io.FileInputStream;
11+
import java.io.FileNotFoundException;
1212
import java.io.IOException;
1313
import java.net.URISyntaxException;
1414
import java.net.URL;
1515
import java.security.Key;
1616
import java.security.KeyStore;
1717
import java.security.KeyStoreException;
1818
import java.security.NoSuchAlgorithmException;
19+
import java.security.PrivateKey;
1920
import java.security.cert.CertificateException;
2021
import java.security.cert.X509Certificate;
2122
import java.util.ArrayList;
@@ -33,6 +34,7 @@
3334
import com.onelogin.saml2.exception.Error;
3435
import com.onelogin.saml2.exception.SettingsException;
3536
import com.onelogin.saml2.model.Contact;
37+
import com.onelogin.saml2.model.KeyStoreSettings;
3638
import com.onelogin.saml2.model.Organization;
3739
import com.onelogin.saml2.settings.Saml2Settings;
3840
import com.onelogin.saml2.settings.SettingsBuilder;
@@ -65,6 +67,26 @@ public void testLoadFromFileNotExist() throws IOException, SettingsException, Er
6567
new SettingsBuilder().fromFile("config/config.notfound.properties").build();
6668
}
6769

70+
/**
71+
* Returns KeyStore details from src/test/resources for testing
72+
*
73+
* @return
74+
* @throws KeyStoreException
75+
* @throws NoSuchAlgorithmException
76+
* @throws CertificateException
77+
* @throws FileNotFoundException
78+
* @throws IOException
79+
*/
80+
private KeyStoreSettings getKeyStoreSettings() throws KeyStoreException, NoSuchAlgorithmException, CertificateException, FileNotFoundException, IOException {
81+
String password = "changeit";
82+
String keyStoreFile = "src/test/resources/keystore/oneloginTestKeystore.jks";
83+
String alias = "onelogintest";
84+
85+
KeyStore ks = KeyStore.getInstance("JKS");
86+
ks.load(new FileInputStream(keyStoreFile), password.toCharArray());
87+
return new KeyStoreSettings(ks, alias, password);
88+
}
89+
6890
/**
6991
* Tests SettingsBuilder fromFile method
7092
* Case: Config file with KeyStore
@@ -81,15 +103,8 @@ public void testLoadFromFileNotExist() throws IOException, SettingsException, Er
81103
*/
82104
@Test
83105
public void testLoadFromFileAndKeyStore() throws IOException, CertificateException, URISyntaxException, SettingsException, Error, KeyStoreException, NoSuchAlgorithmException {
106+
Saml2Settings setting = new SettingsBuilder().fromFile("config/config.empty.properties", getKeyStoreSettings()).build();
84107

85-
String password = "changeit";
86-
String keyStoreFile = "src/test/resources/keystore/oneloginTestKeystore.jks";
87-
String alias = "onelogintest";
88-
89-
KeyStore ks = KeyStore.getInstance("JKS");
90-
ks.load(new FileInputStream(keyStoreFile), password.toCharArray());
91-
92-
Saml2Settings setting = new SettingsBuilder().fromFile("config/config.empty.properties", ks, alias, password).build();
93108
assertNotNull(setting.getSPcert() instanceof X509Certificate);
94109
assertNotNull(setting.getSPkey() instanceof Key);
95110
}
@@ -781,6 +796,23 @@ public void testLoadFromValues() throws Exception {
781796
assertEquals("Support Guy", c2.getGivenName());
782797

783798
assertEquals("_", setting.getUniqueIDPrefix());
799+
800+
// Test with samlData and KeyStoreSettings
801+
X509Certificate previousCert = setting.getSPcert();
802+
PrivateKey previousKey = setting.getSPkey();
803+
804+
samlData.remove(SP_X509CERT_PROPERTY_KEY);
805+
samlData.remove(SP_PRIVATEKEY_PROPERTY_KEY);
806+
setting = new SettingsBuilder().fromValues(samlData, getKeyStoreSettings()).build();
807+
808+
X509Certificate newCert = setting.getSPcert();
809+
PrivateKey newKey = setting.getSPkey();
810+
811+
assertNotNull(newCert);
812+
assertNotNull(newKey);
813+
assertFalse(previousCert.equals(newCert));
814+
assertFalse(previousKey.equals(newKey));
815+
784816
}
785817

786818
/**

toolkit/src/main/java/com/onelogin/saml2/Auth.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.onelogin.saml2.http.HttpRequest;
3131
import com.onelogin.saml2.logout.LogoutRequest;
3232
import com.onelogin.saml2.logout.LogoutResponse;
33+
import com.onelogin.saml2.model.KeyStoreSettings;
3334
import com.onelogin.saml2.servlet.ServletUtils;
3435
import com.onelogin.saml2.settings.Saml2Settings;
3536
import com.onelogin.saml2.settings.SettingsBuilder;
@@ -185,9 +186,9 @@ public Auth(String filename) throws IOException, SettingsException, Error {
185186
* @throws SettingsException
186187
* @throws Error
187188
*/
188-
public Auth(String filename, KeyStore ks, String alias, String password)
189+
public Auth(String filename, KeyStoreSettings keyStoreSetting)
189190
throws IOException, SettingsException, Error {
190-
this(new SettingsBuilder().fromFile(filename, ks, alias, password).build(), null, null);
191+
this(new SettingsBuilder().fromFile(filename, keyStoreSetting).build(), null, null);
191192
}
192193

193194
/**
@@ -217,9 +218,9 @@ public Auth(HttpServletRequest request, HttpServletResponse response) throws IOE
217218
* @throws SettingsException
218219
* @throws Error
219220
*/
220-
public Auth(KeyStore ks, String alias, String password, HttpServletRequest request, HttpServletResponse response)
221+
public Auth(KeyStoreSettings keyStoreSetting, HttpServletRequest request, HttpServletResponse response)
221222
throws IOException, SettingsException, Error {
222-
this(new SettingsBuilder().fromFile("onelogin.saml.properties", ks, alias, password).build(), request,
223+
this(new SettingsBuilder().fromFile("onelogin.saml.properties", keyStoreSetting).build(), request,
223224
response);
224225
}
225226

@@ -236,7 +237,7 @@ public Auth(KeyStore ks, String alias, String password, HttpServletRequest reque
236237
*/
237238
public Auth(String filename, HttpServletRequest request, HttpServletResponse response)
238239
throws SettingsException, IOException, Error {
239-
this(filename, null, null, null, request, response);
240+
this(filename, null, request, response);
240241
}
241242

242243
/**
@@ -253,9 +254,9 @@ public Auth(String filename, HttpServletRequest request, HttpServletResponse res
253254
* @throws IOException
254255
* @throws Error
255256
*/
256-
public Auth(String filename, KeyStore ks, String alias, String password, HttpServletRequest request,
257+
public Auth(String filename, KeyStoreSettings keyStoreSetting, HttpServletRequest request,
257258
HttpServletResponse response) throws SettingsException, IOException, Error {
258-
this(new SettingsBuilder().fromFile(filename, ks, alias, password).build(), request, response);
259+
this(new SettingsBuilder().fromFile(filename, keyStoreSetting).build(), request, response);
259260
}
260261

261262
/**

0 commit comments

Comments
 (0)