1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AerialShip\SamlSPBundle\Security\Core\Authentication\Token; |
4
|
|
|
|
5
|
|
|
use AerialShip\SamlSPBundle\Bridge\SamlSpInfo; |
6
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken; |
7
|
|
|
|
8
|
|
|
class SamlSpToken extends AbstractToken |
9
|
|
|
{ |
10
|
|
|
const ATTRIBUTE_NAME_ID = 'saml_name_id'; |
11
|
|
|
const ATTRIBUTE_NAME_ID_FORMAT = 'saml_name_id_format'; |
12
|
|
|
const ATTRIBUTE_SESSION_INDEX = 'saml_session_index'; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
private $providerKey; |
17
|
|
|
|
18
|
|
|
/** @var SamlSpInfo */ |
19
|
|
|
private $samlSpInfo; |
20
|
|
|
|
21
|
|
|
|
22
|
21 |
|
public function __construct($providerKey, array $roles = array()) |
23
|
|
|
{ |
24
|
21 |
|
parent::__construct($roles); |
25
|
|
|
// If the user has roles, consider it authenticated |
26
|
21 |
|
$this->setAuthenticated(count($roles) > 0); |
27
|
21 |
|
$this->providerKey = $providerKey; |
28
|
21 |
|
} |
29
|
|
|
|
30
|
|
|
|
31
|
13 |
|
public function getProviderKey() |
32
|
|
|
{ |
33
|
13 |
|
return $this->providerKey; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
|
38
|
|
|
public function getCredentials() |
39
|
|
|
{ |
40
|
|
|
return ''; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
|
45
|
|
|
|
46
|
16 |
|
public function setSamlSpInfo(SamlSpInfo $info) |
47
|
|
|
{ |
48
|
16 |
|
$this->samlSpInfo = $info; |
49
|
|
|
|
50
|
16 |
|
if ($info->getNameID()) { |
51
|
16 |
|
$this->setAttribute(self::ATTRIBUTE_NAME_ID, $info->getNameID()->getValue()); |
52
|
16 |
|
$this->setAttribute(self::ATTRIBUTE_NAME_ID_FORMAT, $info->getNameID()->getFormat()); |
53
|
16 |
|
} |
54
|
16 |
|
if ($info->getAttributes()) { |
55
|
16 |
|
foreach ($info->getAttributes() as $attribute) { |
56
|
16 |
|
$value = $attribute->getValues(); |
57
|
16 |
|
if (count($value) == 1) { |
58
|
16 |
|
$value = array_shift($value); |
59
|
16 |
|
} |
60
|
16 |
|
$this->setAttribute($attribute->getName(), $value); |
61
|
16 |
|
} |
62
|
16 |
|
} |
63
|
16 |
|
if ($info->getAuthnStatement()) { |
64
|
16 |
|
$this->setAttribute(self::ATTRIBUTE_SESSION_INDEX, $info->getAuthnStatement()->getSessionIndex()); |
65
|
16 |
|
} |
66
|
16 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return \AerialShip\SamlSPBundle\Bridge\SamlSpInfo |
70
|
|
|
*/ |
71
|
12 |
|
public function getSamlSpInfo() |
72
|
|
|
{ |
73
|
12 |
|
return $this->samlSpInfo; |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
public function serialize() |
77
|
|
|
{ |
78
|
2 |
|
return serialize(array($this->providerKey, $this->samlSpInfo, parent::serialize())); |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
public function unserialize($serialized) |
82
|
|
|
{ |
83
|
2 |
|
list($this->providerKey, $this->samlSpInfo, $parentStr) = unserialize($serialized); |
84
|
2 |
|
parent::unserialize($parentStr); |
85
|
2 |
|
} |
86
|
|
|
} |
87
|
|
|
|