How to Request a Token to the PicketLink STS

import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.Service.Mode;
import javax.xml.ws.soap.AddressingFeature;

import org.oasis_open.docs.ws_sx.ws_trust._200512.RequestSecurityTokenResponseCollectionType;
import org.oasis_open.docs.ws_sx.ws_trust._200512.RequestSecurityTokenType;

public final class PicketLinkSample {

  public static void main(final String[] args) throws Exception {
    Authenticator.setDefault(new Authenticator() {
 
      public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("username", "password".toCharArray());
      }
 
    });

    final Service service = Service.create(new URL("http://host:8080/picketlink-sts?WSDL"), new QName("urn:picketlink:identity-federation:sts", "PicketLinkSTS"));

    final Dispatch dispatch = service.createDispatch(new QName("urn:picketlink:identity-federation:sts", "PicketLinkSTSPort"), JAXBContext.newInstance("org.oasis_open.docs.ws_sx.ws_trust._200512"), Mode.PAYLOAD, new AddressingFeature());

    final BindingProvider provider = (BindingProvider) dispatch;

    final Map requestContext = provider.getRequestContext();

    requestContext.put(BindingProvider.SOAPACTION_URI_PROPERTY, "http://docs.oasis-open.org/ws-sx/ws-trust/200512/RST/Issue"); // same action for all operations

    final RequestSecurityTokenType request = new RequestSecurityTokenType();

    request.getAny().add(new JAXBElement<String>(new QName("http://docs.oasis-open.org/ws-sx/ws-trust/200512", "TokenType"), String.class, "http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0" /* or http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV1.1 */ ));

    request.getAny().add(new JAXBElement<String>(new QName("http://docs.oasis-open.org/ws-sx/ws-trust/200512", "RequestType"), String.class, "http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue"));

    dispatch.invoke(new JAXBElement<RequestSecurityTokenType>(new QName("http://docs.oasis-open.org/ws-sx/ws-trust/200512", "RequestSecurityToken"), RequestSecurityTokenType.class, request)));
  }

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s