1 | <?php |
||
21 | class WsseProvider implements AuthenticationProviderInterface |
||
22 | { |
||
23 | /** |
||
24 | * @var UserProviderInterface |
||
25 | */ |
||
26 | private $userProvider; |
||
27 | |||
28 | /** |
||
29 | * @var CacheItemPoolInterface |
||
30 | */ |
||
31 | private $cacheService; |
||
32 | |||
33 | /** |
||
34 | * @var int |
||
35 | */ |
||
36 | private $lifetime; |
||
37 | |||
38 | /** |
||
39 | * @var LoggerInterface |
||
40 | */ |
||
41 | private $logger; |
||
42 | |||
43 | /** |
||
44 | * WsseProvider constructor. |
||
45 | * |
||
46 | * @param UserProviderInterface $userProvider |
||
47 | * @param CacheItemPoolInterface $cacheService |
||
48 | * @param $lifetime |
||
49 | */ |
||
50 | public function __construct(UserProviderInterface $userProvider, CacheItemPoolInterface $cacheService, $lifetime) |
||
56 | |||
57 | /** |
||
58 | * @param WsseUserToken $token |
||
59 | * |
||
60 | * @return WsseUserToken |
||
61 | */ |
||
62 | public function authenticate(TokenInterface $token) |
||
75 | |||
76 | /** |
||
77 | * This function is specific to Wsse authentication and is only used to help this example. |
||
78 | * |
||
79 | * For more information specific to the logic here, see |
||
80 | * https://github.com/symfony/symfony-docs/pull/3134#issuecomment-27699129 |
||
81 | */ |
||
82 | protected function validateDigest($digest, $nonce, $created, $secret) |
||
126 | |||
127 | /** |
||
128 | * @param TokenInterface $token |
||
129 | * |
||
130 | * @return bool |
||
131 | */ |
||
132 | public function supports(TokenInterface $token) |
||
136 | |||
137 | /** |
||
138 | * @param LoggerInterface $logger |
||
139 | * |
||
140 | * @return WsseProvider |
||
141 | */ |
||
142 | public function setLogger(LoggerInterface $logger) |
||
148 | |||
149 | /** |
||
150 | * @param string $level |
||
151 | * @param string $message |
||
152 | * @param array $context |
||
153 | */ |
||
154 | private function log($level, $message, array $context = []) |
||
161 | |||
162 | /** |
||
163 | * @param TokenInterface $token |
||
164 | * |
||
165 | * @return \Symfony\Component\Security\Core\User\UserInterface |
||
166 | * |
||
167 | * @throws AuthenticationException |
||
168 | */ |
||
169 | protected function getUser(TokenInterface $token) |
||
179 | } |
||
180 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: