Skip to content

Commit

Permalink
Add cas_pretend_user variable
Browse files Browse the repository at this point in the history
  • Loading branch information
Roussel Xavier committed May 4, 2015
1 parent 9328edd commit b54f320
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 19 deletions.
31 changes: 13 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CAS server SSO authentication in Laravel 4.x & 5.x

## Installation

Require this package in your composer.json and run composer update.
Require this package in your composer.json and run composer update.

For Laravel 4 use v1.1.* :

Expand Down Expand Up @@ -38,33 +38,28 @@ For Laravel 5 :
Configuration
==

Configuration should be pretty straightforward for anyone who's ever used the phpCAS client. However, I've added the possibility to easily turn your application into a CAS Proxy, a CAS Service or both. You only need to set the cas_proxy setting to true (if you need to proxy services) and set the cas_service to whatever proxy you want to allow (this is all explained in the config file).
Configuration should be pretty straightforward for anyone who's ever used the phpCAS client. Using the .env file will allow you to have different environments without even touching the cas.php config file. I've added the possibility to easily turn your application into a CAS Proxy, a CAS Service or both. You only need to set the cas_proxy setting to true (if you need to proxy services) and set the cas_service to whatever proxy you want to allow (this is all explained in the config file).

A new config variable (cas_pretend_user) available in the 1.2 release allows you to pretend to be a selected CAS user. The idea came with the usage of laravel homestead. My application was running on a private network, on a fake domain. The CAS server was not able to redirect to that application. So activating the CAS plugin on that application was not possible, but I needed a user id to query my LDAP and allow/disallow the user in my application. You only need to give it a user id and the application will act just as if you ware logged in with that CAS user.

Usage
==

Authenticate against the CAS server
Authenticate against the CAS server. This should be called before trying to retrieve the CAS user id.

```php
Cas::authenticate();
```

Exemple of Cas authentication in a route filter :
Then get the current user id this way :

```php
Route::group(array('https', 'before' => 'cas'), function()
{
Route::controller('toolbar', 'ToolbarController');

Route::controller('bibsearch', 'BibsearchController');
});
Cas::getCurrentUser();
```

Route::controller('bibimages', 'BibimagesController');
OR

Route::filter('cas', function()
{
Cas::authenticate();
});
```php
Cas::user();
```

Then get the current user id this way :

Cas::getCurrentUser();
30 changes: 29 additions & 1 deletion src/Xavrsl/Cas/Sso.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,30 @@ private function configureSslValidation()
*/
private function configureProxyChain()
{
if (is_array($this->config['cas_proxied_services']) && !empty($this->config['cas_proxied_services']))
if (is_array($this->config['cas_proxied_services'])
&& !empty($this->config['cas_proxied_services']))
{
phpCAS::allowProxyChain(new \CAS_ProxyChain($this->config['cas_proxied_services']));
}
}

/**
* isPretending
*
* When on dev environment, you can sometimes be on a private network that can't access to the CAS
* server. Sometimes, you may also want to check the application as if you where one user or
* another. This is why you may specify a CAS_PRETEND_USER config variable.
*/
private function isPretending()
{
if (isset($this->config['cas_pretend_user'])
&& !empty($this->config['cas_pretend_user']))
{
return true;
}
return false;
}

/**
* Authenticates the user based on the current request.
*
Expand All @@ -121,6 +139,8 @@ private function configureProxyChain()
*/
public function authenticate()
{
if($this->isPretending()) return true;

try
{
phpCAS::forceAuthentication();
Expand All @@ -138,6 +158,8 @@ public function authenticate()
*/
public function isAuthenticated()
{
if($this->isPretending()) return true;

return phpCAS::isAuthenticated();
}

Expand All @@ -151,6 +173,8 @@ public function isAuthenticated()
*/
public function getCurrentUser()
{
if($this->isPretending()) return $this->config['cas_pretend_user'];

return phpCAS::getUser();
}

Expand All @@ -161,6 +185,8 @@ public function getCurrentUser()
*/
public function user()
{
if($this->isPretending()) return $this->config['cas_pretend_user'];

return phpCAS::getUser();
}

Expand All @@ -183,6 +209,8 @@ public function getAttributes()
*/
public function logout($params = array())
{
if($this->isPretending()) return true;

if(!phpCAS::isAuthenticated())
{
$this->initializeCas();
Expand Down

0 comments on commit b54f320

Please sign in to comment.