#!/usr/bin/perl
#
# PROGRAM: resumepath.pl
#
# PURPOSE: This script checks the request for cookies and environment variables and redirects customer
# back to the federation environment with good session
# CREATED: June 30, 2015 by Mitchell Lewars
# Thanks for help to Björn Vildljung
# Lots of examples used from Perl Monks web site
use CGI qw(:standard);
#use warnings;
$query = new CGI;
$perror = 0; #//Set to 1 in case of an error.
$wearelooping = 0; #//Set to one if a user returns withing 15 seconds, indicating a redirect-loop.
$redirectURL = "https://federate-qa.localhost.com";
#---- Next get the current values
$gotcookies = $ENV{"HTTP_COOKIE"};
$env = $query->param('env');
$resumepath = $query->param('resumePath');
#// Check for the env= entry in the URL. If it is set to prod, use prod federation, else use QA.
if ( $env =~ 'prod') { $redirectURL = "https://federate.localhost.com"}
#// Check that the PF-session information is passed as expected, if not, we got an error. Otherwise, add it to redirectURL
if ( $resumepath ) { $redirectURL .= $resumepath }
else { $perror=1 }
# Verify that there is an SMSESSION, otherwise we got somebody accessing us the wrong way, and therefor probably for the wrong reasons. ERROR!
#$perror = 1; # if customer has no SMSESSION they reached this page in error
if (!( $gotcookies =~ /smsession/i)) { $perror = 1}
# Check if there is already an SMPF, if so we are looping
if ( $gotcookies =~ /smpf/i) { $wearelooping = 1}
#// Check to see if a Cookie named SMPF is avalible. If not, we set it and give it a 15 second lifetime. If it is there, we got a redirect loop.
if (($wearelooping eq 0) && ($perror eq 0)) {
$cookie = $query->cookie(-name=>'SMPF',
-value=>'1',
-expires=>'+15s',
-path=>'/');
print $query->redirect(
-cookie => $cookie,
-uri => "$redirectURL");
print $query->start_html(
-title=>'Login');
print $query->end_html;
}
else {
$cookie = $query->cookie(-name=>'SMPF',
-value=>'',
-expires=>'now',
-path=>'/');
print $query->header(-cookie=>$cookie);
print $query->meta('');
print $query->start_html('Login');
print $query->body("Redirect loop!
You have been assigned a SESSION-cookie, as confirmation that you have successfully logged in. For some reason the login-servers who needs this cookie is not getting it from your browser, causing a loop of redirection. Please try to go back to the site you want to login to and try again. You should not need to enter your credentials again. If this error is reoccurring for you, try using a different browser.");
print $query->end_html;
}