Search in sources :

Example 41 with UsernameToken

use of org.apache.wss4j.dom.message.token.UsernameToken in project cxf by apache.

the class CustomUsernameTokenProvider method createToken.

public TokenProviderResponse createToken(TokenProviderParameters tokenParameters) {
    try {
        Document doc = DOMUtils.getEmptyDocument();
        // Mock up a UsernameToken
        UsernameToken usernameToken = new UsernameToken(true, doc, WSS4JConstants.PASSWORD_TEXT);
        usernameToken.setName("alice");
        usernameToken.setPassword("password");
        String id = "UT-1234";
        usernameToken.addWSSENamespace();
        usernameToken.addWSUNamespace();
        usernameToken.setID(id);
        TokenProviderResponse response = new TokenProviderResponse();
        response.setToken(usernameToken.getElement());
        response.setTokenId(id);
        // Store the token in the cache
        if (tokenParameters.getTokenStore() != null) {
            SecurityToken securityToken = new SecurityToken(usernameToken.getID());
            securityToken.setToken(usernameToken.getElement());
            int hashCode = usernameToken.hashCode();
            String identifier = Integer.toString(hashCode);
            securityToken.setTokenHash(hashCode);
            tokenParameters.getTokenStore().add(identifier, securityToken);
        }
        return response;
    } catch (Exception e) {
        e.printStackTrace();
        throw new STSException("Can't serialize SAML assertion", e, STSException.REQUEST_FAILED);
    }
}
Also used : SecurityToken(org.apache.cxf.ws.security.tokenstore.SecurityToken) UsernameToken(org.apache.wss4j.dom.message.token.UsernameToken) STSException(org.apache.cxf.ws.security.sts.provider.STSException) TokenProviderResponse(org.apache.cxf.sts.token.provider.TokenProviderResponse) Document(org.w3c.dom.Document) STSException(org.apache.cxf.ws.security.sts.provider.STSException)

Example 42 with UsernameToken

use of org.apache.wss4j.dom.message.token.UsernameToken in project tesb-rt-se by Talend.

the class PropertyFileCallbackHandlerTest method testPropertyFileCallbackHandler.

@Test
public void testPropertyFileCallbackHandler() throws Exception {
    PropertyFileCallbackHandler handler = new PropertyFileCallbackHandler();
    Document doc = getDocument();
    UsernameToken ut = new UsernameToken(true, doc, WSS4JConstants.PASSWORD_TEXT);
    ut.setName("tadmin");
    ut.setPassword("tadmin");
    Credential credential = new Credential();
    credential.setUsernametoken(ut);
    RequestData data = new RequestData();
    handler.validate(credential, data);
}
Also used : Credential(org.apache.wss4j.dom.validate.Credential) RequestData(org.apache.wss4j.dom.handler.RequestData) UsernameToken(org.apache.wss4j.dom.message.token.UsernameToken) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 43 with UsernameToken

use of org.apache.wss4j.dom.message.token.UsernameToken in project tesb-rt-se by Talend.

the class PropertyFileCallbackHandlerTest method testPropertyFileCallbackHandlerNullUser.

@Test
public void testPropertyFileCallbackHandlerNullUser() throws Exception {
    PropertyFileCallbackHandler handler = new PropertyFileCallbackHandler();
    Document doc = getDocument();
    UsernameToken ut = new UsernameToken(true, doc, WSS4JConstants.PASSWORD_TEXT);
    ut.setName(null);
    ut.setPassword("tadmin");
    Credential credential = new Credential();
    credential.setUsernametoken(ut);
    RequestData data = new RequestData();
    try {
        handler.validate(credential, data);
    } catch (WSSecurityException ex) {
        return;
    }
    Assert.fail("Expected WSSecurityException is not thrown ");
}
Also used : Credential(org.apache.wss4j.dom.validate.Credential) RequestData(org.apache.wss4j.dom.handler.RequestData) UsernameToken(org.apache.wss4j.dom.message.token.UsernameToken) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 44 with UsernameToken

use of org.apache.wss4j.dom.message.token.UsernameToken in project jbossws-cxf by jbossws.

the class UsernameTokenCallbackHandler method createWSSEUsernameToken.

private UsernameToken createWSSEUsernameToken(String username, String password, Document doc) {
    UsernameToken usernameToken = new UsernameToken(true, doc, (password == null) ? null : WSConstants.PASSWORD_TEXT);
    usernameToken.setName(username);
    usernameToken.addWSUNamespace();
    usernameToken.addWSSENamespace();
    usernameToken.setID("id-" + username);
    if (password != null) {
        usernameToken.setPassword(password);
    }
    return usernameToken;
}
Also used : UsernameToken(org.apache.wss4j.dom.message.token.UsernameToken)

Example 45 with UsernameToken

use of org.apache.wss4j.dom.message.token.UsernameToken in project jbossws-cxf by jbossws.

the class UsernameTokenCallbackHandler method getUsernameTokenElement.

/**
 * Provide UsernameToken as a DOM Element.
 * @param ctx
 * @return
 */
public Element getUsernameTokenElement(Map<String, Object> ctx) {
    Document doc = DOMUtils.createDocument();
    Element result = null;
    UsernameToken usernameToken = null;
    String username = (String) ctx.get(SecurityConstants.USERNAME);
    String password = (String) ctx.get(SecurityConstants.PASSWORD);
    if (username != null) {
        usernameToken = createWSSEUsernameToken(username, password, doc);
        result = usernameToken.getElement();
    }
    return result;
}
Also used : Element(org.w3c.dom.Element) UsernameToken(org.apache.wss4j.dom.message.token.UsernameToken) Document(org.w3c.dom.Document)

Aggregations

UsernameToken (org.apache.wss4j.dom.message.token.UsernameToken)46 Document (org.w3c.dom.Document)32 Credential (org.apache.wss4j.dom.validate.Credential)16 RequestData (org.apache.wss4j.dom.handler.RequestData)15 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)12 Element (org.w3c.dom.Element)10 Principal (java.security.Principal)9 WSUsernameTokenPrincipalImpl (org.apache.wss4j.common.principal.WSUsernameTokenPrincipalImpl)5 Test (org.junit.Test)5 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)4 JAXBElement (javax.xml.bind.JAXBElement)4 AuthorizationPolicy (org.apache.cxf.configuration.security.AuthorizationPolicy)4 Message (org.apache.cxf.message.Message)4 ReceivedToken (org.apache.cxf.sts.request.ReceivedToken)4 UsernameTokenType (org.apache.cxf.ws.security.sts.provider.model.secext.UsernameTokenType)4 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)4 CustomTokenPrincipal (org.apache.wss4j.common.principal.CustomTokenPrincipal)4 CallbackHandler (javax.security.auth.callback.CallbackHandler)3 Fault (org.apache.cxf.interceptor.Fault)3 STSPropertiesMBean (org.apache.cxf.sts.STSPropertiesMBean)3