phpCAS specify return URL after successful authentication

89 Views Asked by At

I have successfully implemented a CAS authentication in my application with phpCAS. However, after logging in, I am always redirected back to the application's start page, even if I requested a specific URL within the application.

I have been trying to find out how to keep track of the URL to return to after login, but could not find anything.

Put simply, this is my code:

phpCAS::client(
    CAS_VERSION_2_0,
    'cas.example.host',
    443,
    'https://cas.example.host:443/login',
    'https://my-application.host'
);

$client = phpCAS::getCasClient();

$client->renewAuthentication();

Does someone know how I can store the URL to return to anywhere? Using a URL parameter would be fine, but I don't know where to set it.

1

There are 1 best solutions below

0
AeonOfTime On

As a session must already be active in the application when running the phpCAS authentication callbacks, so the return URL can simply be handled on the application level by storing it in the application's session.

Pseudo code to demonstrate the logic:

// User is not authenticated?
if(!$user->isLoggedIn())
{
    // Save the URL the user requested
    $session->set('return_url', $_SERVER['REQUEST_URI']);

    // Let phpCAS handle the auth callbacks
    $phpCAS->renewAuthentication();
}

// User is now logged in. 

$returnURL = $session->get('return_url');

// Check if a return URL is present, and redirect there as necessary
if(!empty($returnURL))
{
    $session->remove('return_url'); // Important to avoid infinite redirects
    header('Location: '.$return_url);
    exit;
}