|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace EcPhp\CasBundle\Security\Core\User; |
|
6
|
|
|
|
|
7
|
|
|
use EcPhp\CasLib\Introspection\Contract\IntrospectorInterface; |
|
8
|
|
|
use EcPhp\CasLib\Introspection\Contract\ServiceValidate; |
|
9
|
|
|
use InvalidArgumentException; |
|
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
11
|
|
|
use Symfony\Component\Security\Core\Exception\AuthenticationException; |
|
12
|
|
|
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
|
13
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
14
|
|
|
|
|
15
|
|
|
use function get_class; |
|
16
|
|
|
|
|
17
|
|
|
class CasUserProvider implements CasUserProviderInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var IntrospectorInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $introspector; |
|
23
|
|
|
|
|
24
|
6 |
|
public function __construct(IntrospectorInterface $introspector) |
|
25
|
|
|
{ |
|
26
|
6 |
|
$this->introspector = $introspector; |
|
27
|
6 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
2 |
|
public function loadUserByResponse(ResponseInterface $response): CasUserInterface |
|
33
|
|
|
{ |
|
34
|
|
|
try { |
|
35
|
2 |
|
$introspect = $this->introspector->detect($response); |
|
36
|
|
|
} catch (InvalidArgumentException $exception) { |
|
37
|
|
|
throw new AuthenticationException($exception->getMessage()); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
2 |
|
if ($introspect instanceof ServiceValidate) { |
|
41
|
2 |
|
return new CasUser($introspect->getCredentials()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
2 |
|
throw new AuthenticationException('Unable to load user from response.'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function loadUserByUsername(string $username): UserInterface |
|
51
|
|
|
{ |
|
52
|
1 |
|
throw new UnsupportedUserException(sprintf('Username "%s" does not exist.', $username)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function refreshUser(UserInterface $user): UserInterface |
|
59
|
|
|
{ |
|
60
|
1 |
|
if (!$user instanceof CasUserInterface) { |
|
61
|
1 |
|
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user))); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
1 |
|
return $user; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* {@inheritdoc} |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function supportsClass(string $class): bool |
|
71
|
|
|
{ |
|
72
|
1 |
|
return CasUser::class === $class; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|