JDBCVisa.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 IBM Corp.
  9. */
  10. import java.sql.SQLException;
  11. import com.cognos.CAM_AAA.authentication.IBiBusHeader;
  12. import com.cognos.CAM_AAA.authentication.ICredential;
  13. import com.cognos.CAM_AAA.authentication.ITrustedCredential;
  14. import com.cognos.CAM_AAA.authentication.ReadOnlyDisplayObject;
  15. import com.cognos.CAM_AAA.authentication.SystemRecoverableException;
  16. import com.cognos.CAM_AAA.authentication.TextNoEchoDisplayObject;
  17. import com.cognos.CAM_AAA.authentication.UnrecoverableException;
  18. import com.cognos.CAM_AAA.authentication.UserRecoverableException;
  19. public class JDBCVisa extends Visa
  20. {
  21. private ConnectionManager connectionManager;
  22. private String password;
  23. private String username;
  24. public JDBCVisa()
  25. {
  26. super();
  27. }
  28. /*
  29. * (non-Javadoc)
  30. *
  31. * @see com.cognos.CAM_AAA.provider.IVisa#generateCredential(com.cognos.CAM_AAA .objectModel.IAuthenticateRequest)
  32. */
  33. @Override
  34. public ICredential generateCredential(final IBiBusHeader theAuthRequest) throws UserRecoverableException,
  35. SystemRecoverableException, UnrecoverableException
  36. {
  37. if (!this.validateConnection(this.username, this.password))
  38. {
  39. final UnrecoverableException e =
  40. new UnrecoverableException("Could not generate credentials for the user.", "Visa contains invalid credentials.");
  41. throw e;
  42. }
  43. final Credential credentials = new Credential();
  44. credentials.addCredentialValue("username", this.username);
  45. credentials.addCredentialValue("password", this.password);
  46. return credentials;
  47. }
  48. /*
  49. * (non-Javadoc)
  50. *
  51. * @see com.cognos.CAM_AAA.authentication.IVisa#generateTrustedCredential(com .cognos.CAM_AAA.authentication.IBiBusHeader)
  52. */
  53. /*
  54. * (non-Javadoc)
  55. *
  56. * @see com.cognos.CAM_AAA.provider.IVisa#generateTrustedCredential(com.cognos .CAM_AAA.objectModel.IAuthenticateRequest)
  57. */
  58. @Override
  59. public ITrustedCredential generateTrustedCredential(final IBiBusHeader theAuthRequest) throws UserRecoverableException,
  60. SystemRecoverableException, UnrecoverableException
  61. {
  62. // 1 - Look for credentials coming from SDK request
  63. JDBCSample.Credential credential = JDBCSample.getCredentialValues(theAuthRequest);
  64. if (credential.isEmpty())
  65. {
  66. // 2 - Look for credentials in formfield
  67. credential = JDBCSample.getFormFieldValues(theAuthRequest);
  68. }
  69. if (credential.isEmpty() || !credential.getUsername().equals(username))
  70. {
  71. credential.setUsername(username);
  72. credential.setPassword(password);
  73. }
  74. if (!validateConnection(credential))
  75. {
  76. final UserRecoverableException e =
  77. new UserRecoverableException("Please type your credentials for authentication.",
  78. "The provided credentials are invalid.");
  79. e.addDisplayObject(new ReadOnlyDisplayObject("User ID:", "CAMUsername", this.username));
  80. e.addDisplayObject(new TextNoEchoDisplayObject("Password:", "CAMPassword"));
  81. throw e;
  82. }
  83. final TrustedCredential tc = new TrustedCredential();
  84. tc.addCredentialValue("username", this.username);
  85. tc.addCredentialValue("password", this.password);
  86. return tc;
  87. }
  88. public void init(final JDBCSample theNamespace, final ConnectionManager theConnectionManager, final String theUsername,
  89. final String thePassword) throws UnrecoverableException
  90. {
  91. try
  92. {
  93. connectionManager = theConnectionManager;
  94. // Create account object for the user.
  95. final Account account = QueryUtil.createAccount(connectionManager, theUsername, thePassword);
  96. super.init(account);
  97. QueryUtil.updateMembership(connectionManager, this);
  98. this.username = theUsername;
  99. this.password = thePassword;
  100. }
  101. catch (final SQLException e)
  102. {
  103. throw new UnrecoverableException("Connection Error", "Database connection failure. Reason: "
  104. + ConnectionManager.getSqlExceptionDetails(e));
  105. }
  106. }
  107. private boolean validateConnection(final JDBCSample.Credential credential)
  108. {
  109. return validateConnection(credential.getUsername(), credential.getPassword());
  110. }
  111. private boolean validateConnection(final String theUsername, final String thePassword)
  112. {
  113. try
  114. {
  115. QueryUtil.createAccount(connectionManager, theUsername, thePassword);
  116. username = theUsername;
  117. password = thePassword;
  118. }
  119. catch (UnrecoverableException ex)
  120. {
  121. return false;
  122. }
  123. return true;
  124. }
  125. }