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 Waca\DataObjects\User; |
12
|
|
|
use Waca\PdoDatabase; |
13
|
|
|
use Waca\SiteConfiguration; |
14
|
|
|
|
15
|
|
|
class PasswordCredentialProvider extends CredentialProviderBase |
16
|
|
|
{ |
17
|
|
|
const PASSWORD_COST = 10; |
18
|
|
|
|
19
|
|
|
public function __construct(PdoDatabase $database, SiteConfiguration $configuration) |
20
|
|
|
{ |
21
|
|
|
parent::__construct($database, $configuration, 'password'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function authenticate(User $user, $data) |
25
|
|
|
{ |
26
|
|
|
$storedData = $this->getCredentialData($user->getId()); |
27
|
|
|
if($storedData === null) |
28
|
|
|
{ |
29
|
|
|
// No available credential matching these parameters |
30
|
|
|
return false; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if($storedData->getVersion() !== 2) { |
34
|
|
|
// Non-2 versions are not supported. |
35
|
|
|
return false; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if(password_verify($data, $storedData->getData())) { |
39
|
|
|
if(password_needs_rehash($storedData->getData(), PASSWORD_BCRYPT, array('cost' => self::PASSWORD_COST))){ |
40
|
|
|
$this->setCredential($user, $storedData->getFactor(), $data); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return true; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return false; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function setCredential(User $user, $factor, $password) |
50
|
|
|
{ |
51
|
|
|
$storedData = $this->getCredentialData($user->getId()); |
52
|
|
|
|
53
|
|
|
if($storedData === null){ |
54
|
|
|
$storedData = $this->createNewCredential($user); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$storedData->setData(password_hash($password, PASSWORD_BCRYPT, array('cost' => self::PASSWORD_COST))); |
58
|
|
|
$storedData->setFactor($factor); |
59
|
|
|
$storedData->setVersion(2); |
60
|
|
|
|
61
|
|
|
$storedData->save(); |
62
|
|
|
} |
63
|
|
|
} |