AuthorizationFailureEventListener.java

/*
 * Copyright 2014 James Pether Sörling
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *	$Id$
 *  $HeadURL$
*/
package com.hack23.cia.web.impl.ui.application.web.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.framework.ReflectiveMethodInvocation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.security.access.event.AuthorizationFailureEvent;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;

import com.hack23.cia.model.internal.application.system.impl.ApplicationEventGroup;
import com.hack23.cia.model.internal.application.system.impl.ApplicationOperationType;
import com.hack23.cia.service.api.ApplicationManager;
import com.hack23.cia.service.api.action.application.CreateApplicationEventRequest;
import com.hack23.cia.web.impl.ui.application.util.UserContextUtil;
import com.vaadin.server.Page;
import com.vaadin.ui.UI;

/**
 * The Class AuthorizationFailureEventListener.
 */
@Service
public final class AuthorizationFailureEventListener implements ApplicationListener<AuthorizationFailureEvent> {

	/** The Constant REQUIRED_AUTHORITIES. */
	private static final String REQUIRED_AUTHORITIES = " , RequiredAuthorities:";

	/** The Constant ACCESS_DENIED. */
	private static final String ACCESS_DENIED = "Access Denied";

	/** The Constant AUTHORITIES. */
	private static final String AUTHORITIES = "Authorities:";

	/**
	 * The Constant
	 * LOG_MSG_AUTHORIZATION_FAILURE_SESSION_ID_AUTHORITIES_REQUIRED_AUTHORITIES.
	 */
	private static final String LOG_MSG_AUTHORIZATION_FAILURE_SESSION_ID_AUTHORITIES_REQUIRED_AUTHORITIES = "Authorization Failure:: url : {} Method : {} SessionId :{} , Authorities : {} , RequiredAuthorities : {}";

	/** The Constant LOGGER. */
	private static final Logger LOGGER = LoggerFactory.getLogger(AuthorizationFailureEventListener.class);

	/** The application manager. */
	@Autowired
	private ApplicationManager applicationManager;

	/**
	 * Instantiates a new authorization failure event listener.
	 */
	public AuthorizationFailureEventListener() {
		super();
	}

	@Override
	public void onApplicationEvent(final AuthorizationFailureEvent authorizationFailureEvent) {

		final String sessionId = RequestContextHolder.currentRequestAttributes().getSessionId();

		final CreateApplicationEventRequest serviceRequest = new CreateApplicationEventRequest();
		serviceRequest.setSessionId(sessionId);

		serviceRequest.setEventGroup(ApplicationEventGroup.APPLICATION);
		serviceRequest.setApplicationOperation(ApplicationOperationType.AUTHORIZATION);

		serviceRequest.setUserId(UserContextUtil.getUserIdFromSecurityContext());

		final Page currentPageIfAny = Page.getCurrent();
		final String requestUrl = UserContextUtil.getRequestUrl(currentPageIfAny);
		final UI currentUiIfAny = UI.getCurrent();
		String methodInfo = "";

		if (currentPageIfAny != null && currentUiIfAny != null && currentUiIfAny.getNavigator() != null
				&& currentUiIfAny.getNavigator().getCurrentView() != null) {
			serviceRequest.setPage(currentUiIfAny.getNavigator().getCurrentView().getClass().getSimpleName());
			serviceRequest.setPageMode(currentPageIfAny.getUriFragment());
		}

		if (authorizationFailureEvent.getSource() instanceof ReflectiveMethodInvocation) {
			final ReflectiveMethodInvocation methodInvocation = (ReflectiveMethodInvocation) authorizationFailureEvent
					.getSource();
			if (methodInvocation.getMethod() != null && methodInvocation.getThis() != null) {
				methodInfo = methodInvocation.getThis().getClass().getSimpleName() + "."
						+ methodInvocation.getMethod().getName();
			}
		}

		serviceRequest.setErrorMessage("Url:" + requestUrl + " , Method" + methodInfo + " ," + AUTHORITIES
				+ authorizationFailureEvent.getAuthentication().getAuthorities() + REQUIRED_AUTHORITIES
				+ authorizationFailureEvent.getConfigAttributes() + " source:" + authorizationFailureEvent.getSource());
		serviceRequest.setApplicationMessage(ACCESS_DENIED);

		applicationManager.service(serviceRequest);

		LOGGER.info(LOG_MSG_AUTHORIZATION_FAILURE_SESSION_ID_AUTHORITIES_REQUIRED_AUTHORITIES, requestUrl, methodInfo,
				sessionId, authorizationFailureEvent.getAuthentication().getAuthorities().toString(),
				authorizationFailureEvent.getConfigAttributes().toString());
	}

}