I would like to share you how to integrate phpCAS in Laravel. As you know, Jasig CAS is a Single-Sign-On System. If you want to connect to Jasig CAS from your system (that is wrote by PHP), you use phpCAS Library
To know how to install a Jasig CAS server, please see: https://sonnguyen.ws/install-jasig-cas-ubuntu-14-04/
Step 1: Download phpCAS Library and locate it in app/library directory
1 2 |
cd app/library git clone https://github.com/Jasig/phpCAS.git |
Step 2: Create CAS Authentication Provider
We create file CasAuthProvider.php
1 2 3 |
mkdir app/cas cd app/cas vi CasAuthProvider.php |
Add source code to file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
<?php namespace cas; use Illuminate\Auth\GenericUser; use Illuminate\Auth\UserInterface; use Illuminate\Auth\UserProviderInterface; class CasAuthProvider implements UserProviderInterface { /** * Retrieve a user by their unique identifier. * * @param mixed $id * @return \Illuminate\Auth\UserInterface|null */ public function retrieveById($id) { return $this->casUser(); } /** * Retrieve a user by the given credentials. * * @param array $credentials * @return \Illuminate\Auth\UserInterface|null */ public function retrieveByCredentials(array $credentials) { return $this->casUser(); } /** * Validate a user against the given credentials. * * @param \Illuminate\Auth\UserInterface $user * @param array $credentials * @return bool */ public function validateCredentials(UserInterface $user, array $credentials) { return true; } protected function casUser() { $cas_host = \Config::get('app.cas_host'); $cas_context = \Config::get('app.cas_context'); $cas_port = \Config::get('app.cas_port'); \phpCAS::setDebug(); \phpCAS::client(CAS_VERSION_2_0, $cas_host, $cas_port, $cas_context); \phpCAS::setNoCasServerValidation(); if (\phpCAS::isAuthenticated()) { $attributes = array( 'id' => \phpCAS::getUser(), 'name' => \phpCAS::getUser() ); return new GenericUser($attributes); } else { \phpCAS::setServerURL(\Config::get('app.url')); \phpCAS::forceAuthentication(); } return null; } /** * Needed by Laravel 4.1.26 and above */ public function retrieveByToken($identifier, $token) { return new \Exception('not implemented'); } /** * Needed by Laravel 4.1.26 and above */ public function updateRememberToken(UserInterface $user, $token) { return new \Exception('not implemented'); } } ?> |
Please make note that, you have to add cas_host, cas_context, cas_port to app/config/app.php file
Step 3: Adding new provider to Laravel
Edit app/start/global.php file by adding code:
1 2 3 4 |
Auth::extend('cas', function($app) { return new cas\CasAuthProvider; }); |
Step 4: Adding phpCAS library, new provider to Laravel Autoload
Open composer.json located at Laravel Root Directory and add app/library/phpCAS, app/cas:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
"autoload": { "classmap": [ "app/commands", "app/controllers", "app/models", "app/library", "app/library/phpCAS", "app/cas", "app/database/migrations", "app/database/seeds", "app/tests/TestCase.php" ] }, |
Dump new Laravel Autoload file:
1 |
php artisan dump-autoload |
Step 5: Finally, create route and controller for login action
In app/routes.php file add:
1 2 |
In app/controllers/Admin.php add:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
function login() { $message_error = ""; if(Auth::check()) // Redirect to link after login else { if (Auth::attempt(array())) { // Redirect to link after login } // Redirect to un-logged in page } } |