/*
  Copyright 2012 Jerome Leleu

   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 com.github.leleuj.ss.oauth.client.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.scribe.up.credential.OAuthCredential;
import org.scribe.up.provider.OAuthProvider;
import org.scribe.up.session.HttpUserSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.util.Assert;

import com.github.leleuj.ss.oauth.client.authentication.OAuthAuthenticationToken;

/**
 * This filter handles OAuth calls after authentication at the OAuth provider. It listens HTTP requests on /j_spring_oauth_security_check by
 * default or whatever suffix url you can define by constructor. An
 * {@link com.github.leleuj.ss.oauth.client.authentication.OAuthAuthenticationToken} is created to finish the OAuth authentication process.
 * 
 * @author Jerome Leleu
 * @since 1.0.0
 */
public final class OAuthAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
    
    private static final Logger logger = LoggerFactory.getLogger(OAuthAuthenticationFilter.class);
    
    private OAuthProvider provider = null;
    
    /**
     * Define the suffix url on which the filter will listen for HTTP requests. It can be used for multiple providers configuration :
     * /j_spring_facebook_security_check, /j_spring_twitter_security_check...
     * 
     * @param suffixUrl
     */
    public OAuthAuthenticationFilter(String suffixUrl) {
        super(suffixUrl);
    }
    
    protected OAuthAuthenticationFilter() {
        super("/j_spring_oauth_security_check");
    }
    
    @Override
    public void afterPropertiesSet() {
        super.afterPropertiesSet();
        Assert.notNull(provider, "provider cannot be null");
    }
    
    @SuppressWarnings("unchecked")
    @Override
    public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
        throws AuthenticationException, IOException, ServletException {
        
        // create OAuth credentials from request
        OAuthCredential credential = provider.getCredential(new HttpUserSession(request), request.getParameterMap());
        logger.debug("credential : {}", credential);
        
        // and token from credential
        OAuthAuthenticationToken token = new OAuthAuthenticationToken(credential);
        // set details
        token.setDetails(authenticationDetailsSource.buildDetails(request));
        logger.debug("token : {}", token);
        
        // authenticate
        Authentication authentication = this.getAuthenticationManager().authenticate(token);
        logger.debug("authentication : {}", authentication);
        return authentication;
    }
    
    public void setProvider(OAuthProvider provider) {
        this.provider = provider;
    }
}

@@ -841,16 +841,60 @@
 ential;%0A
+import org.scribe.up.profile.ProfileHelper;%0A
 import o
@@ -2324,16 +2324,22 @@
 nFilter(
+final 
 String s
@@ -2603,16 +2603,21 @@
 notNull(
+this.
 provider
@@ -2642,24 +2642,69 @@
  be null%22);%0A
+        ProfileHelper.setKeepRawData(false);%0A
     %7D%0A    %0A 
@@ -2799,16 +2799,22 @@
 ication(
+final 
 HttpServ
@@ -2832,16 +2832,22 @@
 request,
+ final
  HttpSer
@@ -3003,24 +3003,30 @@
 est%0A        
+final 
 OAuthCredent
@@ -3042,16 +3042,21 @@
 ntial = 
+this.
 provider
@@ -3099,16 +3099,87 @@
 equest),
+%0A                                                                      
  request
@@ -3306,16 +3306,22 @@
         
+final 
 OAuthAut
@@ -3435,16 +3435,21 @@
 Details(
+this.
 authenti
@@ -3576,16 +3576,22 @@
         
+final 
 Authenti
@@ -3614,21 +3614,16 @@
 ation = 
-this.
 getAuthe
@@ -3793,16 +3793,22 @@
 rovider(
+final 
 OAuthPro

optimize scribe-up profile