View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.jboss.as.quickstarts.picketlink;
18  
19  import org.picketlink.identity.federation.api.wstrust.WSTrustClient;
20  import org.picketlink.identity.federation.api.wstrust.WSTrustClient.SecurityInfo;
21  import org.picketlink.identity.federation.core.saml.v2.util.DocumentUtil;
22  import org.picketlink.identity.federation.core.wstrust.WSTrustException;
23  import org.picketlink.identity.federation.core.wstrust.plugins.saml.SAMLUtil;
24  import org.w3c.dom.Element;
25  
26  /**
27   * This class demonstrates how to request SAML 2.0 security token from PicketLink STS.
28   *   
29   * @author Peter Skopek (pskopek ( at redhat dot com))
30   *
31   */
32  public class WSTrustClientExample {
33  
34      public static void main(String[] args) throws Exception {
35          
36          String userName = (args.length > 0 ? args[0] : "tomcat");
37          String password = (args.length > 1 ? args[1] : "tomcat");
38          
39          // Step 1: Create a WS Trust Client
40          WSTrustClient client = new WSTrustClient("PicketLinkSTS", "PicketLinkSTSPort", "http://localhost:8080/picketlink-sts/PicketLinkSTS", 
41                  new SecurityInfo(userName, password));
42          Element assertionElement = null;
43          try {
44              System.out.println("Invoking token service to get SAML assertion for user:" + userName + " with password:" + password);
45              // Step 2: Get a SAML2 Assertion Token from the PicketLink STS
46              assertionElement = client.issueToken(SAMLUtil.SAML2_TOKEN_TYPE);
47              System.out.println("SAML assertion for user:" + userName + " successfully obtained!");
48          } catch (WSTrustException wse) {
49              System.out.println("Unable to issue assertion: " + wse.getMessage());
50              wse.printStackTrace();
51              System.exit(1);
52          } catch (Exception e) {
53              System.out.println("Problem:" + e.getMessage());
54              e.printStackTrace();
55              System.exit(2);
56          }
57          
58          // Step 3: Display the SAML2 token
59          String el = DocumentUtil.getDOMElementAsString(assertionElement);
60          System.out.println(el);
61      }
62  
63  }