|
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 Verify |
|
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 http://jakejohns.net |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Vperyod\SimpleLock\Verify; |
|
21
|
|
|
|
|
22
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
23
|
|
|
use Vperyod\SimpleLock\VerifyInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Password |
|
27
|
|
|
* |
|
28
|
|
|
* @category Verify |
|
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 Password implements VerifyInterface |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* Password |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
* |
|
41
|
|
|
* @access protected |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $password; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Field |
|
47
|
|
|
* |
|
48
|
|
|
* @var mixed |
|
49
|
|
|
* |
|
50
|
|
|
* @access protected |
|
51
|
|
|
*/ |
|
52
|
|
|
protected $field = 'vperyod/simplelock:password'; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Create a password verifier |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $password required password |
|
58
|
|
|
* @param string $field name of body field |
|
59
|
|
|
* |
|
60
|
|
|
* @access public |
|
61
|
|
|
*/ |
|
62
|
2 |
|
public function __construct($password, $field = 'vperyod/simplelock:password') |
|
63
|
|
|
{ |
|
64
|
2 |
|
$this->password = $password; |
|
65
|
2 |
|
$this->field = $field; |
|
66
|
2 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Is request valid for unlocking the session? |
|
70
|
|
|
* |
|
71
|
|
|
* @param Request $request PSR7 Request |
|
72
|
|
|
* |
|
73
|
|
|
* @return bool |
|
74
|
|
|
* |
|
75
|
|
|
* @access public |
|
76
|
|
|
*/ |
|
77
|
2 |
|
public function isValid(Request $request) |
|
78
|
|
|
{ |
|
79
|
2 |
|
return $this->isAttempt($request) |
|
80
|
2 |
|
&& $this->verifyPassword((array) $request->getParsedBody()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Is request an unlock attempt? |
|
85
|
|
|
* |
|
86
|
|
|
* @param Request $request DESCRIPTION |
|
87
|
|
|
* |
|
88
|
|
|
* @return bool |
|
89
|
|
|
* |
|
90
|
|
|
* @access public |
|
91
|
|
|
*/ |
|
92
|
2 |
|
public function isAttempt(Request $request) |
|
93
|
|
|
{ |
|
94
|
2 |
|
$body = (array) $request->getParsedBody(); |
|
95
|
2 |
|
return $request->getMethod() === 'POST' |
|
96
|
2 |
|
&& array_key_exists($this->field, $body); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Verify password |
|
101
|
|
|
* |
|
102
|
|
|
* @param array $body parsed body |
|
103
|
|
|
* |
|
104
|
|
|
* @return bool |
|
105
|
|
|
* |
|
106
|
|
|
* @access protected |
|
107
|
|
|
*/ |
|
108
|
2 |
|
protected function verifyPassword(array $body) |
|
109
|
|
|
{ |
|
110
|
2 |
|
return isset($body[$this->field]) |
|
111
|
2 |
|
&& hash_equals($this->password, $body[$this->field]); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|