1 | <?php |
||
24 | class ApiKeyAuthenticator implements SimplePreAuthenticatorInterface, AuthenticationFailureHandlerInterface |
||
25 | { |
||
26 | /** |
||
27 | * @var ObjectManager |
||
28 | */ |
||
29 | private $om; |
||
30 | |||
31 | /** |
||
32 | * @var EventDispatcherInterface |
||
33 | */ |
||
34 | private $dispatcher; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $modelName; |
||
40 | |||
41 | /** |
||
42 | * @var ClassMetadata |
||
43 | */ |
||
44 | private $metadata; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | private $header; |
||
50 | |||
51 | /** |
||
52 | * Constructor. |
||
53 | * |
||
54 | * @param ObjectManager $om |
||
55 | * @param EventDispatcherInterface $dispatcher |
||
56 | * @param string $modelName |
||
57 | * @param ClassMetadata $metadata |
||
58 | * @param string $header |
||
59 | */ |
||
60 | 14 | public function __construct(ObjectManager $om, EventDispatcherInterface $dispatcher, $modelName, ClassMetadata $metadata, $header) |
|
68 | |||
69 | /** |
||
70 | * {@inheritdoc} |
||
71 | */ |
||
72 | 3 | public function onAuthenticationFailure(Request $request, AuthenticationException $exception) |
|
76 | |||
77 | /** |
||
78 | * Returns an authenticated token. |
||
79 | * |
||
80 | * @param TokenInterface $token |
||
81 | * @param UserProviderInterface $userProvider |
||
82 | * @param string $providerKey |
||
83 | * |
||
84 | * @throws AuthenticationException If the api key does not exist or is invalid |
||
85 | * @throws \RuntimeException If $userProvider is not an instance of AdvancedUserProviderInterface |
||
86 | * |
||
87 | * @return PreAuthenticatedToken |
||
88 | */ |
||
89 | 10 | public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey) |
|
90 | { |
||
91 | 10 | $apiKey = $token->getCredentials(); |
|
92 | |||
93 | $user = $this |
||
94 | 10 | ->om |
|
95 | 10 | ->getRepository($this->modelName) |
|
96 | 10 | ->findOneBy(array($this->metadata->getPropertyName(ClassMetadata::API_KEY_PROPERTY) => (string) $apiKey)); |
|
97 | |||
98 | 10 | if (!$user) { |
|
99 | 3 | $this->dispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::FIREWALL_FAILURE, new OnFirewallFailureEvent()); |
|
100 | |||
101 | 3 | throw new AuthenticationException( |
|
102 | 3 | sprintf('API key %s does not exist!', $apiKey) |
|
103 | ); |
||
104 | } |
||
105 | |||
106 | 7 | $token = new PreAuthenticatedToken( |
|
107 | 7 | $user, |
|
108 | 7 | $apiKey, |
|
109 | 7 | $providerKey, |
|
110 | 7 | $user->getRoles() ?: array() |
|
111 | ); |
||
112 | |||
113 | 7 | $firewallEvent = new OnFirewallAuthenticationEvent($user); |
|
114 | 7 | $firewallEvent->setToken($token); |
|
115 | |||
116 | 7 | $this->dispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::FIREWALL_LOGIN, $firewallEvent); |
|
117 | |||
118 | 7 | return $token; |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * Checks if the token is supported. |
||
123 | * |
||
124 | * @param TokenInterface $token |
||
125 | * @param string $providerKey |
||
126 | * |
||
127 | * @return bool |
||
128 | */ |
||
129 | 9 | public function supportsToken(TokenInterface $token, $providerKey) |
|
133 | |||
134 | /** |
||
135 | * Creates an api key by the http request. |
||
136 | * |
||
137 | * @param Request $request |
||
138 | * @param string $providerKey |
||
139 | * |
||
140 | * @throws BadCredentialsException If the request token cannot be found |
||
141 | * |
||
142 | * @return PreAuthenticatedToken |
||
143 | */ |
||
144 | 10 | public function createToken(Request $request, $providerKey) |
|
158 | } |
||
159 |