Skip to content

Commit 34fe227

Browse files
committed
Make it easy to subclass Saml2Controller
It is normal to want to extend Saml2Controller, notably to pass your app's own "redirect URL" to the Saml2Auth login() call, so that the RelayState will be set accordingly. This commit makes it easy to do so by adding an optional config value that lets you specify the controller. By default, with no value provided, the routes will be configured as before, with the default Saml2Controller. Updates the Readme with example of how to do it.
1 parent 87065d3 commit 34fe227

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,27 @@ protected function unauthenticated($request, AuthenticationException $exception)
121121
}
122122
```
123123

124-
The $saml2Controller->login('/my/redirect/path') will redirect the user to the IDP and will came back to an endpoint the library serves at /myidp1/acs (or routesPrefix/myidp1/acs). That will process the response and fire an event when ready. The next step for you is to handle that event. You just need to login the user or refuse.
124+
For login requests that come through redirects to the login route, 'routesPrefix/myidp1/login', the default login call does not pass a redirect URL to the Saml login request. That login argument is useful because the ACS handler can gets that value (passed back from the IDP as RelayPath) and by default will redirect there. To pass the redirect URL from the controller login, extend the Saml2Controller class and implement your own `login()` function. Set the saml2_settings value `saml2_controller` to be your extended class so that the routes will direct requests to your controller instead of the default.
125+
E.g.
126+
**saml_settings.php**
127+
```
128+
'saml2_controller' => 'App\Http\Controllers\MyNamespace\MySaml2Controller'
129+
```
130+
**MySaml2Controller.php**
131+
```php
132+
use Aacotroneo\Saml2\Http\Controllers\Saml2Controller;
133+
134+
class MySaml2Controller extends Saml2Controller
135+
{
136+
public function login()
137+
{
138+
$loginRedirect = '...'; // Determine redirect URL
139+
$this->saml2Auth->login($loginRedirect);
140+
}
141+
}
142+
```
143+
144+
After login is called, the user will be redirected to the IDP login page. Then the IDP, which you have configured with an endpoint the library serves, will call back, e.g. `/myidp1/acs` or `/routesPrefix/myidp1/acs`. That will process the response and fire an event when ready. The next step for you is to handle that event. You just need to login the user or refuse.
125145

126146
```php
127147

src/config/saml2_settings.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,12 @@
6060
// SSL.
6161
'proxyVars' => false,
6262

63+
/**
64+
* (Optiona) Which class implements the route functions.
65+
* If left blank, defaults to this lib's controller (Aacotroneo\Saml2\Http\Controllers\Saml2Controller).
66+
* If you need to extend Saml2Controller (e.g. to override the `login()` function to pass
67+
* a `$returnTo` argument), this value allows you to pass your own controller, and have
68+
* it used in the routes definition.
69+
*/
70+
'saml2_controller' => '',
6371
);

src/routes.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,31 @@
33
Route::middleware(config('saml2_settings.routesMiddleware'))
44
->prefix(config('saml2_settings.routesPrefix').'/')->group(function() {
55
Route::prefix('{idpName}')->group(function() {
6+
$saml2_controller = config('saml2_settings.saml2_controller', 'Aacotroneo\Saml2\Http\Controllers\Saml2Controller');
7+
68
Route::get('/logout', array(
79
'as' => 'saml2_logout',
8-
'uses' => 'Aacotroneo\Saml2\Http\Controllers\Saml2Controller@logout',
10+
'uses' => $saml2_controller.'@logout',
911
));
1012

1113
Route::get('/login', array(
1214
'as' => 'saml2_login',
13-
'uses' => 'Aacotroneo\Saml2\Http\Controllers\Saml2Controller@login',
15+
'uses' => $saml2_controller.'@login',
1416
));
1517

1618
Route::get('/metadata', array(
1719
'as' => 'saml2_metadata',
18-
'uses' => 'Aacotroneo\Saml2\Http\Controllers\Saml2Controller@metadata',
20+
'uses' => $saml2_controller.'@metadata',
1921
));
2022

2123
Route::post('/acs', array(
2224
'as' => 'saml2_acs',
23-
'uses' => 'Aacotroneo\Saml2\Http\Controllers\Saml2Controller@acs',
25+
'uses' => $saml2_controller.'@acs',
2426
));
2527

2628
Route::get('/sls', array(
2729
'as' => 'saml2_sls',
28-
'uses' => 'Aacotroneo\Saml2\Http\Controllers\Saml2Controller@sls',
30+
'uses' => $saml2_controller.'@sls',
2931
));
3032
});
3133
});

0 commit comments

Comments
 (0)