Skip to content

Commit

Permalink
Use route parameter for idpName
Browse files Browse the repository at this point in the history
From PR feedback, use routeParam to get the idpName.
Removes need for hardcoded list of routes built by for-loop,
and in controller ctor, removes need for (awkward) request path
parsing.
Instead, just abort(404) if the `$idpName` is not in the configured list

Tested, works with and without routesPrefix defined.
  • Loading branch information
darynmitchell committed Jul 25, 2019
1 parent ccaf8f3 commit 87065d3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ $metadata['http://laravel_url/myidp1/metadata'] = array(

### Usage

When you want your user to login, just redirect to the login route configured for the particular IDP, `route('myIdp1_login')`. You can also instantiate a `Saml2Auth` for the desired IDP using the `Saml2Auth::loadOneLoginAuthFromIpdConfig()` function to load the config and construct the OneLogin auth argment; just remember that it does not use any session storage, so if you ask it to login it will redirect to the IDP whether the user is already logged in or not. For example, you can change your authentication middleware.
When you want your user to login, just redirect to the login route configured for the particular IDP, `route('saml2_login', 'myIdp1')`. You can also instantiate a `Saml2Auth` for the desired IDP using the `Saml2Auth::loadOneLoginAuthFromIpdConfig('myIdp1')` function to load the config and construct the OneLogin auth argment; just remember that it does not use any session storage, so if you ask it to login it will redirect to the IDP whether the user is already logged in or not. For example, you can change your authentication middleware.
```php
public function handle($request, Closure $next)
{
Expand Down
13 changes: 5 additions & 8 deletions src/Aacotroneo/Saml2/Http/Controllers/Saml2Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@ class Saml2Controller extends Controller
protected $idp;

/**
* @param Saml2Auth $saml2Auth injected.
*/
function __construct($idpName){
if (empty($idpName)) {
// Get IDP name from path. IdP name is *2nd-to-last* item in path, whether
// using routesPrefix ("routesPrefix/idpName/page") or no routesPrefix ("idpName/page")
$pathSegments = request()->segments();
$idpName = $pathSegments[count($pathSegments)-2];
function __construct(){
$idpName = request()->route('idpName');
if (!in_array($idpName, config('saml2_settings.idpNames'))) {
abort(404);
}
$this->idp = $idpName ?: 'test';

$this->idp = $idpName;
$auth = Saml2Auth::loadOneLoginAuthFromIpdConfig($this->idp);
$this->saml2Auth = new Saml2Auth($auth);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Aacotroneo/Saml2/Saml2Auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ public static function loadOneLoginAuthFromIpdConfig($idpName)
$config = config('saml2.'.$idpName.'_idp_settings');

if (empty($config['sp']['entityId'])) {
$config['sp']['entityId'] = URL::route($idpName.'_metadata');
$config['sp']['entityId'] = URL::route('saml2_metadata', $idpName);
}
if (empty($config['sp']['assertionConsumerService']['url'])) {
$config['sp']['assertionConsumerService']['url'] = URL::route($idpName.'_acs');
$config['sp']['assertionConsumerService']['url'] = URL::route('saml2_acs', $idpName);
}
if (!empty($config['sp']['singleLogoutService']) &&
empty($config['sp']['singleLogoutService']['url'])) {
$config['sp']['singleLogoutService']['url'] = URL::route($idpName.'_sls');
$config['sp']['singleLogoutService']['url'] = URL::route('saml2_sls', $idpName);
}
if (strpos($config['sp']['privateKey'], 'file://')===0) {
$config['sp']['privateKey'] = $this->extractPkeyFromFile($config['sp']['privateKey']);
Expand Down
23 changes: 9 additions & 14 deletions src/routes.php
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
<?php

foreach (config('saml2_settings.idpNames') as $key => $value) {

Route::group([
'prefix' => config('saml2_settings.routesPrefix').'/'.$value,
'middleware' => config('saml2_settings.routesMiddleware'),
], function () use ($value) {

Route::middleware(config('saml2_settings.routesMiddleware'))
->prefix(config('saml2_settings.routesPrefix').'/')->group(function() {
Route::prefix('{idpName}')->group(function() {
Route::get('/logout', array(
'as' => $value.'_logout',
'as' => 'saml2_logout',
'uses' => 'Aacotroneo\Saml2\Http\Controllers\Saml2Controller@logout',
));

Route::get('/login', array(
'as' => $value.'_login',
'as' => 'saml2_login',
'uses' => 'Aacotroneo\Saml2\Http\Controllers\Saml2Controller@login',
));

Route::get('/metadata', array(
'as' => $value.'_metadata',
'as' => 'saml2_metadata',
'uses' => 'Aacotroneo\Saml2\Http\Controllers\Saml2Controller@metadata',
));

Route::post('/acs', array(
'as' => $value.'_acs',
'as' => 'saml2_acs',
'uses' => 'Aacotroneo\Saml2\Http\Controllers\Saml2Controller@acs',
));

Route::get('/sls', array(
'as' => $value.'_sls',
'as' => 'saml2_sls',
'uses' => 'Aacotroneo\Saml2\Http\Controllers\Saml2Controller@sls',
));
});

}
});

0 comments on commit 87065d3

Please sign in to comment.