package edu.tamu.weaver.token.provider.controller;

import static edu.tamu.weaver.response.ApiStatus.SUCCESS;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.view.RedirectView;

import edu.tamu.weaver.response.ApiResponse;

public abstract class WeaverMockTokenController extends TokenController {

    private static final Logger LOG = Logger.getLogger(WeaverMockTokenController.class);

    private static final Map<String, Map<String, String>> MOCK_CLAIMS = new HashMap<String, Map<String, String>>();

    static {
        Map<String, String> mockAdminClaims = new HashMap<String, String>();
        mockAdminClaims.put("lastName", "Daniels");
        mockAdminClaims.put("firstName", "Jack");
        mockAdminClaims.put("netid", "aggieJack");
        mockAdminClaims.put("uin", "123456789");
        mockAdminClaims.put("email", "aggieJack@tamu.edu");
        mockAdminClaims.put("role", "ROLE_ADMIN");

        MOCK_CLAIMS.put("admin", mockAdminClaims);

        Map<String, String> mockUserClaims = new HashMap<String, String>();
        mockUserClaims.put("lastName", "Boring");
        mockUserClaims.put("firstName", "Bob");
        mockUserClaims.put("netid", "bobBoring");
        mockUserClaims.put("uin", "bobBoring");
        mockUserClaims.put("email", "bobBoring@tamu.edu");
        mockUserClaims.put("role", "ROLE_USER");

        MOCK_CLAIMS.put("user", mockUserClaims);
    }

    @Override
    @RequestMapping("/token")
    public RedirectView token(@RequestParam Map<String, String> params, @RequestHeader Map<String, String> headers) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
        LOG.debug("params: " + params);
        String referer = params.get("referer");
        if (referer == null) {
            LOG.debug("No referer in params!!");
            throw new RuntimeException("No referer in params!!");
        }
        String mock = params.get("mock");
        if (mock == null) {
            mock = "user";
        }
        RedirectView redirect = new RedirectView();
        redirect.setContextRelative(false);
        redirect.setUrl(referer + "?jwt=" + tokenService.craftToken(MOCK_CLAIMS.get(mock)));
        return redirect;
    }

    @Override
    @RequestMapping("/refresh")
    public ApiResponse refresh(@RequestParam Map<String, String> params, @RequestHeader Map<String, String> headers) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
        LOG.debug("Refresh token requested.");
        String token = params.get("token");
        if (token == null) {
            throw new RuntimeException("Cannot refresh without token!");
        }
        return new ApiResponse(SUCCESS, "Token refresh successful.", tokenService.refreshToken(token));
    }

    protected void setMockClaims(String mock, Map<String, String> claims) {
        MOCK_CLAIMS.put(mock, claims);
    }

}

@@ -104,16 +104,52 @@
 CCESS;%0A%0A
+import java.net.URISyntaxException;%0A
 import j
@@ -411,16 +411,64 @@
 ption;%0A%0A
+import org.apache.http.client.utils.URIBuilder;%0A
 import o
@@ -2233,32 +2233,52 @@
 PaddingException
+, URISyntaxException
  %7B%0A        LOG.d
@@ -2320,24 +2320,25 @@
 String refer
+r
 er = params.
@@ -2347,16 +2347,17 @@
 t(%22refer
+r
 er%22);%0A  
@@ -2371,16 +2371,17 @@
 f (refer
+r
 er == nu
@@ -2409,32 +2409,33 @@
 .debug(%22No refer
+r
 er in params!!%22)
@@ -2484,16 +2484,17 @@
 No refer
+r
 er in pa
@@ -2612,32 +2612,290 @@
 ser%22;%0A        %7D%0A
+        URIBuilder builder = new URIBuilder(referrer);%0A        builder.addParameter(%22jwt%22, tokenService.craftToken(MOCK_CLAIMS.get(mock)));%0A        String url = builder.build().toASCIIString();%0A        LOG.debug(String.format(%22Auth url redirect: %25s%22, url));%0A
         Redirect
@@ -3002,74 +3002,11 @@
 Url(
-referer + %22?jwt=%22 + tokenService.craftToken(MOCK_CLAIMS.get(mock))
+url
 );%0A 

update referrer processing in mock token controller