Skip to content

Commit

Permalink
resolve merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
Roussel Xavier committed May 4, 2015
2 parents dda23b6 + b54f320 commit 16974bf
Show file tree
Hide file tree
Showing 12 changed files with 785 additions and 248 deletions.
61 changes: 35 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,65 @@
cas
CAS
===

CAS server SSO authentication in laravel 4.x
CAS server SSO authentication in Laravel 4.x & 5.x

## Installation

Require this package in your composer.json and run composer update (or run `composer require xavrsl/cas:dev-master` directly):
Require this package in your composer.json and run composer update.

"xavrsl/cas": "dev-master"
For Laravel 4 use v1.1.* :

After updating composer, add the ServiceProvider to the providers array in app/config/app.php
"xavrsl/cas": "1.1.*"

'Xavrsl\Cas\CasServiceProvider',
For Laravel 5 use v1.2.* :

As well as the Facade :
"xavrsl/cas": "1.2.*"

After updating composer, add the ServiceProvider to the providers array in app/config/app.php
```php
'Xavrsl\Cas\CasServiceProvider',
```
As well as the Facade :
```php
'Cas' => 'Xavrsl\Cas\Facades\Cas',
```
Then publish the package's config using one of those methods :

You need to publish the conf so you will ffind it in app/config/packages/xavrsl/cas/

For Laravel 4 :
```
$ php artisan config:publish xavrsl/cas
```

For Laravel 5 :
```
$ php artisan vendor:publish
```

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();
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xavrsl/cas",
"description": "Add CAS server SSO authentication to laravel 4.x",
"description": "Add CAS server SSO authentication to Laravel 4 and 5",
"keywords": ["CAS", "phpCAS", "SSO", "laravel"],
"license": "MIT",
"authors": [
Expand All @@ -11,7 +11,7 @@
],
"require": {
"php": ">=5.3.0",
"illuminate/support": "4.x",
"illuminate/support": "4.*|5.*",
"jasig/phpcas": "1.3.3"
},
"autoload": {
Expand Down
5 changes: 5 additions & 0 deletions src/Xavrsl/Cas/CasAuthenticationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php namespace Xavrsl\Cas;

class CasAuthenticationException extends \Exception{

}
67 changes: 9 additions & 58 deletions src/Xavrsl/Cas/CasManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,92 +13,43 @@ class CasManager {
*
* @var array
*/
protected $connections = array();
/**
* @var \Illuminate\Auth\AuthManager
*/
private $auth;
/**
* @var \Illuminate\Session\SessionManager
*/
private $session;
protected $connection;

/**
* @param array $config
* @param AuthManager $auth
* @param SessionManager $session
*/
function __construct(Array $config, AuthManager $auth, SessionManager $session)
function __construct(Array $config)
{
$this->config = $config;
$this->auth = $auth;
$this->session = $session;
}

/**
* Get a Cas connection instance.
*
* @param string $name
* @return Xavrsl\Cas\Directory
*/
public function connection($name = null)
public function connection()
{
if ( ! isset($this->connections[$name]))
if ( empty($this->connection))
{
$this->connections[$name] = $this->createConnection($name);
$this->connection = $this->createConnection();
}

return $this->connections[$name];
return $this->connection;
}

/**
* Create the given connection by name.
*
* @param string $name
* @return Xavrsl\Cas\Sso
*/
protected function createConnection($name)
protected function createConnection()
{
$config = $this->getConfig($name);

$connection = new Sso($config, $this->auth, $this->session);
$connection = new Sso($this->config);

return $connection;
}

/**
* Get the configuration for a connection.
*
* @param string $name
* @return array
*/
protected function getConfig($name)
{
$name = $name ?: $this->getDefaultConnection();

// To get the database connection configuration, we will just pull each of the
// connection configurations and get the configurations for the given name.
// If the configuration doesn't exist, we'll throw an exception and bail.
$connections = $this->config;

if (is_null($config = array_get($connections, $name)))
{
throw new \InvalidArgumentException("Cas [$name] not configured.");
}

return $config;
}

/**
* Get the default connection name.
*
* @return string
*/
protected function getDefaultConnection()
{
return 'default';
}

/**
* Dynamically pass methods to the default connection.
*
Expand All @@ -111,4 +62,4 @@ public function __call($method, $parameters)
return call_user_func_array(array($this->connection(), $method), $parameters);
}

}
}
30 changes: 7 additions & 23 deletions src/Xavrsl/Cas/CasServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
<?php namespace Xavrsl\Cas;

use App;
use Config;
use Illuminate\Session\SessionManager;
use Illuminate\Support\ServiceProvider;

class CasServiceProvider extends ServiceProvider {

var $session;
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

// function __construct(SessionManager $session)
// {
// $this->session = $session;
// }

/**
* Bootstrap the application events.
* Bootstrap the application.
*
* @return void
*/
public function boot()
{
$this->package('xavrsl/cas');
$this->publishes([
__DIR__.'/../../config/cas.php' => config_path('cas.php'),
]);
}

/**
Expand All @@ -39,10 +25,8 @@ public function register()
{
$this->app['cas'] = $this->app->share(function()
{
$config = Config::get('cas::config');
$auth = App::make('auth');
$session = App::make('session');
return new CasManager($config, $auth, $session);
$config = $this->app['config']->get('cas');
return new CasManager($config);
});
}

Expand All @@ -56,4 +40,4 @@ public function provides()
return array('cas');
}

}
}
Loading

0 comments on commit 16974bf

Please sign in to comment.