@@ -18,60 +18,60 @@ |
||
18 | 18 | |
19 | 19 | class RefreshOAuthDataTask extends ConsoleTaskBase |
20 | 20 | { |
21 | - public function execute() |
|
22 | - { |
|
23 | - $database = $this->getDatabase(); |
|
21 | + public function execute() |
|
22 | + { |
|
23 | + $database = $this->getDatabase(); |
|
24 | 24 | |
25 | - $idList = $database |
|
26 | - ->query('SELECT user FROM oauthtoken WHERE type = \'access\' AND expiry IS NULL') |
|
27 | - ->fetchAll(PDO::FETCH_COLUMN); |
|
25 | + $idList = $database |
|
26 | + ->query('SELECT user FROM oauthtoken WHERE type = \'access\' AND expiry IS NULL') |
|
27 | + ->fetchAll(PDO::FETCH_COLUMN); |
|
28 | 28 | |
29 | - if (count($idList) > 0) { |
|
30 | - /** @var User[] $users */ |
|
31 | - $users = UserSearchHelper::get($database)->inIds($idList)->fetch(); |
|
29 | + if (count($idList) > 0) { |
|
30 | + /** @var User[] $users */ |
|
31 | + $users = UserSearchHelper::get($database)->inIds($idList)->fetch(); |
|
32 | 32 | |
33 | - $expiredStatement = $database |
|
34 | - ->prepare('UPDATE oauthtoken SET expiry = CURRENT_TIMESTAMP() WHERE user = :u AND type = \'access\''); |
|
33 | + $expiredStatement = $database |
|
34 | + ->prepare('UPDATE oauthtoken SET expiry = CURRENT_TIMESTAMP() WHERE user = :u AND type = \'access\''); |
|
35 | 35 | |
36 | - foreach ($users as $u) { |
|
37 | - try { |
|
38 | - $database->beginTransaction(); |
|
36 | + foreach ($users as $u) { |
|
37 | + try { |
|
38 | + $database->beginTransaction(); |
|
39 | 39 | |
40 | - $oauth = new OAuthUserHelper($u, $database, $this->getOAuthProtocolHelper(), |
|
41 | - $this->getSiteConfiguration()); |
|
40 | + $oauth = new OAuthUserHelper($u, $database, $this->getOAuthProtocolHelper(), |
|
41 | + $this->getSiteConfiguration()); |
|
42 | 42 | |
43 | - if ($oauth->getIdentity(true)->getAudience() !== $this->getSiteConfiguration() |
|
44 | - ->getOAuthConsumerToken()) { |
|
45 | - // not the current consumer token. Approval from the user is *required* for this. |
|
46 | - printf("\n\nBoldly refusing to update OAuth data for user with legacy consumer: %s\n", $u->getUsername()); |
|
47 | - continue; |
|
48 | - } |
|
43 | + if ($oauth->getIdentity(true)->getAudience() !== $this->getSiteConfiguration() |
|
44 | + ->getOAuthConsumerToken()) { |
|
45 | + // not the current consumer token. Approval from the user is *required* for this. |
|
46 | + printf("\n\nBoldly refusing to update OAuth data for user with legacy consumer: %s\n", $u->getUsername()); |
|
47 | + continue; |
|
48 | + } |
|
49 | 49 | |
50 | - try { |
|
51 | - $oauth->refreshIdentity(); |
|
52 | - } |
|
53 | - catch (OAuthException $ex) { |
|
54 | - $expiredStatement->execute(array(':u' => $u->getId())); |
|
55 | - } |
|
50 | + try { |
|
51 | + $oauth->refreshIdentity(); |
|
52 | + } |
|
53 | + catch (OAuthException $ex) { |
|
54 | + $expiredStatement->execute(array(':u' => $u->getId())); |
|
55 | + } |
|
56 | 56 | |
57 | - $database->commit(); |
|
58 | - } |
|
59 | - catch (Exception $ex) { |
|
60 | - $database->rollBack(); |
|
57 | + $database->commit(); |
|
58 | + } |
|
59 | + catch (Exception $ex) { |
|
60 | + $database->rollBack(); |
|
61 | 61 | |
62 | - printf("\n\nFailed updating OAuth data for %s\n", $u->getUsername()); |
|
63 | - printf($ex->getMessage()); |
|
64 | - } |
|
65 | - finally { |
|
66 | - if ($database->hasActiveTransaction()) { |
|
67 | - $database->rollBack(); |
|
68 | - } |
|
69 | - } |
|
70 | - } |
|
71 | - } |
|
62 | + printf("\n\nFailed updating OAuth data for %s\n", $u->getUsername()); |
|
63 | + printf($ex->getMessage()); |
|
64 | + } |
|
65 | + finally { |
|
66 | + if ($database->hasActiveTransaction()) { |
|
67 | + $database->rollBack(); |
|
68 | + } |
|
69 | + } |
|
70 | + } |
|
71 | + } |
|
72 | 72 | |
73 | - $database->beginTransaction(); |
|
74 | - $database->exec('DELETE FROM oauthtoken WHERE expiry IS NOT NULL AND expiry < NOW() AND type = \'request\''); |
|
75 | - $database->commit(); |
|
76 | - } |
|
73 | + $database->beginTransaction(); |
|
74 | + $database->exec('DELETE FROM oauthtoken WHERE expiry IS NOT NULL AND expiry < NOW() AND type = \'request\''); |
|
75 | + $database->commit(); |
|
76 | + } |
|
77 | 77 | } |
78 | 78 | \ No newline at end of file |
@@ -13,50 +13,50 @@ |
||
13 | 13 | |
14 | 14 | class ClearOldDataTask extends ConsoleTaskBase |
15 | 15 | { |
16 | - public function execute() |
|
17 | - { |
|
18 | - $dataClearInterval = $this->getSiteConfiguration()->getDataClearInterval(); |
|
19 | - $database = $this->getDatabase(); |
|
16 | + public function execute() |
|
17 | + { |
|
18 | + $dataClearInterval = $this->getSiteConfiguration()->getDataClearInterval(); |
|
19 | + $database = $this->getDatabase(); |
|
20 | 20 | |
21 | - $query = $database->prepare(<<<SQL |
|
21 | + $query = $database->prepare(<<<SQL |
|
22 | 22 | UPDATE request |
23 | 23 | SET ip = :ip, forwardedip = null, email = :mail, useragent = '' |
24 | 24 | WHERE date < DATE_SUB(curdate(), INTERVAL {$dataClearInterval}) |
25 | 25 | AND status = 'Closed'; |
26 | 26 | SQL |
27 | - ); |
|
27 | + ); |
|
28 | 28 | |
29 | - $success = $query->execute(array( |
|
30 | - ":ip" => $this->getSiteConfiguration()->getDataClearIp(), |
|
31 | - ":mail" => $this->getSiteConfiguration()->getDataClearEmail(), |
|
32 | - )); |
|
29 | + $success = $query->execute(array( |
|
30 | + ":ip" => $this->getSiteConfiguration()->getDataClearIp(), |
|
31 | + ":mail" => $this->getSiteConfiguration()->getDataClearEmail(), |
|
32 | + )); |
|
33 | 33 | |
34 | - if (!$success) { |
|
35 | - throw new Exception("Error in transaction 1: Could not clear data."); |
|
36 | - } |
|
34 | + if (!$success) { |
|
35 | + throw new Exception("Error in transaction 1: Could not clear data."); |
|
36 | + } |
|
37 | 37 | |
38 | - $dataQuery = $database->prepare(<<<SQL |
|
38 | + $dataQuery = $database->prepare(<<<SQL |
|
39 | 39 | DELETE rd |
40 | 40 | FROM requestdata rd |
41 | 41 | INNER JOIN request r ON r.id = rd.request |
42 | 42 | WHERE r.date < DATE_SUB(curdate(), INTERVAL {$dataClearInterval}) |
43 | 43 | AND r.status = 'Closed'; |
44 | 44 | SQL |
45 | - ); |
|
45 | + ); |
|
46 | 46 | |
47 | - $success = $dataQuery->execute(); |
|
47 | + $success = $dataQuery->execute(); |
|
48 | 48 | |
49 | - if (!$success) { |
|
50 | - throw new Exception("Error in transaction 2: Could not clear data."); |
|
51 | - } |
|
49 | + if (!$success) { |
|
50 | + throw new Exception("Error in transaction 2: Could not clear data."); |
|
51 | + } |
|
52 | 52 | |
53 | - // FIXME: domains! |
|
54 | - $flaggedCommentsQuery = $database->query(<<<SQL |
|
53 | + // FIXME: domains! |
|
54 | + $flaggedCommentsQuery = $database->query(<<<SQL |
|
55 | 55 | SELECT COUNT(1) FROM comment c INNER JOIN request r ON c.request = r.id WHERE c.flagged = 1 AND r.status = 'Closed' |
56 | 56 | SQL |
57 | - ); |
|
57 | + ); |
|
58 | 58 | |
59 | - $flaggedCommentsCount = $flaggedCommentsQuery->fetchColumn(); |
|
60 | - $this->getNotificationHelper()->alertFlaggedComments($flaggedCommentsCount); |
|
61 | - } |
|
59 | + $flaggedCommentsCount = $flaggedCommentsQuery->fetchColumn(); |
|
60 | + $this->getNotificationHelper()->alertFlaggedComments($flaggedCommentsCount); |
|
61 | + } |
|
62 | 62 | } |
63 | 63 | \ No newline at end of file |
@@ -15,20 +15,20 @@ |
||
15 | 15 | |
16 | 16 | class ClearOAuthDataTask extends ConsoleTaskBase |
17 | 17 | { |
18 | - public function execute() |
|
19 | - { |
|
20 | - $database = $this->getDatabase(); |
|
18 | + public function execute() |
|
19 | + { |
|
20 | + $database = $this->getDatabase(); |
|
21 | 21 | |
22 | - $users = UserSearchHelper::get($database)->inIds( |
|
23 | - $database->query('SELECT user FROM oauthtoken WHERE type = \'access\'')->fetchAll(PDO::FETCH_COLUMN) |
|
24 | - ); |
|
22 | + $users = UserSearchHelper::get($database)->inIds( |
|
23 | + $database->query('SELECT user FROM oauthtoken WHERE type = \'access\'')->fetchAll(PDO::FETCH_COLUMN) |
|
24 | + ); |
|
25 | 25 | |
26 | - foreach ($users as $u) { |
|
27 | - $oauth = new OAuthUserHelper($u, $database, $this->getOAuthProtocolHelper(), $this->getSiteConfiguration()); |
|
28 | - $oauth->detach(); |
|
29 | - } |
|
26 | + foreach ($users as $u) { |
|
27 | + $oauth = new OAuthUserHelper($u, $database, $this->getOAuthProtocolHelper(), $this->getSiteConfiguration()); |
|
28 | + $oauth->detach(); |
|
29 | + } |
|
30 | 30 | |
31 | - $database->exec('DELETE FROM oauthtoken'); |
|
32 | - $database->exec('DELETE FROM oauthidentity'); |
|
33 | - } |
|
31 | + $database->exec('DELETE FROM oauthtoken'); |
|
32 | + $database->exec('DELETE FROM oauthidentity'); |
|
33 | + } |
|
34 | 34 | } |
35 | 35 | \ No newline at end of file |
@@ -14,8 +14,8 @@ |
||
14 | 14 | |
15 | 15 | class MigrateToDomains extends ConsoleTaskBase |
16 | 16 | { |
17 | - public function execute() |
|
18 | - { |
|
19 | - echo "This migration script must be run with the entire application at an earlier version."; |
|
20 | - } |
|
17 | + public function execute() |
|
18 | + { |
|
19 | + echo "This migration script must be run with the entire application at an earlier version."; |
|
20 | + } |
|
21 | 21 | } |
@@ -16,55 +16,55 @@ |
||
16 | 16 | |
17 | 17 | class MigrateToRoles extends ConsoleTaskBase |
18 | 18 | { |
19 | - public function execute() |
|
20 | - { |
|
21 | - $communityUser = User::getCommunity(); |
|
19 | + public function execute() |
|
20 | + { |
|
21 | + $communityUser = User::getCommunity(); |
|
22 | 22 | |
23 | - $database = $this->getDatabase(); |
|
24 | - $statement = $database->query('SELECT id, status, checkuser FROM user;'); |
|
25 | - $update = $database->prepare("UPDATE user SET status = 'Active' WHERE id = :id;"); |
|
23 | + $database = $this->getDatabase(); |
|
24 | + $statement = $database->query('SELECT id, status, checkuser FROM user;'); |
|
25 | + $update = $database->prepare("UPDATE user SET status = 'Active' WHERE id = :id;"); |
|
26 | 26 | |
27 | - $users = $statement->fetchAll(PDO::FETCH_ASSOC); |
|
27 | + $users = $statement->fetchAll(PDO::FETCH_ASSOC); |
|
28 | 28 | |
29 | - foreach ($users as $user) { |
|
30 | - $toAdd = array('user'); |
|
29 | + foreach ($users as $user) { |
|
30 | + $toAdd = array('user'); |
|
31 | 31 | |
32 | - if ($user['status'] === 'Admin') { |
|
33 | - $toAdd[] = 'admin'; |
|
34 | - } |
|
32 | + if ($user['status'] === 'Admin') { |
|
33 | + $toAdd[] = 'admin'; |
|
34 | + } |
|
35 | 35 | |
36 | - if ($user['checkuser'] == 1) { |
|
37 | - $toAdd[] = 'checkuser'; |
|
38 | - } |
|
36 | + if ($user['checkuser'] == 1) { |
|
37 | + $toAdd[] = 'checkuser'; |
|
38 | + } |
|
39 | 39 | |
40 | - foreach ($toAdd as $x) { |
|
41 | - $a = new UserRole(); |
|
42 | - $a->setUser($user['id']); |
|
43 | - $a->setRole($x); |
|
44 | - $a->setDatabase($database); |
|
45 | - $a->save(); |
|
46 | - } |
|
40 | + foreach ($toAdd as $x) { |
|
41 | + $a = new UserRole(); |
|
42 | + $a->setUser($user['id']); |
|
43 | + $a->setRole($x); |
|
44 | + $a->setDatabase($database); |
|
45 | + $a->save(); |
|
46 | + } |
|
47 | 47 | |
48 | - $logData = serialize(array( |
|
49 | - 'added' => $toAdd, |
|
50 | - 'removed' => array(), |
|
51 | - 'reason' => 'Initial migration' |
|
52 | - )); |
|
48 | + $logData = serialize(array( |
|
49 | + 'added' => $toAdd, |
|
50 | + 'removed' => array(), |
|
51 | + 'reason' => 'Initial migration' |
|
52 | + )); |
|
53 | 53 | |
54 | - $log = new Log(); |
|
55 | - $log->setDatabase($database); |
|
56 | - $log->setAction('RoleChange'); |
|
57 | - $log->setObjectId($user['id']); |
|
58 | - $log->setObjectType('User'); |
|
59 | - $log->setUser($communityUser); |
|
60 | - $log->setComment($logData); |
|
61 | - $log->save(); |
|
54 | + $log = new Log(); |
|
55 | + $log->setDatabase($database); |
|
56 | + $log->setAction('RoleChange'); |
|
57 | + $log->setObjectId($user['id']); |
|
58 | + $log->setObjectType('User'); |
|
59 | + $log->setUser($communityUser); |
|
60 | + $log->setComment($logData); |
|
61 | + $log->save(); |
|
62 | 62 | |
63 | - if ($user['status'] === 'Admin' || $user['status'] === 'User') { |
|
64 | - $update->execute(array('id' => $user['id'])); |
|
65 | - } |
|
66 | - } |
|
63 | + if ($user['status'] === 'Admin' || $user['status'] === 'User') { |
|
64 | + $update->execute(array('id' => $user['id'])); |
|
65 | + } |
|
66 | + } |
|
67 | 67 | |
68 | - $database->exec("UPDATE schemaversion SET version = 25;"); |
|
69 | - } |
|
68 | + $database->exec("UPDATE schemaversion SET version = 25;"); |
|
69 | + } |
|
70 | 70 | } |
@@ -20,136 +20,136 @@ |
||
20 | 20 | |
21 | 21 | class PasswordCredentialProvider extends CredentialProviderBase |
22 | 22 | { |
23 | - const PASSWORD_COST = 10; |
|
24 | - const PASSWORD_ALGO = PASSWORD_BCRYPT; |
|
25 | - |
|
26 | - public function __construct(PdoDatabase $database, SiteConfiguration $configuration) |
|
27 | - { |
|
28 | - parent::__construct($database, $configuration, 'password'); |
|
29 | - } |
|
30 | - |
|
31 | - public function authenticate(User $user, $data) |
|
32 | - { |
|
33 | - $storedData = $this->getCredentialData($user->getId()); |
|
34 | - if ($storedData === null) { |
|
35 | - // No available credential matching these parameters |
|
36 | - return false; |
|
37 | - } |
|
38 | - |
|
39 | - if ($storedData->getVersion() !== 2) { |
|
40 | - // Non-2 versions are not supported. |
|
41 | - return false; |
|
42 | - } |
|
43 | - |
|
44 | - if (!password_verify($data, $storedData->getData())) { |
|
45 | - return false; |
|
46 | - } |
|
47 | - |
|
48 | - if (password_needs_rehash($storedData->getData(), self::PASSWORD_ALGO, |
|
49 | - array('cost' => self::PASSWORD_COST))) { |
|
50 | - try { |
|
51 | - $this->reallySetCredential($user, $storedData->getFactor(), $data); |
|
52 | - } |
|
53 | - catch (OptimisticLockFailedException $e) { |
|
54 | - // optimistic lock failed, but no biggie. We'll catch it on the next login. |
|
55 | - } |
|
56 | - } |
|
57 | - |
|
58 | - $strengthTester = new Zxcvbn(); |
|
59 | - $strength = $strengthTester->passwordStrength($data, [$user->getUsername(), $user->getOnWikiName(), $user->getEmail()]); |
|
60 | - |
|
61 | - /* 0 means the password is extremely guessable (within 10^3 guesses), dictionary words like 'password' or 'mother' score a 0 |
|
23 | + const PASSWORD_COST = 10; |
|
24 | + const PASSWORD_ALGO = PASSWORD_BCRYPT; |
|
25 | + |
|
26 | + public function __construct(PdoDatabase $database, SiteConfiguration $configuration) |
|
27 | + { |
|
28 | + parent::__construct($database, $configuration, 'password'); |
|
29 | + } |
|
30 | + |
|
31 | + public function authenticate(User $user, $data) |
|
32 | + { |
|
33 | + $storedData = $this->getCredentialData($user->getId()); |
|
34 | + if ($storedData === null) { |
|
35 | + // No available credential matching these parameters |
|
36 | + return false; |
|
37 | + } |
|
38 | + |
|
39 | + if ($storedData->getVersion() !== 2) { |
|
40 | + // Non-2 versions are not supported. |
|
41 | + return false; |
|
42 | + } |
|
43 | + |
|
44 | + if (!password_verify($data, $storedData->getData())) { |
|
45 | + return false; |
|
46 | + } |
|
47 | + |
|
48 | + if (password_needs_rehash($storedData->getData(), self::PASSWORD_ALGO, |
|
49 | + array('cost' => self::PASSWORD_COST))) { |
|
50 | + try { |
|
51 | + $this->reallySetCredential($user, $storedData->getFactor(), $data); |
|
52 | + } |
|
53 | + catch (OptimisticLockFailedException $e) { |
|
54 | + // optimistic lock failed, but no biggie. We'll catch it on the next login. |
|
55 | + } |
|
56 | + } |
|
57 | + |
|
58 | + $strengthTester = new Zxcvbn(); |
|
59 | + $strength = $strengthTester->passwordStrength($data, [$user->getUsername(), $user->getOnWikiName(), $user->getEmail()]); |
|
60 | + |
|
61 | + /* 0 means the password is extremely guessable (within 10^3 guesses), dictionary words like 'password' or 'mother' score a 0 |
|
62 | 62 | 1 is still very guessable (guesses < 10^6), an extra character on a dictionary word can score a 1 |
63 | 63 | 2 is somewhat guessable (guesses < 10^8), provides some protection from unthrottled online attacks |
64 | 64 | 3 is safely unguessable (guesses < 10^10), offers moderate protection from offline slow-hash scenario |
65 | 65 | 4 is very unguessable (guesses >= 10^10) and provides strong protection from offline slow-hash scenario */ |
66 | 66 | |
67 | - if ($strength['score'] <= 1 || CommonPasswords::isCommon($data) || mb_strlen($data) < 8) { |
|
68 | - // prevent login for extremely weak passwords |
|
69 | - // at this point the user has authenticated via password, so they *know* it's weak. |
|
70 | - SessionAlert::error('Your password is too weak to permit login. Please choose the "forgotten your password" option below and set a new one.', null); |
|
71 | - return false; |
|
72 | - } |
|
73 | - |
|
74 | - $this->revokePasswordResetTokens($user->getId()); |
|
75 | - |
|
76 | - return true; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * @param User $user |
|
81 | - * @param int $factor |
|
82 | - * @param string $password |
|
83 | - * |
|
84 | - * @throws OptimisticLockFailedException |
|
85 | - */ |
|
86 | - private function reallySetCredential(User $user, int $factor, string $password) : void { |
|
87 | - $storedData = $this->getCredentialData($user->getId()); |
|
88 | - |
|
89 | - if ($storedData === null) { |
|
90 | - $storedData = $this->createNewCredential($user); |
|
91 | - } |
|
92 | - |
|
93 | - $storedData->setData(password_hash($password, self::PASSWORD_ALGO, array('cost' => self::PASSWORD_COST))); |
|
94 | - $storedData->setFactor($factor); |
|
95 | - $storedData->setVersion(2); |
|
96 | - |
|
97 | - $storedData->save(); |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * @param User $user |
|
102 | - * @param int $factor |
|
103 | - * @param string $password |
|
104 | - * |
|
105 | - * @throws ApplicationLogicException |
|
106 | - * @throws OptimisticLockFailedException |
|
107 | - */ |
|
108 | - public function setCredential(User $user, $factor, $password) |
|
109 | - { |
|
110 | - if (CommonPasswords::isCommon($password)) { |
|
111 | - throw new ApplicationLogicException("Your new password is listed in the top 100,000 passwords. Please choose a stronger one.", null); |
|
112 | - } |
|
113 | - |
|
114 | - $strengthTester = new Zxcvbn(); |
|
115 | - $strength = $strengthTester->passwordStrength($password, [$user->getUsername(), $user->getOnWikiName(), $user->getEmail()]); |
|
116 | - |
|
117 | - /* 0 means the password is extremely guessable (within 10^3 guesses), dictionary words like 'password' or 'mother' score a 0 |
|
67 | + if ($strength['score'] <= 1 || CommonPasswords::isCommon($data) || mb_strlen($data) < 8) { |
|
68 | + // prevent login for extremely weak passwords |
|
69 | + // at this point the user has authenticated via password, so they *know* it's weak. |
|
70 | + SessionAlert::error('Your password is too weak to permit login. Please choose the "forgotten your password" option below and set a new one.', null); |
|
71 | + return false; |
|
72 | + } |
|
73 | + |
|
74 | + $this->revokePasswordResetTokens($user->getId()); |
|
75 | + |
|
76 | + return true; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * @param User $user |
|
81 | + * @param int $factor |
|
82 | + * @param string $password |
|
83 | + * |
|
84 | + * @throws OptimisticLockFailedException |
|
85 | + */ |
|
86 | + private function reallySetCredential(User $user, int $factor, string $password) : void { |
|
87 | + $storedData = $this->getCredentialData($user->getId()); |
|
88 | + |
|
89 | + if ($storedData === null) { |
|
90 | + $storedData = $this->createNewCredential($user); |
|
91 | + } |
|
92 | + |
|
93 | + $storedData->setData(password_hash($password, self::PASSWORD_ALGO, array('cost' => self::PASSWORD_COST))); |
|
94 | + $storedData->setFactor($factor); |
|
95 | + $storedData->setVersion(2); |
|
96 | + |
|
97 | + $storedData->save(); |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * @param User $user |
|
102 | + * @param int $factor |
|
103 | + * @param string $password |
|
104 | + * |
|
105 | + * @throws ApplicationLogicException |
|
106 | + * @throws OptimisticLockFailedException |
|
107 | + */ |
|
108 | + public function setCredential(User $user, $factor, $password) |
|
109 | + { |
|
110 | + if (CommonPasswords::isCommon($password)) { |
|
111 | + throw new ApplicationLogicException("Your new password is listed in the top 100,000 passwords. Please choose a stronger one.", null); |
|
112 | + } |
|
113 | + |
|
114 | + $strengthTester = new Zxcvbn(); |
|
115 | + $strength = $strengthTester->passwordStrength($password, [$user->getUsername(), $user->getOnWikiName(), $user->getEmail()]); |
|
116 | + |
|
117 | + /* 0 means the password is extremely guessable (within 10^3 guesses), dictionary words like 'password' or 'mother' score a 0 |
|
118 | 118 | 1 is still very guessable (guesses < 10^6), an extra character on a dictionary word can score a 1 |
119 | 119 | 2 is somewhat guessable (guesses < 10^8), provides some protection from unthrottled online attacks |
120 | 120 | 3 is safely unguessable (guesses < 10^10), offers moderate protection from offline slow-hash scenario |
121 | 121 | 4 is very unguessable (guesses >= 10^10) and provides strong protection from offline slow-hash scenario */ |
122 | 122 | |
123 | - if ($strength['score'] <= 2 || mb_strlen($password) < 8) { |
|
124 | - throw new ApplicationLogicException("Your new password is too weak. Please choose a stronger one.", null); |
|
125 | - } |
|
126 | - |
|
127 | - if ($strength['score'] <= 3) { |
|
128 | - SessionAlert::warning("Your new password is not as strong as it could be. Consider replacing it with a stronger password.", null); |
|
129 | - } |
|
130 | - |
|
131 | - $this->reallySetCredential($user, $factor, $password); |
|
132 | - } |
|
133 | - |
|
134 | - /** |
|
135 | - * @param User $user |
|
136 | - * |
|
137 | - * @throws ApplicationLogicException |
|
138 | - */ |
|
139 | - public function deleteCredential(User $user) |
|
140 | - { |
|
141 | - throw new ApplicationLogicException('Deletion of password credential is not allowed.'); |
|
142 | - } |
|
143 | - |
|
144 | - private function revokePasswordResetTokens(int $userId) |
|
145 | - { |
|
146 | - $statement = $this->getDatabase()->prepare("SELECT * FROM credential WHERE type = 'reset' AND user = :user;"); |
|
147 | - $statement->execute([':user' => $userId]); |
|
148 | - $existing = $statement->fetchAll(PdoDatabase::FETCH_CLASS, Credential::class); |
|
149 | - |
|
150 | - foreach ($existing as $c) { |
|
151 | - $c->setDatabase($this->getDatabase()); |
|
152 | - $c->delete(); |
|
153 | - } |
|
154 | - } |
|
123 | + if ($strength['score'] <= 2 || mb_strlen($password) < 8) { |
|
124 | + throw new ApplicationLogicException("Your new password is too weak. Please choose a stronger one.", null); |
|
125 | + } |
|
126 | + |
|
127 | + if ($strength['score'] <= 3) { |
|
128 | + SessionAlert::warning("Your new password is not as strong as it could be. Consider replacing it with a stronger password.", null); |
|
129 | + } |
|
130 | + |
|
131 | + $this->reallySetCredential($user, $factor, $password); |
|
132 | + } |
|
133 | + |
|
134 | + /** |
|
135 | + * @param User $user |
|
136 | + * |
|
137 | + * @throws ApplicationLogicException |
|
138 | + */ |
|
139 | + public function deleteCredential(User $user) |
|
140 | + { |
|
141 | + throw new ApplicationLogicException('Deletion of password credential is not allowed.'); |
|
142 | + } |
|
143 | + |
|
144 | + private function revokePasswordResetTokens(int $userId) |
|
145 | + { |
|
146 | + $statement = $this->getDatabase()->prepare("SELECT * FROM credential WHERE type = 'reset' AND user = :user;"); |
|
147 | + $statement->execute([':user' => $userId]); |
|
148 | + $existing = $statement->fetchAll(PdoDatabase::FETCH_CLASS, Credential::class); |
|
149 | + |
|
150 | + foreach ($existing as $c) { |
|
151 | + $c->setDatabase($this->getDatabase()); |
|
152 | + $c->delete(); |
|
153 | + } |
|
154 | + } |
|
155 | 155 | } |
@@ -15,139 +15,139 @@ |
||
15 | 15 | |
16 | 16 | abstract class CredentialProviderBase implements ICredentialProvider |
17 | 17 | { |
18 | - /** |
|
19 | - * @var PdoDatabase |
|
20 | - */ |
|
21 | - private $database; |
|
22 | - /** |
|
23 | - * @var SiteConfiguration |
|
24 | - */ |
|
25 | - private $configuration; |
|
26 | - /** @var string */ |
|
27 | - private $type; |
|
28 | - |
|
29 | - /** |
|
30 | - * CredentialProviderBase constructor. |
|
31 | - * |
|
32 | - * @param PdoDatabase $database |
|
33 | - * @param SiteConfiguration $configuration |
|
34 | - * @param string $type |
|
35 | - */ |
|
36 | - public function __construct(PdoDatabase $database, SiteConfiguration $configuration, $type) |
|
37 | - { |
|
38 | - $this->database = $database; |
|
39 | - $this->configuration = $configuration; |
|
40 | - $this->type = $type; |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * @param int $userId |
|
45 | - * |
|
46 | - * @param bool $disabled |
|
47 | - * |
|
48 | - * @return Credential |
|
49 | - */ |
|
50 | - protected function getCredentialData($userId, $disabled = false) |
|
51 | - { |
|
52 | - $sql = 'SELECT * FROM credential WHERE type = :t AND user = :u'; |
|
53 | - $parameters = array( |
|
54 | - ':u' => $userId, |
|
55 | - ':t' => $this->type |
|
56 | - ); |
|
57 | - |
|
58 | - if ($disabled !== null) { |
|
59 | - $sql .= ' AND disabled = :d'; |
|
60 | - $parameters[':d'] = $disabled ? 1 : 0; |
|
61 | - } |
|
62 | - |
|
63 | - $statement = $this->database->prepare($sql); |
|
64 | - $statement->execute($parameters); |
|
65 | - |
|
66 | - /** @var Credential $obj */ |
|
67 | - $obj = $statement->fetchObject(Credential::class); |
|
68 | - |
|
69 | - if ($obj === false) { |
|
70 | - return null; |
|
71 | - } |
|
72 | - |
|
73 | - $obj->setDatabase($this->database); |
|
74 | - |
|
75 | - $statement->closeCursor(); |
|
76 | - |
|
77 | - return $obj; |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * @return PdoDatabase |
|
82 | - */ |
|
83 | - public function getDatabase() |
|
84 | - { |
|
85 | - return $this->database; |
|
86 | - } |
|
87 | - |
|
88 | - /** |
|
89 | - * @return SiteConfiguration |
|
90 | - */ |
|
91 | - public function getConfiguration() |
|
92 | - { |
|
93 | - return $this->configuration; |
|
94 | - } |
|
95 | - |
|
96 | - public function deleteCredential(User $user) |
|
97 | - { |
|
98 | - // get this factor |
|
99 | - $statement = $this->database->prepare('SELECT * FROM credential WHERE user = :user AND type = :type'); |
|
100 | - $statement->execute(array(':user' => $user->getId(), ':type' => $this->type)); |
|
101 | - /** @var Credential $credential */ |
|
102 | - $credential = $statement->fetchObject(Credential::class); |
|
103 | - $credential->setDatabase($this->database); |
|
104 | - $statement->closeCursor(); |
|
105 | - |
|
106 | - $stage = $credential->getFactor(); |
|
107 | - |
|
108 | - $statement = $this->database->prepare('SELECT COUNT(*) FROM credential WHERE user = :user AND factor = :factor'); |
|
109 | - $statement->execute(array(':user' => $user->getId(), ':factor' => $stage)); |
|
110 | - $alternates = $statement->fetchColumn(); |
|
111 | - $statement->closeCursor(); |
|
112 | - |
|
113 | - if ($alternates <= 1) { |
|
114 | - // decrement the factor for every stage above this |
|
115 | - $sql = 'UPDATE credential SET factor = factor - 1 WHERE user = :user AND factor > :factor'; |
|
116 | - $statement = $this->database->prepare($sql); |
|
117 | - $statement->execute(array(':user' => $user->getId(), ':factor' => $stage)); |
|
118 | - } |
|
119 | - else { |
|
120 | - // There are other auth factors at this point. Don't renumber the factors just yet. |
|
121 | - } |
|
122 | - |
|
123 | - // delete this credential. |
|
124 | - $credential->delete(); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * @param User $user |
|
129 | - * |
|
130 | - * @return Credential |
|
131 | - */ |
|
132 | - protected function createNewCredential(User $user) |
|
133 | - { |
|
134 | - $credential = new Credential(); |
|
135 | - $credential->setDatabase($this->getDatabase()); |
|
136 | - $credential->setUserId($user->getId()); |
|
137 | - $credential->setType($this->type); |
|
138 | - |
|
139 | - return $credential; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * @param int $userId |
|
144 | - * |
|
145 | - * @return bool |
|
146 | - */ |
|
147 | - public function userIsEnrolled($userId) |
|
148 | - { |
|
149 | - $cred = $this->getCredentialData($userId); |
|
150 | - |
|
151 | - return $cred !== null; |
|
152 | - } |
|
18 | + /** |
|
19 | + * @var PdoDatabase |
|
20 | + */ |
|
21 | + private $database; |
|
22 | + /** |
|
23 | + * @var SiteConfiguration |
|
24 | + */ |
|
25 | + private $configuration; |
|
26 | + /** @var string */ |
|
27 | + private $type; |
|
28 | + |
|
29 | + /** |
|
30 | + * CredentialProviderBase constructor. |
|
31 | + * |
|
32 | + * @param PdoDatabase $database |
|
33 | + * @param SiteConfiguration $configuration |
|
34 | + * @param string $type |
|
35 | + */ |
|
36 | + public function __construct(PdoDatabase $database, SiteConfiguration $configuration, $type) |
|
37 | + { |
|
38 | + $this->database = $database; |
|
39 | + $this->configuration = $configuration; |
|
40 | + $this->type = $type; |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * @param int $userId |
|
45 | + * |
|
46 | + * @param bool $disabled |
|
47 | + * |
|
48 | + * @return Credential |
|
49 | + */ |
|
50 | + protected function getCredentialData($userId, $disabled = false) |
|
51 | + { |
|
52 | + $sql = 'SELECT * FROM credential WHERE type = :t AND user = :u'; |
|
53 | + $parameters = array( |
|
54 | + ':u' => $userId, |
|
55 | + ':t' => $this->type |
|
56 | + ); |
|
57 | + |
|
58 | + if ($disabled !== null) { |
|
59 | + $sql .= ' AND disabled = :d'; |
|
60 | + $parameters[':d'] = $disabled ? 1 : 0; |
|
61 | + } |
|
62 | + |
|
63 | + $statement = $this->database->prepare($sql); |
|
64 | + $statement->execute($parameters); |
|
65 | + |
|
66 | + /** @var Credential $obj */ |
|
67 | + $obj = $statement->fetchObject(Credential::class); |
|
68 | + |
|
69 | + if ($obj === false) { |
|
70 | + return null; |
|
71 | + } |
|
72 | + |
|
73 | + $obj->setDatabase($this->database); |
|
74 | + |
|
75 | + $statement->closeCursor(); |
|
76 | + |
|
77 | + return $obj; |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * @return PdoDatabase |
|
82 | + */ |
|
83 | + public function getDatabase() |
|
84 | + { |
|
85 | + return $this->database; |
|
86 | + } |
|
87 | + |
|
88 | + /** |
|
89 | + * @return SiteConfiguration |
|
90 | + */ |
|
91 | + public function getConfiguration() |
|
92 | + { |
|
93 | + return $this->configuration; |
|
94 | + } |
|
95 | + |
|
96 | + public function deleteCredential(User $user) |
|
97 | + { |
|
98 | + // get this factor |
|
99 | + $statement = $this->database->prepare('SELECT * FROM credential WHERE user = :user AND type = :type'); |
|
100 | + $statement->execute(array(':user' => $user->getId(), ':type' => $this->type)); |
|
101 | + /** @var Credential $credential */ |
|
102 | + $credential = $statement->fetchObject(Credential::class); |
|
103 | + $credential->setDatabase($this->database); |
|
104 | + $statement->closeCursor(); |
|
105 | + |
|
106 | + $stage = $credential->getFactor(); |
|
107 | + |
|
108 | + $statement = $this->database->prepare('SELECT COUNT(*) FROM credential WHERE user = :user AND factor = :factor'); |
|
109 | + $statement->execute(array(':user' => $user->getId(), ':factor' => $stage)); |
|
110 | + $alternates = $statement->fetchColumn(); |
|
111 | + $statement->closeCursor(); |
|
112 | + |
|
113 | + if ($alternates <= 1) { |
|
114 | + // decrement the factor for every stage above this |
|
115 | + $sql = 'UPDATE credential SET factor = factor - 1 WHERE user = :user AND factor > :factor'; |
|
116 | + $statement = $this->database->prepare($sql); |
|
117 | + $statement->execute(array(':user' => $user->getId(), ':factor' => $stage)); |
|
118 | + } |
|
119 | + else { |
|
120 | + // There are other auth factors at this point. Don't renumber the factors just yet. |
|
121 | + } |
|
122 | + |
|
123 | + // delete this credential. |
|
124 | + $credential->delete(); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * @param User $user |
|
129 | + * |
|
130 | + * @return Credential |
|
131 | + */ |
|
132 | + protected function createNewCredential(User $user) |
|
133 | + { |
|
134 | + $credential = new Credential(); |
|
135 | + $credential->setDatabase($this->getDatabase()); |
|
136 | + $credential->setUserId($user->getId()); |
|
137 | + $credential->setType($this->type); |
|
138 | + |
|
139 | + return $credential; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * @param int $userId |
|
144 | + * |
|
145 | + * @return bool |
|
146 | + */ |
|
147 | + public function userIsEnrolled($userId) |
|
148 | + { |
|
149 | + $cred = $this->getCredentialData($userId); |
|
150 | + |
|
151 | + return $cred !== null; |
|
152 | + } |
|
153 | 153 | } |
154 | 154 | \ No newline at end of file |
@@ -115,8 +115,7 @@ |
||
115 | 115 | $sql = 'UPDATE credential SET factor = factor - 1 WHERE user = :user AND factor > :factor'; |
116 | 116 | $statement = $this->database->prepare($sql); |
117 | 117 | $statement->execute(array(':user' => $user->getId(), ':factor' => $stage)); |
118 | - } |
|
119 | - else { |
|
118 | + } else { |
|
120 | 119 | // There are other auth factors at this point. Don't renumber the factors just yet. |
121 | 120 | } |
122 | 121 |
@@ -20,136 +20,136 @@ |
||
20 | 20 | |
21 | 21 | class ScratchTokenCredentialProvider extends CredentialProviderBase |
22 | 22 | { |
23 | - /** @var EncryptionHelper */ |
|
24 | - private $encryptionHelper; |
|
25 | - /** @var array the tokens generated in the last generation round. */ |
|
26 | - private $generatedTokens; |
|
27 | - |
|
28 | - /** |
|
29 | - * ScratchTokenCredentialProvider constructor. |
|
30 | - * |
|
31 | - * @param PdoDatabase $database |
|
32 | - * @param SiteConfiguration $configuration |
|
33 | - */ |
|
34 | - public function __construct(PdoDatabase $database, SiteConfiguration $configuration) |
|
35 | - { |
|
36 | - parent::__construct($database, $configuration, 'scratch'); |
|
37 | - $this->encryptionHelper = new EncryptionHelper($configuration); |
|
38 | - } |
|
39 | - |
|
40 | - /** |
|
41 | - * Validates a user-provided credential |
|
42 | - * |
|
43 | - * @param User $user The user to test the authentication against |
|
44 | - * @param string $data The raw credential data to be validated |
|
45 | - * |
|
46 | - * @return bool |
|
47 | - * @throws ApplicationLogicException|OptimisticLockFailedException |
|
48 | - */ |
|
49 | - public function authenticate(User $user, $data) |
|
50 | - { |
|
51 | - if (is_array($data)) { |
|
52 | - return false; |
|
53 | - } |
|
54 | - |
|
55 | - $storedData = $this->getCredentialData($user->getId()); |
|
56 | - |
|
57 | - if ($storedData === null) { |
|
58 | - throw new ApplicationLogicException('Credential data not found'); |
|
59 | - } |
|
60 | - |
|
61 | - $scratchTokens = unserialize($this->encryptionHelper->decryptData($storedData->getData())); |
|
62 | - |
|
63 | - $usedToken = null; |
|
64 | - foreach ($scratchTokens as $scratchToken) { |
|
65 | - if (password_verify($data, $scratchToken)) { |
|
66 | - $usedToken = $scratchToken; |
|
67 | - SessionAlert::quick("Hey, it looks like you used a scratch token to log in. Would you like to change your multi-factor authentication configuration?", 'alert-warning'); |
|
68 | - WebRequest::setPostLoginRedirect($this->getConfiguration()->getBaseUrl() . "/internal.php/multiFactor"); |
|
69 | - break; |
|
70 | - } |
|
71 | - } |
|
72 | - |
|
73 | - if ($usedToken === null) { |
|
74 | - return false; |
|
75 | - } |
|
76 | - |
|
77 | - $scratchTokens = array_diff($scratchTokens, [$usedToken]); |
|
78 | - |
|
79 | - $storedData->setData($this->encryptionHelper->encryptData(serialize($scratchTokens))); |
|
80 | - $storedData->save(); |
|
81 | - |
|
82 | - return true; |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * @param User $user The user the credential belongs to |
|
87 | - * @param int $factor The factor this credential provides |
|
88 | - * @param string $data Unused. |
|
89 | - * |
|
90 | - * @throws OptimisticLockFailedException |
|
91 | - */ |
|
92 | - public function setCredential(User $user, $factor, $data) |
|
93 | - { |
|
94 | - $plaintextScratch = array(); |
|
95 | - $storedScratch = array(); |
|
96 | - for ($i = 0; $i < 5; $i++) { |
|
97 | - $token = Base32::encodeUpper(openssl_random_pseudo_bytes(10)); |
|
98 | - $plaintextScratch[] = $token; |
|
99 | - |
|
100 | - $storedScratch[] = password_hash( |
|
101 | - $token, |
|
102 | - PasswordCredentialProvider::PASSWORD_ALGO, |
|
103 | - array('cost' => PasswordCredentialProvider::PASSWORD_COST) |
|
104 | - ); |
|
105 | - } |
|
106 | - |
|
107 | - $storedData = $this->getCredentialData($user->getId(), null); |
|
108 | - |
|
109 | - if ($storedData !== null) { |
|
110 | - $storedData->delete(); |
|
111 | - } |
|
112 | - |
|
113 | - $storedData = $this->createNewCredential($user); |
|
114 | - |
|
115 | - $storedData->setData($this->encryptionHelper->encryptData(serialize($storedScratch))); |
|
116 | - $storedData->setFactor($factor); |
|
117 | - $storedData->setVersion(1); |
|
118 | - $storedData->setPriority(9); |
|
119 | - |
|
120 | - $storedData->save(); |
|
121 | - $this->generatedTokens = $plaintextScratch; |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * Gets the count of remaining valid tokens |
|
126 | - * |
|
127 | - * @param int $userId |
|
128 | - * |
|
129 | - * @return int |
|
130 | - */ |
|
131 | - public function getRemaining($userId) |
|
132 | - { |
|
133 | - $storedData = $this->getCredentialData($userId); |
|
134 | - |
|
135 | - if ($storedData === null) { |
|
136 | - return 0; |
|
137 | - } |
|
138 | - |
|
139 | - $scratchTokens = unserialize($this->encryptionHelper->decryptData($storedData->getData())); |
|
140 | - |
|
141 | - return count($scratchTokens); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * @return array |
|
146 | - */ |
|
147 | - public function getTokens() |
|
148 | - { |
|
149 | - if ($this->generatedTokens != null) { |
|
150 | - return $this->generatedTokens; |
|
151 | - } |
|
152 | - |
|
153 | - return array(); |
|
154 | - } |
|
23 | + /** @var EncryptionHelper */ |
|
24 | + private $encryptionHelper; |
|
25 | + /** @var array the tokens generated in the last generation round. */ |
|
26 | + private $generatedTokens; |
|
27 | + |
|
28 | + /** |
|
29 | + * ScratchTokenCredentialProvider constructor. |
|
30 | + * |
|
31 | + * @param PdoDatabase $database |
|
32 | + * @param SiteConfiguration $configuration |
|
33 | + */ |
|
34 | + public function __construct(PdoDatabase $database, SiteConfiguration $configuration) |
|
35 | + { |
|
36 | + parent::__construct($database, $configuration, 'scratch'); |
|
37 | + $this->encryptionHelper = new EncryptionHelper($configuration); |
|
38 | + } |
|
39 | + |
|
40 | + /** |
|
41 | + * Validates a user-provided credential |
|
42 | + * |
|
43 | + * @param User $user The user to test the authentication against |
|
44 | + * @param string $data The raw credential data to be validated |
|
45 | + * |
|
46 | + * @return bool |
|
47 | + * @throws ApplicationLogicException|OptimisticLockFailedException |
|
48 | + */ |
|
49 | + public function authenticate(User $user, $data) |
|
50 | + { |
|
51 | + if (is_array($data)) { |
|
52 | + return false; |
|
53 | + } |
|
54 | + |
|
55 | + $storedData = $this->getCredentialData($user->getId()); |
|
56 | + |
|
57 | + if ($storedData === null) { |
|
58 | + throw new ApplicationLogicException('Credential data not found'); |
|
59 | + } |
|
60 | + |
|
61 | + $scratchTokens = unserialize($this->encryptionHelper->decryptData($storedData->getData())); |
|
62 | + |
|
63 | + $usedToken = null; |
|
64 | + foreach ($scratchTokens as $scratchToken) { |
|
65 | + if (password_verify($data, $scratchToken)) { |
|
66 | + $usedToken = $scratchToken; |
|
67 | + SessionAlert::quick("Hey, it looks like you used a scratch token to log in. Would you like to change your multi-factor authentication configuration?", 'alert-warning'); |
|
68 | + WebRequest::setPostLoginRedirect($this->getConfiguration()->getBaseUrl() . "/internal.php/multiFactor"); |
|
69 | + break; |
|
70 | + } |
|
71 | + } |
|
72 | + |
|
73 | + if ($usedToken === null) { |
|
74 | + return false; |
|
75 | + } |
|
76 | + |
|
77 | + $scratchTokens = array_diff($scratchTokens, [$usedToken]); |
|
78 | + |
|
79 | + $storedData->setData($this->encryptionHelper->encryptData(serialize($scratchTokens))); |
|
80 | + $storedData->save(); |
|
81 | + |
|
82 | + return true; |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * @param User $user The user the credential belongs to |
|
87 | + * @param int $factor The factor this credential provides |
|
88 | + * @param string $data Unused. |
|
89 | + * |
|
90 | + * @throws OptimisticLockFailedException |
|
91 | + */ |
|
92 | + public function setCredential(User $user, $factor, $data) |
|
93 | + { |
|
94 | + $plaintextScratch = array(); |
|
95 | + $storedScratch = array(); |
|
96 | + for ($i = 0; $i < 5; $i++) { |
|
97 | + $token = Base32::encodeUpper(openssl_random_pseudo_bytes(10)); |
|
98 | + $plaintextScratch[] = $token; |
|
99 | + |
|
100 | + $storedScratch[] = password_hash( |
|
101 | + $token, |
|
102 | + PasswordCredentialProvider::PASSWORD_ALGO, |
|
103 | + array('cost' => PasswordCredentialProvider::PASSWORD_COST) |
|
104 | + ); |
|
105 | + } |
|
106 | + |
|
107 | + $storedData = $this->getCredentialData($user->getId(), null); |
|
108 | + |
|
109 | + if ($storedData !== null) { |
|
110 | + $storedData->delete(); |
|
111 | + } |
|
112 | + |
|
113 | + $storedData = $this->createNewCredential($user); |
|
114 | + |
|
115 | + $storedData->setData($this->encryptionHelper->encryptData(serialize($storedScratch))); |
|
116 | + $storedData->setFactor($factor); |
|
117 | + $storedData->setVersion(1); |
|
118 | + $storedData->setPriority(9); |
|
119 | + |
|
120 | + $storedData->save(); |
|
121 | + $this->generatedTokens = $plaintextScratch; |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * Gets the count of remaining valid tokens |
|
126 | + * |
|
127 | + * @param int $userId |
|
128 | + * |
|
129 | + * @return int |
|
130 | + */ |
|
131 | + public function getRemaining($userId) |
|
132 | + { |
|
133 | + $storedData = $this->getCredentialData($userId); |
|
134 | + |
|
135 | + if ($storedData === null) { |
|
136 | + return 0; |
|
137 | + } |
|
138 | + |
|
139 | + $scratchTokens = unserialize($this->encryptionHelper->decryptData($storedData->getData())); |
|
140 | + |
|
141 | + return count($scratchTokens); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * @return array |
|
146 | + */ |
|
147 | + public function getTokens() |
|
148 | + { |
|
149 | + if ($this->generatedTokens != null) { |
|
150 | + return $this->generatedTokens; |
|
151 | + } |
|
152 | + |
|
153 | + return array(); |
|
154 | + } |
|
155 | 155 | } |
@@ -21,66 +21,66 @@ |
||
21 | 21 | |
22 | 22 | class AuthenticationManager |
23 | 23 | { |
24 | - const AUTH_OK = 1; |
|
25 | - const AUTH_FAIL = 2; |
|
26 | - const AUTH_REQUIRE_NEXT_STAGE = 3; |
|
27 | - private $typeMap = array(); |
|
28 | - /** |
|
29 | - * @var PdoDatabase |
|
30 | - */ |
|
31 | - private $database; |
|
24 | + const AUTH_OK = 1; |
|
25 | + const AUTH_FAIL = 2; |
|
26 | + const AUTH_REQUIRE_NEXT_STAGE = 3; |
|
27 | + private $typeMap = array(); |
|
28 | + /** |
|
29 | + * @var PdoDatabase |
|
30 | + */ |
|
31 | + private $database; |
|
32 | 32 | |
33 | - /** |
|
34 | - * AuthenticationManager constructor. |
|
35 | - * |
|
36 | - * @param PdoDatabase $database |
|
37 | - * @param SiteConfiguration $siteConfiguration |
|
38 | - * @param HttpHelper $httpHelper |
|
39 | - */ |
|
40 | - public function __construct(PdoDatabase $database, SiteConfiguration $siteConfiguration, HttpHelper $httpHelper) |
|
41 | - { |
|
42 | - // setup providers |
|
43 | - // note on type map: this *must* be the value in the database, as this is what it maps. |
|
44 | - $this->typeMap['password'] = new PasswordCredentialProvider($database, $siteConfiguration); |
|
45 | - $this->typeMap['yubikeyotp'] = new YubikeyOtpCredentialProvider($database, $siteConfiguration, $httpHelper); |
|
46 | - $this->typeMap['totp'] = new TotpCredentialProvider($database, $siteConfiguration); |
|
47 | - $this->typeMap['scratch'] = new ScratchTokenCredentialProvider($database, $siteConfiguration); |
|
48 | - $this->database = $database; |
|
49 | - } |
|
33 | + /** |
|
34 | + * AuthenticationManager constructor. |
|
35 | + * |
|
36 | + * @param PdoDatabase $database |
|
37 | + * @param SiteConfiguration $siteConfiguration |
|
38 | + * @param HttpHelper $httpHelper |
|
39 | + */ |
|
40 | + public function __construct(PdoDatabase $database, SiteConfiguration $siteConfiguration, HttpHelper $httpHelper) |
|
41 | + { |
|
42 | + // setup providers |
|
43 | + // note on type map: this *must* be the value in the database, as this is what it maps. |
|
44 | + $this->typeMap['password'] = new PasswordCredentialProvider($database, $siteConfiguration); |
|
45 | + $this->typeMap['yubikeyotp'] = new YubikeyOtpCredentialProvider($database, $siteConfiguration, $httpHelper); |
|
46 | + $this->typeMap['totp'] = new TotpCredentialProvider($database, $siteConfiguration); |
|
47 | + $this->typeMap['scratch'] = new ScratchTokenCredentialProvider($database, $siteConfiguration); |
|
48 | + $this->database = $database; |
|
49 | + } |
|
50 | 50 | |
51 | - public function authenticate(User $user, $data, $stage) |
|
52 | - { |
|
53 | - $sql = 'SELECT type FROM credential WHERE user = :user AND factor = :stage AND disabled = 0 ORDER BY priority ASC'; |
|
54 | - $statement = $this->database->prepare($sql); |
|
55 | - $statement->execute(array(':user' => $user->getId(), ':stage' => $stage)); |
|
56 | - $options = $statement->fetchAll(PDO::FETCH_COLUMN); |
|
51 | + public function authenticate(User $user, $data, $stage) |
|
52 | + { |
|
53 | + $sql = 'SELECT type FROM credential WHERE user = :user AND factor = :stage AND disabled = 0 ORDER BY priority ASC'; |
|
54 | + $statement = $this->database->prepare($sql); |
|
55 | + $statement->execute(array(':user' => $user->getId(), ':stage' => $stage)); |
|
56 | + $options = $statement->fetchAll(PDO::FETCH_COLUMN); |
|
57 | 57 | |
58 | - $sql = 'SELECT count(DISTINCT factor) FROM credential WHERE user = :user AND factor > :stage AND disabled = 0 AND type <> :scratch'; |
|
59 | - $statement = $this->database->prepare($sql); |
|
60 | - $statement->execute(array(':user' => $user->getId(), ':stage' => $stage, ':scratch' => 'scratch')); |
|
61 | - $requiredFactors = $statement->fetchColumn(); |
|
58 | + $sql = 'SELECT count(DISTINCT factor) FROM credential WHERE user = :user AND factor > :stage AND disabled = 0 AND type <> :scratch'; |
|
59 | + $statement = $this->database->prepare($sql); |
|
60 | + $statement->execute(array(':user' => $user->getId(), ':stage' => $stage, ':scratch' => 'scratch')); |
|
61 | + $requiredFactors = $statement->fetchColumn(); |
|
62 | 62 | |
63 | - // prep the correct OK response based on how many factors are ahead of this one |
|
64 | - $success = self::AUTH_OK; |
|
65 | - if ($requiredFactors > 0) { |
|
66 | - $success = self::AUTH_REQUIRE_NEXT_STAGE; |
|
67 | - } |
|
63 | + // prep the correct OK response based on how many factors are ahead of this one |
|
64 | + $success = self::AUTH_OK; |
|
65 | + if ($requiredFactors > 0) { |
|
66 | + $success = self::AUTH_REQUIRE_NEXT_STAGE; |
|
67 | + } |
|
68 | 68 | |
69 | - foreach ($options as $type) { |
|
70 | - if (!isset($this->typeMap[$type])) { |
|
71 | - // does this type have a credentialProvider registered? |
|
72 | - continue; |
|
73 | - } |
|
69 | + foreach ($options as $type) { |
|
70 | + if (!isset($this->typeMap[$type])) { |
|
71 | + // does this type have a credentialProvider registered? |
|
72 | + continue; |
|
73 | + } |
|
74 | 74 | |
75 | - /** @var ICredentialProvider $credentialProvider */ |
|
76 | - $credentialProvider = $this->typeMap[$type]; |
|
77 | - if ($credentialProvider->authenticate($user, $data)) { |
|
78 | - return $success; |
|
79 | - } |
|
80 | - } |
|
75 | + /** @var ICredentialProvider $credentialProvider */ |
|
76 | + $credentialProvider = $this->typeMap[$type]; |
|
77 | + if ($credentialProvider->authenticate($user, $data)) { |
|
78 | + return $success; |
|
79 | + } |
|
80 | + } |
|
81 | 81 | |
82 | - // We've iterated over all the available providers for this stage. |
|
83 | - // They all hate you. |
|
84 | - return self::AUTH_FAIL; |
|
85 | - } |
|
82 | + // We've iterated over all the available providers for this stage. |
|
83 | + // They all hate you. |
|
84 | + return self::AUTH_FAIL; |
|
85 | + } |
|
86 | 86 | } |
87 | 87 | \ No newline at end of file |