Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.80 KB | None | 0 0
  1. package com.petukhovsky.wat.webserver;
  2.  
  3. import com.petukhovsky.wat.Tools;
  4. import com.petukhovsky.wat.server.Account;
  5. import com.petukhovsky.wat.server.Auth;
  6. import org.eclipse.jetty.server.Request;
  7. import org.eclipse.jetty.server.handler.AbstractHandler;
  8.  
  9. import javax.servlet.ServletException;
  10. import javax.servlet.http.Cookie;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13. import java.io.IOException;
  14. import java.util.*;
  15.  
  16. /**
  17.  * Created by Arthur on 3/30/2015.
  18.  */
  19. public class WebHandler extends AbstractHandler {
  20.  
  21.     @Override
  22.     public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
  23.         response.setContentType("text/html;charset=utf-8");
  24.         response.setStatus(HttpServletResponse.SC_OK);
  25.         baseRequest.setHandled(true);
  26.         switch (target) {
  27.             case "/login":
  28.                 CookieAuth session = CookieAuth.getCookieAuth(baseRequest);
  29.                 if (session != null) {
  30.                     response.sendRedirect("/main");
  31.                     break;
  32.                 }
  33.                 String username = baseRequest.getParameter("user");
  34.                 String password = baseRequest.getParameter("pass");
  35.                 if (username == null || password == null) {
  36.                     response.getWriter().println(Tools.readFile("www/loginpage.html"));
  37.                     break;
  38.                 }
  39.                 Account account = Auth.auth(username, password);
  40.                 if (account == null) {
  41.                     response.getWriter().println("Wrong password or username");
  42.                     break;
  43.                 }
  44.                 new CookieAuth(account, response);
  45.                 response.sendRedirect("/main");
  46.                 break;
  47.             case "/logout":
  48.                 CookieAuth.clearAuth(baseRequest);
  49.                 response.sendRedirect("/login");
  50.                 break;
  51.             default:
  52.                 session = CookieAuth.getCookieAuth(baseRequest);
  53.                 if (session == null) {
  54.                     response.sendRedirect("/login");
  55.                     break;
  56.                 }
  57.                 account = session.getAccount();
  58.                 response.getWriter().println("<h1>Hello World!</h1><br>You are " + account.getFormattedUsername());
  59.         }
  60.     }
  61. }
  62.  
  63. class CookieAuth{
  64.     private static char[] symbols = new char[36];
  65.     static{
  66.         int k = 0;
  67.         for (char c = 'a'; c <= 'z'; c++) symbols[k++] = c;
  68.         for (char c = '0'; c <= '9'; c++) symbols[k++] = c;
  69.     }
  70.  
  71.     private static Random random = new Random();
  72.     private static HashMap<String, CookieAuth> users = new HashMap<>();
  73.  
  74.     private static String getRandomString() {
  75.         StringBuilder sb = new StringBuilder();
  76.         for (int i = 0; i < 16; i++) sb.append(symbols[random.nextInt(symbols.length)]);
  77.         return sb.toString();
  78.     }
  79.  
  80.     private String q;
  81.     private String w;
  82.     private String e;
  83.     private String r;
  84.     private Account account;
  85.  
  86.     CookieAuth(Account account, HttpServletResponse response) {
  87.         q = getRandomString();
  88.         w = getRandomString();
  89.         e = getRandomString();
  90.         r = getRandomString();
  91.         this.account = account;
  92.         users.put(q, this);
  93.         response.addCookie(createCookie("q", q));
  94.         response.addCookie(createCookie("w", w));
  95.         response.addCookie(createCookie("e", e));
  96.         response.addCookie(createCookie("r", r));
  97.     }
  98.  
  99.     private static Cookie createCookie (String key, String value) {
  100.         Cookie c = new Cookie(key, value);
  101.         c.setMaxAge(2678400);
  102.         return c;
  103.     }
  104.  
  105.     public static CookieAuth getCookieAuth(Request baseRequest) {
  106.         String q, w, e, r;
  107.         q = w = e = r = null;
  108.         if (baseRequest.getCookies() != null)
  109.             for (Cookie c : baseRequest.getCookies()) {
  110.                 switch (c.getName()) {
  111.                     case "q":
  112.                         q = c.getValue();
  113.                         break;
  114.                     case "w":
  115.                         w = c.getValue();
  116.                         break;
  117.                     case "e":
  118.                         e = c.getValue();
  119.                         break;
  120.                     case "r":
  121.                         r = c.getValue();
  122.                         break;
  123.                 }
  124.             }
  125.         if (q == null || w == null || e == null || r == null) return null;
  126.         CookieAuth cookieAuth = users.get(q);
  127.         if (cookieAuth != null && cookieAuth.check(q, w, e, r)) return cookieAuth;
  128.         else return null;
  129.     }
  130.  
  131.     private boolean check(String q, String w, String e, String r) {
  132.         return this.q.equals(q) && this.w.equals(w) && this.e.equals(e) && this.r.equals(r);
  133.     }
  134.  
  135.     public Account getAccount() {
  136.         return account;
  137.     }
  138.  
  139.     public static void clearAuth(Request baseRequest) {
  140.         String q, w, e, r;
  141.         q = w = e = r = null;
  142.         if (baseRequest.getCookies() != null)
  143.             for (Cookie c : baseRequest.getCookies()) {
  144.                 switch (c.getName()) {
  145.                     case "q":
  146.                         q = c.getValue();
  147.                         break;
  148.                     case "w":
  149.                         w = c.getValue();
  150.                         break;
  151.                     case "e":
  152.                         e = c.getValue();
  153.                         break;
  154.                     case "r":
  155.                         r = c.getValue();
  156.                         break;
  157.                 }
  158.             }
  159.         if (q == null || w == null || e == null || r == null) return;
  160.         CookieAuth cookieAuth = users.get(q);
  161.         if (cookieAuth != null && cookieAuth.check(q, w, e, r)) users.remove(q);
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement