cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From owu...@apache.org
Subject svn commit: r1241977 - in /cxf/trunk/services/sts/sts-core/src: main/java/org/apache/cxf/sts/token/provider/DefaultSubjectProvider.java test/java/org/apache/cxf/sts/token/provider/SAMLProviderCustomTest.java
Date Wed, 08 Feb 2012 17:07:57 GMT
Author: owulff
Date: Wed Feb  8 17:07:57 2012
New Revision: 1241977

URL: http://svn.apache.org/viewvc?rev=1241977&view=rev
Log:
[CXF-4093] NameIDFormat of SAML Subject configurable

Modified:
    cxf/trunk/services/sts/sts-core/src/main/java/org/apache/cxf/sts/token/provider/DefaultSubjectProvider.java
    cxf/trunk/services/sts/sts-core/src/test/java/org/apache/cxf/sts/token/provider/SAMLProviderCustomTest.java

Modified: cxf/trunk/services/sts/sts-core/src/main/java/org/apache/cxf/sts/token/provider/DefaultSubjectProvider.java
URL: http://svn.apache.org/viewvc/cxf/trunk/services/sts/sts-core/src/main/java/org/apache/cxf/sts/token/provider/DefaultSubjectProvider.java?rev=1241977&r1=1241976&r2=1241977&view=diff
==============================================================================
--- cxf/trunk/services/sts/sts-core/src/main/java/org/apache/cxf/sts/token/provider/DefaultSubjectProvider.java
(original)
+++ cxf/trunk/services/sts/sts-core/src/main/java/org/apache/cxf/sts/token/provider/DefaultSubjectProvider.java
Wed Feb  8 17:07:57 2012
@@ -57,6 +57,7 @@ public class DefaultSubjectProvider impl
     
     private static final Logger LOG = LogUtils.getL7dLogger(DefaultSubjectProvider.class);
     private String subjectNameQualifier = "http://cxf.apache.org/sts";
+    private String subjectNameIDFormat;
     
     /**
      * Set the SubjectNameQualifier.
@@ -65,6 +66,14 @@ public class DefaultSubjectProvider impl
         this.subjectNameQualifier = subjectNameQualifier;
         LOG.fine("Setting Subject Name Qualifier: " + subjectNameQualifier);
     }
+    
+    /**
+     * Set the SubjectNameIDFormat.
+     */
+    public void setSubjectNameIDFormat(String subjectNameIDFormat) {
+        this.subjectNameIDFormat = subjectNameIDFormat;
+        LOG.fine("Setting Subject Name format: " + subjectNameIDFormat);
+    }
 
     /**
      * Get a SubjectBean object.
@@ -95,7 +104,10 @@ public class DefaultSubjectProvider impl
         SubjectBean subjectBean = 
             new SubjectBean(principal.getName(), subjectNameQualifier, confirmationMethod);
         LOG.fine("Creating new subject with principal name: " + principal.getName());
-
+        if (subjectNameIDFormat != null && subjectNameIDFormat.length() > 0) {
+            subjectBean.setSubjectNameIDFormat(subjectNameIDFormat);
+        }
+        
         if (STSConstants.SYMMETRIC_KEY_KEYTYPE.equals(keyType)) {
             Crypto crypto = stsProperties.getEncryptionCrypto();
             CryptoType cryptoType = new CryptoType(CryptoType.TYPE.ALIAS);

Modified: cxf/trunk/services/sts/sts-core/src/test/java/org/apache/cxf/sts/token/provider/SAMLProviderCustomTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/services/sts/sts-core/src/test/java/org/apache/cxf/sts/token/provider/SAMLProviderCustomTest.java?rev=1241977&r1=1241976&r2=1241977&view=diff
==============================================================================
--- cxf/trunk/services/sts/sts-core/src/test/java/org/apache/cxf/sts/token/provider/SAMLProviderCustomTest.java
(original)
+++ cxf/trunk/services/sts/sts-core/src/test/java/org/apache/cxf/sts/token/provider/SAMLProviderCustomTest.java
Wed Feb  8 17:07:57 2012
@@ -39,6 +39,7 @@ import org.apache.ws.security.WSConstant
 import org.apache.ws.security.WSSecurityException;
 import org.apache.ws.security.components.crypto.Crypto;
 import org.apache.ws.security.components.crypto.CryptoFactory;
+import org.apache.ws.security.saml.ext.builder.SAML1Constants;
 import org.apache.ws.security.util.DOM2Writer;
 
 /**
@@ -213,6 +214,33 @@ public class SAMLProviderCustomTest exte
         assertTrue(tokenString.contains("http://cxf.apache.org/sts/custom"));
     }
     
+    /**
+     * Create a Saml1 Assertion with a custom NameID Format of the Subject
+     */
+    @org.junit.Test
+    public void testCustomSaml1SubjectNameIDFormat() throws Exception {
+        TokenProvider samlTokenProvider = new SAMLTokenProvider();
+        TokenProviderParameters providerParameters = 
+            createProviderParameters(WSConstants.WSS_SAML_TOKEN_TYPE, STSConstants.BEARER_KEY_KEYTYPE);
+        
+        DefaultSubjectProvider subjectProvider = new DefaultSubjectProvider();
+        subjectProvider.setSubjectNameIDFormat(SAML1Constants.NAMEID_FORMAT_EMAIL_ADDRESS);
+        ((SAMLTokenProvider)samlTokenProvider).setSubjectProvider(subjectProvider);
+        
+        assertTrue(samlTokenProvider.canHandleToken(WSConstants.WSS_SAML_TOKEN_TYPE));
+        TokenProviderResponse providerResponse = samlTokenProvider.createToken(providerParameters);
+        assertTrue(providerResponse != null);
+        assertTrue(providerResponse.getToken() != null && providerResponse.getTokenId()
!= null);
+        
+        Element token = providerResponse.getToken();
+        String tokenString = DOM2Writer.nodeToString(token);
+        assertTrue(tokenString.contains(providerResponse.getTokenId()));
+        assertTrue(tokenString.contains("AttributeStatement"));
+        assertFalse(tokenString.contains("AuthenticationStatement"));
+        assertTrue(tokenString.contains("alice"));
+        assertTrue(tokenString.contains(SAML1Constants.NAMEID_FORMAT_EMAIL_ADDRESS));
+    }
+    
     private TokenProviderParameters createProviderParameters(
         String tokenType, String keyType
     ) throws WSSecurityException {



Mime
View raw message