Skip to content

Commit 9ca65ea

Browse files
committed
Further cleanup
1 parent ec9f6fb commit 9ca65ea

File tree

4 files changed

+106
-237
lines changed

4 files changed

+106
-237
lines changed

src/Security/Core/Exception/AccountNotLinkedException.php

+99-207
Original file line numberDiff line numberDiff line change
@@ -12,217 +12,109 @@
1212
namespace HWI\Bundle\OAuthBundle\Security\Core\Exception;
1313

1414
use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\AbstractOAuthToken;
15-
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
1615
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
1716

18-
if (class_exists(UserNotFoundException::class)) {
19-
final class AccountNotLinkedException extends UserNotFoundException implements OAuthAwareExceptionInterface
17+
final class AccountNotLinkedException extends UserNotFoundException implements OAuthAwareExceptionInterface
18+
{
19+
private ?string $resourceOwnerName = null;
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function __serialize(): array
25+
{
26+
return [
27+
$this->resourceOwnerName,
28+
parent::__serialize(),
29+
];
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function __unserialize(array $data): void
36+
{
37+
[
38+
$this->resourceOwnerName,
39+
$parentData
40+
] = $data;
41+
42+
parent::__unserialize($parentData);
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function getMessageKey(): string
49+
{
50+
return 'Account could not be linked correctly.';
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function getAccessToken(): string
57+
{
58+
/** @var AbstractOAuthToken $token */
59+
$token = $this->getToken();
60+
61+
return $token->getAccessToken();
62+
}
63+
64+
public function getRawToken(): array
65+
{
66+
/** @var AbstractOAuthToken $token */
67+
$token = $this->getToken();
68+
69+
return $token->getRawToken();
70+
}
71+
72+
/**
73+
* {@inheritdoc}
74+
*/
75+
public function getRefreshToken(): ?string
76+
{
77+
/** @var AbstractOAuthToken $token */
78+
$token = $this->getToken();
79+
80+
return $token->getRefreshToken();
81+
}
82+
83+
/**
84+
* {@inheritdoc}
85+
*/
86+
public function getExpiresIn(): ?int
87+
{
88+
/** @var AbstractOAuthToken $token */
89+
$token = $this->getToken();
90+
91+
return $token->getExpiresIn();
92+
}
93+
94+
/**
95+
* {@inheritdoc}
96+
*/
97+
public function getTokenSecret(): ?string
98+
{
99+
/** @var AbstractOAuthToken $token */
100+
$token = $this->getToken();
101+
102+
return $token->getTokenSecret();
103+
}
104+
105+
/**
106+
* {@inheritdoc}
107+
*/
108+
public function getResourceOwnerName(): ?string
20109
{
21-
private ?string $resourceOwnerName = null;
22-
23-
/**
24-
* {@inheritdoc}
25-
*/
26-
public function __serialize(): array
27-
{
28-
return [
29-
$this->resourceOwnerName,
30-
parent::__serialize(),
31-
];
32-
}
33-
34-
/**
35-
* {@inheritdoc}
36-
*/
37-
public function __unserialize(array $data): void
38-
{
39-
[
40-
$this->resourceOwnerName,
41-
$parentData
42-
] = $data;
43-
44-
parent::__unserialize($parentData);
45-
}
46-
47-
/**
48-
* {@inheritdoc}
49-
*/
50-
public function getMessageKey(): string
51-
{
52-
return 'Account could not be linked correctly.';
53-
}
54-
55-
/**
56-
* {@inheritdoc}
57-
*/
58-
public function getAccessToken(): string
59-
{
60-
/** @var AbstractOAuthToken $token */
61-
$token = $this->getToken();
62-
63-
return $token->getAccessToken();
64-
}
65-
66-
public function getRawToken(): array
67-
{
68-
/** @var AbstractOAuthToken $token */
69-
$token = $this->getToken();
70-
71-
return $token->getRawToken();
72-
}
73-
74-
/**
75-
* {@inheritdoc}
76-
*/
77-
public function getRefreshToken(): ?string
78-
{
79-
/** @var AbstractOAuthToken $token */
80-
$token = $this->getToken();
81-
82-
return $token->getRefreshToken();
83-
}
84-
85-
/**
86-
* {@inheritdoc}
87-
*/
88-
public function getExpiresIn(): ?int
89-
{
90-
/** @var AbstractOAuthToken $token */
91-
$token = $this->getToken();
92-
93-
return $token->getExpiresIn();
94-
}
95-
96-
/**
97-
* {@inheritdoc}
98-
*/
99-
public function getTokenSecret(): ?string
100-
{
101-
/** @var AbstractOAuthToken $token */
102-
$token = $this->getToken();
103-
104-
return $token->getTokenSecret();
105-
}
106-
107-
/**
108-
* {@inheritdoc}
109-
*/
110-
public function getResourceOwnerName(): ?string
111-
{
112-
return $this->resourceOwnerName;
113-
}
114-
115-
/**
116-
* {@inheritdoc}
117-
*/
118-
public function setResourceOwnerName($resourceOwnerName): void
119-
{
120-
$this->resourceOwnerName = $resourceOwnerName;
121-
}
110+
return $this->resourceOwnerName;
122111
}
123-
} else {
124-
final class AccountNotLinkedException extends UsernameNotFoundException implements OAuthAwareExceptionInterface
112+
113+
/**
114+
* {@inheritdoc}
115+
*/
116+
public function setResourceOwnerName($resourceOwnerName): void
125117
{
126-
private ?string $resourceOwnerName = null;
127-
128-
/**
129-
* {@inheritdoc}
130-
*/
131-
public function __serialize(): array
132-
{
133-
return [
134-
$this->resourceOwnerName,
135-
parent::__serialize(),
136-
];
137-
}
138-
139-
/**
140-
* {@inheritdoc}
141-
*/
142-
public function __unserialize(array $data): void
143-
{
144-
[
145-
$this->resourceOwnerName,
146-
$parentData
147-
] = $data;
148-
149-
parent::__unserialize($parentData);
150-
}
151-
152-
/**
153-
* {@inheritdoc}
154-
*/
155-
public function getMessageKey(): string
156-
{
157-
return 'Account could not be linked correctly.';
158-
}
159-
160-
/**
161-
* {@inheritdoc}
162-
*/
163-
public function getAccessToken(): string
164-
{
165-
/** @var AbstractOAuthToken $token */
166-
$token = $this->getToken();
167-
168-
return $token->getAccessToken();
169-
}
170-
171-
public function getRawToken(): array
172-
{
173-
/** @var AbstractOAuthToken $token */
174-
$token = $this->getToken();
175-
176-
return $token->getRawToken();
177-
}
178-
179-
/**
180-
* {@inheritdoc}
181-
*/
182-
public function getRefreshToken(): ?string
183-
{
184-
/** @var AbstractOAuthToken $token */
185-
$token = $this->getToken();
186-
187-
return $token->getRefreshToken();
188-
}
189-
190-
/**
191-
* {@inheritdoc}
192-
*/
193-
public function getExpiresIn(): ?int
194-
{
195-
/** @var AbstractOAuthToken $token */
196-
$token = $this->getToken();
197-
198-
return $token->getExpiresIn();
199-
}
200-
201-
/**
202-
* {@inheritdoc}
203-
*/
204-
public function getTokenSecret(): ?string
205-
{
206-
/** @var AbstractOAuthToken $token */
207-
$token = $this->getToken();
208-
209-
return $token->getTokenSecret();
210-
}
211-
212-
/**
213-
* {@inheritdoc}
214-
*/
215-
public function getResourceOwnerName(): ?string
216-
{
217-
return $this->resourceOwnerName;
218-
}
219-
220-
/**
221-
* {@inheritdoc}
222-
*/
223-
public function setResourceOwnerName($resourceOwnerName): void
224-
{
225-
$this->resourceOwnerName = $resourceOwnerName;
226-
}
118+
$this->resourceOwnerName = $resourceOwnerName;
227119
}
228120
}

src/Security/Core/User/EntityUserProvider.php

+5-28
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
1818
use Symfony\Component\PropertyAccess\PropertyAccess;
1919
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
20-
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
2120
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
2221
use Symfony\Component\Security\Core\User\UserInterface;
2322
use Symfony\Component\Security\Core\User\UserProviderInterface;
@@ -57,10 +56,7 @@ public function loadUserByIdentifier(string $identifier): UserInterface
5756
$user = $this->findUser(['username' => $identifier]);
5857

5958
if (!$user) {
60-
$exception = new UserNotFoundException(sprintf("User '%s' not found.", $identifier));
61-
$exception->setUserIdentifier($identifier);
62-
63-
throw $exception;
59+
throw $this->createUserNotFoundException($identifier, sprintf("User '%s' not found.", $identifier));
6460
}
6561

6662
return $user;
@@ -75,12 +71,7 @@ public function loadUserByIdentifier(string $identifier): UserInterface
7571
*/
7672
public function loadUserByUsername($username)
7773
{
78-
$user = $this->findUser(['username' => $username]);
79-
if (!$user) {
80-
throw $this->createUserNotFoundException($username, sprintf("User '%s' not found.", $username));
81-
}
82-
83-
return $user;
74+
return $this->loadUserByIdentifier($username);
8475
}
8576

8677
public function loadUserByOAuthUserResponse(UserResponseInterface $response): ?UserInterface
@@ -133,24 +124,10 @@ private function findUser(array $criteria): ?UserInterface
133124
return $this->repository->findOneBy($criteria);
134125
}
135126

136-
/**
137-
* @return UsernameNotFoundException|UserNotFoundException
138-
*/
139-
private function createUserNotFoundException(string $username, string $message)
127+
private function createUserNotFoundException(string $username, string $message): UserNotFoundException
140128
{
141-
if (class_exists(UserNotFoundException::class)) {
142-
$exception = new UserNotFoundException($message);
143-
$exception->setUserIdentifier($username);
144-
} else {
145-
if (!class_exists(UsernameNotFoundException::class)) {
146-
throw new \RuntimeException('Unsupported Symfony version used!');
147-
}
148-
149-
$exception = new UsernameNotFoundException($message);
150-
if (method_exists($exception, 'setUsername')) {
151-
$exception->setUsername($username); // @phpstan-ignore-this-line Symfony <5.4 BC layer
152-
}
153-
}
129+
$exception = new UserNotFoundException($message);
130+
$exception->setUserIdentifier($username);
154131

155132
return $exception;
156133
}

src/Security/Core/User/OAuthAwareUserProviderInterface.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace HWI\Bundle\OAuthBundle\Security\Core\User;
1313

1414
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
15-
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
1615
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
1716
use Symfony\Component\Security\Core\User\UserInterface;
1817

@@ -28,7 +27,7 @@ interface OAuthAwareUserProviderInterface
2827
*
2928
* @return UserInterface
3029
*
31-
* @throws UsernameNotFoundException|UserNotFoundException if the user is not found
30+
* @throws UserNotFoundException if the user is not found
3231
*/
3332
public function loadUserByOAuthUserResponse(UserResponseInterface $response);
3433
}

0 commit comments

Comments
 (0)