iSeries EGL

All things EGL for the iSeries / i5 / Power System

Archive for the ‘EGL Date Math’ Category

Calculate an Interval Between TimeStamps

leave a comment »

Introduction

This post shows how to use Date, Time, Intervals, and Timestamps to determine if the EGL application has been launched within the past five minutes using only the hours, minutes and seconds provided as an encoded parameter on the URL. Using the HHMMSS, a full timestamp object is created containing the current date. Using an Interval, a determination can be made about how far in the past the timestamp lies as compared to a timestamp that represents the current date and time.

 

Some Background

Without access to LDAP services that can be used to authenticate a user, alternate methods are employed to prevent users from bookmarking a web page that can be used later. The scheme chosen for this purpose relies upon encoding of the time at which the menu option was selected to launch the application. At that point the time (HHMMSS) is encrypted, encoded as a parameter and integrated within the URL string that directs the browser to the web page.

When the EGL application starts it obtains the full URL string (using document.location), isolates the parameter representing the HHMMSS, decrypts it and then performs the EGL date calculations presented below to determine if this is a “fresh” invocation of the application as opposed to a “stale” request as a consequence of using a bookmark.

 

Validate the Parameters

The task of isolating the parameters from the URL has been performed. The parameters are passed in an array and it is the second parameter that contains the HHMMSS that is the focus of the function presented below.

The first task to perform is to decrypt the parameters. This is performed using a PCML based call to the RPG program residing on the local iSeries. The HHMMSS is then decrypted from “TVWXTY” into a human readable “145617”. Now, EGL can use this as the basis for a timestamp object.

    //***************************************************************
    // Validate the parms in the string array.
    //  o The first parm is the encoding method key
    //  o The second value is the time expressed as hhmmss
    //  o The third value is the user id
    //

    private function validateParms(parms string[] in) returns(boolean)

        isValid boolean = false;
        
        PCode string = parms[1];
        hhmmssEncrypted string = parms[2];
        userIDEncrypted string = parms[3];

        hhmmss string = "";
        userId string = "";

        //**************************************************
        //Instantiate the Java encryption class with a 
        //connection to the Production iSeries to decrypt
        //the time and user id.
        //**************************************************        
        
        //*WARNING* THE MACHINE NAME, USER ID and PASSWORD SHOULD BE EXTERNALIZED TO A CONFIG FILE.
        //IN ADDITION LOGIC NEEDS TO DETERMINE IF AND WHEN THIS APPLICATION IS RUNNING ON EITHER
        //THE TOMCAT TEST SERVER OR THE TOMCAT PRODUCTION SERVER SO THAT THE CORRECT CONNECTION 
        //INFORMATION IS USED.  
        ncryp01s1 CallNCRYP01S1 = new CallNCRYP01S1("MachId", "UserId", "Password");
        
        //Decrypt the time
        hhmmss = ncryp01s1.decryptValue(hhmmssEncrypted, PCode);
        
        //Decrypt the user id
        userId = ncryp01s1.decryptValue(userIDEncrypted, PCode);
        
        
        //**************************************************
        //Determine the number of minutes that have passed 
        //between 5 minutes ago and now.
        //**************************************************
        urlTime time = DateTimeLib.timeValue(hhmmss);
        urlTimeStamp timeStamp = DateTimeLib.timestampFrom(DateTimeLib.currentDate(), urlTime);
       
        currentTime timeStamp = DateTimeLib.currentTime();
        minutesInterval interval("mm") = "05";
        timeFiveMinutesAgo timeStamp = currentTime - minutesInterval;
        
        //Create an interval expressed in minutes by subtracting time as of 5 minutes ago from the
        //time given on the URL.
        differenceInMinutes interval("mm") = urlTimeStamp - timeFiveMinutesAgo;
        if(differenceInMinutes <= 5)
        	isValid = true;
        else
        	isValid = false;
        end
        
        return(isValid);
    end

Fig.1 – Source code for the validate function

 

This section of the function decrypts the parameters passed on the URL. The HHMMSS to be decrypted is contained in the variable named ‘hhmmssEncrypted’.

        //*WARNING* THE MACHINE NAME, USER ID and PASSWORD SHOULD BE EXTERNALIZED TO A CONFIG FILE.
        //IN ADDITION LOGIC NEEDS TO DETERMINE IF AND WHEN THIS APPLICATION IS RUNNING ON EITHER
        //THE TOMCAT TEST SERVER OR THE TOMCAT PRODUCTION SERVER SO THAT THE CORRECT CONNECTION 
        //INFORMATION IS USED.  
        ncryp01s1 CallNCRYP01S1 = new CallNCRYP01S1("MachId", "UserId", "Password");
        
        //Decrypt the time
        hhmmss = ncryp01s1.decryptValue(hhmmssEncrypted, PCode);
        
        //Decrypt the user id
        userId = ncryp01s1.decryptValue(userIDEncrypted, PCode);

Fig.2 – Decrypting the URL parameters.

 

 

Now that the time parameter has been decrypted, the first thing to do is to create a timestamp. To do this the time in HHMMSS is used to create a time object which is then used as the basis for a fully formed timestamp.

Next, another timestamp is created, based upon the current time. An interval object is created representing 5 minutes which is subtracted from the current time giving a new timestamp as of 5 minutes ago

The EGL code used to do this is isolated below.

        //**************************************************
        //Determine the number of minutes that have passed 
        //between 5 minutes ago and now.
        //**************************************************
        urlTime time = DateTimeLib.timeValue(hhmmss);
        urlTimeStamp timeStamp = DateTimeLib.timestampFrom(DateTimeLib.currentDate(), urlTime);
       
        currentTime timeStamp = DateTimeLib.currentTime();
        minutesInterval interval("mm") = "05";
        timeFiveMinutesAgo timeStamp = currentTime - minutesInterval;

Fig.3 – Creating urlTime from the decrypted HHMMSS.

 

 
 

This section of the function determines if the difference between the time passed on the URL is within the last 5 minutes of the present time. It does this by determining the difference between the timestamp of the HHMMSS on the URL and the timestamp for 5 minutes ago. The answer is placed into another interval of minutes called ‘differenceInMinutes’.

A test is made to see if that difference is within 5 minutes which is the range allowed to declare a URL request as “fresh”. The boolean for true is returned to the RichUI Handler’s start function which then allows processing to continue, i.e. the user did not use a bookmark to launch the application, they used the proper menu option on an internally controlled web page that is the launch point for all applications.

        //Create an interval expressed in minutes by subtracting time as of 5 minutes ago from the
        //time given on the URL.
        differenceInMinutes interval("mm") = urlTimeStamp - timeFiveMinutesAgo;
        if(differenceInMinutes <= 5)
        	isValid = true;
        else
        	isValid = false;
        end
        
        return(isValid);

Fig.4 – Determining the time elapsed.

 

 

The values of each variable derived in the code above are shown in this variables list as seen in the IDE.

The values of the variables at end of execution. Click to enlarge.

 

The values shown above where taken during testing of the function. Astute observers will note a very stale time has been supplied. Needles to say, the boolean returned is ‘false’. Also, the weakness of the scheme is also revealed in that a bookmarked URL will always be determined to be within 5 minutes of the current time for each day.

This scheme prevents launching of applications from bookmarks.
 

Advertisements

Written by iseriesadmin

August 22, 2012 at 2:52 pm

Posted in EGL Date Math

Tagged with