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