/*******************************************************************************
 * Copyright 2012 Apigee Corporation
 * 
 * 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.
 ******************************************************************************/
package org.usergrid.rest.security.shiro.filters;

import static org.apache.commons.lang.StringUtils.isNotBlank;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Context;

import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.usergrid.security.shiro.PrincipalCredentialsToken;
import org.usergrid.security.shiro.utils.SubjectUtils;

import com.sun.jersey.spi.container.ContainerRequest;

@Component
public class ClientCredentialsSecurityFilter extends SecurityFilter {

	private static final Logger logger = LoggerFactory
			.getLogger(ClientCredentialsSecurityFilter.class);

	@Context
	protected HttpServletRequest httpServletRequest;

	public ClientCredentialsSecurityFilter() {
		logger.info("ClientCredentialsSecurityFilter is installed");
	}

	@Override
	public ContainerRequest filter(ContainerRequest request) {
		String clientId = httpServletRequest.getParameter("client_id");
		String clientSecret = httpServletRequest.getParameter("client_secret");

		if (isNotBlank(clientId) && isNotBlank(clientSecret)) {
			try {
				PrincipalCredentialsToken token = management
						.getPrincipalCredentialsTokenForClientCredentials(
								clientId, clientSecret);
				Subject subject = SubjectUtils.getSubject();
				subject.login(token);
			} catch (Exception e) {
			}
		}
		return request;
	}

}

@@ -863,16 +863,184 @@
 otBlank;
+%0Aimport static org.usergrid.rest.exceptions.AuthErrorInfo.OAUTH2_INVALID_CLIENT;%0Aimport static org.usergrid.rest.exceptions.SecurityException.mappableSecurityException;
 %0A%0Aimport
@@ -1515,17 +1515,20 @@
 lter %7B%0A%0A
-%09
+    
 private 
@@ -1570,19 +1570,28 @@
 Factory%0A
-%09%09%09
+            
 .getLogg
@@ -1634,17 +1634,20 @@
 lass);%0A%0A
-%09
+    
 @Context
@@ -1647,17 +1647,20 @@
 Context%0A
-%09
+    
 protecte
@@ -1701,17 +1701,20 @@
 quest;%0A%0A
-%09
+    
 public C
@@ -1748,18 +1748,24 @@
 ter() %7B%0A
-%09%09
+        
 logger.i
@@ -1821,13 +1821,19 @@
 %22);%0A
-%09%7D%0A%0A%09
+    %7D%0A%0A    
 @Ove
@@ -1838,17 +1838,20 @@
 verride%0A
-%09
+    
 public C
@@ -1901,18 +1901,24 @@
 uest) %7B%0A
-%09%09
+        
 String c
@@ -1973,18 +1973,24 @@
 t_id%22);%0A
-%09%09
+        
 String c
@@ -2054,18 +2054,24 @@
 ret%22);%0A%0A
-%09%09
+        
 if (isNo
@@ -2122,21 +2122,42 @@
 ) %7B%0A
-%09%09%09try %7B%0A%09%09%09%09
+            try %7B%0A                
 Prin
@@ -2197,22 +2197,40 @@
 agement%0A
-%09%09%09%09%09%09
+                        
 .getPrin
@@ -2276,16 +2276,40 @@
 ls(%0A
-%09%09%09%09%09%09%09%09
+                                
 clie
@@ -2329,20 +2329,32 @@
 ecret);%0A
-%09%09%09%09
+                
 Subject 
@@ -2394,12 +2394,24 @@
 ();%0A
-%09%09%09%09
+                
 subj
@@ -2428,19 +2428,28 @@
 token);%0A
-%09%09%09
+            
 %7D catch 
@@ -2468,19 +2468,112 @@
 ) %7B%0A
-%09%09%09%7D%0A%09%09%7D%0A%09%09
+                throw mappableSecurityException(OAUTH2_INVALID_CLIENT);%0A            %7D%0A        %7D%0A        
 retu
@@ -2588,9 +2588,12 @@
 st;%0A
-%09
+    
 %7D%0A%0A%7D

throw error for invalid client_id and client_secret