package com.coding4people.mosquitoreport.api.controllers;

import javax.inject.Inject;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.ws.rs.Consumes;
import javax.ws.rs.ForbiddenException;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import com.coding4people.mosquitoreport.api.models.Email;
import com.coding4people.mosquitoreport.api.repositories.EmailRepository;

@Path("/auth/email")
public class AuthEmailController {
    @Inject
    EmailRepository emailRepository;

    @POST
    @Consumes("application/json")
    @Produces("application/json;charset=UTF-8")
    public Email auth(@Valid AuthInput input) {
        Email email = emailRepository.load(input.getEmail());
        
        if (email == null || !Email.checkPassword(input.getPassword(), email.getPassword())) {
            throw new ForbiddenException("Invalid email or password");
        }

        return email;
    }

    public static class AuthInput {
        @NotNull
        private String email;
        
        @NotNull
        private String password;

        public String getEmail() {
            return email;
        }

        public void setEmail(String email) {
            this.email = email;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
    }
}

@@ -50,24 +50,52 @@
 rollers;%0D%0A%0D%0A
+import java.util.Base64;%0D%0A%0D%0A
 import javax
@@ -476,16 +476,125 @@
 sitory;%0D
+%0Aimport com.fasterxml.jackson.databind.ObjectMapper;%0D%0Aimport com.fasterxml.jackson.databind.node.ObjectNode;%0D
 %0A%0D%0A@Path
@@ -805,21 +805,26 @@
  public 
-Email
+ObjectNode
  auth(@V
@@ -1092,32 +1092,40 @@
 %22);%0D%0A        %7D%0D%0A
+        
 %0D%0A        return
@@ -1114,36 +1114,236 @@
   %0D%0A        
-return email
+//TODO implement decent authentication%0D%0A        final String token = Base64.getEncoder().encodeToString(email.getUserguid().getBytes());%0D%0A%0D%0A        return new ObjectMapper().createObjectNode().put(%22token%22, token)
 ;%0D%0A    %7D%0D%0A%0D%0A

Send user token after login