SampleTenantProvider.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * IBM Confidential
  3. *
  4. * OCO Source Materials
  5. *
  6. * IBM Cognos Products: CAMAAA
  7. *
  8. * (C) Copyright IBM Corp. 2011, 2012
  9. *
  10. * The source code for this program is not published or otherwise divested of its trade secrets, irrespective of what has been
  11. * deposited with the U.S. Copyright Office.
  12. */
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.util.Map;
  16. import java.util.Properties;
  17. import com.cognos.CAM_AAA.authentication.IAccount;
  18. import com.cognos.CAM_AAA.authentication.ITenantProvider;
  19. import com.cognos.CAM_AAA.authentication.UnrecoverableException;
  20. /**
  21. * Sample implementation of the {@link ITenantProvider} interface. The user to tenant mapping is defined in a properties file.
  22. */
  23. public class SampleTenantProvider implements ITenantProvider
  24. {
  25. private static final String USERS_TO_TENANTS_FILE_PROPERTY_NAME = "usersToTenantsFile";
  26. private static final String INITIALIZATION_EXCEPTION_CAPTION = "Error initializing tenant provider.";
  27. private final Properties usersToTenants;
  28. public SampleTenantProvider()
  29. {
  30. this.usersToTenants = new Properties();
  31. }
  32. public void destroy()
  33. {
  34. }
  35. @Override
  36. public String getTenantId(final IAccount account) throws UnrecoverableException
  37. {
  38. final String userName = account.getUserName();
  39. final String tenantId = this.usersToTenants.getProperty(userName);
  40. if (tenantId == null)
  41. throw new UnrecoverableException("Unknown user", "User '" + userName + "' does not belong to any tenant.");
  42. if (this.isTenantDisabled(tenantId))
  43. throw new UnrecoverableException("Tenant disabled", "Tenant '" + tenantId + "' does not have access to the system.");
  44. return tenantId;
  45. }
  46. public void init(final Map< String, String > advancedConfigurations, final String namespaceId) throws UnrecoverableException
  47. {
  48. final String fileLocation = advancedConfigurations.get(SampleTenantProvider.USERS_TO_TENANTS_FILE_PROPERTY_NAME);
  49. if (fileLocation == null)
  50. throw new UnrecoverableException(SampleTenantProvider.INITIALIZATION_EXCEPTION_CAPTION, "The advanced property '"
  51. + SampleTenantProvider.USERS_TO_TENANTS_FILE_PROPERTY_NAME + "' must be set.");
  52. final InputStream inStream = this.getClass().getResourceAsStream(fileLocation);
  53. if (inStream == null)
  54. throw new UnrecoverableException(SampleTenantProvider.INITIALIZATION_EXCEPTION_CAPTION,
  55. "Unable to locate user to tenants mapping file '" + fileLocation + "'");
  56. try
  57. {
  58. //
  59. // Java Properties files only support ISO 8859-1 character encoding, therefore user names or tenant IDs of certain
  60. // locales are not supported by the sample. They are supported in the product.
  61. //
  62. this.usersToTenants.load(inStream);
  63. }
  64. catch (final IOException ioe)
  65. {
  66. throw new UnrecoverableException(SampleTenantProvider.INITIALIZATION_EXCEPTION_CAPTION,
  67. "Unable to load user to tenant mapping.");
  68. }
  69. finally
  70. {
  71. try
  72. {
  73. inStream.close();
  74. }
  75. catch (final IOException ioe)
  76. {
  77. // ignore it
  78. }
  79. }
  80. }
  81. private boolean isTenantDisabled(final String tenantID)
  82. {
  83. //
  84. // Hard-coded to TenantB, however this could be extended to query an external source to determine if a tenant has been
  85. // disabled.
  86. //
  87. if ("TenantB".equals(tenantID))
  88. return true;
  89. return false;
  90. }
  91. }