Skip to content

Commit 35f6417

Browse files
authored
Merge pull request #195 from danmichaelo/patch-1
Use dependency injection in controller
2 parents 55d36cf + 7587a93 commit 35f6417

File tree

3 files changed

+41
-45
lines changed

3 files changed

+41
-45
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
}
1717
],
1818
"require": {
19-
"php": ">=5.4.0",
19+
"php": ">=5.5.0",
2020
"ext-openssl": "*",
2121
"illuminate/support": ">=5.0.0",
2222
"onelogin/php-saml": "^3.0.0"

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

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,45 @@
66
use Aacotroneo\Saml2\Saml2Auth;
77
use Illuminate\Routing\Controller;
88
use Illuminate\Http\Request;
9-
use OneLogin\Saml2\Auth as OneLogin_Saml2_Auth;
10-
use URL;
119

1210
class Saml2Controller extends Controller
1311
{
14-
15-
protected $saml2Auth;
16-
17-
protected $idp;
18-
1912
/**
20-
*/
21-
function __construct(){
22-
$idpName = request()->route('idpName');
23-
if (!in_array($idpName, config('saml2_settings.idpNames'))) {
24-
abort(404);
25-
}
26-
27-
$this->idp = $idpName;
28-
$auth = Saml2Auth::loadOneLoginAuthFromIpdConfig($this->idp);
29-
$this->saml2Auth = new Saml2Auth($auth);
30-
}
31-
32-
/**
33-
* Generate local sp metadata
13+
* Generate local sp metadata.
14+
*
15+
* @param Saml2Auth $saml2Auth
3416
* @return \Illuminate\Http\Response
3517
*/
36-
public function metadata()
18+
public function metadata(Saml2Auth $saml2Auth)
3719
{
38-
39-
$metadata = $this->saml2Auth->getMetadata();
20+
$metadata = $saml2Auth->getMetadata();
4021

4122
return response($metadata, 200, ['Content-Type' => 'text/xml']);
4223
}
4324

4425
/**
4526
* Process an incoming saml2 assertion request.
46-
* Fires 'Saml2LoginEvent' event if a valid user is Found
27+
* Fires 'Saml2LoginEvent' event if a valid user is found.
28+
*
29+
* @param Saml2Auth $saml2Auth
30+
* @param $idpName
31+
* @return \Illuminate\Http\Response
4732
*/
48-
public function acs()
33+
public function acs(Saml2Auth $saml2Auth, $idpName)
4934
{
50-
$errors = $this->saml2Auth->acs();
35+
$errors = $saml2Auth->acs();
5136

5237
if (!empty($errors)) {
53-
logger()->error('Saml2 error_detail', ['error' => $this->saml2Auth->getLastErrorReason()]);
54-
session()->flash('saml2_error_detail', [$this->saml2Auth->getLastErrorReason()]);
38+
logger()->error('Saml2 error_detail', ['error' => $saml2Auth->getLastErrorReason()]);
39+
session()->flash('saml2_error_detail', [$saml2Auth->getLastErrorReason()]);
5540

5641
logger()->error('Saml2 error', $errors);
5742
session()->flash('saml2_error', $errors);
5843
return redirect(config('saml2_settings.errorRoute'));
5944
}
60-
$user = $this->saml2Auth->getSaml2User();
45+
$user = $saml2Auth->getSaml2User();
6146

62-
event(new Saml2LoginEvent($this->idp, $user, $this->saml2Auth));
47+
event(new Saml2LoginEvent($idpName, $user, $saml2Auth));
6348

6449
$redirectUrl = $user->getIntendedUrl();
6550

@@ -74,11 +59,15 @@ public function acs()
7459
/**
7560
* Process an incoming saml2 logout request.
7661
* Fires 'Saml2LogoutEvent' event if its valid.
77-
* This means the user logged out of the SSO infrastructure, you 'should' log him out locally too.
62+
* This means the user logged out of the SSO infrastructure, you 'should' log them out locally too.
63+
*
64+
* @param Saml2Auth $saml2Auth
65+
* @param $idpName
66+
* @return \Illuminate\Http\Response
7867
*/
79-
public function sls()
68+
public function sls(Saml2Auth $saml2Auth, $idpName)
8069
{
81-
$errors = $this->saml2Auth->sls($this->idp, config('saml2_settings.retrieveParametersFromServer'));
70+
$errors = $saml2Auth->sls($idpName, config('saml2_settings.retrieveParametersFromServer'));
8271
if (!empty($errors)) {
8372
logger()->error('Saml2 error', $errors);
8473
session()->flash('saml2_error', $errors);
@@ -89,23 +78,27 @@ public function sls()
8978
}
9079

9180
/**
92-
* This initiates a logout request across all the SSO infrastructure.
81+
* Initiate a logout request across all the SSO infrastructure.
82+
*
83+
* @param Saml2Auth $saml2Auth
84+
* @param Request $request
9385
*/
94-
public function logout(Request $request)
86+
public function logout(Saml2Auth $saml2Auth, Request $request)
9587
{
9688
$returnTo = $request->query('returnTo');
9789
$sessionIndex = $request->query('sessionIndex');
9890
$nameId = $request->query('nameId');
99-
$this->saml2Auth->logout($returnTo, $nameId, $sessionIndex); //will actually end up in the sls endpoint
91+
$saml2Auth->logout($returnTo, $nameId, $sessionIndex); //will actually end up in the sls endpoint
10092
//does not return
10193
}
10294

103-
10495
/**
105-
* This initiates a login request
96+
* Initiate a login request.
97+
*
98+
* @param Saml2Auth $saml2Auth
10699
*/
107-
public function login()
100+
public function login(Saml2Auth $saml2Auth)
108101
{
109-
$this->saml2Auth->login(config('saml2_settings.loginRoute'));
102+
$saml2Auth->login(config('saml2_settings.loginRoute'));
110103
}
111104
}

src/Aacotroneo/Saml2/Saml2ServiceProvider.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22
namespace Aacotroneo\Saml2;
33

4-
use OneLogin\Saml2\Auth as OneLogin_Saml2_Auth;
54
use OneLogin\Saml2\Utils as OneLogin_Saml2_Utils;
6-
use URL;
75
use Illuminate\Support\ServiceProvider;
86

97
class Saml2ServiceProvider extends ServiceProvider
@@ -44,6 +42,11 @@ public function boot()
4442
*/
4543
public function register()
4644
{
45+
$this->app->singleton(Saml2Auth::class, function ($app) {
46+
$idpName = $app->request->route('idpName');
47+
$auth = Saml2Auth::loadOneLoginAuthFromIpdConfig($idpName);
48+
return new Saml2Auth($auth);
49+
});
4750
}
4851

4952
/**
@@ -53,7 +56,7 @@ public function register()
5356
*/
5457
public function provides()
5558
{
56-
return array();
59+
return [Saml2Auth::class];
5760
}
5861

5962
}

0 commit comments

Comments
 (0)