package com.rewayaat.web.core;

import com.rewayaat.web.auth.GoogleTokenVerifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.logging.Logger;

/**
 * Redirects to the changelog web page.
 */
@Controller
public class LoginController {

    public static final String AUTHENTICATED = "authenticated";
    private static Logger log = Logger.getLogger(LoginController.class.getName());
    @Value("classpath:admins.txt")
    private Resource res;

    @RequestMapping(value = "/google/signin", method = RequestMethod.POST)
    public final ResponseEntity<String> home(HttpServletRequest request, @RequestParam(value = "idtoken") String idtoken) {
        log.info("User signed in with idtoken: " + idtoken);
        try {
            String email = new GoogleTokenVerifier().authenticate(idtoken);
            List<String> lines = Files.readAllLines(Paths.get(res.getURI()),
                    StandardCharsets.UTF_8);
            if (lines.contains(email)) {
                request.getSession().setAttribute(AUTHENTICATED, true);
                return new ResponseEntity<>("Authenticated as an admin.", HttpStatus.OK);
            } else {
                return new ResponseEntity<>("Not an admin.", HttpStatus.ACCEPTED);
            }
        } catch (Exception e) {
            return new ResponseEntity<>("Invalid id token", HttpStatus.UNAUTHORIZED);
        }
    }
}

@@ -71,24 +71,64 @@
 enVerifier;%0A
+import org.apache.commons.io.FileUtils;%0A
 import org.s
@@ -167,20 +167,69 @@
 otation.
-Valu
+Autowired;%0Aimport org.springframework.core.io.Resourc
 e;%0Aimpor
@@ -258,32 +258,38 @@
 core.io.Resource
+Loader
 ;%0Aimport org.spr
@@ -653,106 +653,8 @@
 st;%0A
-import java.nio.charset.StandardCharsets;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Paths;%0A
 impo
@@ -949,67 +949,66 @@
 ));%0A
+%0A%0A
     @
-Value(%22classpath:admins.txt%22)%0A    private Resource res
+Autowired%0A    private ResourceLoader resourceLoader
 ;%0A%0A 
@@ -1348,32 +1348,116 @@
 icate(idtoken);%0A
+            Resource resource = resourceLoader.getResource(%22classpath:admins.txt%22);%0A
             List
@@ -1481,47 +1481,44 @@
 File
+Util
 s.read
-All
 Lines(
-Paths.get(res.getURI()),
+resource.getFile());
 %0A   
@@ -1530,81 +1530,90 @@
     
-        StandardCharsets.UTF_8);%0A            if (lines.contains(email)) %7B
+if (lines.contains(email)) %7B%0A                log.info(%22User is an administrator%22);
 %0A   
@@ -1792,16 +1792,74 @@
  else %7B%0A
+                log.info(%22User is not an administrator%22);%0A
         
@@ -1975,24 +1975,83 @@
 eption e) %7B%0A
+            log.info(%22Could not process id token.%5Cn%22 + e);%0A
             

Fix admin login.