SHARE
TWEET

Untitled

a guest May 30th, 2017 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package util;
  2.  
  3. import config.Config;
  4. import haxe.Md5;
  5.  
  6. class Auth {
  7.  
  8.     public static function isAuth():Bool {
  9.         var c = neko.Web.getCookies();
  10.         if (c.exists("hx_a") && c.exists("hx_u")) {
  11.             var user = db.User.manager.get(Std.parseInt(c.get("hx_u")));
  12.             if (user == null)
  13.                 return false;
  14.             if (Auth.buildHash(user.name,user.password) == c.get("hx_a")) {
  15.                 var t = getCookieTime();
  16.                 neko.Web.setCookie("hx_u", Std.string(user.id), t, Config.config.cookie.domain, Config.config.cookie.path);
  17.                 neko.Web.setCookie("hx_a", buildHash(user.name,user.password), t, Config.config.cookie.domain, Config.config.cookie.path);
  18.                 return true;               
  19.             }
  20.         }
  21.         return false;  
  22.     }
  23.    
  24.     public static inline function getUser():db.User
  25.         return db.User.manager.get(Std.parseInt(neko.Web.getCookies().get("hx_u")))
  26.    
  27.     private static inline function buildHash(name:String,password:String)
  28.         return Md5.encode(Config.config.cookie.salt+name+Md5.encode(password))
  29.        
  30.     private static inline function getCookieTime()
  31.         return Date.fromTime((Date.now().getTime())+60*1000*Config.config.cookie.time)
  32.    
  33.     public static function login(name:String,password:String) {
  34.         var u = db.User.manager.search({name:name}).first();
  35.         if (u.password == Md5.encode(password)) {
  36.             var t = getCookieTime();
  37.             neko.Web.setCookie("hx_u", Std.string(u.id), t, Config.config.cookie.domain, Config.config.cookie.path);
  38.             neko.Web.setCookie("hx_a", buildHash(u.name,u.password), t, Config.config.cookie.domain, Config.config.cookie.path);
  39.             return true;       
  40.         }      
  41.         return false;
  42.     }
  43.    
  44.     public static function logout() {
  45.         neko.Web.setCookie("hx_u","", Date.now(), Config.config.cookie.domain, Config.config.cookie.path);
  46.         neko.Web.setCookie("hx_a","", Date.now(), Config.config.cookie.domain, Config.config.cookie.path);
  47.         return true;
  48.     }
  49.    
  50.    
  51. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top