Pages

Friday, June 4, 2021

How to add CAS authentication to a PHP website ?

The main purpose of this article is to explain what you have to do in order to activate the CAS authentication. 

We will cover all steps needed to begin quickly with CAS !

My configuration :

  • Debian 9 Stretch
  • PHP 7.0
  • jasig/phpcas 1.4

Installation

You need the following Debian package :

apt-get install wget php-cli php-zip unzip

apt-get install php7.0-curl

Then, you have to install composer :

cd /root

wget -O composer-setup.php https://getcomposer.org/installer

php composer-setup.php --install-dir=/usr/local/bin --filename=composer

The next step is to add  the file composer.json with the referencies of phpcas:

{

    "require": {

        "jasig/phpcas": "^1.4"

    }

}

The composer update command retreives the phpcas library in your project (in vendor directory).So you need to execute the following :

cd /var/www/html/myproject

composer update

If you use git, don't fortget to add these lines in your .gitignore file :

vendor

composer.lock


How to use it in your PHP file ?

Here is a sample :

require __DIR__ . '/vendor/autoload.php';


// Enable debugging
phpCAS::setLogger();
// Enable verbose error messages. Disable in production!
phpCAS::setVerbose(true);
//phpCAS::setDebug("./err.log");

// initialize phpCAS
phpCAS::client(CAS_VERSION_2_0,'monserveur_cas.toto.fr',443,'/cas',FALSE);

// force CAS authentication
//phpCAS::setCasServerCACert($cas_server_ca_cert_path);
phpCAS::setNoCasServerValidation();
phpCAS::forceAuthentication();

// at this step, the user has been authenticated by the CAS server
// and the user's login name can be read with phpCAS::getUser().

if (!phpCAS::checkAuthentication()) {
die("Echec !");
}
$login = phpCAS::getUser();
print_r(phpCAS::getAttributes());
echo "===> LOGIN : $login";

No comments:

Post a Comment

Débutez en crypto-monnaie de manière ludique avec les projets "Move to earn"

Les projets "Move to earn" sont des projets très en vogue en ce moment dans le monde de la crypto-monnaie. Le principe est simple ...