CXFSAMLCallbackHandler implements CallbackHandler { @Override public void handle(final Callback[] callbacks) throws IOException, UnsupportedCallbackException { LOG.trace("CXFSAMLCallbackHandler.handle begin"); for (final Callback callback : callbacks) { if (callback instanceof SAMLCallback) { try { final Message message = getCurrentMessage(); AssertionType custAssertion = getCustAssertion(message.get("assertion")); NhinTargetSystemType systemType = (NhinTargetSystemType) message.get("targetSystemType"); String exchangeName = systemType != null ? systemType.getExchangeName() : InternalExchangeManager .getInstance().getDefaultExchange(); LOG.trace("Is this exchange {} default for building assertion? {}", exchangeName, systemType != null); final SAMLCallback oSAMLCallback = (SAMLCallback) callback; oSAMLCallback.setIssuerKeyName( accessor.getProperty(NhincConstants.SAML_PROPERTY_FILE, SamlConstants.ISSUER_KEY_STRING)); oSAMLCallback.setIssuerKeyPassword( accessor.getProperty(NhincConstants.SAML_PROPERTY_FILE, SamlConstants.ISSUER_KEY_VALUE)); oSAMLCallback.setSignAssertion(accessor.getPropertyBoolean(NhincConstants.SAML_PROPERTY_FILE, SamlConstants.SIGN_ASSERTION_BOOL)); oSAMLCallback.setSendKeyValue(accessor.getPropertyBoolean(NhincConstants.SAML_PROPERTY_FILE, SamlConstants.SEND_KEYVALUE_BOOL)); issuerCrypto = CryptoFactory.getInstance(accessor.getProperty(NhincConstants.SAML_PROPERTY_FILE, SamlConstants.SIGNATURE_PROPERTIES_STRING)); oSAMLCallback.setIssuerCrypto(issuerCrypto); oSAMLCallback.setSamlVersion(Version.SAML_20); oSAMLCallback.setSignatureAlgorithm(SAMLUtils.extractSignatureFromAssertion(custAssertion)); oSAMLCallback.setSignatureDigestAlgorithm(SAMLUtils.extractDigestFromAssertion(custAssertion)); final SamlTokenCreator creator = new SamlTokenCreator(); final CallbackProperties properties = new CallbackMapProperties(addMessageProperties( creator.createRequestContext(custAssertion, getResource(message), null), message)); oSAMLCallback.setAssertionElement(builder.build(properties, StoreUtil.getGatewayCertificateAlias(exchangeName))); } catch (WSSecurityException | PropertyAccessException e) { LOG.error("Failed to create saml: {}", e.getLocalizedMessage(), e); throw new SAMLAssertionBuilderException(e.getMessage(), e); } } } LOG.trace("CXFSAMLCallbackHandler.handle end"); } CXFSAMLCallbackHandler();  CXFSAMLCallbackHandler(final HOKSAMLAssertionBuilder builder); @Override void handle(final Callback[] callbacks); static final String HOK_CONFIRM; }
@Test public void testHandle() throws Exception { Callback[] callbackList = new Callback[1]; SAMLCallback samlCallback = new SAMLCallback(); callbackList[0] = samlCallback; final Message message = mock(Message.class); AssertionType assertionType = mock(AssertionType.class); HOKSAMLAssertionBuilder builder = mock(HOKSAMLAssertionBuilder.class); CXFSAMLCallbackHandler callbackHandler = new CXFSAMLCallbackHandler(builder) { @Override protected Message getCurrentMessage() { return message; } }; when(message.get("assertion")).thenReturn(assertionType); when(message.get(Message.INBOUND_MESSAGE)).thenReturn(false); when(message.get(NhincConstants.WS_SOAP_TARGET_HOME_COMMUNITY_ID)).thenReturn("1.1"); when(message.get(SamlConstants.TARGET_API_LEVEL)).thenReturn("G0"); when(message.get(SamlConstants.ACTION_PROP)).thenReturn("Soap Action"); when(builder.build(any(CallbackProperties.class), any(String.class))).thenReturn(assertionElement); callbackHandler.handle(callbackList); assertEquals(samlCallback.getSamlVersion(), org.opensaml.saml.common.SAMLVersion.VERSION_20); assertEquals(samlCallback.getAssertionElement().getTextContent(), ASSERTION); }