Search in sources :

Example 1 with CommonCallbackHandler

use of org.apache.cxf.systest.sts.common.CommonCallbackHandler in project cxf by apache.

the class IssueUnitTest method testSAMLinWSSecToOtherRealm.

@org.junit.Test
public void testSAMLinWSSecToOtherRealm() throws Exception {
    createBus(getClass().getResource("cxf-client.xml").toString());
    Crypto crypto = CryptoFactory.getInstance(getEncryptionProperties());
    CallbackHandler callbackHandler = new CommonCallbackHandler();
    // Create SAML token
    Element samlToken = createSAMLAssertion(WSS4JConstants.WSS_SAML2_TOKEN_TYPE, crypto, "mystskey", callbackHandler, null, "alice", "a-issuer");
    String id = null;
    QName elName = DOMUtils.getElementQName(samlToken);
    if (elName.equals(new QName(WSS4JConstants.SAML_NS, "Assertion")) && samlToken.hasAttributeNS(null, "AssertionID")) {
        id = samlToken.getAttributeNS(null, "AssertionID");
    } else if (elName.equals(new QName(WSS4JConstants.SAML2_NS, "Assertion")) && samlToken.hasAttributeNS(null, "ID")) {
        id = samlToken.getAttributeNS(null, "ID");
    }
    if (id == null) {
        id = samlToken.getAttributeNS(WSS4JConstants.WSU_NS, "Id");
    }
    SecurityToken wstoken = new SecurityToken(id, samlToken, null, null);
    Map<String, Object> properties = new HashMap<>();
    properties.put(SecurityConstants.TOKEN, wstoken);
    properties.put(SecurityConstants.TOKEN_ID, wstoken.getId());
    // Get a token
    SecurityToken token = requestSecurityToken(SAML2_TOKEN_TYPE, BEARER_KEYTYPE, null, bus, DEFAULT_ADDRESS, null, properties, "b-issuer", "Transport_SAML_Port");
    /*
        SecurityToken token =
                requestSecurityToken(SAML2_TOKEN_TYPE, BEARER_KEYTYPE, null,
                        bus, DEFAULT_ADDRESS, null, properties, "b-issuer", null);
                        */
    assertEquals(SAML2_TOKEN_TYPE, token.getTokenType());
    assertNotNull(token.getToken());
    List<WSSecurityEngineResult> results = processToken(token);
    assertTrue(results != null && results.size() == 1);
    SamlAssertionWrapper assertion = (SamlAssertionWrapper) results.get(0).get(WSSecurityEngineResult.TAG_SAML_ASSERTION);
    assertNotNull(assertion);
    assertTrue(assertion.isSigned());
    List<String> methods = assertion.getConfirmationMethods();
    String confirmMethod = null;
    if (methods != null && !methods.isEmpty()) {
        confirmMethod = methods.get(0);
    }
    assertTrue(confirmMethod != null && confirmMethod.contains("bearer"));
    assertEquals("b-issuer", assertion.getIssuerString());
    String subjectName = assertion.getSaml2().getSubject().getNameID().getValue();
    assertEquals("Subject must be ALICE instead of " + subjectName, "ALICE", subjectName);
}
Also used : CommonCallbackHandler(org.apache.cxf.systest.sts.common.CommonCallbackHandler) CallbackHandler(javax.security.auth.callback.CallbackHandler) CommonCallbackHandler(org.apache.cxf.systest.sts.common.CommonCallbackHandler) HashMap(java.util.HashMap) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) SamlAssertionWrapper(org.apache.wss4j.common.saml.SamlAssertionWrapper) WSSecurityEngineResult(org.apache.wss4j.dom.engine.WSSecurityEngineResult) SecurityToken(org.apache.cxf.ws.security.tokenstore.SecurityToken) Crypto(org.apache.wss4j.common.crypto.Crypto)

Example 2 with CommonCallbackHandler

use of org.apache.cxf.systest.sts.common.CommonCallbackHandler in project cxf by apache.

the class SAMLDelegationTest method testTransportForgedDelegationToken.

@org.junit.Test
public void testTransportForgedDelegationToken() throws Exception {
    createBus(getClass().getResource("cxf-client.xml").toString());
    Crypto crypto = CryptoFactory.getInstance(getEncryptionProperties());
    CallbackHandler callbackHandler = new CommonCallbackHandler();
    // Create SAML token
    Element samlToken = createSAMLAssertion(WSS4JConstants.WSS_SAML2_TOKEN_TYPE, STSConstants.BEARER_KEY_KEYTYPE, crypto, "eve", callbackHandler, "alice", "a-issuer");
    try {
        requestSecurityToken(SAML2_TOKEN_TYPE, BEARER_KEYTYPE, samlToken, bus, DEFAULT_ADDRESS, true, "Transport_Port");
        fail("Failure expected on a forged delegation token");
    } catch (Exception ex) {
    // expected
    }
    try {
        requestSecurityToken(SAML2_TOKEN_TYPE, BEARER_KEYTYPE, samlToken, bus, DEFAULT_ADDRESS, false, "Transport_Port");
        fail("Failure expected on a forged delegation token");
    } catch (Exception ex) {
    // expected
    }
}
Also used : Crypto(org.apache.wss4j.common.crypto.Crypto) CommonCallbackHandler(org.apache.cxf.systest.sts.common.CommonCallbackHandler) CallbackHandler(javax.security.auth.callback.CallbackHandler) CommonCallbackHandler(org.apache.cxf.systest.sts.common.CommonCallbackHandler) Element(org.w3c.dom.Element) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException)

Aggregations

CallbackHandler (javax.security.auth.callback.CallbackHandler)2 CommonCallbackHandler (org.apache.cxf.systest.sts.common.CommonCallbackHandler)2 Crypto (org.apache.wss4j.common.crypto.Crypto)2 Element (org.w3c.dom.Element)2 HashMap (java.util.HashMap)1 QName (javax.xml.namespace.QName)1 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)1 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)1 SamlAssertionWrapper (org.apache.wss4j.common.saml.SamlAssertionWrapper)1 WSSecurityEngineResult (org.apache.wss4j.dom.engine.WSSecurityEngineResult)1