package fr.gouv.franceconnect.stub;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.security.Security;

/**
 * Created by tchabaud on 15/10/16.
 * Stub for France Connect authorize endpoint.
 */
public class Authorize extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    public void init() {
        Security.addProvider(new BouncyCastleProvider());
    }

    @Override
    protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException,
            IOException {
        req.getSession().setAttribute("state", req.getParameter("state"));
        req.getSession().setAttribute("redirect_uri", req.getParameter("redirect_uri"));
        req.getSession().setAttribute("client_id", req.getParameter("client_id"));
        req.getSession().setAttribute("nonce", req.getParameter("nonce"));
        
        
        final String scope = URLEncoder.encode(req.getParameter("scope"), "UTF-8");
        final String response_type = URLEncoder.encode(req.getParameter("response_type"), "UTF-8");
        final String nonce = URLEncoder.encode(req.getParameter("nonce"), "UTF-8");
        final String redirect_uri = URLEncoder.encode(req.getParameter("redirect_uri"), "UTF-8");
        final String state = URLEncoder.encode(req.getParameter("state"), "UTF-8");
        final String client_id = URLEncoder.encode(req.getParameter("client_id"), "UTF-8");

        final StringBuilder uri = new StringBuilder("/index.jsp").append("?") //
                .append("scope").append('=').append(scope) //
                .append("&response_type").append('=').append(response_type) //
                .append("&nonce").append('=').append(nonce) //
                .append("&redirect_uri").append('=').append(redirect_uri) //
                .append("&state").append('=').append(state) //
                .append("&client_id").append('=').append(client_id);
        final URI redirectUri;
        try {
            redirectUri = new URI(uri.toString());
        } catch (final URISyntaxException e) {
            throw new ServletException(e);
        }
        resp.setContentType("Content-type: text/html");
        resp.sendRedirect(redirectUri.toString());

        //resp.sendRedirect("/index.jsp");
    }
}

@@ -30,16 +30,61 @@
 .stub;%0A%0A
+import org.apache.commons.lang3.StringUtils;%0A
 import o
@@ -1776,24 +1776,182 @@
  %22UTF-8%22);%0A%0A
+        String contextPath = req.getContextPath();%0A        if (StringUtils.isNotBlank(contextPath)) %7B%0A            contextPath = contextPath + %22/%22;%0A        %7D%0A%0A
         fina
@@ -1990,18 +1990,37 @@
 Builder(
+contextPath).append(
 %22
-/
 index.js

Fix context path handling