Skip to content

Commit 3ca12dd

Browse files
committed
Add a property for unique ID prefix
1 parent 7f614e1 commit 3ca12dd

14 files changed

Lines changed: 179 additions & 15 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,11 @@ onelogin.saml2.contacts.technical.given_name = Technical Guy
344344
onelogin.saml2.contacts.technical.email_address = technical@example.com
345345
onelogin.saml2.contacts.support.given_name = Support Guy
346346
onelogin.saml2.contacts.support.email_address = support@example.com
347+
348+
# Prefix used in generated Unique IDs.
349+
# Optional, defaults to ONELOGIN_ or full ID is like ONELOGIN_ebb0badd-4f60-4b38-b20a-a8e01f0592b1.
350+
# At minimun, the prefix can be non-numeric character such as "_".
351+
# onelogin.saml2.unique_id_prefix = _
347352
```
348353

349354
##### Dynamic Settings

core/src/main/java/com/onelogin/saml2/authn/AuthnRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public AuthnRequest(Saml2Settings settings) {
8686
* When true the AuthNReuqest will set a nameIdPolicy
8787
*/
8888
public AuthnRequest(Saml2Settings settings, boolean forceAuthn, boolean isPassive, boolean setNameIdPolicy) {
89-
this.id = Util.generateUniqueID();
89+
this.id = Util.generateUniqueID(settings.getUniqueIDPrefix());
9090
issueInstant = Calendar.getInstance();
9191
this.isPassive = isPassive;
9292
this.settings = settings;

core/src/main/java/com/onelogin/saml2/logout/LogoutRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public LogoutRequest(Saml2Settings settings, HttpRequest request, String nameId,
132132
}
133133

134134
if (samlLogoutRequest == null) {
135-
id = Util.generateUniqueID();
135+
id = Util.generateUniqueID(settings.getUniqueIDPrefix());
136136
issueInstant = Calendar.getInstance();
137137
this.nameId = nameId;
138138
this.nameIdFormat = nameIdFormat;

core/src/main/java/com/onelogin/saml2/logout/LogoutResponse.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ private NodeList query (String query) throws XPathExpressionException {
343343
* InResponseTo attribute value to bet set at the Logout Response.
344344
*/
345345
public void build(String inResponseTo) {
346-
id = Util.generateUniqueID();
346+
id = Util.generateUniqueID(settings.getUniqueIDPrefix());
347347
issueInstant = Calendar.getInstance();
348348
this.inResponseTo = inResponseTo;
349349

@@ -418,4 +418,4 @@ private static StringBuilder getLogoutResponseTemplate() {
418418
public String getError() {
419419
return error;
420420
}
421-
}
421+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ private StrSubstitutor generateSubstitutor(Saml2Settings settings) throws Certif
139139
Map<String, String> valueMap = new HashMap<String, String>();
140140
Boolean wantsEncrypted = settings.getWantAssertionsEncrypted() || settings.getWantNameIdEncrypted();
141141

142-
valueMap.put("id", Util.generateUniqueID());
142+
valueMap.put("id", Util.generateUniqueID(settings.getUniqueIDPrefix()));
143143
valueMap.put("validUntilTime", Util.formatDateTime(validUntilTime.getTimeInMillis()));
144144
valueMap.put("cacheDuration", String.valueOf(cacheDuration));
145145
valueMap.put("spEntityId", settings.getSpEntityId());

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ public class Saml2Settings {
3030
*/
3131
private static final Logger LOGGER = LoggerFactory.getLogger(Saml2Settings.class);
3232

33+
public static final String DEFAULT_UNIQUE_ID_PREFIX = "ONELOGIN_";
34+
3335
// Toolkit settings
3436
private boolean strict = false;
3537
private boolean debug = false;
@@ -73,6 +75,7 @@ public class Saml2Settings {
7375
private String signatureAlgorithm = Constants.RSA_SHA1;
7476
private String digestAlgorithm = Constants.SHA1;
7577
private boolean rejectUnsolicitedResponsesWithInResponseTo = false;
78+
private String uniqueIDPrefix = DEFAULT_UNIQUE_ID_PREFIX;
7679

7780
// Compress
7881
private boolean compressRequest = true;
@@ -340,6 +343,13 @@ public Organization getOrganization() {
340343
return this.organization;
341344
}
342345

346+
/**
347+
* @return Unique ID prefix
348+
*/
349+
public String getUniqueIDPrefix() {
350+
return this.uniqueIDPrefix;
351+
}
352+
343353
/**
344354
* @return if the debug is active or not
345355
*/
@@ -447,6 +457,16 @@ protected final void setSpPrivateKey(PrivateKey spPrivateKey) {
447457
this.spPrivateKey = spPrivateKey;
448458
}
449459

460+
/**
461+
* Set the uniqueIDPrefix setting value
462+
*
463+
* @param uniqueIDPrefix
464+
* the Unique ID prefix used when generating Unique ID
465+
*/
466+
protected final void setUniqueIDPrefix(String uniqueIDPrefix) {
467+
this.uniqueIDPrefix = uniqueIDPrefix;
468+
}
469+
450470
/**
451471
* Set the idpEntityId setting value
452472
*

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ public class SettingsBuilder {
103103
public final static String ORGANIZATION_DISPLAYNAME = "onelogin.saml2.organization.displayname";
104104
public final static String ORGANIZATION_URL = "onelogin.saml2.organization.url";
105105
public final static String ORGANIZATION_LANG = "onelogin.saml2.organization.lang";
106+
public final static String UNIQUE_ID_PREFIX_PROPERTY_KEY = "onelogin.saml2.unique_id_prefix";
106107

107108
/**
108109
* Load settings from the file
@@ -206,6 +207,8 @@ public Saml2Settings build(Saml2Settings saml2Setting) {
206207

207208
saml2Setting.setOrganization(loadOrganization());
208209

210+
saml2Setting.setUniqueIDPrefix(loadUniqueIDPrefix());
211+
209212
return saml2Setting;
210213
}
211214

@@ -379,6 +382,18 @@ private List<Contact> loadContacts() {
379382
return contacts;
380383
}
381384

385+
/**
386+
* Loads the unique ID prefix. Uses default if property not set.
387+
*/
388+
private String loadUniqueIDPrefix() {
389+
String uniqueIDPrefix = loadStringProperty(UNIQUE_ID_PREFIX_PROPERTY_KEY);
390+
if (StringUtils.isNotEmpty(uniqueIDPrefix)) {
391+
return uniqueIDPrefix;
392+
} else {
393+
return Saml2Settings.DEFAULT_UNIQUE_ID_PREFIX;
394+
}
395+
}
396+
382397
/**
383398
* Loads the SP settings from the properties file
384399
*/

core/src/main/java/com/onelogin/saml2/util/Util.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ public final class Util {
105105

106106
private static final DateTimeFormatter DATE_TIME_FORMAT = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss'Z'").withZone(DateTimeZone.UTC);
107107
private static final DateTimeFormatter DATE_TIME_FORMAT_MILLS = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withZone(DateTimeZone.UTC);
108-
public static final String UNIQUE_ID_PREFIX = "ONELOGIN_";
109108
public static final String RESPONSE_SIGNATURE_XPATH = "/samlp:Response/ds:Signature";
110109
public static final String ASSERTION_SIGNATURE_XPATH = "/samlp:Response/saml:Assertion/ds:Signature";
111110
/** Indicates if JAXP 1.5 support has been detected. */
@@ -1531,10 +1530,18 @@ private static SecretKey generateSymmetricKey() throws Exception {
15311530
/**
15321531
* Generates a unique string (used for example as ID of assertions)
15331532
*
1533+
* @param prefix
1534+
* Prefix for the Unique ID.
1535+
* Use property <code>onelogin.saml2.unique_id_prefix</code> to set this.
1536+
*
15341537
* @return A unique string
15351538
*/
1536-
public static String generateUniqueID() {
1537-
return UNIQUE_ID_PREFIX + UUID.randomUUID();
1539+
public static String generateUniqueID(String prefix) {
1540+
if (StringUtils.isNotEmpty(prefix)) {
1541+
return prefix + UUID.randomUUID();
1542+
} else {
1543+
throw new IllegalArgumentException("Prefix cannot be null or empty.");
1544+
}
15381545
}
15391546

15401547
/**

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,42 @@ public void testValidateMetadataNoDescriptor() throws Exception {
356356
assertFalse(errors.isEmpty());
357357
assertTrue(errors.contains("noEntityDescriptor_xml"));
358358
}
359-
359+
360+
/**
361+
* Test that Unique ID prefix is read
362+
*
363+
* @throws Exception
364+
*/
365+
@Test
366+
public void testGivenUniqueIDPrefixIsUsed() throws Exception {
367+
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.all.properties").build();
368+
369+
assertEquals("EXAMPLE", settings.getUniqueIDPrefix());
370+
}
371+
372+
/**
373+
* Test that "_" value is ok for Unique ID prefix
374+
*
375+
* @throws Exception
376+
*/
377+
@Test
378+
public void testUniqueIDPrefixIsUsed() throws Exception {
379+
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.min_uniqueid.properties").build();
380+
381+
assertEquals("_", settings.getUniqueIDPrefix());
382+
}
383+
384+
/**
385+
* Tests that if property Unique ID prefix is unset, default value is used
386+
* @throws Exception
387+
*/
388+
@Test
389+
public void testUniqueIDPrefixUsesDefaultWhenNotSet() throws Exception {
390+
Saml2Settings settings = new SettingsBuilder().fromFile("config/config.min.properties").build();
391+
392+
assertEquals("ONELOGIN_", settings.getUniqueIDPrefix());
393+
}
394+
360395
/**
361396
* Tests the validateMetadata method of the Saml2Settings
362397
* Case Invalid: onlySPSSODescriptor_allowed_xml

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ public void testLoadFromFileMinProp() throws IOException, CertificateException,
166166

167167
assertNull(setting.getOrganization());
168168
assertTrue(setting.getContacts().isEmpty());
169+
170+
assertEquals("ONELOGIN_", setting.getUniqueIDPrefix());
169171
}
170172

171173
/**
@@ -235,6 +237,8 @@ public void testLoadFromFileAllProp() throws IOException, CertificateException,
235237
assertEquals("support", c2.getContactType());
236238
assertEquals("support@example.com", c2.getEmailAddress());
237239
assertEquals("Support Guy", c2.getGivenName());
240+
241+
assertEquals("EXAMPLE", setting.getUniqueIDPrefix());
238242
}
239243

240244
/**
@@ -522,6 +526,21 @@ public void testLoadFromFileDifferentProp() throws IOException, CertificateExcep
522526
assertTrue(c2.getGivenName().isEmpty());
523527
}
524528

529+
/**
530+
* Tests SettingsBuilder fromFile method
531+
* Case: min settings with minimal Unique ID config file
532+
*
533+
* @throws Exception
534+
*
535+
* @see com.onelogin.saml2.settings.SettingsBuilder#fromFile
536+
*/
537+
@Test
538+
public void testLoadFromFileMinUniqueIDProp() throws Exception {
539+
Saml2Settings setting = new SettingsBuilder().fromFile("config/config.min_uniqueid.properties").build();
540+
541+
assertEquals("_", setting.getUniqueIDPrefix());
542+
}
543+
525544
/**
526545
* Tests SettingsBuilder fromProperties method
527546
*
@@ -586,6 +605,8 @@ public void testFromProperties() throws IOException, Error, CertificateException
586605

587606
assertNull(setting2.getOrganization());
588607
assertTrue(setting2.getContacts().isEmpty());
608+
609+
assertEquals("ONELOGIN_", setting2.getUniqueIDPrefix());
589610
}
590611

591612
/**
@@ -654,6 +675,8 @@ public void testLoadFromValues() throws Exception {
654675
samlData.put(CONTACT_TECHNICAL_EMAIL_ADDRESS, "technical@example.org");
655676
samlData.put(CONTACT_SUPPORT_GIVEN_NAME, "Support Guy");
656677
samlData.put(CONTACT_SUPPORT_EMAIL_ADDRESS, "support@example.org");
678+
679+
samlData.put(UNIQUE_ID_PREFIX_PROPERTY_KEY, "_");
657680

658681
Saml2Settings setting = new SettingsBuilder().fromValues(samlData).build();
659682

@@ -717,6 +740,8 @@ public void testLoadFromValues() throws Exception {
717740
assertEquals("support", c2.getContactType());
718741
assertEquals("support@example.org", c2.getEmailAddress());
719742
assertEquals("Support Guy", c2.getGivenName());
743+
744+
assertEquals("_", setting.getUniqueIDPrefix());
720745
}
721746

722747
/**
@@ -840,6 +865,8 @@ public void testLoadFromValuesWithObjects() throws Exception {
840865
assertEquals("support", c2.getContactType());
841866
assertEquals("support@example.org", c2.getEmailAddress());
842867
assertEquals("Support Guy", c2.getGivenName());
868+
869+
assertEquals("ONELOGIN_", setting.getUniqueIDPrefix());
843870
}
844871

845872
/**

0 commit comments

Comments
 (0)