1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: dsmrt |
5
|
|
|
* Date: 1/12/18 |
6
|
|
|
* Time: 10:44 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\saml\sp\services; |
10
|
|
|
|
11
|
|
|
use craft\elements\User; |
12
|
|
|
use flipbox\saml\core\exceptions\InvalidMessage; |
13
|
|
|
use flipbox\saml\core\records\ProviderInterface; |
14
|
|
|
use flipbox\saml\core\services\AbstractProviderIdentityService; |
15
|
|
|
use flipbox\saml\sp\records\ProviderIdentityRecord; |
16
|
|
|
use flipbox\saml\sp\Saml; |
17
|
|
|
use flipbox\saml\sp\services\login\AssertionTrait; |
18
|
|
|
use flipbox\saml\sp\traits\SamlPluginEnsured; |
19
|
|
|
use LightSaml\Model\Protocol\Response as SamlResponse; |
20
|
|
|
use yii\base\UserException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class ProviderIdentity |
24
|
|
|
* |
25
|
|
|
* @package flipbox\saml\sp\services |
26
|
|
|
*/ |
27
|
|
|
class ProviderIdentity extends AbstractProviderIdentityService |
28
|
|
|
{ |
29
|
|
|
|
30
|
|
|
use SamlPluginEnsured, AssertionTrait; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* ACS Methods |
34
|
|
|
*/ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param User $user |
38
|
|
|
* @param SamlResponse $response |
39
|
|
|
* @return ProviderIdentityRecord |
40
|
|
|
* @throws InvalidMessage |
41
|
|
|
* @throws UserException |
42
|
|
|
*/ |
43
|
|
|
public function getByUserAndResponse(User $user, \LightSaml\Model\Protocol\Response $response) |
44
|
|
|
{ |
45
|
|
|
|
46
|
|
|
$idpProvider = Saml::getInstance()->getProvider()->findByEntityId( |
47
|
|
|
$response->getIssuer()->getValue() |
48
|
|
|
)->one(); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get Identity |
52
|
|
|
*/ |
53
|
|
|
$identity = $this->forceGet( |
54
|
|
|
$this->getFirstAssertion($response)->getSubject()->getNameID()->getValue(), |
55
|
|
|
$idpProvider |
56
|
|
|
); |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get Session |
60
|
|
|
*/ |
61
|
|
|
$sessionIndex = null; |
62
|
|
|
if ($response->getFirstAssertion()->hasAnySessionIndex()) { |
63
|
|
|
$sessionIndex = $response->getFirstAssertion()->getFirstAuthnStatement()->getSessionIndex(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Set Identity Properties |
68
|
|
|
*/ |
69
|
|
|
$identity->userId = $user->id; |
70
|
|
|
$identity->enabled = true; |
71
|
|
|
$identity->sessionId = $sessionIndex; |
72
|
|
|
return $identity; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $nameId |
78
|
|
|
* @param ProviderInterface $provider |
79
|
|
|
* @return ProviderIdentityRecord |
80
|
|
|
* @throws UserException |
81
|
|
|
*/ |
82
|
|
|
protected function forceGet($nameId, ProviderInterface $provider) |
83
|
|
|
{ |
84
|
|
|
// @var \flipbox\saml\sp\records\ProviderIdentityRecord $identity |
85
|
|
|
if (! $identity = $this->findByNameId( |
86
|
|
|
$nameId, |
87
|
|
|
$provider |
88
|
|
|
)->one() |
89
|
|
|
) { |
90
|
|
|
if (! Saml::getInstance()->getSettings()->createUser) { |
91
|
|
|
throw new UserException("System doesn't have permission to create a new user."); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Create the new identity if one wasn't found above. |
96
|
|
|
* Since we now have the user id, and we might not have above, |
97
|
|
|
* do this last. |
98
|
|
|
*/ |
99
|
|
|
$identity = new ProviderIdentityRecord( |
100
|
|
|
[ |
101
|
|
|
'providerId' => $provider->id, |
|
|
|
|
102
|
|
|
'nameId' => $nameId, |
103
|
|
|
] |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $identity; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: