// Copyright 2014 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

package com.google.u2f.gaedemo.servlets;

import java.io.IOException;

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

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.google.u2f.U2FException;
import com.google.u2f.server.U2FServer;
import com.google.u2f.server.messages.RegistrationRequest;
import com.google.u2f.server.messages.U2fSignRequest;

@SuppressWarnings("serial")
@Singleton
public class BeginEnrollServlet extends HttpServlet {
	
	private final UserService userService =  UserServiceFactory.getUserService();
	private final U2FServer u2fServer;
	
	@Inject
	public BeginEnrollServlet(U2FServer u2fServer) {
		this.u2fServer = u2fServer;
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
		User user = userService.getCurrentUser();
		boolean allowReregistration = Boolean.valueOf(req.getParameter("reregistration"));
		RegistrationRequest registrationRequest;
		U2fSignRequest signRequest;
		String appId = (req.isSecure() ? "https://" : "http://") + req.getHeader("Host");

		try {
      registrationRequest = u2fServer.getRegistrationRequest(user.getUserId(), appId);
		  signRequest = u2fServer.getSignRequest(user.getUserId(), appId);
    } catch (U2FException e) {
      throw new ServletException("couldn't get registration request", e);
    }

    JsonObject result = new JsonObject();
    result.addProperty("appId", appId);
    result.addProperty("sessionId", registrationRequest.getSessionId());
    
	  JsonObject registerRequests = new JsonObject();
		registerRequests.addProperty("challenge", registrationRequest.getChallenge());
		registerRequests.addProperty("version", registrationRequest.getVersion());
		result.add("registerRequests", registerRequests);

		if(allowReregistration) {
		  result.add("registeredKeys", new JsonArray());
		} else {
		  result.add("registeredKeys", signRequest.getRegisteredKeysAsJson(appId));
		}

		resp.setContentType("application/json");
		resp.getWriter().println(result.toString());
	}
}

@@ -1032,19 +1032,19 @@
 rvlet %7B%0A
-%09%0A%09
+%0A  
 private 
@@ -1113,17 +1113,18 @@
 vice();%0A
-%09
+  
 private 
@@ -1154,19 +1154,19 @@
 er;%0A
-%09%0A%09
+%0A  
 @Inject%0A
 %09pub
@@ -1161,17 +1161,18 @@
 @Inject%0A
-%09
+  
 public B
@@ -1212,18 +1212,20 @@
 rver) %7B%0A
-%09%09
+    
 this.u2f
@@ -1248,14 +1248,15 @@
 er;%0A
-%09%7D%0A%09%0A%09
+  %7D%0A%0A  
 publ
@@ -1319,16 +1319,22 @@
 se resp)
+%0A     
  throws 
@@ -1365,18 +1365,20 @@
 ption %7B%0A
-%09%09
+    
 User use
@@ -1411,18 +1411,20 @@
 User();%0A
-%09%09
+    
 boolean 
@@ -1498,18 +1498,20 @@
 ion%22));%0A
-%09%09
+    
 Registra
@@ -1543,18 +1543,20 @@
 equest;%0A
-%09%09
+    
 U2fSignR
@@ -1575,18 +1575,20 @@
 equest;%0A
-%09%09
+    
 String a
@@ -1662,18 +1662,20 @@
 ost%22);%0A%0A
-%09%09
+    
 try %7B%0A  
@@ -1759,18 +1759,20 @@
 appId);%0A
-%09%09
+    
   signRe
@@ -2101,14 +2101,11 @@
 ));%0A
+%0A
   
-  %0A%09
   Js
@@ -2150,18 +2150,20 @@
 ject();%0A
-%09%09
+    
 register
@@ -2233,18 +2233,20 @@
 nge());%0A
-%09%09
+    
 register
@@ -2312,18 +2312,20 @@
 ion());%0A
-%09%09
+    
 result.a
@@ -2367,18 +2367,20 @@
 ests);%0A%0A
-%09%09
+    
 if(allow
@@ -2397,20 +2397,22 @@
 tion) %7B%0A
-%09%09
+   
   
+ 
 result.a
@@ -2450,18 +2450,20 @@
 ray());%0A
-%09%09
+    
 %7D else %7B
@@ -2463,18 +2463,20 @@
  else %7B%0A
-%09%09
+    
   result
@@ -2547,15 +2547,19 @@
 ));%0A
-%09%09%7D%0A%0A%09%09
+    %7D%0A%0A    
 resp
@@ -2599,10 +2599,12 @@
 %22);%0A
-%09%09
+    
 resp
@@ -2648,9 +2648,10 @@
 ));%0A
-%09
+  
 %7D%0A%7D%0A

Fix whitespace issues