1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Slides\Saml2; |
4
|
|
|
|
5
|
|
|
use OneLogin\Saml2\Auth as OneLoginAuth; |
6
|
|
|
use Slides\Saml2\Models\Tenant; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Saml2User |
10
|
|
|
* |
11
|
|
|
* @package Slides\Saml2 |
12
|
|
|
*/ |
13
|
|
|
class Saml2User |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* OneLogin authentication handler. |
17
|
|
|
* |
18
|
|
|
* @var OneLoginAuth |
19
|
|
|
*/ |
20
|
|
|
protected $auth; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The tenant user belongs to. |
24
|
|
|
* |
25
|
|
|
* @var Tenant |
26
|
|
|
*/ |
27
|
|
|
protected $tenant; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Saml2User constructor. |
31
|
|
|
* |
32
|
|
|
* @param OneLoginAuth $auth |
33
|
|
|
* @param Tenant $tenant |
34
|
|
|
*/ |
35
|
3 |
|
public function __construct(OneLoginAuth $auth, Tenant $tenant) |
36
|
|
|
{ |
37
|
3 |
|
$this->auth = $auth; |
38
|
3 |
|
$this->tenant = $tenant; |
39
|
3 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get the user ID retrieved from assertion processed this request. |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function getUserId() |
47
|
|
|
{ |
48
|
|
|
return $this->auth->getNameId(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the attributes retrieved from assertion processed this request |
53
|
|
|
* |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
public function getAttributes() |
57
|
|
|
{ |
58
|
|
|
return $this->auth->getAttributes(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns the requested SAML attribute |
63
|
|
|
* |
64
|
|
|
* @param string $name The requested attribute of the user. |
65
|
|
|
* |
66
|
|
|
* @return array|null Requested SAML attribute ($name). |
67
|
|
|
*/ |
68
|
3 |
|
public function getAttribute($name) |
69
|
|
|
{ |
70
|
3 |
|
return $this->auth->getAttribute($name); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* The attributes retrieved from assertion processed this request. |
75
|
|
|
* |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
|
|
public function getAttributesWithFriendlyName() |
79
|
|
|
{ |
80
|
|
|
return $this->auth->getAttributesWithFriendlyName(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* The SAML assertion processed this request. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
public function getRawSamlAssertion() |
89
|
|
|
{ |
90
|
|
|
return app('request')->input('SAMLResponse'); //just this request |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get the intended URL. |
95
|
|
|
* |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
|
|
public function getIntendedUrl() |
99
|
|
|
{ |
100
|
|
|
$relayState = app('request')->input('RelayState'); |
|
|
|
|
101
|
|
|
|
102
|
|
|
$url = app('Illuminate\Contracts\Routing\UrlGenerator'); |
103
|
|
|
|
104
|
|
|
if ($relayState && $url->full() != $relayState) { |
105
|
|
|
return $relayState; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return null; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Parses a SAML property and adds this property to this user or returns the value. |
113
|
|
|
* |
114
|
|
|
* @param string $samlAttribute |
115
|
|
|
* @param string $propertyName |
116
|
|
|
* |
117
|
|
|
* @return array|null |
118
|
|
|
*/ |
119
|
2 |
|
public function parseUserAttribute($samlAttribute = null, $propertyName = null) |
120
|
|
|
{ |
121
|
2 |
|
if(empty($samlAttribute)) { |
122
|
|
|
return null; |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
if(empty($propertyName)) { |
126
|
|
|
return $this->getAttribute($samlAttribute); |
127
|
|
|
} |
128
|
|
|
|
129
|
2 |
|
return $this->{$propertyName} = $this->getAttribute($samlAttribute); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Parse the SAML attributes and add them to this user. |
134
|
|
|
* |
135
|
|
|
* @param array $attributes Array of properties which need to be parsed, like ['email' => 'urn:oid:0.9.2342.19200300.100.1.3'] |
136
|
|
|
* |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
1 |
|
public function parseAttributes($attributes = []) |
140
|
|
|
{ |
141
|
1 |
|
foreach($attributes as $propertyName => $samlAttribute) { |
142
|
1 |
|
$this->parseUserAttribute($samlAttribute, $propertyName); |
143
|
|
|
} |
144
|
1 |
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Get user's session index. |
148
|
|
|
* |
149
|
|
|
* @return null|string |
150
|
|
|
*/ |
151
|
|
|
public function getSessionIndex() |
152
|
|
|
{ |
153
|
|
|
return $this->auth->getSessionIndex(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get user's name ID. |
158
|
|
|
* |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
public function getNameId() |
162
|
|
|
{ |
163
|
|
|
return $this->auth->getNameId(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Set a tenant |
168
|
|
|
* |
169
|
|
|
* @param Tenant $tenant |
170
|
|
|
* |
171
|
|
|
* @return void |
172
|
|
|
*/ |
173
|
|
|
public function setTenant(Tenant $tenant) |
174
|
|
|
{ |
175
|
|
|
$this->tenant = $tenant; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get a resolved tenant. |
180
|
|
|
* |
181
|
|
|
* @return Tenant|null |
182
|
|
|
*/ |
183
|
|
|
public function getTenant() |
184
|
|
|
{ |
185
|
|
|
return $this->tenant; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|