Github user mike-jumper commented on a diff in the pull request:
https://github.com/apache/incubator-guacamole-client/pull/183#discussion_r134758113
--- Diff:
extensions/guacamole-auth-cas/src/main/java/org/apache/guacamole/auth/cas/AuthenticationProviderService.java
---
@@ -105,4 +137,76 @@ public AuthenticatedUser authenticateUser(Credentials
credentials)
}
+ /**
+ * Takes an encrypted string representing a password provided by
+ * the CAS ClearPass service and decrypts it using the private
+ * key configured for this extension. Returns null if it is
+ * unable to decrypt the password.
+ *
+ * @param encryptedPassword
+ * A string with the encrypted password provided by the
+ * CAS service.
+ *
+ * @return
+ * The decrypted password, or null if it is unable to
+ * decrypt the password.
+ * @throws GuacamoleException
+ * If unable to get Guacamole configuration data
+ */
+ private final String decryptPassword(String encryptedPassword)
+ throws GuacamoleException {
+
+ // If we get nothing, we return nothing.
+ if (encryptedPassword == null || encryptedPassword.isEmpty())
+ return null;
+
+ try {
+
+ // Open and read the file specified in the configuration.
+ File keyFile = new File(new
LocalEnvironment().getGuacamoleHome(),
confService.getClearpassKey().toString());
+ InputStream keyInput = new BufferedInputStream(new
FileInputStream(keyFile));
+ final byte[] keyBytes = new byte[(int) keyFile.length()];
+ keyInput.read(keyBytes);
+ keyInput.close();
+
+ // Set up decryption infrastructure
+ KeyFactory keyFactory = KeyFactory.getInstance("RSA");
+ KeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
+ final PrivateKey privateKey =
keyFactory.generatePrivate(keySpec);
+ final Cipher cipher =
Cipher.getInstance(privateKey.getAlgorithm());
+ final byte[] pass64 =
DatatypeConverter.parseBase64Binary(encryptedPassword);
+ cipher.init(Cipher.DECRYPT_MODE, privateKey);
+
+ // Decrypt and return a new string.
+ final byte[] cipherData = cipher.doFinal(pass64);
+ return new String(cipherData);
+ }
+ catch (FileNotFoundException e) {
+ logger.error("ClearPass key file not found, password will not
be decrypted.");
+ logger.debug("Error locating the ClearPass key file: {}",
e.getMessage());
--- End diff --
`logger.debug()` calls should receive the exception itself, not just the
message. That way, when debug logging is enabled, full stacktraces are logged.
Assuming the exception message is meaningful, it's the higher-level log
statement which would receive `e.getMessage()` rather than `e` (in this case,
the call to `logger.error()`).
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---