Skip to content

Commit 87065d3

Browse files
committed
Use route parameter for idpName
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.
1 parent ccaf8f3 commit 87065d3

File tree

4 files changed

+18
-26
lines changed

4 files changed

+18
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ $metadata['http://laravel_url/myidp1/metadata'] = array(
8686

8787
### Usage
8888

89-
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.
89+
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.
9090
```php
9191
public function handle($request, Closure $next)
9292
{

src/Aacotroneo/Saml2/Http/Controllers/Saml2Controller.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,14 @@ class Saml2Controller extends Controller
1717
protected $idp;
1818

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

27+
$this->idp = $idpName;
3128
$auth = Saml2Auth::loadOneLoginAuthFromIpdConfig($this->idp);
3229
$this->saml2Auth = new Saml2Auth($auth);
3330
}

src/Aacotroneo/Saml2/Saml2Auth.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ public static function loadOneLoginAuthFromIpdConfig($idpName)
4444
$config = config('saml2.'.$idpName.'_idp_settings');
4545

4646
if (empty($config['sp']['entityId'])) {
47-
$config['sp']['entityId'] = URL::route($idpName.'_metadata');
47+
$config['sp']['entityId'] = URL::route('saml2_metadata', $idpName);
4848
}
4949
if (empty($config['sp']['assertionConsumerService']['url'])) {
50-
$config['sp']['assertionConsumerService']['url'] = URL::route($idpName.'_acs');
50+
$config['sp']['assertionConsumerService']['url'] = URL::route('saml2_acs', $idpName);
5151
}
5252
if (!empty($config['sp']['singleLogoutService']) &&
5353
empty($config['sp']['singleLogoutService']['url'])) {
54-
$config['sp']['singleLogoutService']['url'] = URL::route($idpName.'_sls');
54+
$config['sp']['singleLogoutService']['url'] = URL::route('saml2_sls', $idpName);
5555
}
5656
if (strpos($config['sp']['privateKey'], 'file://')===0) {
5757
$config['sp']['privateKey'] = $this->extractPkeyFromFile($config['sp']['privateKey']);

src/routes.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,31 @@
11
<?php
22

3-
foreach (config('saml2_settings.idpNames') as $key => $value) {
4-
5-
Route::group([
6-
'prefix' => config('saml2_settings.routesPrefix').'/'.$value,
7-
'middleware' => config('saml2_settings.routesMiddleware'),
8-
], function () use ($value) {
9-
3+
Route::middleware(config('saml2_settings.routesMiddleware'))
4+
->prefix(config('saml2_settings.routesPrefix').'/')->group(function() {
5+
Route::prefix('{idpName}')->group(function() {
106
Route::get('/logout', array(
11-
'as' => $value.'_logout',
7+
'as' => 'saml2_logout',
128
'uses' => 'Aacotroneo\Saml2\Http\Controllers\Saml2Controller@logout',
139
));
1410

1511
Route::get('/login', array(
16-
'as' => $value.'_login',
12+
'as' => 'saml2_login',
1713
'uses' => 'Aacotroneo\Saml2\Http\Controllers\Saml2Controller@login',
1814
));
1915

2016
Route::get('/metadata', array(
21-
'as' => $value.'_metadata',
17+
'as' => 'saml2_metadata',
2218
'uses' => 'Aacotroneo\Saml2\Http\Controllers\Saml2Controller@metadata',
2319
));
2420

2521
Route::post('/acs', array(
26-
'as' => $value.'_acs',
22+
'as' => 'saml2_acs',
2723
'uses' => 'Aacotroneo\Saml2\Http\Controllers\Saml2Controller@acs',
2824
));
2925

3026
Route::get('/sls', array(
31-
'as' => $value.'_sls',
27+
'as' => 'saml2_sls',
3228
'uses' => 'Aacotroneo\Saml2\Http\Controllers\Saml2Controller@sls',
3329
));
3430
});
35-
36-
}
31+
});

0 commit comments

Comments
 (0)