package authorization;

import authorization.models.Session;
import authorization.models.User;
import org.mindrot.jbcrypt.BCrypt;

import java.util.Date;
import java.util.Random;

import static play.mvc.Controller.session;

/**
 * Created by szymon on 5/3/17.
 */
public class Authenticator {

    public static User getUser() throws NoSuchFieldException, IllegalAccessException {
        if(session().containsKey("sessionId")) {
            String sessionId = session().get("sessionId");
            Session session = new Session();

            if(session.find("sessionId", session.sessionId)) {
                User user = new User();
                user.get(session.userId);
                return user;
            }
        }
        return null;
    }

    public static void logIn(String email, String pass) throws NoSuchFieldException, IllegalAccessException {
        User user = new User();
        if(user.find("mail", email) && checkPass(pass, user.passHash)) {
            makeNewSession(user);
        } else {
            throw new NoSuchUserException();
        }
    }

    public static void LogOut() throws NoSuchFieldException, IllegalAccessException {
        if(session().containsKey("sessionId")) {
            String sessionId = session().get("sessionId");
            Session session = new Session();

            if(session.find("sessionId", session.sessionId)) {
                session.delete();
            }
            session().remove("sessionId");
        }
    }

    public static void SignUp(User user, String pass) throws NoSuchFieldException, IllegalAccessException {
        if(new User().find("mail", user.mail)) {
            throw new ThereIsSuchUserNameException();
        }
        user.passHash = getHash(pass);
        user.save();
    }

    public static String getHash(String pass) {
        return BCrypt.hashpw(pass, BCrypt.gensalt());
    }

    public static boolean checkPass(String pass, String passHash) {
        return BCrypt.checkpw(pass, passHash);
    }


    public static void makeNewSession(User user) throws NoSuchFieldException, IllegalAccessException {
        final int LEN_OF_SESSION_ID = 20;
        final long TEN_DAYS = 1000*3600*24*10;
        Session session = new Session(0, "0", user.id, new Date().getTime() + TEN_DAYS);
        session.getNewId(LEN_OF_SESSION_ID);
        session.save();
        session().put("sessionId", session.sessionId);
    }

}

class NoSuchUserException extends RuntimeException {}

class ThereIsSuchUserNameException extends RuntimeException {}
@@ -150,33 +150,8 @@
 ate;
-%0Aimport java.util.Random;
 %0A%0Aim
@@ -196,48 +196,8 @@
 n;%0A%0A
-/**%0A * Created by szymon on 5/3/17.%0A */%0A
 publ
@@ -722,86 +722,18 @@
 gIn(
-String email, String pass) throws NoSuchFieldException, IllegalAccessException
+User user)
  %7B%0A 
@@ -736,33 +736,35 @@
  %7B%0A        User 
-u
+dbU
 ser = new User()
@@ -777,12 +777,32 @@
     
-if(u
+try %7B%0A            if(dbU
 ser.
@@ -811,20 +811,31 @@
 nd(%22
+e
 mail%22, 
-e
+user.getE
 mail
+()
 ) &&
@@ -849,29 +849,50 @@
 ass(
-pass, user.p
+user.getPassHash(), dbUser.getP
 assHash
+(
 ))
+)
  %7B%0A 
@@ -902,16 +902,20 @@
         
+ 
+   
 makeNewS
@@ -936,16 +936,20 @@
         
+    
 %7D else %7B
@@ -949,16 +949,147 @@
  else %7B%0A
+                throw new NoSuchUserException();%0A            %7D%0A        %7D catch (NoSuchFieldException %7C IllegalAccessException e) %7B%0A
         
@@ -1165,17 +1165,17 @@
 ic void 
-L
+l
 ogOut() 
@@ -1576,17 +1576,17 @@
 ic void 
-S
+s
 ignUp(Us
@@ -1592,29 +1592,16 @@
 ser user
-, String pass
 ) throws
@@ -1676,16 +1676,17 @@
 ).find(%22
+e
 mail%22, u
@@ -1689,16 +1689,17 @@
 %22, user.
+e
 mail)) %7B
@@ -1767,47 +1767,8 @@
   %7D%0A
-        user.passHash = getHash(pass);%0A
     
@@ -2535,28 +2535,29 @@
  extends RuntimeException %7B%7D
+%0A

update Authenticator.java