From 34a440a6bb18de9fc339ab48b491f677f5516959 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Mon, 3 Oct 2016 17:43:22 +0200 Subject: [PATCH] LoginServiceImpl --- .../context/security/persistence/Sessions.java | 8 +- .../resources/controllers/LoginController.java | 110 ++---------------- .../resources/controllers/PagesController.java | 2 +- .../com/prueba/services/impl/LoginServiceImpl.java | 124 +++++++++++++++++++++ 4 files changed, 136 insertions(+), 108 deletions(-) diff --git a/src/main/java/com/prueba/core/context/security/persistence/Sessions.java b/src/main/java/com/prueba/core/context/security/persistence/Sessions.java index 25b772e..2a21a40 100644 --- a/src/main/java/com/prueba/core/context/security/persistence/Sessions.java +++ b/src/main/java/com/prueba/core/context/security/persistence/Sessions.java @@ -23,7 +23,7 @@ public class Sessions { return SessionsContextHolder.INSTANCE; } - public SessionInfo getSession(final UUID uuid) { + public SessionInfo getSession(UUID uuid) { return sessions.get(uuid); } @@ -33,8 +33,8 @@ public class Sessions { sessions.put(uuid, newSessionInfo); } - public void removeSession(SessionInfo sessionInfo) { - sessions.remove(sessionInfo); + public void removeSession(UUID uuid) { + sessions.remove(uuid); } public boolean isValidSession(HttpExchange httpExchange) { @@ -47,7 +47,7 @@ public class Sessions { if (sessionInfo.getLastSessionTime().plusMinutes(5).compareTo(currentDateTime) > 0) { isValid = true; } else { - sessions.remove(sessionInfo); + sessions.remove(sessionInfo.getUUID()); } } diff --git a/src/main/java/com/prueba/resources/controllers/LoginController.java b/src/main/java/com/prueba/resources/controllers/LoginController.java index 275aa6a..33aed71 100644 --- a/src/main/java/com/prueba/resources/controllers/LoginController.java +++ b/src/main/java/com/prueba/resources/controllers/LoginController.java @@ -1,124 +1,28 @@ package com.prueba.resources.controllers; -import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.URI; -import java.nio.charset.Charset; -import java.util.UUID; -import com.prueba.core.context.security.handle.SessionHandler; -import com.prueba.core.context.security.persistence.SessionInfo; -import com.prueba.core.context.security.persistence.Sessions; import com.prueba.core.web.controller.Controller; import com.prueba.services.impl.LoginServiceImpl; -import com.prueba.view.login.LoginFormImpl; -import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; public class LoginController implements Controller { - private static final String URI = "/login/login.html"; - + @Override public void handle(HttpExchange httpExchange) throws IOException { - final String requestMethod = httpExchange.getRequestMethod(); - - switch (requestMethod) { - case "GET": - this.processLoginGet(httpExchange); - break; - case "POST": - this.processLoginPost(httpExchange); - break; - default: - httpExchange.sendResponseHeaders(404, 0); - break; - } - - } - - protected void processLoginGet(HttpExchange httpExchange) throws IOException { + final LoginServiceImpl loginService = new LoginServiceImpl(); final String requestedURI = httpExchange.getRequestURI().toString(); - final SessionInfo sessionInfo = SessionHandler.getLocalSession(); - final LoginFormImpl loginForm = new LoginFormImpl(); - String html = ""; - if (Sessions.getInstance().isValidSession(httpExchange)) { - html = loginForm.doNoRequiredLogin(); - Sessions.getInstance().refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername()); + if (requestedURI.startsWith("/app/login/login.html")) { + loginService.processLogin(httpExchange); + } else if (requestedURI.startsWith("/app/login/logout.html")) { + loginService.processLogoutGet(httpExchange); } else { - html = loginForm.doRequiredLogin(requestedURI); - } - - httpExchange.sendResponseHeaders(200, html.length()); - - try (final OutputStream os = httpExchange.getResponseBody()) { - os.write(html.getBytes()); + httpExchange.sendResponseHeaders(404, 0); } - - } - private void processLoginPost(HttpExchange httpExchange) throws IOException { - final SessionInfo sessionInfo = SessionHandler.getLocalSession(); - - if (!Sessions.getInstance().isValidSession(httpExchange)) { - String body = this.getBody(httpExchange); - String [] formData = body.split("&"); - if (formData.length == 2) { - String username = formData[0].split("=")[1]; - String password = formData[1].split("=")[1]; - - LoginServiceImpl loginService = new LoginServiceImpl(); - if (loginService.isValidUser(username, password)) { - UUID uuid = UUID.randomUUID(); - this.setCookieHeader(httpExchange, uuid.toString()); - Sessions.getInstance().refreshSession(uuid, username); - this.doRedirect(httpExchange); - } else { - httpExchange.sendResponseHeaders(401, 0); - } - } - } else { - Sessions.getInstance().refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername()); - doRedirect(httpExchange); - } } - private String getBody (HttpExchange httpExchange) throws IOException { - try(final InputStream inputStream = httpExchange.getRequestBody(); - final ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream()) { - - final int bufferSize = 1024; - final byte[] buffer = new byte[bufferSize]; - - int len = 0; - while ((len = inputStream.read(buffer)) != -1) { - byteBuffer.write(buffer, 0, len); - } - - return new String(byteBuffer.toByteArray(), Charset.forName("UTF-8")); - } - } - protected void setCookieHeader(HttpExchange httpExchange, String UUIDString) { - Headers headers = httpExchange.getResponseHeaders(); - - headers.remove("Set-Cookie"); - headers.set("Set-Cookie", UUIDString + "; path=/"); - } - - protected void doRedirect(HttpExchange httpExchange) throws IOException { - String requestURIString = httpExchange.getRequestURI().toString(); - String[] urls = requestURIString.split("serviceName="); - String serviceName = ""; - if (urls.length == 2) { - serviceName = urls[1]; - } - - Headers responseHeaders = httpExchange.getResponseHeaders(); - responseHeaders.add("Location", serviceName); - httpExchange.sendResponseHeaders(302, 0); - } } diff --git a/src/main/java/com/prueba/resources/controllers/PagesController.java b/src/main/java/com/prueba/resources/controllers/PagesController.java index deda099..e19100b 100644 --- a/src/main/java/com/prueba/resources/controllers/PagesController.java +++ b/src/main/java/com/prueba/resources/controllers/PagesController.java @@ -40,7 +40,7 @@ public class PagesController implements Controller { html = pageImpl.doPage(2, getSafeUserName()); break; case "/app/pages/page_3.html": - html = pageImpl.doPage(2, getSafeUserName()); + html = pageImpl.doPage(3, getSafeUserName()); break; default: responseStatus = 404; diff --git a/src/main/java/com/prueba/services/impl/LoginServiceImpl.java b/src/main/java/com/prueba/services/impl/LoginServiceImpl.java index 90621a1..772fe68 100644 --- a/src/main/java/com/prueba/services/impl/LoginServiceImpl.java +++ b/src/main/java/com/prueba/services/impl/LoginServiceImpl.java @@ -1,8 +1,22 @@ package com.prueba.services.impl; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.charset.Charset; +import java.util.UUID; + +import com.prueba.core.context.security.handle.SessionHandler; +import com.prueba.core.context.security.persistence.SessionInfo; +import com.prueba.core.context.security.persistence.Sessions; import com.prueba.persistence.dao.LoginDao; +import com.prueba.view.login.LoginFormImpl; +import com.sun.net.httpserver.Headers; +import com.sun.net.httpserver.HttpExchange; public class LoginServiceImpl { + private static final String COOKIE_HEADER = "Cookie"; public boolean isValidUser(String username, String password) { final LoginDao dao = new LoginDao(); @@ -14,4 +28,114 @@ public class LoginServiceImpl { } } + + public void processLogoutGet(HttpExchange httpExchange) throws IOException { + final Headers headers = httpExchange.getRequestHeaders(); + final String cookieValue = headers.getFirst(COOKIE_HEADER); + if (cookieValue != null) { + final UUID uuid = UUID.fromString(cookieValue); + Sessions.getInstance().removeSession(uuid); + } + + httpExchange.sendResponseHeaders(200, 0); + } + + public void processLogin(HttpExchange httpExchange) throws IOException { + final String requestMethod = httpExchange.getRequestMethod(); + + switch (requestMethod) { + case "GET": + this.processLoginGet(httpExchange); + break; + case "POST": + this.processLoginPost(httpExchange); + break; + default: + httpExchange.sendResponseHeaders(404, 0); + break; + } + } + + protected void processLoginGet(HttpExchange httpExchange) throws IOException { + final String requestedURI = httpExchange.getRequestURI().toString(); + final SessionInfo sessionInfo = SessionHandler.getLocalSession(); + final LoginFormImpl loginForm = new LoginFormImpl(); + + String html = ""; + if (Sessions.getInstance().isValidSession(httpExchange)) { + html = loginForm.doNoRequiredLogin(); + Sessions.getInstance().refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername()); + } else { + html = loginForm.doRequiredLogin(requestedURI); + } + + httpExchange.sendResponseHeaders(200, html.length()); + + try (final OutputStream os = httpExchange.getResponseBody()) { + os.write(html.getBytes()); + } + + } + + protected void processLoginPost(HttpExchange httpExchange) throws IOException { + final SessionInfo sessionInfo = SessionHandler.getLocalSession(); + + if (!Sessions.getInstance().isValidSession(httpExchange)) { + String body = this.getBody(httpExchange); + String [] formData = body.split("&"); + if (formData.length == 2) { + String username = formData[0].split("=")[1]; + String password = formData[1].split("=")[1]; + + LoginServiceImpl loginService = new LoginServiceImpl(); + if (loginService.isValidUser(username, password)) { + UUID uuid = UUID.randomUUID(); + this.setCookieHeader(httpExchange, uuid.toString()); + Sessions.getInstance().refreshSession(uuid, username); + this.doRedirect(httpExchange); + } else { + httpExchange.sendResponseHeaders(401, 0); + } + } + } else { + Sessions.getInstance().refreshSession(sessionInfo.getUUID(), sessionInfo.getUsername()); + doRedirect(httpExchange); + } + } + + protected String getBody (HttpExchange httpExchange) throws IOException { + try(final InputStream inputStream = httpExchange.getRequestBody(); + final ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream()) { + + final int bufferSize = 1024; + final byte[] buffer = new byte[bufferSize]; + + int len = 0; + while ((len = inputStream.read(buffer)) != -1) { + byteBuffer.write(buffer, 0, len); + } + + return new String(byteBuffer.toByteArray(), Charset.forName("UTF-8")); + } + } + + protected void setCookieHeader(HttpExchange httpExchange, String UUIDString) { + Headers headers = httpExchange.getResponseHeaders(); + + headers.remove("Set-Cookie"); + headers.set("Set-Cookie", UUIDString + "; path=/"); + } + + protected void doRedirect(HttpExchange httpExchange) throws IOException { + String requestURIString = httpExchange.getRequestURI().toString(); + String[] urls = requestURIString.split("serviceName="); + String serviceName = ""; + if (urls.length == 2) { + serviceName = urls[1]; + } + + Headers responseHeaders = httpExchange.getResponseHeaders(); + responseHeaders.add("Location", serviceName); + httpExchange.sendResponseHeaders(302, 0); + } } -- 2.1.4