Search in sources :

Example 6 with SAMLRealmCodec

use of org.apache.cxf.sts.token.realm.SAMLRealmCodec in project cxf by apache.

the class SAMLTokenValidatorCachedRealmTest method testValidCachedRealm.

/**
 * Test a SAML Assertion with valid cached realm.
 */
@org.junit.Test
public void testValidCachedRealm() throws Exception {
    TokenValidator samlTokenValidator = new SAMLTokenValidator();
    TokenValidatorParameters validatorParameters = createValidatorParameters();
    TokenRequirements tokenRequirements = validatorParameters.getTokenRequirements();
    // Create a ValidateTarget consisting of a SAML Assertion
    Crypto crypto = CryptoFactory.getInstance(getEncryptionProperties());
    CallbackHandler callbackHandler = new PasswordCallbackHandler();
    Element samlToken = createSAMLAssertion(WSS4JConstants.WSS_SAML_TOKEN_TYPE, crypto, "mystskey", callbackHandler, "A");
    Document doc = samlToken.getOwnerDocument();
    samlToken = (Element) doc.appendChild(samlToken);
    ReceivedToken validateTarget = new ReceivedToken(samlToken);
    tokenRequirements.setValidateTarget(validateTarget);
    validatorParameters.setToken(validateTarget);
    // Now set the SAMLRealmCodec implementation on the Validator
    SAMLRealmCodec samlRealmCodec = new IssuerSAMLRealmCodec();
    ((SAMLTokenValidator) samlTokenValidator).setSamlRealmCodec(samlRealmCodec);
    TokenValidatorResponse validatorResponse = samlTokenValidator.validateToken(validatorParameters);
    assertTrue(validatorResponse != null);
    assertTrue(validatorResponse.getToken() != null);
    assertTrue(validatorResponse.getToken().getState() == STATE.VALID);
    assertTrue(validatorResponse.getTokenRealm().equals("A"));
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) PasswordCallbackHandler(org.apache.cxf.sts.common.PasswordCallbackHandler) Element(org.w3c.dom.Element) SAMLRealmCodec(org.apache.cxf.sts.token.realm.SAMLRealmCodec) Document(org.w3c.dom.Document) Crypto(org.apache.wss4j.common.crypto.Crypto) TokenRequirements(org.apache.cxf.sts.request.TokenRequirements) PasswordCallbackHandler(org.apache.cxf.sts.common.PasswordCallbackHandler) ReceivedToken(org.apache.cxf.sts.request.ReceivedToken)

Example 7 with SAMLRealmCodec

use of org.apache.cxf.sts.token.realm.SAMLRealmCodec in project cxf by apache.

the class SAMLTokenValidatorCachedRealmTest method testInValidCachedRealm.

/**
 * Test a SAML Assertion with invalid cached realm.
 */
@org.junit.Test
public void testInValidCachedRealm() throws Exception {
    TokenValidator samlTokenValidator = new SAMLTokenValidator();
    TokenValidatorParameters validatorParameters = createValidatorParameters();
    TokenRequirements tokenRequirements = validatorParameters.getTokenRequirements();
    // Create a ValidateTarget consisting of a SAML Assertion
    Crypto crypto = CryptoFactory.getInstance(getEncryptionProperties());
    CallbackHandler callbackHandler = new PasswordCallbackHandler();
    Element samlToken = createSAMLAssertion(WSS4JConstants.WSS_SAML_TOKEN_TYPE, crypto, "mystskey", callbackHandler, "A");
    Document doc = samlToken.getOwnerDocument();
    samlToken = (Element) doc.appendChild(samlToken);
    ReceivedToken validateTarget = new ReceivedToken(samlToken);
    tokenRequirements.setValidateTarget(validateTarget);
    validatorParameters.setToken(validateTarget);
    // Now set the SAMLRealmCodec implementation on the Validator
    SAMLRealmCodec samlRealmCodec = new CacheSAMLRealmCodec();
    ((SAMLTokenValidator) samlTokenValidator).setSamlRealmCodec(samlRealmCodec);
    TokenValidatorResponse validatorResponse = samlTokenValidator.validateToken(validatorParameters);
    assertTrue(validatorResponse != null);
    assertTrue(validatorResponse.getToken() != null);
    assertTrue(validatorResponse.getToken().getState() == STATE.INVALID);
    assertNull(validatorResponse.getTokenRealm());
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) PasswordCallbackHandler(org.apache.cxf.sts.common.PasswordCallbackHandler) Element(org.w3c.dom.Element) SAMLRealmCodec(org.apache.cxf.sts.token.realm.SAMLRealmCodec) Document(org.w3c.dom.Document) Crypto(org.apache.wss4j.common.crypto.Crypto) TokenRequirements(org.apache.cxf.sts.request.TokenRequirements) PasswordCallbackHandler(org.apache.cxf.sts.common.PasswordCallbackHandler) ReceivedToken(org.apache.cxf.sts.request.ReceivedToken)

Aggregations

CallbackHandler (javax.security.auth.callback.CallbackHandler)7 ReceivedToken (org.apache.cxf.sts.request.ReceivedToken)7 SAMLRealmCodec (org.apache.cxf.sts.token.realm.SAMLRealmCodec)7 Crypto (org.apache.wss4j.common.crypto.Crypto)7 Element (org.w3c.dom.Element)7 PasswordCallbackHandler (org.apache.cxf.sts.common.PasswordCallbackHandler)6 TokenRequirements (org.apache.cxf.sts.request.TokenRequirements)6 Document (org.w3c.dom.Document)6 CustomTokenPrincipal (org.apache.wss4j.common.principal.CustomTokenPrincipal)4 Principal (java.security.Principal)3 TokenProviderParameters (org.apache.cxf.sts.token.provider.TokenProviderParameters)2 RealmProperties (org.apache.cxf.sts.token.realm.RealmProperties)2 IssuerSAMLRealmCodec (org.apache.cxf.sts.token.validator.IssuerSAMLRealmCodec)2 SAMLTokenValidator (org.apache.cxf.sts.token.validator.SAMLTokenValidator)2 TokenValidator (org.apache.cxf.sts.token.validator.TokenValidator)2 TokenValidatorParameters (org.apache.cxf.sts.token.validator.TokenValidatorParameters)2 TokenValidatorResponse (org.apache.cxf.sts.token.validator.TokenValidatorResponse)2 X509Certificate (java.security.cert.X509Certificate)1 HashMap (java.util.HashMap)1 STSPropertiesMBean (org.apache.cxf.sts.STSPropertiesMBean)1