TrustedSignonMapping.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /**
  2. * Licensed Materials - Property of IBM
  3. *
  4. * IBM Cognos Products: CAMAAA
  5. *
  6. * (C) Copyright IBM Corp. 2005, 2012
  7. *
  8. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with
  9. * IBM Corp.
  10. */
  11. import java.io.BufferedReader;
  12. import java.io.DataInputStream;
  13. import java.io.FileInputStream;
  14. import java.io.InputStreamReader;
  15. import java.util.HashMap;
  16. import com.cognos.CAM_AAA.authentication.INamespaceConfiguration;
  17. import com.cognos.CAM_AAA.authentication.INamespaceTrustedSignonProvider;
  18. import com.cognos.CAM_AAA.authentication.ITrustedSignonRequest;
  19. import com.cognos.CAM_AAA.authentication.SystemRecoverableException;
  20. import com.cognos.CAM_AAA.authentication.UnrecoverableException;
  21. import com.cognos.CAM_AAA.authentication.UserRecoverableException;
  22. public class TrustedSignonMapping
  23. extends Namespace
  24. implements INamespaceTrustedSignonProvider
  25. {
  26. private static String CONFIG_FILE = "domainMapping.xml";
  27. private static String DOMAIN_SPLIT_DELIMITER = "domainSplitDelimiter";
  28. private static String DOMAIN_SPLIT_POSITION = "domainSplitPosition";
  29. private static String REMOTE_USER_VALUE_SPLIT = "remoteUserSplitPosition";
  30. private static String BEFORE = "before";
  31. private static String AFTER = "after";
  32. private HashMap <String, String>domainMapping;
  33. private String domainSplitDelimiter;
  34. private String domainSplitPosition;
  35. private String remoteUserSplitPosition;
  36. private String passedRemoteUserName;
  37. public TrustedSignonMapping()
  38. {
  39. super();
  40. // read the mapping of the domain information
  41. this.domainMapping = new HashMap <String, String> ();
  42. }
  43. @Override
  44. public void init(final INamespaceConfiguration theNamespaceConfiguration) throws UnrecoverableException
  45. {
  46. super.init(theNamespaceConfiguration);
  47. //
  48. // Read our configuration from this IBM Cognos 8 install's
  49. // configuration directory.
  50. //
  51. final String configPath = theNamespaceConfiguration.getInstallLocation() + "/configuration";
  52. final String configFile = configPath + "/" + TrustedSignonMapping.CONFIG_FILE;
  53. this.readConfigInfo(configFile);
  54. }
  55. private void readConfigInfo(final String configFile )
  56. {
  57. try
  58. {
  59. // Open the configuration file
  60. FileInputStream fstream = new FileInputStream(configFile);
  61. // Get the object of DataInputStream
  62. DataInputStream in = new DataInputStream(fstream);
  63. BufferedReader br = new BufferedReader(new InputStreamReader(in));
  64. String strLine;
  65. //Read File Line By Line
  66. while ((strLine = br.readLine()) != null)
  67. {
  68. // Print the content on the console
  69. // the configuration format is like: domain=namespaceID
  70. // System.out.println("----Read configure: " + strLine + "---");
  71. final String[] splitObject = strLine.split("=");
  72. final String domainName = splitObject[0];
  73. final String namespaceID = splitObject[1].replaceAll("\\p{Cntrl}", "");
  74. if ( domainName.equalsIgnoreCase(TrustedSignonMapping.DOMAIN_SPLIT_DELIMITER))
  75. this.domainSplitDelimiter = namespaceID;
  76. else if ( domainName.equalsIgnoreCase(TrustedSignonMapping.DOMAIN_SPLIT_POSITION))
  77. this.domainSplitPosition = namespaceID;
  78. else if ( domainName.equalsIgnoreCase(TrustedSignonMapping.REMOTE_USER_VALUE_SPLIT))
  79. this.remoteUserSplitPosition = namespaceID;
  80. else
  81. // since namespaceID is case sensitive, so we don't normalize it.
  82. // but we do normalize the domain name
  83. this.domainMapping.put(domainName.toUpperCase(), namespaceID);
  84. // System.out.println("domain: " + domainName + " namespace: " + namespaceID);
  85. }
  86. //Close the input stream
  87. in.close();
  88. }
  89. catch (Exception e)
  90. {//Catch exception if any
  91. System.err.println("Error: " + e.getMessage());
  92. }
  93. }
  94. public void processLogonRequest(ITrustedSignonRequest theRequest)
  95. throws
  96. UserRecoverableException,
  97. SystemRecoverableException,
  98. UnrecoverableException
  99. {
  100. String[] username = null;
  101. // 1 - Look for trusted credentials
  102. username = theRequest.getTrustedEnvVarValue("REMOTE_USER");
  103. if (username == null)
  104. {
  105. String[] theRequestedVars = new String[] {"REMOTE_USER"};
  106. SystemRecoverableException e = new SystemRecoverableException(
  107. "Requesting trusted REMOTE_USER.",
  108. theRequestedVars);
  109. throw e;
  110. }
  111. // extract the domain information
  112. String domainName = this.extractUserDomainInformation(username);
  113. // System.out.println("--Extrated domain is: " + domainName + " from username: " + username[0]);
  114. String namespaceID = null;
  115. if ( domainName != null )
  116. {
  117. namespaceID = this.mapToNamespaceID(domainName);
  118. }
  119. // Map the namespace information
  120. // The namespace ID of the authentication namespace to use. For the purpose of this sample, it is hardcoded.
  121. if ( namespaceID == null )
  122. {
  123. // Sorry, we need to know the namespaceID to be able to redirect to the the right namespace.
  124. UnrecoverableException e = new UnrecoverableException("Could not redirect to the specific namespace", "Missing domain information. Please check the system setting");
  125. throw e;
  126. }
  127. if ( namespaceID != null )
  128. theRequest.setNamespaceID( namespaceID );
  129. //
  130. // Set the trusted environment variable REMOTE_USER to achieve SSO against the TS namespace.
  131. //
  132. theRequest.removeTrustedEnvVar( "REMOTE_USER" );
  133. theRequest.addTrustedEnvVar("REMOTE_USER", this.passedRemoteUserName);
  134. // System.out.println("Set namespace: " + namespaceID);
  135. // System.out.println("Set Remote_user: " + this.passedRemoteUserName);
  136. }
  137. private String extractUserDomainInformation(final String[] username )
  138. {
  139. String userDomain = null;
  140. this.passedRemoteUserName = username[0];
  141. if ( this.domainSplitDelimiter != null )
  142. {
  143. int slashPosition = username[0].indexOf(this.domainSplitDelimiter);
  144. if (slashPosition > 0)
  145. {
  146. userDomain = this.splitValueBasedOnPosition(username, slashPosition, this.domainSplitPosition);
  147. if ( this.remoteUserSplitPosition != null)
  148. {
  149. this.passedRemoteUserName = this.splitValueBasedOnPosition(username, slashPosition, this.remoteUserSplitPosition);
  150. }
  151. else
  152. this.passedRemoteUserName = username[0];
  153. }
  154. }
  155. return userDomain;
  156. }
  157. private String splitValueBasedOnPosition(final String[] username, final int slashPosition, final String splitPosition)
  158. {
  159. String userDomain;
  160. if ( splitPosition.equalsIgnoreCase(TrustedSignonMapping.BEFORE))
  161. userDomain = username[0].substring(0, slashPosition);
  162. else if ( splitPosition.equalsIgnoreCase(TrustedSignonMapping.AFTER))
  163. userDomain = username[0].substring(slashPosition+1);
  164. else
  165. userDomain = username[0];
  166. return userDomain;
  167. }
  168. private final String mapToNamespaceID(final String domainName)
  169. {
  170. final String namespaceID = (String) this.domainMapping.get(domainName.toUpperCase());
  171. return namespaceID;
  172. }
  173. }