Search This Blog

Friday, July 19, 2019

Strong Authentication

Strong Authentication CREATE TABLE "OTSL_USERS" ( "USER_ID" NUMBER, "USER_NAME" VARCHAR2(100), "PASSWORD" VARCHAR2(4000), "CREATED_ON" DATE, "DEPARTMENT" VARCHAR2(200), "AUT_SECTION" VARCHAR2(200), "EXPIRES_ON" DATE, "ADMIN_USER" CHAR(1), "FACTORY_NAME" VARCHAR2(200), "ACCESSCODE" NUMBER(20,0), "ACTIVE" CHAR(2), "PHOTO" BLOB, "SCHOOL_NAME" VARCHAR2(200) ) ================================== CREATE OR REPLACE TRIGGER "USER_T1" BEFORE INSERT OR UPDATE ON OTSL_USERS FOR EACH ROW BEGIN :NEW.PASSWORD:=CUSTOM_HASH(UPPER(:NEW.USER_NAME),:NEW.PASSWORD); if inserting then IF :NEW.USER_ID IS NULL THEN SELECT NVL(MAX(USER_ID)+1,1) INTO :NEW.USER_ID FROM OTSL_USERS; END if; end if; END; / ALTER TRIGGER "USER_T1" ENABLE / ========================================== create or replace function custom_hash (p_username in varchar2, p_password in varchar2) return varchar2 is l_password varchar2(4000); l_salt varchar2(4000) := 'UYUKQULATJYQ2RJTWPTHHIU7K1O7FR'; begin -- This function should be wrapped, as the hash algorhythm is exposed here. -- You can change the value of l_salt or the method of which to call the -- DBMS_OBFUSCATOIN toolkit, but you much reset all of your passwords -- if you choose to do this. l_password := utl_raw.cast_to_raw(dbms_obfuscation_toolkit.md5 (input_string => p_password || substr(l_salt,10,13) || p_username || substr(l_salt, 4,10))); return l_password; end; ========================================= create or replace function custom_auth (p_username in VARCHAR2, p_password in VARCHAR2) return BOOLEAN is l_password varchar2(4000); l_stored_password varchar2(4000); l_expires_on date; t NUMBER; l_count number; begin t:=0; select count(*) into l_count from otsl_users where user_name = p_username; if l_count > 0 and t=0 then select password, expires_on into l_stored_password, l_expires_on from otsl_users where user_name = p_username; if l_expires_on > sysdate or l_expires_on is null then l_password := custom_hash(p_username, p_password); if l_password = l_stored_password then return true; else return false; end if; else return false; end if; else return false; end if; end; CREATE TABLE  "OTSL_USERS"
   ( "USER_ID" NUMBER,
"USER_NAME" VARCHAR2(100),
"PASSWORD" VARCHAR2(4000),
"CREATED_ON" DATE,
"DEPARTMENT" VARCHAR2(200),
"AUT_SECTION" VARCHAR2(200),
"EXPIRES_ON" DATE,
"ADMIN_USER" CHAR(1),
"FACTORY_NAME" VARCHAR2(200),
"ACCESSCODE" NUMBER(20,0),
"ACTIVE" CHAR(2),
"PHOTO" BLOB,
"SCHOOL_NAME" VARCHAR2(200)
   )
 
 ==================================

CREATE OR REPLACE TRIGGER  "USER_T1" BEFORE INSERT OR UPDATE ON OTSL_USERS FOR EACH ROW
BEGIN

:NEW.PASSWORD:=CUSTOM_HASH(UPPER(:NEW.USER_NAME),:NEW.PASSWORD);

if inserting then
IF :NEW.USER_ID IS NULL THEN
SELECT NVL(MAX(USER_ID)+1,1) INTO :NEW.USER_ID FROM OTSL_USERS;
END if;
end if;
END;

/
ALTER TRIGGER  "USER_T1" ENABLE
/

==========================================
create or replace function custom_hash (p_username in varchar2, p_password in varchar2)
return varchar2
is
  l_password varchar2(4000);
  l_salt varchar2(4000) := 'UYUKQULATJYQ2RJTWPTHHIU7K1O7FR';
begin

-- This function should be wrapped, as the hash algorhythm is exposed here.
-- You can change the value of l_salt or the method of which to call the
-- DBMS_OBFUSCATOIN toolkit, but you much reset all of your passwords
-- if you choose to do this.

l_password := utl_raw.cast_to_raw(dbms_obfuscation_toolkit.md5
  (input_string => p_password || substr(l_salt,10,13) || p_username ||
    substr(l_salt, 4,10)));
return l_password;
end;
=========================================
create or replace function custom_auth (p_username in VARCHAR2, p_password in VARCHAR2)
return BOOLEAN
is
  l_password varchar2(4000);
  l_stored_password varchar2(4000);
  l_expires_on date;
   t NUMBER;
  l_count number;
begin
t:=0;
select count(*) into l_count from otsl_users where user_name = p_username;
if l_count > 0 and t=0 then
  select password, expires_on into l_stored_password, l_expires_on
   from otsl_users where user_name = p_username;
  if l_expires_on > sysdate or l_expires_on is null then
    l_password := custom_hash(p_username, p_password);
    if l_password = l_stored_password then
      return true;
    else
      return false;
    end if;
  else
    return false;
end if;
else
  return false;
end if;
end;

No comments:

Post a Comment

Restrict File Upload by File Type in Oracle Apex

If you want to restrict file upload by file type/extension/format you can follow the below steps.  Goto File Browser Item --> Advanced --...