FLEXNET OPERATIONS SOAP WEB SERVICES GUIDE

FlexNet Operations Web Services Guide

Appendix C: ClientSecurityCredentials Class Source

FlexNet Operations Web services requires a client to provide credentials in the SOAP header or as BASIC HTTP user authentication. BASIC authentication encrypts the user and password with Base64 encoding and passes it as an HTTP request header parameter. For example:

Authorization: Basic QWxhghwelhijhrcGVuIHNlc2FtZQ==

FlexNet Operations also provides a ClientSecurityCredentials helper class as a convenience. The source of this class is included in this appendix and in FlexNetOperationsWebServices12_10_0.zip.

Listing of ClientSecurityCredentials.java:

// COPYRIGHT (c) 2009 by Flexera

import org.apache.axis.client.Stub;
import org.apache.axis.encoding.Base64;
import org.apache.axis.message.SOAPHeaderElement;

import java.io.UnsupportedEncodingException;
import java.rmi.*;

import javax.xml.soap.Name;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPFactory;

/*
 * This class consist of convenient methods that allow to set
 * the user name and password (credentials) for the invoker of
 * the webservice operation. The user name/password can be set
 * in the client context (HTTP Authorization Header) or in the
 * SOAPHeader of the SOAPEnvelope.
 *
 *  User Name Syntax :
 *  ---------------------- 
 *  userName##domainName
 *        where userName --> name of user.
 *              domainName --> domain to which the user belongs.
 *        If domain is not provided, default domain (FLEXnet) is used.
 */

public class ClientSecurityCredentials {

   public static final String AUTH_SOAP_HEADER = "auth.soapHeader";
   public static final String nameSpace = "flex";
   public static final String nameSpaceURI = "urn:com.macrovision:flexnet/
                                              platform";

   private final static String CHAR_SET = "UTF-8";

   private Stub stub;
   private SOAPFactory soapFactory;

   /*
    * Allows setting user credentials in the client context (HTTP Authorization
    * Header).
    */

   public ClientSecurityCredentials(Remote service) {
      this.stub = (Stub)service;
   }

   /*
    * Allows setting user credentials in the SOAP Header.
    */
   public ClientSecurityCredentials(Remote service, String authType) throws
          SOAPException {
      this(service);
      if (AUTH_SOAP_HEADER.equals(authType)) {
         soapFactory = SOAPFactory.newInstance();
      }
   }

   /*
    * Set the user name in the client context (HTTP Authorization Header) or in
    * the SOAP Header, depending on the constructor used.
    */
   public void setUsername(String username) throws SOAPException {
      if (stub != null) {
         if(soapFactory != null) {
            this.setSoapHeader("UserId", username);
         }
         else {
            stub.setUsername(username);
         }
      }
   }



   /*
    * Set the password in the client context (HTTP Authorization Header) or in
    * the SOAP Header, depending on the constructor used.
    */
   public void setPassword(String password) throws SOAPException,
               UnsupportedEncodingException {

      if (stub != null) {
         if(soapFactory != null) {
            String encryptedPwd = Base64.encode(password.getBytes(CHAR_SET));
            this.setSoapHeader("UserPassword", encryptedPwd);
         }
         else {
            stub.setPassword(password);
         }
      }
   }

   /*
    *
    */
   private void setSoapHeader(String elementName, String value) throws
                 SOAPException {
      Name nameElem = soapFactory.createName(elementName, nameSpace,
                      nameSpaceURI);
      SOAPHeaderElement headerElem = new SOAPHeaderElement(nameElem);
      headerElem.addTextNode(value);
      this.stub.setHeader(headerElem);
   }
}