forked from quarkiverse/quarkus-cxf
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test security.saml-callback-handler WS-Security configuration option q…
- Loading branch information
Showing
18 changed files
with
624 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...y-policy/src/main/java/io/quarkiverse/cxf/it/security/policy/Saml1PolicyHelloService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.quarkiverse.cxf.it.security.policy; | ||
|
||
import jakarta.jws.WebMethod; | ||
import jakarta.jws.WebService; | ||
|
||
import org.apache.cxf.annotations.Policy; | ||
|
||
/** | ||
*/ | ||
@WebService(serviceName = "Saml1PolicyHelloService") | ||
@Policy(placement = Policy.Placement.BINDING, uri = "saml1-policy.xml") | ||
public interface Saml1PolicyHelloService extends AbstractHelloService { | ||
@WebMethod | ||
@Override | ||
String hello(String text); | ||
} |
15 changes: 15 additions & 0 deletions
15
...licy/src/main/java/io/quarkiverse/cxf/it/security/policy/Saml1PolicyHelloServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.quarkiverse.cxf.it.security.policy; | ||
|
||
import jakarta.jws.WebMethod; | ||
import jakarta.jws.WebService; | ||
|
||
@WebService | ||
public class Saml1PolicyHelloServiceImpl implements Saml1PolicyHelloService { | ||
|
||
@WebMethod | ||
@Override | ||
public String hello(String text) { | ||
return "Hello " + text + " from helloSaml1!"; | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
...y-policy/src/main/java/io/quarkiverse/cxf/it/security/policy/Saml2PolicyHelloService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package io.quarkiverse.cxf.it.security.policy; | ||
|
||
import jakarta.jws.WebMethod; | ||
import jakarta.jws.WebService; | ||
|
||
import org.apache.cxf.annotations.Policy; | ||
|
||
/** | ||
*/ | ||
@WebService(serviceName = "Saml2PolicyHelloService") | ||
@Policy(placement = Policy.Placement.BINDING, uri = "saml2-policy.xml") | ||
public interface Saml2PolicyHelloService extends AbstractHelloService { | ||
@WebMethod | ||
@Override | ||
String hello(String text); | ||
} |
15 changes: 15 additions & 0 deletions
15
...licy/src/main/java/io/quarkiverse/cxf/it/security/policy/Saml2PolicyHelloServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package io.quarkiverse.cxf.it.security.policy; | ||
|
||
import jakarta.jws.WebMethod; | ||
import jakarta.jws.WebService; | ||
|
||
@WebService | ||
public class Saml2PolicyHelloServiceImpl implements Saml2PolicyHelloService { | ||
|
||
@WebMethod | ||
@Override | ||
public String hello(String text) { | ||
return "Hello " + text + " from helloSaml2!"; | ||
} | ||
|
||
} |
155 changes: 155 additions & 0 deletions
155
...ecurity-policy/src/main/java/io/quarkiverse/cxf/it/security/policy/SamlBeanProducers.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
package io.quarkiverse.cxf.it.security.policy; | ||
|
||
import java.io.IOException; | ||
import java.security.cert.X509Certificate; | ||
import java.util.Collections; | ||
|
||
import javax.security.auth.callback.Callback; | ||
import javax.security.auth.callback.CallbackHandler; | ||
import javax.security.auth.callback.UnsupportedCallbackException; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.enterprise.inject.Produces; | ||
import jakarta.inject.Named; | ||
|
||
import org.apache.wss4j.common.crypto.Crypto; | ||
import org.apache.wss4j.common.crypto.CryptoType; | ||
import org.apache.wss4j.common.saml.SAMLCallback; | ||
import org.apache.wss4j.common.saml.bean.AttributeBean; | ||
import org.apache.wss4j.common.saml.bean.AttributeStatementBean; | ||
import org.apache.wss4j.common.saml.bean.ConditionsBean; | ||
import org.apache.wss4j.common.saml.bean.KeyInfoBean; | ||
import org.apache.wss4j.common.saml.bean.KeyInfoBean.CERT_IDENTIFIER; | ||
import org.apache.wss4j.common.saml.bean.SubjectBean; | ||
import org.apache.wss4j.common.saml.bean.Version; | ||
import org.apache.wss4j.common.saml.builder.SAML1Constants; | ||
import org.apache.wss4j.common.saml.builder.SAML2Constants; | ||
import org.apache.wss4j.dom.WSConstants; | ||
|
||
public class SamlBeanProducers { | ||
|
||
@Produces | ||
@ApplicationScoped | ||
@Named | ||
public CallbackHandler saml2CallbackHandler() { | ||
return new SamlCallbackHandler(Version.SAML_20); | ||
} | ||
|
||
@Produces | ||
@ApplicationScoped | ||
@Named | ||
public CallbackHandler saml1CallbackHandler() { | ||
return new SamlCallbackHandler(Version.SAML_11); | ||
} | ||
|
||
/** | ||
* A CallbackHandler instance that is used by the STS to mock up a SAML Attribute Assertion. | ||
* <p> | ||
* Adapted from <a href= | ||
* "https://github.com/apache/cxf/blob/cxf-4.0.3/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/saml/client/SamlCallbackHandler.java">https://github.com/apache/cxf/blob/cxf-4.0.3/systests/ws-security/src/test/java/org/apache/cxf/systest/ws/saml/client/SamlCallbackHandler.java</a> | ||
*/ | ||
public static class SamlCallbackHandler implements CallbackHandler { | ||
private final Version samlVersion; | ||
private String confirmationMethod; | ||
private CERT_IDENTIFIER keyInfoIdentifier = CERT_IDENTIFIER.X509_CERT; | ||
private final boolean signAssertion = false; | ||
private ConditionsBean conditions; | ||
private String cryptoAlias = "alice"; | ||
private String cryptoPassword = "password"; | ||
private String cryptoKeystoreFile = "alice.jks"; | ||
private String signatureAlgorithm = WSConstants.RSA_SHA1; | ||
private String digestAlgorithm = WSConstants.SHA1; | ||
|
||
public SamlCallbackHandler(Version samlVersion) { | ||
this.samlVersion = samlVersion; | ||
switch (samlVersion) { | ||
case SAML_20: | ||
this.confirmationMethod = SAML2Constants.CONF_SENDER_VOUCHES; | ||
break; | ||
case SAML_11: | ||
case SAML_10: | ||
this.confirmationMethod = SAML1Constants.CONF_SENDER_VOUCHES; | ||
break; | ||
default: | ||
throw new IllegalStateException("Unexpected " + Version.class.getName() + ": " + samlVersion); | ||
} | ||
} | ||
|
||
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { | ||
for (int i = 0; i < callbacks.length; i++) { | ||
if (callbacks[i] instanceof SAMLCallback) { | ||
SAMLCallback callback = (SAMLCallback) callbacks[i]; | ||
callback.setSamlVersion(samlVersion); | ||
if (conditions != null) { | ||
callback.setConditions(conditions); | ||
} | ||
|
||
callback.setIssuer("sts"); | ||
String subjectName = "uid=sts-client,o=mock-sts.com"; | ||
String subjectQualifier = "www.mock-sts.com"; | ||
SubjectBean subjectBean = new SubjectBean( | ||
subjectName, subjectQualifier, confirmationMethod); | ||
if (SAML2Constants.CONF_HOLDER_KEY.equals(confirmationMethod) | ||
|| SAML1Constants.CONF_HOLDER_KEY.equals(confirmationMethod)) { | ||
try { | ||
KeyInfoBean keyInfo = createKeyInfo(); | ||
subjectBean.setKeyInfo(keyInfo); | ||
} catch (Exception ex) { | ||
throw new IOException("Problem creating KeyInfo: " + ex.getMessage()); | ||
} | ||
} | ||
callback.setSubject(subjectBean); | ||
|
||
AttributeStatementBean attrBean = new AttributeStatementBean(); | ||
attrBean.setSubject(subjectBean); | ||
|
||
AttributeBean attributeBean = new AttributeBean(); | ||
switch (samlVersion) { | ||
case SAML_20: | ||
attributeBean.setQualifiedName("subject-role"); | ||
break; | ||
case SAML_11: | ||
case SAML_10: | ||
attributeBean.setSimpleName("subject-role"); | ||
attributeBean.setQualifiedName("http://custom-ns"); | ||
break; | ||
default: | ||
throw new IllegalStateException("Unexpected " + Version.class.getName() + ": " + samlVersion); | ||
} | ||
attributeBean.addAttributeValue("system-user"); | ||
attrBean.setSamlAttributes(Collections.singletonList(attributeBean)); | ||
callback.setAttributeStatementData(Collections.singletonList(attrBean)); | ||
callback.setSignatureAlgorithm(signatureAlgorithm); | ||
callback.setSignatureDigestAlgorithm(digestAlgorithm); | ||
|
||
Crypto crypto = io.quarkiverse.cxf.it.security.policy.CryptoProducers.createCrypto("jks", cryptoAlias, | ||
cryptoPassword, cryptoKeystoreFile); | ||
callback.setIssuerCrypto(crypto); | ||
callback.setIssuerKeyName(cryptoAlias); | ||
callback.setIssuerKeyPassword(cryptoPassword); | ||
callback.setSignAssertion(signAssertion); | ||
} | ||
} | ||
} | ||
|
||
protected KeyInfoBean createKeyInfo() throws Exception { | ||
Crypto crypto = io.quarkiverse.cxf.it.security.policy.CryptoProducers.createCrypto("jks", cryptoAlias, | ||
cryptoPassword, cryptoKeystoreFile); | ||
CryptoType cryptoType = new CryptoType(CryptoType.TYPE.ALIAS); | ||
cryptoType.setAlias(cryptoAlias); | ||
X509Certificate[] certs = crypto.getX509Certificates(cryptoType); | ||
|
||
KeyInfoBean keyInfo = new KeyInfoBean(); | ||
keyInfo.setCertIdentifer(keyInfoIdentifier); | ||
if (keyInfoIdentifier == CERT_IDENTIFIER.X509_CERT) { | ||
keyInfo.setCertificate(certs[0]); | ||
} else if (keyInfoIdentifier == CERT_IDENTIFIER.KEY_VALUE) { | ||
keyInfo.setPublicKey(certs[0].getPublicKey()); | ||
} | ||
|
||
return keyInfo; | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.