1212class StaticAuthenticator implements Authenticator
1313{
1414
15- /** @var mixed[][] */
16- private $ list ;
17-
18- /** @var Passwords */
19- private $ passwords ;
15+ /** @var array<string, array{password: string, unsecured: bool, identity: IIdentity}> */
16+ private array $ list = [];
2017
2118 /**
22- * @param mixed[] $list
23- * @throws InvalidArgumentException
19+ * @param array<string, array<string, mixed>> $list
2420 */
25- public function __construct (array $ list , Passwords $ passwords )
21+ public function __construct (
22+ array $ list ,
23+ private readonly Passwords $ passwords ,
24+ )
2625 {
2726 foreach ($ list as $ username => $ values ) {
2827 if (!isset ($ values ['password ' ])) {
2928 throw new InvalidArgumentException (sprintf ('Missing parameter `password` for user `%s` ' , $ username ));
3029 }
3130
31+ if (!is_string ($ values ['password ' ])) {
32+ throw new InvalidArgumentException (sprintf ('Password for user `%s` must be a string ' , $ username ));
33+ }
34+
3235 $ this ->list [$ username ] = [
3336 'password ' => $ values ['password ' ],
34- 'unsecured ' => $ values ['unsecured ' ] ?? false ,
37+ 'unsecured ' => ( bool ) ( $ values ['unsecured ' ] ?? false ) ,
3538 'identity ' => $ this ->createIdentity ($ username , $ values ),
3639 ];
3740 }
38-
39- $ this ->passwords = $ passwords ;
4041 }
4142
42- /**
43- * @throws AuthenticationException
44- */
4543 public function authenticate (string $ username , string $ password ): IIdentity
4644 {
4745 if (!isset ($ this ->list [$ username ])) {
48- throw new AuthenticationException (sprintf ('User `%s` not found ' , $ username ), Authenticator::IDENTITY_NOT_FOUND );
46+ throw new AuthenticationException (sprintf ('User `%s` not found ' , $ username ), Authenticator::IdentityNotFound );
4947 }
5048
5149 $ user = $ this ->list [$ username ];
@@ -54,14 +52,14 @@ public function authenticate(string $username, string $password): IIdentity
5452 ($ user ['unsecured ' ] === true && !hash_equals ($ password , $ user ['password ' ])) ||
5553 ($ user ['unsecured ' ] === false && !$ this ->passwords ->verify ($ password , $ user ['password ' ]))
5654 ) {
57- throw new AuthenticationException ('Invalid password ' , Authenticator::INVALID_CREDENTIAL );
55+ throw new AuthenticationException ('Invalid password ' , Authenticator::InvalidCredential );
5856 }
5957
6058 return $ user ['identity ' ];
6159 }
6260
6361 /**
64- * @param mixed[] $values
62+ * @param array<string, mixed> $values
6563 */
6664 private function createIdentity (string $ username , array $ values ): IIdentity
6765 {
@@ -75,11 +73,15 @@ private function createIdentity(string $username, array $values): IIdentity
7573 return $ identity ;
7674 }
7775
78- if (is_array ($ values ['identity ' ])) {
76+ if (is_array ($ identity )) {
77+ $ id = $ identity ['id ' ] ?? $ username ;
78+ $ roles = $ identity ['roles ' ] ?? null ;
79+ $ data = $ identity ['data ' ] ?? null ;
80+
7981 return new SimpleIdentity (
80- $ identity [ ' id ' ] ?? $ username ,
81- $ identity [ ' roles ' ] ?? null ,
82- $ identity [ ' date ' ] ?? null
82+ is_scalar ( $ id ) ? ( string ) $ id : $ username ,
83+ is_array ( $ roles) ? $ roles : null ,
84+ is_array ( $ data ) ? $ data : null
8385 );
8486 }
8587
0 commit comments