/*******************************************************************************
 * Copyright (c) 2015 IBM Corp.
 *
 * 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 net.wasdev.gameon.auth.dummy;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.wasdev.gameon.auth.JwtAuth;

/**
 * A backend-less auth impl for testing.
 *
 * Accepts the username as a parameter, and returns a signed jwt for that
 * username.
 */
@WebServlet("/DummyAuth")
public class DummyAuth extends JwtAuth {
    private static final long serialVersionUID = 1L;

    @Resource(lookup = "authCallbackURLSuccess")
    String callbackSuccess;

    public DummyAuth() {
        super();
    }

    @PostConstruct
    private void verifyInit() {
        if (callbackSuccess == null) {
            System.err.println("Error finding auth callback url; please set this in your environment variables!");
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String s = request.getParameter("dummyUserName");

        if (s == null) {
            s = "AnonymousUser";
        }

        Map<String, String> claims = new HashMap<String, String>();

        claims.put("id", "dummy." + s);
        claims.put("name", s);
        claims.put("email", s+"@DUMMYEMAIL.DUMMY");

        String newJwt = createJwt(claims);

        // debug.
        System.out.println("New User Authed: " + claims.get("id"));

        response.sendRedirect(callbackSuccess + "/" + newJwt);

    }

}

@@ -859,16 +859,48 @@
 til.Map;
+%0Aimport java.util.logging.Level;
 %0A%0Aimport
@@ -1181,16 +1181,51 @@
 JwtAuth;
+%0Aimport net.wasdev.gameon.auth.Log;
 %0A%0A/**%0A *
@@ -1478,16 +1478,105 @@
  = 1L;%0A%0A
+    /** Something like https://127.0.0.1/#/login/callback provided by the environment */%0A
     @Res
@@ -1909,16 +1909,183 @@
       %7D%0A
+        %0A        // Convert https://127.0.0.1/#/login/callback to /#/login/callback%0A        callbackSuccess = callbackSuccess.substring(callbackSuccess.indexOf('#'));%0A
     %7D%0A%0A 
@@ -2599,54 +2599,131 @@
 s);%0A
-%0A        // debug.%0A        System.out.println(
+        String callbackHost = request.getParameter(%22callbackHost%22);%0A%0A        // debug.%0A        Log.log(Level.FINEST, this, 
 %22New
@@ -2736,19 +2736,21 @@
 Authed: 
-%22 +
+%7B0%7D%22,
  claims.
@@ -2761,16 +2761,353 @@
 %22id%22));%0A
+        Log.log(Level.FINEST, this, %22Remote callbackHost: %7B0%7D%22, callbackHost);%0A        Log.log(Level.FINEST, this, %22Remote callbackSuccess: %7B0%7D%22, callbackSuccess);%0A        Log.log(Level.FINEST, this, %22Result url: %7B0%7D%22, callbackHost + '/' + callbackSuccess);%0A        %0A        // Append /#/login/callback to the end of the original referer
 %0A       
@@ -3129,16 +3129,37 @@
 edirect(
+callbackHost + '/' + 
 callback
@@ -3172,11 +3172,11 @@
 s + 
-%22/%22
+'/'
  + n
@@ -3183,16 +3183,27 @@
 ewJwt);%0A
+        // 
 %0A    %7D%0A%0A

Support arbitrary starting URL with Dummy User (#15)