This is an automated email from the ASF dual-hosted git repository. robertlazarski pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/axis-axis2-java-rampart.git
commit 4604cf2a0695a8fbf9dea577b917df03c8f7b714 Author: Robert Lazarski <[email protected]> AuthorDate: Wed Nov 18 11:15:11 2026 -1000 RAMPART-349: let a callback handler choose the SAML subject NameID The SAML 2.0 holder-of-key subject has carried the principal's NameID since 2012; what was left open was the hard-coded email Format. Both issuers now offer the configured SAMLCallbackHandler a SAMLNameIdentifierCallback for every subject, SAML 2.0 via the new setSAML2NameID, and fall back to the email format when it stays unset. SAML2TokenIssuerTest covers the default and the override. Co-Authored-By: Claude Fable 5.1 <[email protected]> --- .../org/apache/rahas/impl/SAML2TokenIssuer.java | 35 ++++++++++++- .../org/apache/rahas/impl/SAMLTokenIssuer.java | 52 ++++++++++++-------- .../impl/util/SAMLNameIdentifierCallback.java | 30 ++++++++++-- .../apache/rahas/impl/SAML2TokenIssuerTest.java | 57 ++++++++++++++++++++++ .../test/util/TestSAML2NameIDCallbackHandler.java | 49 +++++++++++++++++++ src/site/xdoc/setting-up-sts.xml | 8 +++ 6 files changed, 204 insertions(+), 27 deletions(-) diff --git a/modules/rampart-trust/src/main/java/org/apache/rahas/impl/SAML2TokenIssuer.java b/modules/rampart-trust/src/main/java/org/apache/rahas/impl/SAML2TokenIssuer.java index 70d1a6ce..c4d71a13 100644 --- a/modules/rampart-trust/src/main/java/org/apache/rahas/impl/SAML2TokenIssuer.java +++ b/modules/rampart-trust/src/main/java/org/apache/rahas/impl/SAML2TokenIssuer.java @@ -414,7 +414,7 @@ public class SAML2TokenIssuer implements TokenIssuer { // Set the subject name identifier if (data.getPrincipal() != null) { - setSubjectNamedIdentifierData(subject, data.getPrincipal().getName(), NameID.EMAIL); + subject.setNameID(createSubjectNameID(data)); } // Create KeyInfo @@ -480,7 +480,7 @@ public class SAML2TokenIssuer implements TokenIssuer { Subject subject = (Subject)CommonUtil.buildXMLObject(Subject.DEFAULT_ELEMENT_NAME); //Create NameID and attach it to the subject - setSubjectNamedIdentifierData(subject, data.getPrincipal().getName(), NameID.EMAIL); + subject.setNameID(createSubjectNameID(data)); //Build the Subject Confirmation SubjectConfirmation subjectConfirmation @@ -712,6 +712,37 @@ public class SAML2TokenIssuer implements TokenIssuer { return authenticationStatement; } + /** + * Builds the subject NameID for the authenticated principal. The configured + * {@link SAMLCallbackHandler} is offered a {@link SAMLNameIdentifierCallback} + * first so that deployments can pick the NameID value and Format; when no + * handler is configured, or the handler leaves the SAML 2.0 NameID unset, + * the principal name is used with the email address format, as the SAML 1.1 + * issuer does. + * @param data The RST data, which carries the principal. + * @return The NameID to attach to the subject. + * @throws TrustException If the handler fails or the NameID cannot be built. + */ + protected NameID createSubjectNameID(RahasData data) throws TrustException { + String principalName = data.getPrincipal().getName(); + + SAMLCallbackHandler handler = CommonUtil.getSAMLCallbackHandler(this.tokenIssuerConfiguration, data); + if (handler != null) { + SAMLNameIdentifierCallback cb = new SAMLNameIdentifierCallback(data); + cb.setUserId(principalName); + try { + handler.handle(cb); + } catch (Exception e) { + throw new TrustException("errorCallingSAMLCallback", e); + } + if (cb.getSAML2NameID() != null) { + return cb.getSAML2NameID(); + } + } + + return SAML2Utils.createNamedIdentifier(principalName, NameID.EMAIL); + } + /** * This method will set the subject principal details to the given subject. * @param subject The subject. diff --git a/modules/rampart-trust/src/main/java/org/apache/rahas/impl/SAMLTokenIssuer.java b/modules/rampart-trust/src/main/java/org/apache/rahas/impl/SAMLTokenIssuer.java index d2306015..49aa6f10 100644 --- a/modules/rampart-trust/src/main/java/org/apache/rahas/impl/SAMLTokenIssuer.java +++ b/modules/rampart-trust/src/main/java/org/apache/rahas/impl/SAMLTokenIssuer.java @@ -218,6 +218,35 @@ public class SAMLTokenIssuer implements TokenIssuer { + /** + * Builds the subject NameIdentifier for the authenticated principal. The + * configured {@link SAMLCallbackHandler} is offered a + * {@link SAMLNameIdentifierCallback} first so that deployments can pick the + * value and Format; when no handler is configured, or the handler leaves the + * NameIdentifier unset, the principal name is used with the email address + * format. + */ + private NameIdentifier createSubjectNameIdentifier(SAMLTokenIssuerConfig config, RahasData data) + throws TrustException { + String principalName = data.getPrincipal().getName(); + + SAMLCallbackHandler handler = CommonUtil.getSAMLCallbackHandler(config, data); + if (handler != null) { + SAMLNameIdentifierCallback cb = new SAMLNameIdentifierCallback(data); + cb.setUserId(principalName); + try { + handler.handle(cb); + } catch (SAMLException e) { + throw new TrustException("errorCallingSAMLCallback", e); + } + if (cb.getNameId() != null) { + return cb.getNameId(); + } + } + + return SAMLUtils.createNamedIdentifier(principalName, NameIdentifier.EMAIL); + } + private Assertion createBearerAssertion(SAMLTokenIssuerConfig config, Document doc, Crypto crypto, Instant creationTime, Instant expirationTime, RahasData data) throws TrustException { @@ -226,21 +255,7 @@ public class SAMLTokenIssuer implements TokenIssuer { Assertion assertion; // In the case where the principal is a UT if (principal instanceof WSUsernameTokenPrincipalImpl) { - NameIdentifier nameId = null; - if (config.getCallbackHandler() != null) { - SAMLNameIdentifierCallback cb = new SAMLNameIdentifierCallback(data); - cb.setUserId(principal.getName()); - SAMLCallbackHandler callbackHandler = config.getCallbackHandler(); - try { - callbackHandler.handle(cb); - } catch (SAMLException e) { - throw new TrustException("unableToRetrieveCallbackHandler", e); - } - nameId = cb.getNameId(); - } else { - - nameId = SAMLUtils.createNamedIdentifier(principal.getName(), NameIdentifier.EMAIL); - } + NameIdentifier nameId = createSubjectNameIdentifier(config, data); assertion = createAuthAssertion(RahasConstants.SAML11_SUBJECT_CONFIRMATION_BEARER, nameId, null, config, crypto, creationTime, @@ -263,8 +278,7 @@ public class SAMLTokenIssuer implements TokenIssuer { // TODO what if principal is null ? NameIdentifier nameIdentifier = null; if (data.getPrincipal() != null) { - String subjectNameId = data.getPrincipal().getName(); - nameIdentifier =SAMLUtils.createNamedIdentifier(subjectNameId, NameIdentifier.EMAIL); + nameIdentifier = createSubjectNameIdentifier(config, data); } /** @@ -324,9 +338,7 @@ public class SAMLTokenIssuer implements TokenIssuer { * </KeyInfo> */ - String subjectNameId = data.getPrincipal().getName(); - - NameIdentifier nameId = SAMLUtils.createNamedIdentifier(subjectNameId, NameIdentifier.EMAIL); + NameIdentifier nameId = createSubjectNameIdentifier(config, data); // Create the ds:KeyValue element with the ds:X509Data X509Certificate clientCert = data.getClientCert(); diff --git a/modules/rampart-trust/src/main/java/org/apache/rahas/impl/util/SAMLNameIdentifierCallback.java b/modules/rampart-trust/src/main/java/org/apache/rahas/impl/util/SAMLNameIdentifierCallback.java index 640422b1..0adc5ff2 100644 --- a/modules/rampart-trust/src/main/java/org/apache/rahas/impl/util/SAMLNameIdentifierCallback.java +++ b/modules/rampart-trust/src/main/java/org/apache/rahas/impl/util/SAMLNameIdentifierCallback.java @@ -2,24 +2,29 @@ package org.apache.rahas.impl.util; import org.apache.rahas.RahasData; import org.opensaml.saml.saml1.core.NameIdentifier; +import org.opensaml.saml.saml2.core.NameID; /** * This is used retrieve data for the SAMLNameIdentifier. * SAMLNameIdentifier can have different formats. * Depending on it, NameIdentifier must have different values. * It should be implementation specific. - * + * <p> + * The SAML 1.1 issuer reads {@link #getNameId()}; the SAML 2.0 issuer reads + * {@link #getSAML2NameID()}. A handler that leaves the relevant one null gets + * the issuer's default: the user id in the email address format. */ public class SAMLNameIdentifierCallback implements SAMLCallback{ - + private NameIdentifier nameId = null; + private NameID saml2NameID = null; private String userId = null; private RahasData data = null; - + public SAMLNameIdentifierCallback(RahasData data){ this.data = data; } - + public int getCallbackType(){ return SAMLCallback.NAME_IDENTIFIER_CALLBACK; } @@ -32,6 +37,21 @@ public class SAMLNameIdentifierCallback implements SAMLCallback{ this.nameId = nameId; } + /** + * @return The SAML 2.0 subject NameID supplied by the handler, or null. + */ + public NameID getSAML2NameID() { + return saml2NameID; + } + + /** + * Supplies the SAML 2.0 subject NameID, including its Format. + * @param saml2NameID The NameID to put in the assertion's Subject. + */ + public void setSAML2NameID(NameID saml2NameID) { + this.saml2NameID = saml2NameID; + } + public void setUserId(String userId) { this.userId = userId; } @@ -43,5 +63,5 @@ public class SAMLNameIdentifierCallback implements SAMLCallback{ public RahasData getData() { return data; } - + } diff --git a/modules/rampart-trust/src/test/java/org/apache/rahas/impl/SAML2TokenIssuerTest.java b/modules/rampart-trust/src/test/java/org/apache/rahas/impl/SAML2TokenIssuerTest.java index 3e682f38..69d35d7c 100644 --- a/modules/rampart-trust/src/test/java/org/apache/rahas/impl/SAML2TokenIssuerTest.java +++ b/modules/rampart-trust/src/test/java/org/apache/rahas/impl/SAML2TokenIssuerTest.java @@ -26,12 +26,16 @@ import org.apache.rahas.RahasConstants; import org.apache.rahas.RahasData; import org.apache.rahas.Token; import org.apache.rahas.test.util.AbstractTestCase; +import org.apache.rahas.test.util.TestSAML2NameIDCallbackHandler; import org.apache.rahas.test.util.TestSTSClient; import org.apache.rahas.test.util.TestUtil; import org.apache.wss4j.common.crypto.Crypto; import org.opensaml.saml.common.xml.SAMLConstants; +import org.opensaml.saml.saml2.core.NameID; import org.w3c.dom.Document; +import javax.xml.namespace.QName; + import java.io.File; /** @@ -74,6 +78,59 @@ public class SAML2TokenIssuerTest extends AbstractTestCase { Assert.assertNotNull(token.getToken()); } + /** + * RAMPART-349: the holder-of-key subject of a symmetric-key assertion must + * carry the principal as its NameID, in the email format by default. + */ + public void testHolderOfKeySubjectHasNameID() throws Exception { + OMElement subject = issueAndGetSubject(this.configurationElement); + + OMElement confirmation = subject.getFirstChildWithName( + new QName(SAMLConstants.SAML20_NS, "SubjectConfirmation")); + Assert.assertEquals("urn:oasis:names:tc:SAML:2.0:cm:holder-of-key", + confirmation.getAttributeValue(new QName("Method"))); + + OMElement nameID = subject.getFirstChildWithName(new QName(SAMLConstants.SAML20_NS, "NameID")); + Assert.assertNotNull("NameID missing from the holder-of-key subject", nameID); + Assert.assertEquals("apache", nameID.getText()); + Assert.assertEquals(NameID.EMAIL, nameID.getAttributeValue(new QName("Format"))); + } + + /** + * A configured SAMLCallbackHandler decides the NameID value and Format. + */ + public void testCallbackHandlerSuppliesNameID() throws Exception { + String config = this.configurationElement.replace("<keyComputation>", + "<dataCallbackHandlerClass>" + TestSAML2NameIDCallbackHandler.class.getName() + + "</dataCallbackHandlerClass><keyComputation>"); + OMElement subject = issueAndGetSubject(config); + + OMElement nameID = subject.getFirstChildWithName(new QName(SAMLConstants.SAML20_NS, "NameID")); + Assert.assertNotNull("NameID missing from the holder-of-key subject", nameID); + Assert.assertEquals(TestSAML2NameIDCallbackHandler.VALUE, nameID.getText()); + Assert.assertEquals(TestSAML2NameIDCallbackHandler.FORMAT, + nameID.getAttributeValue(new QName("Format"))); + } + + private OMElement issueAndGetSubject(String config) throws Exception { + String appliesTo = "http://10.100.3.196:9768/services/echo/"; + RahasData rahasData = TestUtil.createTestRahasData(appliesTo); + + SAML2TokenIssuer tokenIssuer = new SAML2TokenIssuer(); + tokenIssuer.setConfigurationElement(AXIOMUtil.stringToOM(config)); + SOAPEnvelope envelope = tokenIssuer.issue(rahasData); + + TestSTSClient stsClient = TestUtil.createTestSTSClient(SAMLConstants.SAML20_NS); + Token token = stsClient.processResponse(RahasConstants.VERSION_05_02, + envelope.getBody().getFirstElement(), appliesTo); + + OMElement assertion = token.getToken(); + Assert.assertEquals(SAMLConstants.SAML20_NS, assertion.getQName().getNamespaceURI()); + OMElement subject = assertion.getFirstChildWithName(new QName(SAMLConstants.SAML20_NS, "Subject")); + Assert.assertNotNull("Subject missing from the assertion", subject); + return subject; + } + public void testCreateSubjectWithBearerSubjectConfirmation() { // TODO } diff --git a/modules/rampart-trust/src/test/java/org/apache/rahas/test/util/TestSAML2NameIDCallbackHandler.java b/modules/rampart-trust/src/test/java/org/apache/rahas/test/util/TestSAML2NameIDCallbackHandler.java new file mode 100644 index 00000000..e4c5b740 --- /dev/null +++ b/modules/rampart-trust/src/test/java/org/apache/rahas/test/util/TestSAML2NameIDCallbackHandler.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.rahas.test.util; + +import org.apache.rahas.TrustException; +import org.apache.rahas.impl.util.SAMLCallback; +import org.apache.rahas.impl.util.SAMLCallbackHandler; +import org.apache.rahas.impl.util.SAMLNameIdentifierCallback; +import org.apache.rahas.impl.util.SAML2Utils; +import org.opensaml.saml.common.SAMLException; +import org.opensaml.saml.saml2.core.NameID; + +/** + * Supplies a SAML 2.0 subject NameID with a non-default Format and ignores + * every other callback, so attribute statements fall back to the defaults. + */ +public class TestSAML2NameIDCallbackHandler implements SAMLCallbackHandler { + + public static final String VALUE = "subject-from-handler"; + public static final String FORMAT = NameID.PERSISTENT; + + public void handle(SAMLCallback callback) throws SAMLException { + if (callback.getCallbackType() == SAMLCallback.NAME_IDENTIFIER_CALLBACK) { + try { + ((SAMLNameIdentifierCallback) callback).setSAML2NameID( + SAML2Utils.createNamedIdentifier(VALUE, FORMAT)); + } catch (TrustException e) { + throw new SAMLException("Error creating the NameID", e); + } + } + } +} diff --git a/src/site/xdoc/setting-up-sts.xml b/src/site/xdoc/setting-up-sts.xml index b3293dd9..39d386cf 100644 --- a/src/site/xdoc/setting-up-sts.xml +++ b/src/site/xdoc/setting-up-sts.xml @@ -26,6 +26,14 @@ <h1>Setting up a Security Token Service</h1> <h3>Setting up a Security Token Service</h3> <p>Security Token Service can be set up as per WS-Trust specification using Rampart. The default security token service shipped with the rampart distribution is contained in the rampart-trust.mar module. It can issue SCT tokens and SAML tokens. Sample05 contains a client that connects to the default STS and obtain a SAML token. The services.xml in the sample contains "saml-issuer-config" parameter that is used to configure the default SAML issuer.</p> + <strong>Subject NameID and attributes</strong> + <p>The default SAML issuers put the authenticated principal into the assertion's subject, as a <code>saml:NameIdentifier</code> (SAML 1.1) or a <code>saml2:NameID</code> (SAML 2.0), using the email address format. To choose the value or the Format yourself, or to add attributes, register a class implementing <code>org.apache.rahas.impl.util.SAMLCallbackHandler</code> in the saml-issuer-config:</p> + + <pre> +<dataCallbackHandlerClass>org.custom.MySAMLCallbackHandler</dataCallbackHandlerClass> +</pre> + <p>The handler receives a <code>SAMLNameIdentifierCallback</code> for the subject and a <code>SAMLAttributeCallback</code> for the attribute statement; check <code>getCallbackType()</code> and ignore the ones you do not handle. For a SAML 2.0 assertion set the NameID with <code>setSAML2NameID()</code>; for SAML 1.1 use <code>setNameId()</code>. The default is used when the handler leaves it unset.</p> + <strong>STS with a custom issuer</strong> <p>Nothing has to be removed from the modules directory. The default STS is the RequestSecurityToken operation that the rahas module adds to every service it is engaged to, so a service with its own issuer simply does not engage rahas. Write your own issuer implementing the <code>"org.apache.rahas.TokenIssuer"</code> interface. Let's say that your issuer is <code>"org.custom.MyIssuer"</code>. Then create an Axis2 service archive with the following in the services.xml. The issuer [...]
