Skip to content

Commit 898d580

Browse files
authored
Added Guard AuthenticatorInterface.stubphp (#72)
1 parent da8d170 commit 898d580

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"phpunit/phpunit": "~7.5",
2121
"symfony/console": "*",
2222
"symfony/messenger": "^4.2 || ^5.0",
23+
"symfony/security-guard": "^4.0 || ^5.0",
2324
"symfony/validator": "*",
2425
"weirdan/codeception-psalm-module": "~0.8"
2526
},
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Symfony\Component\Security\Guard;
4+
5+
use Symfony\Component\HttpFoundation\Request;
6+
use Symfony\Component\Security\Core\User\UserInterface;
7+
use Symfony\Component\Security\Core\User\UserProviderInterface;
8+
use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
9+
10+
/**
11+
* @template Credentials
12+
* @template User of UserInterface
13+
*/
14+
interface AuthenticatorInterface extends AuthenticationEntryPointInterface
15+
{
16+
/**
17+
* @psalm-return Credentials
18+
*/
19+
public function getCredentials(Request $request);
20+
21+
/**
22+
* @psalm-param Credentials $credentials
23+
* @psalm-return ?User
24+
*/
25+
public function getUser($credentials, UserProviderInterface $userProvider);
26+
27+
/**
28+
* @psalm-param Credentials $credentials
29+
* @psalm-param User $user
30+
*/
31+
public function checkCredentials($credentials, UserInterface $user);
32+
33+
/**
34+
* @psalm-param User $user
35+
*/
36+
public function createAuthenticatedToken(UserInterface $user, string $providerKey);
37+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
@symfony-common
2+
Feature: AuthenticatorInterface
3+
4+
Background:
5+
Given I have the following config
6+
"""
7+
<?xml version="1.0"?>
8+
<psalm errorLevel="1">
9+
<projectFiles>
10+
<directory name="."/>
11+
<ignoreFiles> <directory name="../../vendor"/> </ignoreFiles>
12+
</projectFiles>
13+
14+
<plugins>
15+
<pluginClass class="Psalm\SymfonyPsalmPlugin\Plugin"/>
16+
</plugins>
17+
</psalm>
18+
"""
19+
20+
Scenario: Authenticator correctly resolves $credentials and $user types
21+
Given I have the following code
22+
"""
23+
<?php
24+
25+
use Symfony\Component\HttpFoundation\Request;
26+
use Symfony\Component\Security\Core\User\User;
27+
use Symfony\Component\Security\Core\User\UserInterface;
28+
use Symfony\Component\Security\Core\User\UserProviderInterface;
29+
use Symfony\Component\Security\Guard\AuthenticatorInterface;
30+
use Symfony\Component\Security\Guard\Token\PreAuthenticationGuardToken;
31+
32+
/**
33+
* @implements AuthenticatorInterface<string, User>
34+
*/
35+
abstract class SomeAuthenticator implements AuthenticatorInterface
36+
{
37+
public function getCredentials(Request $request)
38+
{
39+
return '';
40+
}
41+
42+
public function getUser($credentials, UserProviderInterface $provider)
43+
{
44+
/** @psalm-trace $credentials */
45+
46+
return new User('name', 'pass');
47+
}
48+
49+
public function checkCredentials($credentials, UserInterface $user)
50+
{
51+
/** @psalm-trace $credentials */
52+
53+
return true;
54+
55+
/** @psalm-trace $user */
56+
}
57+
58+
public function createAuthenticatedToken(UserInterface $user, string $providerKey)
59+
{
60+
/** @psalm-trace $user */
61+
62+
return new PreAuthenticationGuardToken($user->getPassword(), $providerKey);
63+
}
64+
}
65+
66+
"""
67+
When I run Psalm
68+
Then I see these errors
69+
| Type | Message |
70+
| Trace | $credentials: string |
71+
| Trace | $credentials: string |
72+
| Trace | $user: Symfony\Component\Security\Core\User\User |
73+
| Trace | $user: Symfony\Component\Security\Core\User\User |
74+
And I see no other errors

0 commit comments

Comments
 (0)