|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Slides\Saml2; |
|
4
|
|
|
|
|
5
|
|
|
use OneLogin\Saml2\Auth as OneLoginAuth; |
|
6
|
|
|
use OneLogin\Saml2\Utils as OneLoginUtils; |
|
7
|
|
|
use Illuminate\Support\Facades\URL; |
|
8
|
|
|
use Illuminate\Contracts\Container\Container; |
|
9
|
|
|
use Slides\Saml2\Models\Tenant; |
|
10
|
|
|
use Illuminate\Support\Arr; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class OneLoginBuilder |
|
14
|
|
|
* |
|
15
|
|
|
* @package Slides\Saml2 |
|
16
|
|
|
*/ |
|
17
|
|
|
class OneLoginBuilder |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var Container |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $app; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The resolved tenant. |
|
26
|
|
|
* |
|
27
|
|
|
* @var Tenant |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $tenant; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* OneLoginBuilder constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param Container $app |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(Container $app) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->app = $app; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Set a tenant. |
|
43
|
|
|
* |
|
44
|
|
|
* @param Tenant $tenant |
|
45
|
|
|
* |
|
46
|
|
|
* @return $this |
|
47
|
|
|
*/ |
|
48
|
|
|
public function withTenant(Tenant $tenant) |
|
49
|
|
|
{ |
|
50
|
|
|
$this->tenant = $tenant; |
|
51
|
|
|
|
|
52
|
|
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Bootstrap the OneLogin toolkit. |
|
57
|
|
|
* |
|
58
|
|
|
* @param Tenant $tenant |
|
59
|
|
|
* |
|
60
|
|
|
* @return void |
|
61
|
|
|
*/ |
|
62
|
|
|
public function bootstrap() |
|
63
|
|
|
{ |
|
64
|
|
|
if ($this->app['config']->get('saml2.proxyVars', false)) { |
|
65
|
|
|
OneLoginUtils::setProxyVars(true); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->app->singleton('OneLogin_Saml2_Auth', function ($app) { |
|
69
|
|
|
$config = $app['config']['saml2']; |
|
70
|
|
|
|
|
71
|
|
|
$this->setConfigDefaultValues($config); |
|
72
|
|
|
|
|
73
|
|
|
$oneLoginConfig = $config; |
|
74
|
|
|
$oneLoginConfig['idp'] = [ |
|
75
|
|
|
'entityId' => $this->tenant->idp_entity_id, |
|
76
|
|
|
'singleSignOnService' => ['url' => $this->tenant->idp_login_url], |
|
77
|
|
|
'singleLogoutService' => ['url' => $this->tenant->idp_logout_url], |
|
78
|
|
|
'x509cert' => $this->tenant->idp_x509_cert |
|
79
|
|
|
]; |
|
80
|
|
|
|
|
81
|
|
|
$oneLoginConfig['sp']['NameIDFormat'] = $this->resolveNameIdFormatPrefix($this->tenant->name_id_format); |
|
82
|
|
|
|
|
83
|
|
|
return new OneLoginAuth($oneLoginConfig); |
|
84
|
|
|
}); |
|
85
|
|
|
|
|
86
|
|
|
$this->app->singleton('Slides\Saml2\Auth', function ($app) { |
|
87
|
|
|
return new \Slides\Saml2\Auth($app['OneLogin_Saml2_Auth'], $this->tenant); |
|
88
|
|
|
}); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Set default config values if they weren't set. |
|
93
|
|
|
* |
|
94
|
|
|
* @param array $config |
|
95
|
|
|
* |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function setConfigDefaultValues(array &$config) |
|
99
|
|
|
{ |
|
100
|
|
|
foreach ($this->configDefaultValues() as $key => $default) { |
|
101
|
|
|
if(!Arr::get($config, $key)) { |
|
102
|
|
|
Arr::set($config, $key, $default); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Configuration default values that must be replaced with custom ones. |
|
109
|
|
|
* |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function configDefaultValues() |
|
113
|
|
|
{ |
|
114
|
|
|
return [ |
|
115
|
|
|
'sp.entityId' => URL::route('saml.metadata', ['uuid' => $this->tenant->uuid]), |
|
116
|
|
|
'sp.assertionConsumerService.url' => URL::route('saml.acs', ['uuid' => $this->tenant->uuid]), |
|
117
|
|
|
'sp.singleLogoutService.url' => URL::route('saml.sls', ['uuid' => $this->tenant->uuid]) |
|
118
|
|
|
]; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Resolve the Name ID Format prefix. |
|
123
|
|
|
* |
|
124
|
|
|
* @param string $format |
|
125
|
|
|
* |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function resolveNameIdFormatPrefix(string $format): string |
|
129
|
|
|
{ |
|
130
|
|
|
switch ($format) { |
|
131
|
|
|
case 'emailAddress': |
|
132
|
|
|
case 'X509SubjectName': |
|
133
|
|
|
case 'WindowsDomainQualifiedName': |
|
134
|
|
|
case 'unspecified': |
|
135
|
|
|
return 'urn:oasis:names:tc:SAML:1.1:nameid-format:' . $format; |
|
136
|
|
|
default: |
|
137
|
|
|
return 'urn:oasis:names:tc:SAML:2.0:nameid-format:'. $format; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |