+ /**
+ * Notifies all listeners in the given collection that authentication has
+ * failed.
+ *
+ * @param listeners A collection of all listeners that should be notified.
+ * @param credentials The credentials associated with the authentication
+ * request that failed.
+ */
+ private void notifyFailed(Collection listeners, Credentials credentials) {
+
+ // Build event for auth failure
+ AuthenticationFailureEvent event = new AuthenticationFailureEvent(credentials);
+
+ // Notify all listeners
+ for (Object listener : listeners) {
+ try {
+ if (listener instanceof AuthenticationFailureListener)
+ ((AuthenticationFailureListener) listener).authenticationFailed(event);
+ }
+ catch (GuacamoleException e) {
+ logger.error("Error notifying AuthenticationFailureListener.", e);
+ }
+ }
+
+ }
+
+ /**
+ * Notifies all listeners in the given collection that authentication was
+ * successful.
+ *
+ * @param listeners A collection of all listeners that should be notified.
+ * @param credentials The credentials associated with the authentication
+ * request that succeeded.
+ * @return true if all listeners are allowing the authentication success,
+ * or if there are no listeners, and false if any listener is
+ * canceling the authentication success. Note that once one
+ * listener cancels, no other listeners will run.
+ * @throws GuacamoleException If any listener throws an error while being
+ * notified. Note that if any listener throws an
+ * error, the success is canceled, and no other
+ * listeners will run.
+ */
+ private boolean notifySuccess(Collection listeners, Credentials credentials)
+ throws GuacamoleException {
+
+ // Build event for auth success
+ AuthenticationSuccessEvent event = new AuthenticationSuccessEvent(credentials);
+
+ // Notify all listeners
+ for (Object listener : listeners) {
+ if (listener instanceof AuthenticationSuccessListener) {
+
+ // Cancel immediately if hook returns false
+ if (!((AuthenticationSuccessListener) listener).authenticationSucceeded(event))
+ return false;
+
+ }
+ }
+
+ return true;
+
+ }
+
+ /**
+ * Sends a predefined, generic error message to the user, along with a
+ * "403 - Forbidden" HTTP status code in the response.
+ *
+ * @param response The response to send the error within.
+ * @throws IOException If an error occurs while sending the error.
+ */
+ private void failAuthentication(HttpServletResponse response) throws IOException {
+ response.setHeader("X-Guacamole-Error-Message", AUTH_ERROR_MESSAGE);
+ response.sendError(HttpServletResponse.SC_FORBIDDEN);
+ }
+