Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.DX_57.SR_57;
- /** include default packages for Beans */
- import java.io.Serializable;
- import javax.enterprise.context.SessionScoped;
- // or import javax.faces.bean.SessionScoped;
- import javax.inject.Named;
- /** include package for SHA-256 encryption */
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- /** include SQL Packages */
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import javax.sql.DataSource;
- import javax.annotation.Resource;
- import javax.faces.context.FacesContext;
- import javax.inject.Inject;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpSession;
- // or import javax.faces.bean.ManagedBean;
- import org.DX_57.osgi.CL_27.api.CryptoSHA;
- import org.glassfish.osgicdi.OSGiService;
- import org.DX_57.osgi.SH_27.api.SessionHandle;
- @Named("loginController")
- @SessionScoped
- public class userCheck implements Serializable {
- private String user = null;
- private String password = null;
- private String error_Message = null;
- private String error_Database = null;
- public userCheck(){
- }
- /** Call the Oracle JDBC Connection driver */
- @Resource(name="jdbc/Oracle")
- private DataSource ds;
- /** Call OSGI Bundle SH_27 Session Handle */
- /** Use transient in order to disable serialization when calling OSGI Bundle */
- @Inject @OSGiService(dynamic=true) transient SessionHandle hello;
- public String CallOSGI() throws SQLException{
- return hello.CheckUserDB("Duke");
- }
- /** Call OSGI Bundle CL_27 Crypto Library */
- @Inject @OSGiService(dynamic=true) transient CryptoSHA cryptoString;
- public String zwwe(){
- return cryptoString.sayHello("Duke");
- }
- /** get the content of the variables from the JSF Login page */
- public void setUser(String newValue) {
- user = newValue;
- }
- public String getUser(){
- return user;
- }
- public void setPassword(String newValue) {
- password = newValue;
- }
- public String getPassword(){
- return password;
- }
- public String geterror_Database(){
- return error_Database;
- }
- public String geterror_Message(){
- return error_Message;
- }
- /** method for converting simple string into SHA-256 hash */
- public String string_hash(String hash) throws NoSuchAlgorithmException{
- MessageDigest md = MessageDigest.getInstance("SHA-256");
- md.update(hash.getBytes());
- byte byteData[] = md.digest();
- /** convert the byte to hex format */
- StringBuilder sb = new StringBuilder();
- for (int i = 0; i < byteData.length; i++) {
- sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
- }
- return sb.toString();
- }
- /** method for checking password into the Oracle database */
- public String CheckUserDB(String userToCheck) throws SQLException {
- String storedPassword = null;
- error_Message = null;
- String SQL_Statement = null;
- if (ds == null) throw new SQLException( error_Database = "No data source");
- Connection conn = ds.getConnection();
- if (conn == null) throw new SQLException( error_Database = "No connection");
- try {
- conn.setAutoCommit(false);
- boolean committed = false;
- try {
- SQL_Statement = "SELECT Passwd from USERS WHERE Username = ?";
- PreparedStatement passwordQuery = conn.prepareStatement(SQL_Statement);
- passwordQuery.setString(1, userToCheck);
- ResultSet result = passwordQuery.executeQuery();
- if(result.next()){
- storedPassword = result.getString("Passwd");
- }
- conn.commit();
- committed = true;
- } finally {
- if (!committed) conn.rollback();
- }
- }
- finally {
- conn.close();
- }
- /** if the user is not found or password don't match display error message*/
- if (storedPassword == null){
- error_Message = "Invalid Username!";
- } else {
- error_Message = "Invalid Password!";
- }
- return storedPassword;
- }
- /** method for inserting user credentials into user sessions table */
- /*
- *CREATE TABLE "ACTIVESESSIONS"(
- "SessionId" Char(20 ) NOT NULL,
- "UserId" Varchar2(30 ) NOT NULL,
- "LoginTime" Timestamp(6),
- "LastRefreshTime" Timestamp(6),
- "UserIP" Varchar2(30 ),
- "UserBrowserID" Varchar2(30 )
- )
- /
- */
- public void SessionRegister (String UpdateUser) throws SQLException{
- String SQL_Statement = null;
- error_Message = null;
- /** get user session id */
- FacesContext fCtx = FacesContext.getCurrentInstance();
- HttpSession session = (HttpSession) fCtx.getExternalContext().getSession(false);
- String sessionId = session.getId();
- /** get user IP address */
- FacesContext context = FacesContext.getCurrentInstance();
- HttpServletRequest request = (HttpServletRequest)context.getExternalContext().getRequest();
- String remoteHost = request.getRemoteAddr();
- if (ds == null) throw new SQLException( error_Database = "No data source");
- Connection conn = ds.getConnection();
- if (conn == null) throw new SQLException( error_Database = "No connection");
- try {
- conn.setAutoCommit(false);
- boolean committed = false;
- try { /* insert into Oracle the default system(Linux) time */
- SQL_Statement = "INSERT INTO ACTIVESESSIONS (SessionId, UserId, LoginTime, LastRefreshTime,"
- + "UserIP, UserBrowserID)VALUES ('" + sessionId + "', '" + UpdateUser
- + "' , current_timestamp, current_timestamp, '" + remoteHost + "', 'jjjk')";
- PreparedStatement insertQuery = conn.prepareStatement(SQL_Statement);
- insertQuery.executeUpdate();
- conn.commit();
- committed = true;
- } finally {
- if (!committed) conn.rollback();
- }
- }
- finally {
- conn.close();
- }
- return;
- }
- /** compare the user and the password */
- public String userCompare() throws NoSuchAlgorithmException, SQLException {
- String hash_passwd = null;
- String passwdQuery = null;
- /** check the password into Oracle using the username */
- passwdQuery = CheckUserDB(user);
- /** convert the plain password in SHA-256 hash */
- hash_passwd = string_hash(password);
- /** compare the encrypted passwords */
- if (password.equals(passwdQuery)){ // naro4no nekriptirani se sravnqvat
- /** insert into users session table the time when the user login */
- SessionRegister(user);
- /* success */
- String stri = CallOSGI();
- return "0";
- } else {
- /* failer */
- return "1";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment