// Path: src/main/java/org/iotlabs/dashboard/httpexceptions/Http4xx.java
// public class Http4xx extends BaseHttpException {
// 
//     public Http4xx(int code, String body) {
//         super(code, body);
//     }
// 
// }
// 
// Path: src/main/java/org/iotlabs/dashboard/literals/ContentType.java
// public class ContentType {
//     public static final String JSON = "application/json";
//     public static final String HTML = "text/html";
// }
// 
// Path: src/main/java/org/iotlabs/dashboard/utils/Checker.java
// public class Checker {
// 
//     public static boolean isRequestingHtml(Request request) {
//         String accept = request.headers(Header.ACCEPT);
//         return accept != null
//                 && accept.contains(ContentType.HTML);
//     }
// 
//     public static User authenticate(String username, String passwd) {
//         User user = Dummy.userMap.getOrDefault(username, null);
//         if (user == null) {
//             return null;
//         }
//         if (user.getPasswd().equals(passwd)) {
//             return user;
//         }
//         return null;
//     }
// 
// 
// 
// 
// }
// 
// Path: src/main/java/org/iotlabs/dashboard/utils/ResponseSetter.java
// public class ResponseSetter {
//     public static void setErrorResponse(Response errorResponse, BaseHttpException e) {
//         errorResponse.type(ContentType.HTML);
//         errorResponse.status(e.getCode());
//         errorResponse.body(e.getBody());
//     }
// }
// 
// Path: src/main/java/org/iotlabs/dashboard/blueprints/Complex.java
// public static Complex complex(String endpoint) {
//     return new Complex(endpoint);
// }


import org.iotlabs.storage.models.general.User;
import org.iotlabs.storage.util.StringUtils;
import org.iotlabs.dashboard.httpexceptions.Http4xx;
import org.iotlabs.dashboard.literals.ContentType;
import org.iotlabs.dashboard.utils.Checker;
import org.iotlabs.dashboard.utils.ResponseSetter;
import spark.ModelAndView;
import spark.template.mustache.MustacheTemplateEngine;
import java.util.HashMap;
import java.util.Map;
import static org.iotlabs.dashboard.blueprints.Complex.complex;
package org.iotlabs.dashboard.blueprints;




public class AuthBluePrint extends AbstractBluePrint {


    public AuthBluePrint() {
        super("auth");
    }

    @Override
    public void register() {
        complex(getEndPoint("login"))
                .get(
                        (req, res) -> {
                            Map<String, Object> scopes = new HashMap<>();
                            scopes.put("title", "IotLabs");
                            scopes.put("admin_mail", "toori67@gmail.com");
                            return new ModelAndView(scopes,"login.html");
                        }, new MustacheTemplateEngine(getTemplatePath())
                )
                .post((req, res) -> {
                    String username = req.queryParams("username");
                    String passwd = StringUtils.sha256(req.queryParams("password"));
                    User user = Checker.authenticate(username, passwd);
                    if (user == null) {
                        req.session().removeAttribute("auth");
                        ResponseSetter.setErrorResponse(res, new Http4xx(404, "not authorized"));
                        return res.body();
                    } else {
                        if (Checker.isRequestingHtml(req)) {
                            res.redirect("/");
                        }
                        req.session().attribute("auth", true);
// Path: src/main/java/org/iotlabs/dashboard/httpexceptions/Http4xx.java
// public class Http4xx extends BaseHttpException {
// 
//     public Http4xx(int code, String body) {
//         super(code, body);
//     }
// 
// }
// 
// Path: src/main/java/org/iotlabs/dashboard/literals/ContentType.java
// public class ContentType {
//     public static final String JSON = "application/json";
//     public static final String HTML = "text/html";
// }
// 
// Path: src/main/java/org/iotlabs/dashboard/utils/Checker.java
// public class Checker {
// 
//     public static boolean isRequestingHtml(Request request) {
//         String accept = request.headers(Header.ACCEPT);
//         return accept != null
//                 && accept.contains(ContentType.HTML);
//     }
// 
//     public static User authenticate(String username, String passwd) {
//         User user = Dummy.userMap.getOrDefault(username, null);
//         if (user == null) {
//             return null;
//         }
//         if (user.getPasswd().equals(passwd)) {
//             return user;
//         }
//         return null;
//     }
// 
// 
// 
// 
// }
// 
// Path: src/main/java/org/iotlabs/dashboard/utils/ResponseSetter.java
// public class ResponseSetter {
//     public static void setErrorResponse(Response errorResponse, BaseHttpException e) {
//         errorResponse.type(ContentType.HTML);
//         errorResponse.status(e.getCode());
//         errorResponse.body(e.getBody());
//     }
// }
// 
// Path: src/main/java/org/iotlabs/dashboard/blueprints/Complex.java
// public static Complex complex(String endpoint) {
//     return new Complex(endpoint);
// }


// Path: src/main/java/org/iotlabs/dashboard/blueprints/AuthBluePrint.java
import org.iotlabs.storage.models.general.User;
import org.iotlabs.storage.util.StringUtils;
import org.iotlabs.dashboard.httpexceptions.Http4xx;
import org.iotlabs.dashboard.literals.ContentType;
import org.iotlabs.dashboard.utils.Checker;
import org.iotlabs.dashboard.utils.ResponseSetter;
import spark.ModelAndView;
import spark.template.mustache.MustacheTemplateEngine;
import java.util.HashMap;
import java.util.Map;
import static org.iotlabs.dashboard.blueprints.Complex.complex;

package org.iotlabs.dashboard.blueprints;




public class AuthBluePrint extends AbstractBluePrint {


    public AuthBluePrint() {
        super("auth");
    }

    @Override
    public void register() {
        complex(getEndPoint("login"))
                .get(
                        (req, res) -> {
                            Map<String, Object> scopes = new HashMap<>();
                            scopes.put("title", "IotLabs");
                            scopes.put("admin_mail", "toori67@gmail.com");
                            return new ModelAndView(scopes,"login.html");
                        }, new MustacheTemplateEngine(getTemplatePath())
                )
                .post((req, res) -> {
                    String username = req.queryParams("username");
                    String passwd = StringUtils.sha256(req.queryParams("password"));
                    User user = Checker.authenticate(username, passwd);
                    if (user == null) {
                        req.session().removeAttribute("auth");
                        ResponseSetter.setErrorResponse(res, new Http4xx(404, "not authorized"));
                        return res.body();
                    } else {
                        if (Checker.isRequestingHtml(req)) {
                            res.redirect("/");
                        }
                        req.session().attribute("auth", true);
                        res.type(ContentType.JSON);