/*
 * CODENVY CONFIDENTIAL
 * __________________
 *
 *  [2012] - [2015] Codenvy, S.A.
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Codenvy S.A. and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Codenvy S.A.
 * and its suppliers and may be covered by U.S. and Foreign Patents,
 * patents in process, and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Codenvy S.A..
 */
package com.codenvy.auth.sso.oauth;

import com.codenvy.auth.sso.server.handler.BearerTokenAuthenticationHandler;

import org.eclipse.che.api.core.BadRequestException;
import org.eclipse.che.security.oauth.OAuthAuthenticationException;
import org.eclipse.che.security.oauth.OAuthAuthenticationService;
import org.eclipse.che.security.oauth.OAuthAuthenticator;

import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * RESTful wrapper for OAuthAuthenticator.
 *
 * @author Sergii Kabashniuk
 */
@Path("oauth")
public class SsoOAuthAuthenticationService extends OAuthAuthenticationService {
    @Inject
    BearerTokenAuthenticationHandler authenticationHandler;

    @GET
    @Path("callback")
    @Override
    public Response callback(@QueryParam("errorValues") List<String> errorValues) throws OAuthAuthenticationException, BadRequestException {
        URL requestUrl = getRequestUrl(uriInfo);
        Map<String, List<String>> params = getRequestParameters(getState(requestUrl));
        if (errorValues != null && errorValues.contains("access_denied")) {
            return Response.temporaryRedirect(
                    uriInfo.getRequestUriBuilder().replacePath(errorPage).replaceQuery(null).build()).build();
        }
        final String providerName = getParameter(params, "oauth_provider");
        OAuthAuthenticator oauth = getAuthenticator(providerName);
        final List<String> scopes = params.get("scope");
        final String userId = oauth.callback(requestUrl, scopes == null ? Collections.<String>emptyList() : scopes);
        final String redirectAfterLogin = getParameter(params, "redirect_after_login");
        final String mode = getParameter(params, "mode");
        //federated_login left for backward compatibility
        if ("sso".equals(mode) || "federated_login".equals(mode)) {


            try {

                Map<String, String> payload = new HashMap<>(2);
                payload.put("provider", providerName);
                payload.put("username", userId);

                return Response.temporaryRedirect(
                        UriBuilder.fromUri(URLDecoder.decode(redirectAfterLogin, "UTF-8"))
                                  .queryParam("username", userId)
                                  .queryParam("oauthbearertoken", authenticationHandler.generateBearerToken(userId, payload))
                                  .build())
                               .build();
            } catch (UnsupportedEncodingException e) {
                throw new OAuthAuthenticationException(e.getLocalizedMessage(), e);
            }

        }
        return Response.temporaryRedirect(URI.create(redirectAfterLogin)).build();
    }

}

@@ -2526,30 +2526,24 @@
 s);%0A        
-final 
 String redir
@@ -2600,24 +2600,260 @@
 er_login%22);%0A
+        try %7B%0A            redirectAfterLogin = URLDecoder.decode(redirectAfterLogin, %22UTF-8%22);%0A        %7D catch (UnsupportedEncodingException e) %7B%0A            throw new OAuthAuthenticationException(e.getLocalizedMessage(), e);%0A        %7D%0A
         fina
@@ -3028,33 +3028,8 @@
 ) %7B%0A
-%0A%0A            try %7B%0A%0A    
     
@@ -3088,36 +3088,32 @@
 2);%0A            
-   
- 
 payload.put(%22pro
@@ -3139,36 +3139,32 @@
 e);%0A            
-    
 payload.put(%22use
@@ -3185,36 +3185,32 @@
 );%0A%0A            
-   
- 
 return Response.
@@ -3248,20 +3248,16 @@
         
-    
 UriBuild
@@ -3267,34 +3267,16 @@
 fromUri(
-URLDecoder.decode(
 redirect
@@ -3289,24 +3289,10 @@
 ogin
-, %22UTF-8%22))%0A    
+)%0A
     
@@ -3371,36 +3371,32 @@
                 
-    
 .queryParam(%22oau
@@ -3493,36 +3493,32 @@
                 
-    
 .build())%0A      
@@ -3530,36 +3530,32 @@
                 
-    
 .build();%0A      
@@ -3551,161 +3551,8 @@
 d();
-%0A            %7D catch (UnsupportedEncodingException e) %7B%0A                throw new OAuthAuthenticationException(e.getLocalizedMessage(), e);%0A            %7D
 %0A%0A  

Fix redirectAfterLogin decode in SsoOAuthAuthenticationService