generated from quarkiverse/quarkiverse-template
-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test security.saml-callback-handler WS-Security configuration option #1096
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Missing Override annotation