diff --git a/.gitignore b/.gitignore index 39f709b6958b10cdd3b65e2fccbd8c0c2a5a47f5..c9b0373afef7e8e6517a160f35d3257f977ef6a1 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ /OjekOnlineWebservice/nbproject/private/ /WebApp/build/ /IdentityService/build/ -/OjekOnlineWebservice/build/ \ No newline at end of file +/OjekOnlineWebservice/build/ +/IdentityService/dist/ \ No newline at end of file diff --git a/IdentityService/nbproject/genfiles.properties b/IdentityService/nbproject/genfiles.properties index 262a3a2a53cc1112616580d5bf6d9e04e973c8ab..c5c18567210b4c8ae5e23ddccb39d6bd303f32a7 100644 --- a/IdentityService/nbproject/genfiles.properties +++ b/IdentityService/nbproject/genfiles.properties @@ -1,8 +1,8 @@ -build.xml.data.CRC32=1d216b83 +build.xml.data.CRC32=1d45bd4f build.xml.script.CRC32=e342e6c3 build.xml.stylesheet.CRC32=651128d4@1.77.1.1 # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. -nbproject/build-impl.xml.data.CRC32=1d216b83 +nbproject/build-impl.xml.data.CRC32=1d45bd4f nbproject/build-impl.xml.script.CRC32=21e9d869 nbproject/build-impl.xml.stylesheet.CRC32=99ea4b56@1.77.1.1 diff --git a/IdentityService/nbproject/project.properties b/IdentityService/nbproject/project.properties index 592fb7b8598bcebd2948dc8a2e89cc13cc84c2ff..08bfe2b1174409bbbef3806674717ce058170c9d 100644 --- a/IdentityService/nbproject/project.properties +++ b/IdentityService/nbproject/project.properties @@ -74,7 +74,6 @@ javadoc.use=true javadoc.version=false javadoc.windowtitle= lib.dir=${web.docbase.dir}/WEB-INF/lib -no.dependencies=false persistence.xml.dir=${conf.dir} platform.active=default_platform resource.dir=setup diff --git a/IdentityService/nbproject/project.xml b/IdentityService/nbproject/project.xml index 657f876b53446f7fea6141ed266649f64e381d31..3c4f3dd97720aec5c46102d51d664ad818daf72d 100644 --- a/IdentityService/nbproject/project.xml +++ b/IdentityService/nbproject/project.xml @@ -21,10 +21,10 @@ </web-module-libraries> <web-module-additional-libraries/> <source-roots> - <root id="src.dir" name="Source Packages"/> + <root id="src.dir"/> </source-roots> <test-roots> - <root id="test.src.dir" name="Test Packages"/> + <root id="test.src.dir"/> </test-roots> </data> </configuration> diff --git a/IdentityService/src/java/validate/SignIn.java b/IdentityService/src/java/validate/SignIn.java index efaba05aff83298d24a56255e05cd5f7b8df67c8..72cb8335f2f7027aa1c185c1f38ceed5d79ed51d 100644 --- a/IdentityService/src/java/validate/SignIn.java +++ b/IdentityService/src/java/validate/SignIn.java @@ -3,10 +3,11 @@ * To change this template file, choose Tools | Templates * and open the template in the editor. */ -package Validate; +package validate; import java.io.IOException; import java.io.PrintWriter; +import static java.lang.System.out; import java.math.BigInteger; import java.security.SecureRandom; import java.sql.Connection; @@ -112,54 +113,8 @@ public class SignIn extends HttpServlet { json.put("phone_no",rs.getString("phone_no")); json.put("token",token); } else { - String sqlEmail; - sqlEmail = "SELECT * FROM account WHERE email = ? and password = ?"; - PreparedStatement dbStatementEmail = conn.prepareStatement(sqlEmail,Statement.RETURN_GENERATED_KEYS); - dbStatementEmail.setString(1, email); - dbStatementEmail.setString(2, password); - ResultSet rsEmail = dbStatementEmail.executeQuery(); - if (rsEmail.next()) { - SecureRandom random = new SecureRandom(); - String token = new BigInteger(130, random).toString(32); - - String sqlCek; - boolean ulang = true; - PreparedStatement dbStatementCek; - - while (ulang) { - token = new BigInteger(130, random).toString(32); - sqlCek = "SELECT * FROM token WHERE token = ?"; - dbStatementCek = conn.prepareStatement(sqlCek); - dbStatementCek.setString(1, token); - ResultSet rsCek = dbStatementCek.executeQuery(); - if (rsCek.next()) { - ulang = true; - } else { - ulang = false; - } - } - - Date dNow = new Date(); - SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd"); - SimpleDateFormat timeFormat = new SimpleDateFormat ("HH:mm:ss"); - String time_expired = timeFormat.format(dNow); - String date_expired = dateFormat.format(dNow); - String sqlInputToken; - sqlInputToken = "INSERT INTO token(id_account,token,date_expired,time_expired) VALUES(?,?,?,?)"; - PreparedStatement dbStatementInputToken = conn.prepareStatement(sqlInputToken); - dbStatementInputToken.setInt(1, rs.getInt("id")); - dbStatementInputToken.setString(2, token); - dbStatementInputToken.setString(3, date_expired); - dbStatementInputToken.setString(4, time_expired); - int rsInputToken = dbStatementInputToken.executeUpdate(); - - json.put("id",rsEmail.getInt("id")); - json.put("fullname",rsEmail.getString("fullname")); - json.put("username",rsEmail.getString("username")); - json.put("address",rsEmail.getString("address")); - json.put("postalcode",rsEmail.getString("postalcode")); - json.put("phonenumber",rsEmail.getString("phonenumber")); - json.put("token",token); + boolean h = true; + if (!h) { } else { json.put("id",0); json.put("fullname",""); @@ -220,5 +175,6 @@ public class SignIn extends HttpServlet { public String getServletInfo() { return "Short description"; }// </editor-fold> + } diff --git a/IdentityService/src/java/validate/ValidateToken.java b/IdentityService/src/java/validate/ValidateToken.java index 08f6de13a1420d9ce03af194a7384c9906699afb..c4b73c2d3491001e210caaebe6e6e5e46d4a3a42 100644 --- a/IdentityService/src/java/validate/ValidateToken.java +++ b/IdentityService/src/java/validate/ValidateToken.java @@ -7,15 +7,29 @@ package validate; import java.io.IOException; import java.io.PrintWriter; +import java.math.BigInteger; +import java.security.SecureRandom; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.json.simple.JSONObject; + /** * - * @author TIFAH06 */ @WebServlet(name = "ValidateToken", urlPatterns = {"/ValidateToken"}) public class ValidateToken extends HttpServlet { @@ -33,17 +47,123 @@ public class ValidateToken extends HttpServlet { throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { - /* TODO output your page here. You may use following sample code. */ - out.println("<!DOCTYPE html>"); - out.println("<html>"); - out.println("<head>"); - out.println("<title>Servlet ValidateToken</title>"); - out.println("</head>"); - out.println("<body>"); - out.println("<h1>Servlet ValidateToken at " + request.getContextPath() + "</h1>"); - out.println("</body>"); - out.println("</html>"); - } + java.lang.String token = request.getParameter("token"); + + Class.forName("com.mysql.jdbc.Driver"); + try + ( Connection conn = DriverManager.getConnection( + "jdbc:mysql://localhost:3306/Account?zeroDateTimeBehavior=convertToNull", + "root", + ""); + Statement stmt = conn.createStatement()) { + String sql; + sql = "SELECT id,fullname,username,address,postalcode,phonenumber,time_expired,date_expired FROM account,token WHERE id = id_account and token = ?"; + PreparedStatement dbStatement = conn.prepareStatement(sql); + dbStatement.setString(1, token); + /* Get every data returned by SQL query */ + ResultSet rs = dbStatement.executeQuery(); + /* Get every data returned by SQL query */ + JSONObject json = new JSONObject(); + boolean expires = false; + /* Get every data returned by SQL query */ + Date dateNow = new Date(); + SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); + + if(rs.next()){ + String date = rs.getString("date_expired"); + String time = rs.getString("time_expired"); + String datetime = date + " " + time; + Date dateExpireStart = null; + try { + dateExpireStart = dateFormat.parse(datetime); + } catch (ParseException ex) { + Logger.getLogger(ValidateToken.class.getName()).log(Level.SEVERE, null, ex); + } + long msDiff = dateNow.getTime() - dateExpireStart.getTime(); + if (msDiff > 900000) { + expires = true; + } + if (expires) { + SecureRandom random = new SecureRandom(); + String tokenExpires = new BigInteger(130, random).toString(32); + + String sqlCek; + boolean ulang = true; + PreparedStatement dbStatementCek; + + while (ulang) { + tokenExpires = new BigInteger(130, random).toString(32); + sqlCek = "SELECT * FROM token WHERE token = ?"; + dbStatementCek = conn.prepareStatement(sqlCek); + dbStatementCek.setString(1, tokenExpires); + ResultSet rsCek = dbStatementCek.executeQuery(); + if (rsCek.next()) { + ulang = true; + } else { + ulang = false; + } + } + + Date dNow = new Date(); + SimpleDateFormat dateFormatExpires = new SimpleDateFormat ("yyyy-MM-dd"); + SimpleDateFormat timeFormatExpires = new SimpleDateFormat ("HH:mm:ss"); + String time_expired = timeFormatExpires.format(dNow); + String date_expired = dateFormatExpires.format(dNow); + String sqlInputToken; + sqlInputToken = "INSERT INTO token(id_account,token,date_expired,time_expired) VALUES(?,?,?,?)"; + PreparedStatement dbStatementInputToken = conn.prepareStatement(sqlInputToken); + dbStatementInputToken.setInt(1, rs.getInt("id")); + dbStatementInputToken.setString(2, tokenExpires); + dbStatementInputToken.setString(3, date_expired); + dbStatementInputToken.setString(4, time_expired); + int rsInputToken = dbStatementInputToken.executeUpdate(); + + String sqlDelete; + sqlDelete = "DELETE FROM token WHERE token = ?"; + PreparedStatement dbStatementDelete = conn.prepareStatement(sqlDelete,Statement.RETURN_GENERATED_KEYS); + dbStatementDelete.setString(1, token); + + /* Get every data returned by SQL query */ + int rsDelete = dbStatementDelete.executeUpdate(); + + json.put("id",rs.getInt("id")); + json.put("fullname",rs.getString("fullname")); + json.put("username",rs.getString("username")); + json.put("address",rs.getString("address")); + json.put("postalcode",rs.getString("postalcode")); + json.put("phonenumber",rs.getString("phonenumber")); + json.put("token",tokenExpires); + json.put("validate",2); + } else { + json.put("id",rs.getInt("id")); + json.put("fullname",rs.getString("fullname")); + json.put("username",rs.getString("username")); + json.put("address",rs.getString("address")); + json.put("postalcode",rs.getString("postalcode")); + json.put("phonenumber",rs.getString("phonenumber")); + json.put("token",""); + json.put("validate",1); + } + } else { + json.put("id",0); + json.put("fullname",""); + json.put("username",""); + json.put("address",""); + json.put("postalcode",""); + json.put("phonenumber",""); + json.put("token",""); + json.put("validate",3); + } + + response.setContentType("application/json"); + response.getWriter().write(json.toString()); + rs.close(); + stmt.close(); + conn.close(); + } + } catch (ClassNotFoundException | SQLException ex) { + + } } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> diff --git a/IdentityService/web/WEB-INF/glassfish-web.xml b/IdentityService/web/WEB-INF/glassfish-web.xml new file mode 100644 index 0000000000000000000000000000000000000000..8dd3199aa741a004aed6f053b0dab2f38b1de89e --- /dev/null +++ b/IdentityService/web/WEB-INF/glassfish-web.xml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd"> +<glassfish-web-app error-url=""> + <context-root>/IdentityService</context-root> + <class-loader delegate="true"/> + <jsp-config> + <property name="keepgenerated" value="true"> + <description>Keep a copy of the generated servlet class' java code.</description> + </property> + </jsp-config> +</glassfish-web-app> diff --git a/IdentityService/web/WEB-INF/web.xml b/IdentityService/web/WEB-INF/web.xml new file mode 100644 index 0000000000000000000000000000000000000000..32e36c14cd8fc9bdb1674af4d4a950c12dc5205d --- /dev/null +++ b/IdentityService/web/WEB-INF/web.xml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="UTF-8"?> +<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> + <servlet> + <servlet-name>signin</servlet-name> + <servlet-class>validate.SignIn</servlet-class> + </servlet> + <servlet-mapping> + <servlet-name>signin</servlet-name> + <url-pattern>/SignIn</url-pattern> + </servlet-mapping> +</web-app> \ No newline at end of file diff --git a/WebApp/web/WEB-INF/web.xml b/WebApp/web/WEB-INF/web.xml new file mode 100644 index 0000000000000000000000000000000000000000..6058a36702ad03a4cd622100cfee7881c4e2e62b --- /dev/null +++ b/WebApp/web/WEB-INF/web.xml @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" + version="3.1"> + <session-config> + <session-timeout> + 30 + </session-timeout> + </session-config> +</web-app> diff --git a/WebApp/web/index.jsp b/WebApp/web/index.jsp index d1e438f4a4129a8ae3383f31ac779affb3d36c03..77f775d4b592598ddc9641447233ff9387a4271f 100644 --- a/WebApp/web/index.jsp +++ b/WebApp/web/index.jsp @@ -5,9 +5,107 @@ --%> <%@page contentType="text/html" pageEncoding="UTF-8" - + <%@page import="org.json.JSONException"%> +<%@page import="org.json.JSONObject"%> +<%@page import="java.io.InputStreamReader"%> +<%@page import="java.io.BufferedReader"%> +<%@page import="java.io.DataOutputStream"%> +<%@page import="javax.xml.ws.ProtocolException"%> +<%@page import="java.io.IOException"%> +<%@page import="java.net.HttpURLConnection"%> +<%@page import="java.net.MalformedURLException"%> +<%@page import="java.net.URL"%> - %> + <% + String token = null; + Cookie cookiecek = null; + Cookie[] cookies = null; + cookies = request.getCookies(); + for (int i = 0; i < cookies.length; i++) { + cookiecek = cookies[i]; + if((cookiecek.getName()).compareTo("token") == 0 ){ + token = cookiecek.getValue(); + //cookie.setMaxAge(0); + } + } + //java.lang.String token = "qwertyuiop"; + // TODO process result here + + + String username = request.getParameter("username"); + String password = request.getParameter("password"); + + //String username = "bebas"; + //String password = "bebas"; + + String USER_AGENT = "Mozilla/5.0"; + + String url = "http://localhost:8001/IdentityService/SignIn"; + URL obj = null; + try { + obj = new URL(url); + } catch (MalformedURLException ex) { + } + HttpURLConnection con = null; + try { + con = (HttpURLConnection) obj.openConnection(); + } catch (IOException ex) { + + } + + try { + //add reuqest header + con.setRequestMethod("POST"); + } catch (ProtocolException ex) { + + } + con.setRequestProperty("User-Agent", USER_AGENT); + con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); + + String urlParameters = "username="+username+"&password="+password; + + // Send post request + con.setDoOutput(true); + DataOutputStream wr = null; + + try { + wr = new DataOutputStream(con.getOutputStream()); + wr.writeBytes(urlParameters); + wr.flush(); + wr.close(); + } catch (IOException ex) { + } + + StringBuffer responsebuff = new StringBuffer(); + try { + int responseCode = con.getResponseCode(); + BufferedReader in = new BufferedReader( + new InputStreamReader(con.getInputStream())); + String inputLine; + + while ((inputLine = in.readLine()) != null) { + responsebuff.append(inputLine); + } + in.close(); + } catch (IOException ex) { + } + + JSONObject JSobjek = null; + JSobjek = new JSONObject(responsebuff.toString()); + + //str = (String)JSobjek.get("id"); + int respId = (int)JSobjek.getInt("id"); + //if (!str.equals("null")) { + //respId = Integer.parseInt(str); + //} + if (respId == 0) { + //response.sendRedirect("http://localhost:8000/WebApp/index.jsp"); + } else { + Cookie cookie = new Cookie("token",(String)JSobjek.getString("token")); + response.addCookie(cookie); + response.sendRedirect("http://localhost:8000/WebApp/catalog.jsp"); + } + %> <!DOCTYPE html> <html> <head> diff --git a/data/derby.log b/data/derby.log new file mode 100644 index 0000000000000000000000000000000000000000..c571ef8fe2f0e261ee6caba0c5b0d64fd22a206e --- /dev/null +++ b/data/derby.log @@ -0,0 +1 @@ +Wed Nov 08 17:31:19 ICT 2017 : Apache Derby Network Server - 10.11.1.2 - (1629631) started and ready to accept connections on port 1527 diff --git a/data/derby.properties b/data/derby.properties new file mode 100644 index 0000000000000000000000000000000000000000..01615d5fc683a7382d0848d1f8acba0ef9360be2 --- /dev/null +++ b/data/derby.properties @@ -0,0 +1,2 @@ +#Java DB Properties File +#Wed Nov 08 16:00:37 ICT 2017