|
1
|
|
|
<?php |
|
2
|
|
|
namespace T4web\Authentication\Service; |
|
3
|
|
|
|
|
4
|
|
|
use Zend\Authentication\Adapter\AdapterInterface; |
|
5
|
|
|
use Zend\Authentication\Adapter\ValidatableAdapterInterface; |
|
6
|
|
|
use Zend\Authentication\Result; |
|
7
|
|
|
use Zend\Authentication\Storage\Session; |
|
8
|
|
|
use Zend\Session\SessionManager; |
|
9
|
|
|
use T4web\Authentication\Exception\RuntimeException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class InteractiveAuth |
|
13
|
|
|
*/ |
|
14
|
|
|
class InteractiveAuth |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var Authenticator |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $authService; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var SessionManager |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $sessionManager; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param Authenticator $authService |
|
28
|
|
|
* @param SessionManager $sessionManager |
|
29
|
|
|
* @throws RuntimeException |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(Authenticator $authService, SessionManager $sessionManager) |
|
32
|
|
|
{ |
|
33
|
|
|
if (!$authService->getStorage() instanceof Session) { |
|
34
|
|
|
throw new RuntimeException(__CLASS__ . ' requires SessionStorage'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$this->authService = $authService; |
|
38
|
|
|
$this->sessionManager = $sessionManager; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $identity |
|
43
|
|
|
* @param string $credential |
|
44
|
|
|
* @throws RuntimeException |
|
45
|
|
|
* @return Result |
|
46
|
|
|
*/ |
|
47
|
|
|
public function login($identity, $credential) |
|
48
|
|
|
{ |
|
49
|
|
|
$authAdapter = $this->authService->getAdapter(); |
|
50
|
|
|
|
|
51
|
|
|
if (!$authAdapter instanceof ValidatableAdapterInterface) { |
|
52
|
|
|
throw new RuntimeException(__CLASS__ . ' requires ValidatableAdapterInterface'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$authAdapter->setIdentity($identity) |
|
56
|
|
|
->setCredential($credential); |
|
57
|
|
|
|
|
58
|
|
|
return $this->authService->authenticate(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* |
|
63
|
|
|
*/ |
|
64
|
|
|
public function logout() |
|
65
|
|
|
{ |
|
66
|
|
|
$this->authService->clearIdentity(); |
|
67
|
|
|
$this->sessionManager->destroy([ |
|
68
|
|
|
'send_expire_cookie' => true, |
|
69
|
|
|
'clear_storage' => true, |
|
70
|
|
|
]); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param AdapterInterface $adapter |
|
75
|
|
|
* @return Result |
|
76
|
|
|
*/ |
|
77
|
|
|
public function connect(AdapterInterface $adapter) |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->authService->authenticate($adapter); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|