package com.google.sps.servlets;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthException;
import com.google.firebase.auth.SessionCookieOptions;
import com.google.sps.firebase.FirebaseAppManager;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/init")
public class SessionInit extends HttpServlet {
  // Return empty string if no comment, otherwise return text
  private String getParameter(HttpServletRequest request, String name, String defaultValue) {
    String value = request.getParameter(name);
    if (value == null) {
      return defaultValue;
    } else {
      return value;
    }
  }
}

@@ -875,11 +875,1053 @@
    %7D%0A  %7D
+%0A%0A  @Override%0A  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException %7B%0A    String idToken = getParameter(request, %22idToken%22, %22%22);%0A    // Set session expiration to 5 days.%0A    long expiresIn = TimeUnit.DAYS.toMillis(5);%0A    SessionCookieOptions options = SessionCookieOptions.builder().setExpiresIn(expiresIn).build();%0A    try %7B%0A      // Create the session cookie. This will also verify the ID token in the process.%0A      // The session cookie will have the same claims as the ID token.%0A      String sessionCookie =%0A          FirebaseAuth.getInstance(FirebaseAppManager.getApp())%0A              .createSessionCookie(idToken, options);%0A      // Set cookie policy parameters as required.%0A      Cookie cookie = new Cookie(%22session%22, sessionCookie /* ... other parameters */);%0A      cookie.setMaxAge((int) TimeUnit.MILLISECONDS.toSeconds(expiresIn));%0A      response.addCookie(cookie);%0A    %7D catch (FirebaseAuthException e) %7B%0A      response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);%0A    %7D%0A  %7D
 %0A%7D%0A

Create session cookie that expires in 5 days.