|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Vperyod Simple Lock |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* Copyright (C) 2016 Jake Johns |
|
8
|
|
|
* |
|
9
|
|
|
* This software may be modified and distributed under the terms |
|
10
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
11
|
|
|
* |
|
12
|
|
|
* @category Middleware |
|
13
|
|
|
* @package Vperyod\SimpleLock |
|
14
|
|
|
* @author Jake Johns <[email protected]> |
|
15
|
|
|
* @copyright 2016 Jake Johns |
|
16
|
|
|
* @license http://jnj.mit-license.org/2016 MIT License |
|
17
|
|
|
* @link https://github.com/vperyod/vperyod.simple-lock |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Vperyod\SimpleLock; |
|
21
|
|
|
|
|
22
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
23
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* LockHandler |
|
27
|
|
|
* |
|
28
|
|
|
* @category Middleware |
|
29
|
|
|
* @package Vperyod\SimpleLock |
|
30
|
|
|
* @author Jake Johns <[email protected]> |
|
31
|
|
|
* @license http://jnj.mit-license.org/ MIT License |
|
32
|
|
|
* @link https://github.com/vperyod/vperyod.simple-lock |
|
33
|
|
|
*/ |
|
34
|
|
|
class LockHandler |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* Verify a request can unlock session |
|
38
|
|
|
* |
|
39
|
|
|
* @var VerifyInterface |
|
40
|
|
|
* |
|
41
|
|
|
* @access protected |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $verify; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Prompt user for unlock |
|
47
|
|
|
* |
|
48
|
|
|
* @var Prompt |
|
49
|
|
|
* |
|
50
|
|
|
* @access protected |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $prompt; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Session lock |
|
56
|
|
|
* |
|
57
|
|
|
* @var Lock |
|
58
|
|
|
* |
|
59
|
|
|
* @access protected |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $lock; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Create a simple lock handler |
|
65
|
|
|
* |
|
66
|
|
|
* @param VerifyInterface $verify verifies request for unlock |
|
67
|
|
|
* @param Prompt $prompt prompts user of unlock |
|
68
|
|
|
* @param Lock $lock session lock |
|
69
|
|
|
* |
|
70
|
|
|
* @access public |
|
71
|
|
|
*/ |
|
72
|
5 |
|
public function __construct( |
|
73
|
|
|
VerifyInterface $verify, |
|
74
|
|
|
Prompt $prompt = null, |
|
75
|
|
|
Lock $lock = null |
|
76
|
|
|
) { |
|
77
|
5 |
|
$this->verify = $verify; |
|
78
|
5 |
|
$this->prompt = $prompt ?: new Prompt; |
|
79
|
5 |
|
$this->lock = $lock ?: new Lock; |
|
80
|
5 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Verify session is unlocked, or prompt user |
|
84
|
|
|
* |
|
85
|
|
|
* @param Request $request PSR7 Request |
|
86
|
|
|
* @param Response $response PSR7 Response |
|
87
|
|
|
* @param callable $next next middleware |
|
88
|
|
|
* |
|
89
|
|
|
* @return Response |
|
90
|
|
|
* |
|
91
|
|
|
* @access public |
|
92
|
|
|
*/ |
|
93
|
5 |
|
public function __invoke(Request $request, Response $response, callable $next) |
|
94
|
|
|
{ |
|
95
|
5 |
|
if ($this->isAllowed($request)) { |
|
96
|
3 |
|
$request = $this->lock->getIntent() ?: $request; |
|
97
|
3 |
|
return $next($request, $response); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
$this->storeIntent($request); |
|
101
|
|
|
|
|
102
|
2 |
|
return $this->prompt->respond($response); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Store intent |
|
107
|
|
|
* |
|
108
|
|
|
* @param Request $request PSR7 Request |
|
109
|
|
|
* |
|
110
|
|
|
* @return void |
|
111
|
|
|
* |
|
112
|
|
|
* @access protected |
|
113
|
|
|
*/ |
|
114
|
2 |
|
protected function storeIntent(Request $request) |
|
115
|
|
|
{ |
|
116
|
2 |
|
if (! $this->verify->isAttempt($request)) { |
|
117
|
1 |
|
$this->lock->setIntent($request); |
|
118
|
1 |
|
} |
|
119
|
2 |
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Is request allowed? |
|
123
|
|
|
* |
|
124
|
|
|
* @param Request $request PSR7 Request |
|
125
|
|
|
* |
|
126
|
|
|
* @return bool |
|
127
|
|
|
* |
|
128
|
|
|
* @access protected |
|
129
|
|
|
*/ |
|
130
|
5 |
|
protected function isAllowed(Request $request) |
|
131
|
|
|
{ |
|
132
|
5 |
|
return $this->lock->isUnlocked() |
|
133
|
5 |
|
|| $this->unlock($request); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Attempt to unlock as session based on request |
|
138
|
|
|
* |
|
139
|
|
|
* @param Request $request PSR7 Request |
|
140
|
|
|
* |
|
141
|
|
|
* @return bool |
|
142
|
|
|
* |
|
143
|
|
|
* @access protected |
|
144
|
|
|
*/ |
|
145
|
3 |
|
protected function unlock(Request $request) |
|
146
|
|
|
{ |
|
147
|
3 |
|
if ($this->verify->isValid($request)) { |
|
148
|
1 |
|
$this->lock->unlock(); |
|
149
|
1 |
|
return true; |
|
150
|
|
|
} |
|
151
|
2 |
|
return false; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|