Skip to content

Commit 5c190a8

Browse files
committed
save and migrate to L5
1 parent 569587f commit 5c190a8

File tree

7 files changed

+227
-67
lines changed

7 files changed

+227
-67
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aacotroneo/saml2",
3-
"description": "A laravel package for saml2 integration as a SP (service provider) based on OneLogin toolkit, which is much lightweight than simplesamlphp",
3+
"description": "A Laravel package for Saml2 integration as a SP (service provider) based on OneLogin toolkit, which is much lightweight than simplesamlphp",
44
"authors": [
55
{
66
"name": "aacotroneo",
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
namespace Aacotroneo\Saml2\Facades;
3+
4+
use Illuminate\Support\Facades\Facade;
5+
6+
class Saml2Auth extends Facade{
7+
8+
/**
9+
* Get the registered name of the component.
10+
*
11+
* @return string
12+
*/
13+
protected static function getFacadeAccessor() { return 'saml2auth'; }
14+
15+
}

src/Aacotroneo/Saml2/Saml2Auth.php

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
3+
namespace Aacotroneo\Saml2;
4+
5+
use OneLogin_Saml2_Auth;
6+
use OneLogin_Saml2_Error;
7+
use OneLogin_Saml2_Utils;
8+
9+
class Saml2Auth
10+
{
11+
12+
/**
13+
* @var \OneLogin_Saml2_Auth
14+
*/
15+
protected $auth;
16+
protected $uid_key;
17+
18+
function __construct($config)
19+
{
20+
// session_start();
21+
$this->auth = new OneLogin_Saml2_Auth($config);
22+
// $this->uid_key = $uid_key;
23+
// $this->id_key = $config[]
24+
}
25+
26+
function isAuthenticated()
27+
{
28+
return isset($_SESSION['samlUserdata']);
29+
}
30+
31+
function getUserId()
32+
{
33+
$attributes = $this->getAttributes();
34+
return $attributes[$this->uid_key][0];
35+
}
36+
37+
function getAttributes()
38+
{
39+
$attributes = $_SESSION['samlUserdata'];
40+
return $attributes;
41+
}
42+
43+
function getRawSamlAssertion()
44+
{
45+
return isset($_SESSION['SAMLAssertion']) ? $_SESSION['SAMLAssertion'] : null;
46+
}
47+
48+
function login()
49+
{
50+
$this->auth->login();
51+
}
52+
53+
function acs()
54+
{
55+
56+
/** @var $auth OneLogin_Saml2_Auth */
57+
$auth = $this->auth;
58+
59+
$auth->processResponse();
60+
61+
62+
$errors = $auth->getErrors();
63+
64+
if (!empty($errors)) {
65+
print_r('<p>' . implode(', ', $errors) . '</p>');
66+
exit();
67+
}
68+
69+
if (!$auth->isAuthenticated()) {
70+
echo "<p>Not authenticated</p>";
71+
exit();
72+
}
73+
74+
$_SESSION['samlUserdata'] = $auth->getAttributes();
75+
76+
$_SESSION['SAMLAssertion'] = $_POST['SAMLResponse']; //se lo robo al saml
77+
78+
if (isset($_POST['RelayState']) && OneLogin_Saml2_Utils::getSelfURL() != $_POST['RelayState']) {
79+
$auth->redirectTo($_POST['RelayState']);
80+
}
81+
}
82+
83+
function sls()
84+
{
85+
$auth = $this->auth;
86+
87+
$auth->processSLO();
88+
89+
$errors = $auth->getErrors();
90+
91+
if (empty($errors)) {
92+
print_r('Sucessfully logged out');
93+
} else {
94+
print_r(implode(', ', $errors));
95+
}
96+
}
97+
98+
function getMetadata()
99+
{
100+
$auth = $this->auth;
101+
$settings = $auth->getSettings();
102+
$metadata = $settings->getSPMetadata();
103+
$errors = $settings->validateMetadata($metadata);
104+
105+
106+
if (empty($errors)) {
107+
return $metadata;
108+
// header('Content-Type: text/xml');
109+
// echo $metadata;
110+
} else {
111+
112+
throw new OneLogin_Saml2_Error(
113+
'Invalid SP metadata: ' . implode(', ', $errors),
114+
OneLogin_Saml2_Error::METADATA_SP_INVALID
115+
);
116+
}
117+
}
118+
119+
120+
}
Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,66 @@
11
<?php
22
namespace Aacotroneo\Saml2;
33

4+
use Config;
5+
use Route;
6+
use URL;
47
use Illuminate\Support\ServiceProvider;
58

6-
class Saml2ServiceProvider extends ServiceProvider {
7-
8-
/**
9-
* Indicates if loading of the provider is deferred.
10-
*
11-
* @var bool
12-
*/
13-
protected $defer = false;
14-
15-
/**
16-
* Bootstrap the application events.
17-
*
18-
* @return void
19-
*/
20-
public function boot()
21-
{
22-
$this->package('aacotroneo/saml2');
23-
24-
include __DIR__.'/../../routes.php';
25-
}
26-
27-
/**
28-
* Register the service provider.
29-
*
30-
* @return void
31-
*/
32-
public function register()
33-
{
34-
//
35-
}
36-
37-
/**
38-
* Get the services provided by the provider.
39-
*
40-
* @return array
41-
*/
42-
public function provides()
43-
{
44-
return array();
45-
}
9+
class Saml2ServiceProvider extends ServiceProvider
10+
{
11+
12+
/**
13+
* Indicates if loading of the provider is deferred.
14+
*
15+
* @var bool
16+
*/
17+
protected $defer = false;
18+
19+
/**
20+
* Bootstrap the application events.
21+
*
22+
* @return void
23+
*/
24+
public function boot()
25+
{
26+
$this->package('aacotroneo/saml2');
27+
28+
include __DIR__ . '/../../routes.php';
29+
}
30+
31+
/**
32+
* Register the service provider.
33+
*
34+
* @return void
35+
*/
36+
public function register()
37+
{
38+
$this->app['saml2auth'] = $this->app->share(function ($app) {
39+
$config = Config::get('saml2::saml_settings');
40+
41+
$config['sp']['entityId'] = URL::route('saml_metadata');
42+
43+
$config['sp']['assertionConsumerService']['url'] = URL::route('saml_acs');
44+
45+
$config['sp']['singleLogoutService']['url'] = URL::route('saml_sls');
46+
47+
return new \Aacotroneo\Saml2\Saml2Auth($config);
48+
});
49+
50+
$this->app->booting(function () {
51+
$loader = \Illuminate\Foundation\AliasLoader::getInstance();
52+
$loader->alias('Saml2Auth', 'Aacotroneo\Saml2\Facades\Saml2Auth');
53+
});
54+
}
55+
56+
/**
57+
* Get the services provided by the provider.
58+
*
59+
* @return array
60+
*/
61+
public function provides()
62+
{
63+
return array();
64+
}
4665

4766
}

src/config/saml_settings.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,25 @@
1212

1313
// Service Provider Data that we are deploying
1414
'sp' => array (
15+
16+
// Specifies constraints on the name identifier to be used to
17+
// represent the requested subject.
18+
// Take a look on lib/Saml2/Constants.php to see the NameIdFormat supported
19+
'NameIDFormat' => 'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress',
20+
21+
// Usually x509cert and privateKey of the SP are provided by files placed at
22+
// the certs folder. But we can also provide them with the following parameters
23+
'x509cert' => '',
24+
'privateKey' > '',
25+
26+
//LARAVEL - You don't need to change anything else on the sp
1527
// Identifier of the SP entity (must be a URI)
16-
'entityId' => '',
28+
'entityId' => '', //LARAVEL: This would be set to saml_metadata route
1729
// Specifies info about where and how the <AuthnResponse> message MUST be
1830
// returned to the requester, in this case our SP.
1931
'assertionConsumerService' => array (
2032
// URL Location where the <Response> from the IdP will be returned
21-
'url' => '',
33+
'url' => '', //LARAVEL: This would be set to saml_acs route
2234
// SAML protocol binding to be used when returning the <Response>
2335
// message. Onelogin Toolkit supports for this endpoint the
2436
// HTTP-Redirect binding only
@@ -28,31 +40,22 @@
2840
// returned to the requester, in this case our SP.
2941
'singleLogoutService' => array (
3042
// URL Location where the <Response> from the IdP will be returned
31-
'url' => '',
43+
'url' => '', //LARAVEL: This would be set to saml_sls route
3244
// SAML protocol binding to be used when returning the <Response>
3345
// message. Onelogin Toolkit supports for this endpoint the
3446
// HTTP-Redirect binding only
3547
'binding' => 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect',
3648
),
37-
// Specifies constraints on the name identifier to be used to
38-
// represent the requested subject.
39-
// Take a look on lib/Saml2/Constants.php to see the NameIdFormat supported
40-
'NameIDFormat' => 'urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress',
41-
42-
// Usually x509cert and privateKey of the SP are provided by files placed at
43-
// the certs folder. But we can also provide them with the following parameters
44-
'x509cert' => '',
45-
'privateKey' > '',
4649
),
4750

4851
// Identity Provider Data that we want connect with our SP
4952
'idp' => array (
5053
// Identifier of the IdP entity (must be a URI)
51-
'entityId' => '',
54+
'entityId' => 'http://localhost:8000/simplesaml/saml2/idp/metadata.php',
5255
// SSO endpoint info of the IdP. (Authentication Request protocol)
5356
'singleSignOnService' => array (
5457
// URL Target of the IdP where the SP will send the Authentication Request Message
55-
'url' => '',
58+
'url' => 'http://localhost:8000/simplesaml/saml2/idp/SSOService.php',
5659
// SAML protocol binding to be used when returning the <Response>
5760
// message. Onelogin Toolkit supports for this endpoint the
5861
// HTTP-POST binding only
@@ -61,14 +64,14 @@
6164
// SLO endpoint info of the IdP.
6265
'singleLogoutService' => array (
6366
// URL Location of the IdP where the SP will send the SLO Request
64-
'url' => '',
67+
'url' => 'http://localhost:8000/simplesaml/saml2/idp/SingleLogoutService.php',
6568
// SAML protocol binding to be used when returning the <Response>
6669
// message. Onelogin Toolkit supports for this endpoint the
6770
// HTTP-Redirect binding only
6871
'binding' => 'urn:oasis:names:tc:SAML:2.0:bindings:HTTP-Redirect',
6972
),
7073
// Public x509 certificate of the IdP
71-
'x509cert' => '',
74+
'x509cert' => 'MIID/TCCAuWgAwIBAgIJAI4R3WyjjmB1MA0GCSqGSIb3DQEBCwUAMIGUMQswCQYDVQQGEwJBUjEVMBMGA1UECAwMQnVlbm9zIEFpcmVzMRUwEwYDVQQHDAxCdWVub3MgQWlyZXMxDDAKBgNVBAoMA1NJVTERMA8GA1UECwwIU2lzdGVtYXMxFDASBgNVBAMMC09yZy5TaXUuQ29tMSAwHgYJKoZIhvcNAQkBFhFhZG1pbmlAc2l1LmVkdS5hcjAeFw0xNDEyMDExNDM2MjVaFw0yNDExMzAxNDM2MjVaMIGUMQswCQYDVQQGEwJBUjEVMBMGA1UECAwMQnVlbm9zIEFpcmVzMRUwEwYDVQQHDAxCdWVub3MgQWlyZXMxDDAKBgNVBAoMA1NJVTERMA8GA1UECwwIU2lzdGVtYXMxFDASBgNVBAMMC09yZy5TaXUuQ29tMSAwHgYJKoZIhvcNAQkBFhFhZG1pbmlAc2l1LmVkdS5hcjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMbzW/EpEv+qqZzfT1Buwjg9nnNNVrxkCfuR9fQiQw2tSouS5X37W5h7RmchRt54wsm046PDKtbSz1NpZT2GkmHN37yALW2lY7MyVUC7itv9vDAUsFr0EfKIdCKgxCKjrzkZ5ImbNvjxf7eA77PPGJnQ/UwXY7W+cvLkirp0K5uWpDk+nac5W0JXOCFR1BpPUJRbz2jFIEHyChRt7nsJZH6ejzNqK9lABEC76htNy1Ll/D3tUoPaqo8VlKW3N3MZE0DB9O7g65DmZIIlFqkaMH3ALd8adodJtOvqfDU/A6SxuwMfwDYPjoucykGDu1etRZ7dF2gd+W+1Pn7yizPT1q8CAwEAAaNQME4wHQYDVR0OBBYEFPsn8tUHN8XXf23ig5Qro3beP8BuMB8GA1UdIwQYMBaAFPsn8tUHN8XXf23ig5Qro3beP8BuMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQELBQADggEBAGu60odWFiK+DkQekozGnlpNBQz5lQ/bwmOWdktnQj6HYXu43e7sh9oZWArLYHEOyMUekKQAxOK51vbTHzzw66BZU91/nqvaOBfkJyZKGfluHbD0/hfOl/D5kONqI9kyTu4wkLQcYGyuIi75CJs15uA03FSuULQdY/Liv+czS/XYDyvtSLnu43VuAQWN321PQNhuGueIaLJANb2C5qq5ilTBUw6PxY9Z+vtMjAjTJGKEkE/tQs7CvzLPKXX3KTD9lIILmX5yUC3dLgjVKi1KGDqNApYGOMtjr5eoxPQrqDBmyx3flcy0dQTdLXud3UjWVW3N0PYgJtw5yBsS74QTGD4=',
7275
/*
7376
* Instead of use the whole x509cert you can use a fingerprint
7477
* (openssl x509 -noout -fingerprint -in "idp.crt" to generate it)

src/controllers/Saml2Controller.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
<?php
22

3-
namespace Aacotroneo\Saml2;
3+
namespace Aacotroneo\Saml2\Controllers;
44

5+
use Saml2Auth;
6+
use Controller;
7+
use Response;
58

6-
use Illuminate\Routing\Controller;
7-
use Illuminate\Support\Facades\Config;
89

910
class Saml2Controller extends Controller {
1011

1112

1213
public function metadata(){
1314

14-
$config = Config::get('saml2::saml_settings');
15+
$metadata = Saml2Auth::getMetadata();
16+
$response = Response::make($metadata, 200);
1517

18+
$response->header('Content-Type', 'text/xml');
1619

17-
return print_r($config, true);
20+
return $response;
1821
}
1922

2023
public function acs(){

src/routes.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
//Admin Dashboard
66
Route::get('/metadata', array(
77
'as' => 'saml_metadata',
8-
'uses' => 'Aacotroneo\Saml2\Saml2Controller@metadata',
8+
'uses' => 'Aacotroneo\Saml2\Controllers\Saml2Controller@metadata',
99
));
1010

1111
Route::post('/acs', array(
12-
'as' => 'saml_metadata',
13-
'uses' => 'Aacotroneo\Saml2\Controllers\AdminController@acs',
12+
'as' => 'saml_acs',
13+
'uses' => 'Aacotroneo\Saml2\Controllers\Saml2Controller@acs',
1414
));
1515

1616
Route::get('/sls', array(
17-
'as' => 'saml_metadata',
18-
'uses' => 'Aacotroneo\Saml2\Controllers\AdminController@sls',
17+
'as' => 'saml_sls',
18+
'uses' => 'Aacotroneo\Saml2\Controllers\Saml2Controller@sls',
1919
));
2020
});

0 commit comments

Comments
 (0)