1616
1717namespace SimpleSAML \Module \oidc \Entities ;
1818
19- use DateTime ;
19+ use DateTimeImmutable ;
2020use League \OAuth2 \Server \Entities \UserEntityInterface ;
2121use SimpleSAML \Module \oidc \Entities \Interfaces \ClaimSetInterface ;
2222use SimpleSAML \Module \oidc \Entities \Interfaces \MementoInterface ;
23- use SimpleSAML \Module \oidc \Server \Exceptions \OidcServerException ;
24- use SimpleSAML \Module \oidc \Utils \TimestampGenerator ;
2523
2624/**
2725 * @psalm-suppress PropertyNotSetInConstructor
2826 */
2927class UserEntity implements UserEntityInterface, MementoInterface, ClaimSetInterface
3028{
31- /**
32- * @var string
33- */
34- private string $ identifier ;
35-
36- /**
37- * @var array
38- */
39- private array $ claims ;
40-
41- /**
42- * @var DateTime
43- */
44- private DateTime $ createdAt ;
45-
46- /**
47- * @var DateTime
48- */
49- private DateTime $ updatedAt ;
50-
51- private function __construct ()
52- {
53- }
54-
55- /**
56- * @throws \Exception
57- */
58- public static function fromData (string $ identifier , array $ claims = []): self
59- {
60- $ user = new self ();
61-
62- $ user ->identifier = $ identifier ;
63- $ user ->createdAt = TimestampGenerator::utc ();
64- $ user ->updatedAt = $ user ->createdAt ;
65- $ user ->claims = $ claims ;
66-
67- return $ user ;
68- }
69-
70- /**
71- * @throws \Exception
72- * @throws \SimpleSAML\Module\oidc\Server\Exceptions\OidcServerException
73- */
74- public static function fromState (array $ state ): self
75- {
76- $ user = new self ();
77-
78- if (
79- !is_string ($ state ['id ' ]) ||
80- !is_string ($ state ['claims ' ]) ||
81- !is_string ($ state ['updated_at ' ]) ||
82- !is_string ($ state ['created_at ' ])
83- ) {
84- throw OidcServerException::serverError ('Invalid user entity data ' );
85- }
86-
87- $ user ->identifier = $ state ['id ' ];
88- $ claims = json_decode ($ state ['claims ' ], true , 512 , JSON_INVALID_UTF8_SUBSTITUTE );
89-
90- if (!is_array ($ claims )) {
91- throw OidcServerException::serverError ('Invalid user entity data ' );
92- }
93- $ user ->claims = $ claims ;
94- $ user ->updatedAt = TimestampGenerator::utc ($ state ['updated_at ' ]);
95- $ user ->createdAt = TimestampGenerator::utc ($ state ['created_at ' ]);
96-
97- return $ user ;
29+ public function __construct (
30+ private readonly string $ identifier ,
31+ private readonly DateTimeImmutable $ createdAt ,
32+ private DateTimeImmutable $ updatedAt ,
33+ private array $ claims = [],
34+ ) {
9835 }
9936
10037 /**
@@ -120,23 +57,24 @@ public function getClaims(): array
12057 return $ this ->claims ;
12158 }
12259
123- /**
124- * @throws \Exception
125- */
12660 public function setClaims (array $ claims ): self
12761 {
12862 $ this ->claims = $ claims ;
129- $ this ->updatedAt = TimestampGenerator::utc ();
130-
13163 return $ this ;
13264 }
13365
134- public function getUpdatedAt (): DateTime
66+ public function getUpdatedAt (): DateTimeImmutable
13567 {
13668 return $ this ->updatedAt ;
13769 }
13870
139- public function getCreatedAt (): DateTime
71+ public function setUpdatedAt (DateTimeImmutable $ updatedAt ): self
72+ {
73+ $ this ->updatedAt = $ updatedAt ;
74+ return $ this ;
75+ }
76+
77+ public function getCreatedAt (): DateTimeImmutable
14078 {
14179 return $ this ->createdAt ;
14280 }
0 commit comments