1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* Wikipedia Account Creation Assistance tool * |
4
|
|
|
* * |
5
|
|
|
* All code in this file is released into the public domain by the ACC * |
6
|
|
|
* Development Team. Please see team.json for a list of contributors. * |
7
|
|
|
******************************************************************************/ |
8
|
|
|
|
9
|
|
|
namespace Waca\Security\CredentialProviders; |
10
|
|
|
|
11
|
|
|
use Base32\Base32; |
12
|
|
|
use Waca\DataObjects\User; |
13
|
|
|
use Waca\Exceptions\ApplicationLogicException; |
14
|
|
|
use Waca\PdoDatabase; |
15
|
|
|
use Waca\Security\EncryptionHelper; |
16
|
|
|
use Waca\SiteConfiguration; |
17
|
|
|
|
18
|
|
|
class ScratchTokenCredentialProvider extends CredentialProviderBase |
19
|
|
|
{ |
20
|
|
|
/** @var EncryptionHelper */ |
21
|
|
|
private $encryptionHelper; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* ScratchTokenCredentialProvider constructor. |
25
|
|
|
* |
26
|
|
|
* @param PdoDatabase $database |
27
|
|
|
* @param SiteConfiguration $configuration |
28
|
|
|
*/ |
29
|
|
|
public function __construct(PdoDatabase $database, SiteConfiguration $configuration) |
30
|
|
|
{ |
31
|
|
|
parent::__construct($database, $configuration, 'scratch'); |
32
|
|
|
$this->encryptionHelper = new EncryptionHelper($configuration); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Validates a user-provided credential |
37
|
|
|
* |
38
|
|
|
* @param User $user The user to test the authentication against |
39
|
|
|
* @param string $data The raw credential data to be validated |
40
|
|
|
* |
41
|
|
|
* @return bool |
42
|
|
|
* @throws ApplicationLogicException |
43
|
|
|
*/ |
44
|
|
|
public function authenticate(User $user, $data) |
45
|
|
|
{ |
46
|
|
|
if (is_array($data)) { |
47
|
|
|
return false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$storedData = $this->getCredentialData($user->getId()); |
51
|
|
|
|
52
|
|
|
if ($storedData === null) { |
53
|
|
|
throw new ApplicationLogicException('Credential data not found'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$scratchTokens = unserialize($this->encryptionHelper->decryptData($storedData->getData())); |
57
|
|
|
|
58
|
|
|
$i = array_search($data, $scratchTokens); |
59
|
|
|
|
60
|
|
|
if($i === false) { |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
unset($scratchTokens[$i]); |
65
|
|
|
|
66
|
|
|
$storedData->setData($this->encryptionHelper->encryptData(serialize($scratchTokens))); |
67
|
|
|
$storedData->save(); |
68
|
|
|
|
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param User $user The user the credential belongs to |
74
|
|
|
* @param int $factor The factor this credential provides |
75
|
|
|
* @param string $data Unused. |
76
|
|
|
*/ |
77
|
|
|
public function setCredential(User $user, $factor, $data) |
78
|
|
|
{ |
79
|
|
|
$scratch = array(); |
80
|
|
|
for ($i = 0; $i < 5; $i++) { |
81
|
|
|
$scratch[] = Base32::encode(openssl_random_pseudo_bytes(10)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$storedData = $this->getCredentialData($user->getId(), null); |
|
|
|
|
85
|
|
|
|
86
|
|
|
if ($storedData !== null) { |
87
|
|
|
$storedData->delete(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$storedData = $this->createNewCredential($user); |
91
|
|
|
|
92
|
|
|
$storedData->setData($this->encryptionHelper->encryptData(serialize($scratch))); |
93
|
|
|
$storedData->setFactor($factor); |
94
|
|
|
$storedData->setVersion(1); |
95
|
|
|
$storedData->setPriority(9); |
96
|
|
|
|
97
|
|
|
$storedData->save(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param int $userId |
102
|
|
|
* |
103
|
|
|
* @return int |
104
|
|
|
* @throws ApplicationLogicException |
105
|
|
|
*/ |
106
|
|
View Code Duplication |
public function getRemaining($userId) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
$storedData = $this->getCredentialData($userId); |
109
|
|
|
|
110
|
|
|
if ($storedData === null) { |
111
|
|
|
return 0; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$scratchTokens = unserialize($this->encryptionHelper->decryptData($storedData->getData())); |
115
|
|
|
|
116
|
|
|
return count($scratchTokens); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param int $userId |
121
|
|
|
* |
122
|
|
|
* @return int |
123
|
|
|
* @throws ApplicationLogicException |
124
|
|
|
*/ |
125
|
|
View Code Duplication |
public function getTokens($userId) |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$storedData = $this->getCredentialData($userId); |
128
|
|
|
|
129
|
|
|
if ($storedData === null) { |
130
|
|
|
return 0; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$scratchTokens = unserialize($this->encryptionHelper->decryptData($storedData->getData())); |
134
|
|
|
|
135
|
|
|
return $scratchTokens; |
136
|
|
|
} |
137
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: