1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Victoire\Bundle\APIBusinessEntityBundle\Chain; |
4
|
|
|
|
5
|
|
|
use Victoire\Bundle\APIBusinessEntityBundle\Authentication\Interfaces\APIAuthenticationMethodInterface; |
6
|
|
|
use Victoire\Bundle\APIBusinessEntityBundle\Chain\Exception\UnknownTokenTypeException; |
7
|
|
|
|
8
|
|
|
class ApiAuthenticationChain |
9
|
|
|
{ |
10
|
|
|
protected $authenticationMethods; |
11
|
|
|
|
12
|
|
|
public function __construct() |
13
|
|
|
{ |
14
|
|
|
$this->authenticationMethods = []; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Finds the authentication handler that match with given token type. |
19
|
|
|
* |
20
|
|
|
* @param $tokenType |
21
|
|
|
* |
22
|
|
|
* @throws UnknownTokenTypeException |
23
|
|
|
* |
24
|
|
|
* @return mixed |
25
|
|
|
*/ |
26
|
|
|
public function resolve($tokenType) |
27
|
|
|
{ |
28
|
|
|
if (!array_key_exists($tokenType, $this->authenticationMethods)) { |
29
|
|
|
throw new UnknownTokenTypeException(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
return $this->authenticationMethods[$tokenType]; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Adds an authentication method. |
37
|
|
|
* |
38
|
|
|
* @param APIAuthenticationMethodInterface $authenticationMethod |
39
|
|
|
*/ |
40
|
|
|
public function addAuthenticationMethod(APIAuthenticationMethodInterface $authenticationMethod) |
41
|
|
|
{ |
42
|
|
|
$this->authenticationMethods[$authenticationMethod::getType()] = $authenticationMethod; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* get authenticaton methods. |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
public function getAuthenticationMethods() |
51
|
|
|
{ |
52
|
|
|
return $this->authenticationMethods; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get all available authentication methods. |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function getAuthenticationMethodsTypes() |
61
|
|
|
{ |
62
|
|
|
$types = []; |
63
|
|
|
foreach ($this->authenticationMethods as $type => $authenticationMethod) { |
64
|
|
|
$types[] = $type; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $types; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $alias |
72
|
|
|
* |
73
|
|
|
* @return APIAuthenticationMethodInterface |
74
|
|
|
*/ |
75
|
|
|
public function getAuthenticationMethod($alias) |
76
|
|
|
{ |
77
|
|
|
return $this->authenticationMethods[$alias]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|