@@ -30,13 +30,13 @@ |
||
30 | 30 | public function authenticate(User $user, $data) |
31 | 31 | { |
32 | 32 | $storedData = $this->getCredentialData($user->getId()); |
33 | - if($storedData === null) |
|
33 | + if ($storedData === null) |
|
34 | 34 | { |
35 | 35 | // No available credential matching these parameters |
36 | 36 | return false; |
37 | 37 | } |
38 | 38 | |
39 | - if($storedData->getVersion() !== 2) { |
|
39 | + if ($storedData->getVersion() !== 2) { |
|
40 | 40 | // Non-2 versions are not supported. |
41 | 41 | return false; |
42 | 42 | } |
@@ -30,8 +30,7 @@ |
||
30 | 30 | public function authenticate(User $user, $data) |
31 | 31 | { |
32 | 32 | $storedData = $this->getCredentialData($user->getId()); |
33 | - if($storedData === null) |
|
34 | - { |
|
33 | + if($storedData === null) { |
|
35 | 34 | // No available credential matching these parameters |
36 | 35 | return false; |
37 | 36 | } |
@@ -19,123 +19,123 @@ |
||
19 | 19 | |
20 | 20 | class PasswordCredentialProvider extends CredentialProviderBase |
21 | 21 | { |
22 | - const PASSWORD_COST = 10; |
|
23 | - const PASSWORD_ALGO = PASSWORD_BCRYPT; |
|
24 | - |
|
25 | - public function __construct(PdoDatabase $database, SiteConfiguration $configuration) |
|
26 | - { |
|
27 | - parent::__construct($database, $configuration, 'password'); |
|
28 | - } |
|
29 | - |
|
30 | - public function authenticate(User $user, $data) |
|
31 | - { |
|
32 | - $storedData = $this->getCredentialData($user->getId()); |
|
33 | - if($storedData === null) |
|
34 | - { |
|
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 |
|
22 | + const PASSWORD_COST = 10; |
|
23 | + const PASSWORD_ALGO = PASSWORD_BCRYPT; |
|
24 | + |
|
25 | + public function __construct(PdoDatabase $database, SiteConfiguration $configuration) |
|
26 | + { |
|
27 | + parent::__construct($database, $configuration, 'password'); |
|
28 | + } |
|
29 | + |
|
30 | + public function authenticate(User $user, $data) |
|
31 | + { |
|
32 | + $storedData = $this->getCredentialData($user->getId()); |
|
33 | + if($storedData === null) |
|
34 | + { |
|
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 | - return true; |
|
75 | - } |
|
76 | - |
|
77 | - /** |
|
78 | - * @param User $user |
|
79 | - * @param int $factor |
|
80 | - * @param string $password |
|
81 | - * |
|
82 | - * @throws OptimisticLockFailedException |
|
83 | - */ |
|
84 | - private function reallySetCredential(User $user, int $factor, string $password) : void { |
|
85 | - $storedData = $this->getCredentialData($user->getId()); |
|
86 | - |
|
87 | - if ($storedData === null) { |
|
88 | - $storedData = $this->createNewCredential($user); |
|
89 | - } |
|
90 | - |
|
91 | - $storedData->setData(password_hash($password, self::PASSWORD_ALGO, array('cost' => self::PASSWORD_COST))); |
|
92 | - $storedData->setFactor($factor); |
|
93 | - $storedData->setVersion(2); |
|
94 | - |
|
95 | - $storedData->save(); |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * @param User $user |
|
100 | - * @param int $factor |
|
101 | - * @param string $password |
|
102 | - * |
|
103 | - * @throws ApplicationLogicException |
|
104 | - * @throws OptimisticLockFailedException |
|
105 | - */ |
|
106 | - public function setCredential(User $user, $factor, $password) |
|
107 | - { |
|
108 | - if (CommonPasswords::isCommon($password)) { |
|
109 | - throw new ApplicationLogicException("Your new password is listed in the top 100,000 passwords. Please choose a stronger one.", null); |
|
110 | - } |
|
111 | - |
|
112 | - $strengthTester = new Zxcvbn(); |
|
113 | - $strength = $strengthTester->passwordStrength($password, [$user->getUsername(), $user->getOnWikiName(), $user->getEmail()]); |
|
114 | - |
|
115 | - /* 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 | + return true; |
|
75 | + } |
|
76 | + |
|
77 | + /** |
|
78 | + * @param User $user |
|
79 | + * @param int $factor |
|
80 | + * @param string $password |
|
81 | + * |
|
82 | + * @throws OptimisticLockFailedException |
|
83 | + */ |
|
84 | + private function reallySetCredential(User $user, int $factor, string $password) : void { |
|
85 | + $storedData = $this->getCredentialData($user->getId()); |
|
86 | + |
|
87 | + if ($storedData === null) { |
|
88 | + $storedData = $this->createNewCredential($user); |
|
89 | + } |
|
90 | + |
|
91 | + $storedData->setData(password_hash($password, self::PASSWORD_ALGO, array('cost' => self::PASSWORD_COST))); |
|
92 | + $storedData->setFactor($factor); |
|
93 | + $storedData->setVersion(2); |
|
94 | + |
|
95 | + $storedData->save(); |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * @param User $user |
|
100 | + * @param int $factor |
|
101 | + * @param string $password |
|
102 | + * |
|
103 | + * @throws ApplicationLogicException |
|
104 | + * @throws OptimisticLockFailedException |
|
105 | + */ |
|
106 | + public function setCredential(User $user, $factor, $password) |
|
107 | + { |
|
108 | + if (CommonPasswords::isCommon($password)) { |
|
109 | + throw new ApplicationLogicException("Your new password is listed in the top 100,000 passwords. Please choose a stronger one.", null); |
|
110 | + } |
|
111 | + |
|
112 | + $strengthTester = new Zxcvbn(); |
|
113 | + $strength = $strengthTester->passwordStrength($password, [$user->getUsername(), $user->getOnWikiName(), $user->getEmail()]); |
|
114 | + |
|
115 | + /* 0 means the password is extremely guessable (within 10^3 guesses), dictionary words like 'password' or 'mother' score a 0 |
|
116 | 116 | 1 is still very guessable (guesses < 10^6), an extra character on a dictionary word can score a 1 |
117 | 117 | 2 is somewhat guessable (guesses < 10^8), provides some protection from unthrottled online attacks |
118 | 118 | 3 is safely unguessable (guesses < 10^10), offers moderate protection from offline slow-hash scenario |
119 | 119 | 4 is very unguessable (guesses >= 10^10) and provides strong protection from offline slow-hash scenario */ |
120 | 120 | |
121 | - if ($strength['score'] <= 2 || mb_strlen($password) < 8) { |
|
122 | - throw new ApplicationLogicException("Your new password is too weak. Please choose a stronger one.", null); |
|
123 | - } |
|
124 | - |
|
125 | - if ($strength['score'] <= 3) { |
|
126 | - SessionAlert::warning("Your new password is not as strong as it could be. Consider replacing it with a stronger password.", null); |
|
127 | - } |
|
128 | - |
|
129 | - $this->reallySetCredential($user, $factor, $password); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * @param User $user |
|
134 | - * |
|
135 | - * @throws ApplicationLogicException |
|
136 | - */ |
|
137 | - public function deleteCredential(User $user) |
|
138 | - { |
|
139 | - throw new ApplicationLogicException('Deletion of password credential is not allowed.'); |
|
140 | - } |
|
121 | + if ($strength['score'] <= 2 || mb_strlen($password) < 8) { |
|
122 | + throw new ApplicationLogicException("Your new password is too weak. Please choose a stronger one.", null); |
|
123 | + } |
|
124 | + |
|
125 | + if ($strength['score'] <= 3) { |
|
126 | + SessionAlert::warning("Your new password is not as strong as it could be. Consider replacing it with a stronger password.", null); |
|
127 | + } |
|
128 | + |
|
129 | + $this->reallySetCredential($user, $factor, $password); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * @param User $user |
|
134 | + * |
|
135 | + * @throws ApplicationLogicException |
|
136 | + */ |
|
137 | + public function deleteCredential(User $user) |
|
138 | + { |
|
139 | + throw new ApplicationLogicException('Deletion of password credential is not allowed.'); |
|
140 | + } |
|
141 | 141 | } |
@@ -12,32 +12,32 @@ |
||
12 | 12 | |
13 | 13 | interface ICredentialProvider |
14 | 14 | { |
15 | - /** |
|
16 | - * Validates a user-provided credential |
|
17 | - * |
|
18 | - * @param User $user The user to test the authentication against |
|
19 | - * @param string $data The raw credential data to be validated |
|
20 | - * |
|
21 | - * @return bool |
|
22 | - */ |
|
23 | - public function authenticate(User $user, $data); |
|
15 | + /** |
|
16 | + * Validates a user-provided credential |
|
17 | + * |
|
18 | + * @param User $user The user to test the authentication against |
|
19 | + * @param string $data The raw credential data to be validated |
|
20 | + * |
|
21 | + * @return bool |
|
22 | + */ |
|
23 | + public function authenticate(User $user, $data); |
|
24 | 24 | |
25 | - /** |
|
26 | - * @param User $user The user the credential belongs to |
|
27 | - * @param int $factor The factor this credential provides |
|
28 | - * @param string $data |
|
29 | - */ |
|
30 | - public function setCredential(User $user, $factor, $data); |
|
25 | + /** |
|
26 | + * @param User $user The user the credential belongs to |
|
27 | + * @param int $factor The factor this credential provides |
|
28 | + * @param string $data |
|
29 | + */ |
|
30 | + public function setCredential(User $user, $factor, $data); |
|
31 | 31 | |
32 | - /** |
|
33 | - * @param User $user |
|
34 | - */ |
|
35 | - public function deleteCredential(User $user); |
|
32 | + /** |
|
33 | + * @param User $user |
|
34 | + */ |
|
35 | + public function deleteCredential(User $user); |
|
36 | 36 | |
37 | - /** |
|
38 | - * @param int $userId |
|
39 | - * |
|
40 | - * @return bool |
|
41 | - */ |
|
42 | - public function userIsEnrolled($userId); |
|
37 | + /** |
|
38 | + * @param int $userId |
|
39 | + * |
|
40 | + * @return bool |
|
41 | + */ |
|
42 | + public function userIsEnrolled($userId); |
|
43 | 43 | } |
44 | 44 | \ No newline at end of file |
@@ -16,154 +16,154 @@ |
||
16 | 16 | |
17 | 17 | class YubikeyOtpCredentialProvider extends CredentialProviderBase |
18 | 18 | { |
19 | - /** @var HttpHelper */ |
|
20 | - private $httpHelper; |
|
21 | - /** |
|
22 | - * @var SiteConfiguration |
|
23 | - */ |
|
24 | - private $configuration; |
|
25 | - |
|
26 | - public function __construct(PdoDatabase $database, SiteConfiguration $configuration, HttpHelper $httpHelper) |
|
27 | - { |
|
28 | - parent::__construct($database, $configuration, 'yubikeyotp'); |
|
29 | - $this->httpHelper = $httpHelper; |
|
30 | - $this->configuration = $configuration; |
|
31 | - } |
|
32 | - |
|
33 | - public function authenticate(User $user, $data) |
|
34 | - { |
|
35 | - if (is_array($data)) { |
|
36 | - return false; |
|
37 | - } |
|
38 | - |
|
39 | - $credentialData = $this->getCredentialData($user->getId()); |
|
40 | - |
|
41 | - if ($credentialData === null) { |
|
42 | - return false; |
|
43 | - } |
|
44 | - |
|
45 | - if ($credentialData->getData() !== $this->getYubikeyId($data)) { |
|
46 | - // different device |
|
47 | - return false; |
|
48 | - } |
|
49 | - |
|
50 | - return $this->verifyToken($data); |
|
51 | - } |
|
52 | - |
|
53 | - public function setCredential(User $user, $factor, $data) |
|
54 | - { |
|
55 | - $keyId = $this->getYubikeyId($data); |
|
56 | - $valid = $this->verifyToken($data); |
|
57 | - |
|
58 | - if (!$valid) { |
|
59 | - throw new ApplicationLogicException("Provided token is not valid."); |
|
60 | - } |
|
61 | - |
|
62 | - $storedData = $this->getCredentialData($user->getId()); |
|
63 | - |
|
64 | - if ($storedData === null) { |
|
65 | - $storedData = $this->createNewCredential($user); |
|
66 | - } |
|
67 | - |
|
68 | - $storedData->setData($keyId); |
|
69 | - $storedData->setFactor($factor); |
|
70 | - $storedData->setVersion(1); |
|
71 | - $storedData->setPriority(8); |
|
72 | - |
|
73 | - $storedData->save(); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Get the Yubikey ID. |
|
78 | - * |
|
79 | - * This looks like it's just dumping the "password" that's stored in the database, but it's actually fine. |
|
80 | - * |
|
81 | - * We only store the "serial number" of the Yubikey - if we get a validated (by webservice) token prefixed with the |
|
82 | - * serial number, that's a successful OTP authentication. Thus, retrieving the stored data is just retrieving the |
|
83 | - * yubikey's serial number (in modhex format), since the actual security credentials are stored on the device. |
|
84 | - * |
|
85 | - * Note that the serial number is actually the credential serial number - it's possible to regenerate the keys on |
|
86 | - * the device, and that will change the serial number too. |
|
87 | - * |
|
88 | - * More information about the structure of OTPs can be found here: |
|
89 | - * https://developers.yubico.com/OTP/OTPs_Explained.html |
|
90 | - * |
|
91 | - * @param int $userId |
|
92 | - * |
|
93 | - * @return null|string |
|
94 | - */ |
|
95 | - public function getYubikeyData($userId) |
|
96 | - { |
|
97 | - $credential = $this->getCredentialData($userId); |
|
98 | - |
|
99 | - if ($credential === null) { |
|
100 | - return null; |
|
101 | - } |
|
102 | - |
|
103 | - return $credential->getData(); |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @param $result |
|
108 | - * |
|
109 | - * @return array |
|
110 | - */ |
|
111 | - private function parseYubicoApiResult($result) |
|
112 | - { |
|
113 | - $data = array(); |
|
114 | - foreach (explode("\r\n", $result) as $line) { |
|
115 | - $pos = strpos($line, '='); |
|
116 | - if ($pos === false) { |
|
117 | - continue; |
|
118 | - } |
|
119 | - |
|
120 | - $data[substr($line, 0, $pos)] = substr($line, $pos + 1); |
|
121 | - } |
|
122 | - |
|
123 | - return $data; |
|
124 | - } |
|
125 | - |
|
126 | - private function getYubikeyId($data) |
|
127 | - { |
|
128 | - return substr($data, 0, -32); |
|
129 | - } |
|
130 | - |
|
131 | - private function verifyHmac($apiResponse, $apiKey) |
|
132 | - { |
|
133 | - ksort($apiResponse); |
|
134 | - $signature = $apiResponse['h']; |
|
135 | - unset($apiResponse['h']); |
|
136 | - |
|
137 | - $data = array(); |
|
138 | - foreach ($apiResponse as $key => $value) { |
|
139 | - $data[] = $key . "=" . $value; |
|
140 | - } |
|
141 | - $dataString = implode('&', $data); |
|
142 | - |
|
143 | - $hmac = base64_encode(hash_hmac('sha1', $dataString, base64_decode($apiKey), true)); |
|
144 | - |
|
145 | - return $hmac === $signature; |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * @param $data |
|
150 | - * |
|
151 | - * @return bool |
|
152 | - */ |
|
153 | - private function verifyToken($data) |
|
154 | - { |
|
155 | - $result = $this->httpHelper->get('https://api.yubico.com/wsapi/2.0/verify', array( |
|
156 | - 'id' => $this->configuration->getYubicoApiId(), |
|
157 | - 'otp' => $data, |
|
158 | - 'nonce' => md5(openssl_random_pseudo_bytes(64)), |
|
159 | - )); |
|
160 | - |
|
161 | - $apiResponse = $this->parseYubicoApiResult($result); |
|
162 | - |
|
163 | - if (!$this->verifyHmac($apiResponse, $this->configuration->getYubicoApiKey())) { |
|
164 | - return false; |
|
165 | - } |
|
166 | - |
|
167 | - return $apiResponse['status'] == 'OK'; |
|
168 | - } |
|
19 | + /** @var HttpHelper */ |
|
20 | + private $httpHelper; |
|
21 | + /** |
|
22 | + * @var SiteConfiguration |
|
23 | + */ |
|
24 | + private $configuration; |
|
25 | + |
|
26 | + public function __construct(PdoDatabase $database, SiteConfiguration $configuration, HttpHelper $httpHelper) |
|
27 | + { |
|
28 | + parent::__construct($database, $configuration, 'yubikeyotp'); |
|
29 | + $this->httpHelper = $httpHelper; |
|
30 | + $this->configuration = $configuration; |
|
31 | + } |
|
32 | + |
|
33 | + public function authenticate(User $user, $data) |
|
34 | + { |
|
35 | + if (is_array($data)) { |
|
36 | + return false; |
|
37 | + } |
|
38 | + |
|
39 | + $credentialData = $this->getCredentialData($user->getId()); |
|
40 | + |
|
41 | + if ($credentialData === null) { |
|
42 | + return false; |
|
43 | + } |
|
44 | + |
|
45 | + if ($credentialData->getData() !== $this->getYubikeyId($data)) { |
|
46 | + // different device |
|
47 | + return false; |
|
48 | + } |
|
49 | + |
|
50 | + return $this->verifyToken($data); |
|
51 | + } |
|
52 | + |
|
53 | + public function setCredential(User $user, $factor, $data) |
|
54 | + { |
|
55 | + $keyId = $this->getYubikeyId($data); |
|
56 | + $valid = $this->verifyToken($data); |
|
57 | + |
|
58 | + if (!$valid) { |
|
59 | + throw new ApplicationLogicException("Provided token is not valid."); |
|
60 | + } |
|
61 | + |
|
62 | + $storedData = $this->getCredentialData($user->getId()); |
|
63 | + |
|
64 | + if ($storedData === null) { |
|
65 | + $storedData = $this->createNewCredential($user); |
|
66 | + } |
|
67 | + |
|
68 | + $storedData->setData($keyId); |
|
69 | + $storedData->setFactor($factor); |
|
70 | + $storedData->setVersion(1); |
|
71 | + $storedData->setPriority(8); |
|
72 | + |
|
73 | + $storedData->save(); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Get the Yubikey ID. |
|
78 | + * |
|
79 | + * This looks like it's just dumping the "password" that's stored in the database, but it's actually fine. |
|
80 | + * |
|
81 | + * We only store the "serial number" of the Yubikey - if we get a validated (by webservice) token prefixed with the |
|
82 | + * serial number, that's a successful OTP authentication. Thus, retrieving the stored data is just retrieving the |
|
83 | + * yubikey's serial number (in modhex format), since the actual security credentials are stored on the device. |
|
84 | + * |
|
85 | + * Note that the serial number is actually the credential serial number - it's possible to regenerate the keys on |
|
86 | + * the device, and that will change the serial number too. |
|
87 | + * |
|
88 | + * More information about the structure of OTPs can be found here: |
|
89 | + * https://developers.yubico.com/OTP/OTPs_Explained.html |
|
90 | + * |
|
91 | + * @param int $userId |
|
92 | + * |
|
93 | + * @return null|string |
|
94 | + */ |
|
95 | + public function getYubikeyData($userId) |
|
96 | + { |
|
97 | + $credential = $this->getCredentialData($userId); |
|
98 | + |
|
99 | + if ($credential === null) { |
|
100 | + return null; |
|
101 | + } |
|
102 | + |
|
103 | + return $credential->getData(); |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @param $result |
|
108 | + * |
|
109 | + * @return array |
|
110 | + */ |
|
111 | + private function parseYubicoApiResult($result) |
|
112 | + { |
|
113 | + $data = array(); |
|
114 | + foreach (explode("\r\n", $result) as $line) { |
|
115 | + $pos = strpos($line, '='); |
|
116 | + if ($pos === false) { |
|
117 | + continue; |
|
118 | + } |
|
119 | + |
|
120 | + $data[substr($line, 0, $pos)] = substr($line, $pos + 1); |
|
121 | + } |
|
122 | + |
|
123 | + return $data; |
|
124 | + } |
|
125 | + |
|
126 | + private function getYubikeyId($data) |
|
127 | + { |
|
128 | + return substr($data, 0, -32); |
|
129 | + } |
|
130 | + |
|
131 | + private function verifyHmac($apiResponse, $apiKey) |
|
132 | + { |
|
133 | + ksort($apiResponse); |
|
134 | + $signature = $apiResponse['h']; |
|
135 | + unset($apiResponse['h']); |
|
136 | + |
|
137 | + $data = array(); |
|
138 | + foreach ($apiResponse as $key => $value) { |
|
139 | + $data[] = $key . "=" . $value; |
|
140 | + } |
|
141 | + $dataString = implode('&', $data); |
|
142 | + |
|
143 | + $hmac = base64_encode(hash_hmac('sha1', $dataString, base64_decode($apiKey), true)); |
|
144 | + |
|
145 | + return $hmac === $signature; |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * @param $data |
|
150 | + * |
|
151 | + * @return bool |
|
152 | + */ |
|
153 | + private function verifyToken($data) |
|
154 | + { |
|
155 | + $result = $this->httpHelper->get('https://api.yubico.com/wsapi/2.0/verify', array( |
|
156 | + 'id' => $this->configuration->getYubicoApiId(), |
|
157 | + 'otp' => $data, |
|
158 | + 'nonce' => md5(openssl_random_pseudo_bytes(64)), |
|
159 | + )); |
|
160 | + |
|
161 | + $apiResponse = $this->parseYubicoApiResult($result); |
|
162 | + |
|
163 | + if (!$this->verifyHmac($apiResponse, $this->configuration->getYubicoApiKey())) { |
|
164 | + return false; |
|
165 | + } |
|
166 | + |
|
167 | + return $apiResponse['status'] == 'OK'; |
|
168 | + } |
|
169 | 169 | } |
@@ -22,67 +22,67 @@ |
||
22 | 22 | |
23 | 23 | class AuthenticationManager |
24 | 24 | { |
25 | - const AUTH_OK = 1; |
|
26 | - const AUTH_FAIL = 2; |
|
27 | - const AUTH_REQUIRE_NEXT_STAGE = 3; |
|
28 | - private $typeMap = array(); |
|
29 | - /** |
|
30 | - * @var PdoDatabase |
|
31 | - */ |
|
32 | - private $database; |
|
25 | + const AUTH_OK = 1; |
|
26 | + const AUTH_FAIL = 2; |
|
27 | + const AUTH_REQUIRE_NEXT_STAGE = 3; |
|
28 | + private $typeMap = array(); |
|
29 | + /** |
|
30 | + * @var PdoDatabase |
|
31 | + */ |
|
32 | + private $database; |
|
33 | 33 | |
34 | - /** |
|
35 | - * AuthenticationManager constructor. |
|
36 | - * |
|
37 | - * @param PdoDatabase $database |
|
38 | - * @param SiteConfiguration $siteConfiguration |
|
39 | - * @param HttpHelper $httpHelper |
|
40 | - */ |
|
41 | - public function __construct(PdoDatabase $database, SiteConfiguration $siteConfiguration, HttpHelper $httpHelper) |
|
42 | - { |
|
43 | - // setup providers |
|
44 | - // note on type map: this *must* be the value in the database, as this is what it maps. |
|
45 | - $this->typeMap['password'] = new PasswordCredentialProvider($database, $siteConfiguration); |
|
46 | - $this->typeMap['yubikeyotp'] = new YubikeyOtpCredentialProvider($database, $siteConfiguration, $httpHelper); |
|
47 | - $this->typeMap['totp'] = new TotpCredentialProvider($database, $siteConfiguration); |
|
48 | - $this->typeMap['scratch'] = new ScratchTokenCredentialProvider($database, $siteConfiguration); |
|
49 | - $this->typeMap['u2f'] = new U2FCredentialProvider($database, $siteConfiguration); |
|
50 | - $this->database = $database; |
|
51 | - } |
|
34 | + /** |
|
35 | + * AuthenticationManager constructor. |
|
36 | + * |
|
37 | + * @param PdoDatabase $database |
|
38 | + * @param SiteConfiguration $siteConfiguration |
|
39 | + * @param HttpHelper $httpHelper |
|
40 | + */ |
|
41 | + public function __construct(PdoDatabase $database, SiteConfiguration $siteConfiguration, HttpHelper $httpHelper) |
|
42 | + { |
|
43 | + // setup providers |
|
44 | + // note on type map: this *must* be the value in the database, as this is what it maps. |
|
45 | + $this->typeMap['password'] = new PasswordCredentialProvider($database, $siteConfiguration); |
|
46 | + $this->typeMap['yubikeyotp'] = new YubikeyOtpCredentialProvider($database, $siteConfiguration, $httpHelper); |
|
47 | + $this->typeMap['totp'] = new TotpCredentialProvider($database, $siteConfiguration); |
|
48 | + $this->typeMap['scratch'] = new ScratchTokenCredentialProvider($database, $siteConfiguration); |
|
49 | + $this->typeMap['u2f'] = new U2FCredentialProvider($database, $siteConfiguration); |
|
50 | + $this->database = $database; |
|
51 | + } |
|
52 | 52 | |
53 | - public function authenticate(User $user, $data, $stage) |
|
54 | - { |
|
55 | - $sql = 'SELECT type FROM credential WHERE user = :user AND factor = :stage AND disabled = 0 ORDER BY priority ASC'; |
|
56 | - $statement = $this->database->prepare($sql); |
|
57 | - $statement->execute(array(':user' => $user->getId(), ':stage' => $stage)); |
|
58 | - $options = $statement->fetchAll(PDO::FETCH_COLUMN); |
|
53 | + public function authenticate(User $user, $data, $stage) |
|
54 | + { |
|
55 | + $sql = 'SELECT type FROM credential WHERE user = :user AND factor = :stage AND disabled = 0 ORDER BY priority ASC'; |
|
56 | + $statement = $this->database->prepare($sql); |
|
57 | + $statement->execute(array(':user' => $user->getId(), ':stage' => $stage)); |
|
58 | + $options = $statement->fetchAll(PDO::FETCH_COLUMN); |
|
59 | 59 | |
60 | - $sql = 'SELECT count(DISTINCT factor) FROM credential WHERE user = :user AND factor > :stage AND disabled = 0 AND type <> :scratch'; |
|
61 | - $statement = $this->database->prepare($sql); |
|
62 | - $statement->execute(array(':user' => $user->getId(), ':stage' => $stage, ':scratch' => 'scratch')); |
|
63 | - $requiredFactors = $statement->fetchColumn(); |
|
60 | + $sql = 'SELECT count(DISTINCT factor) FROM credential WHERE user = :user AND factor > :stage AND disabled = 0 AND type <> :scratch'; |
|
61 | + $statement = $this->database->prepare($sql); |
|
62 | + $statement->execute(array(':user' => $user->getId(), ':stage' => $stage, ':scratch' => 'scratch')); |
|
63 | + $requiredFactors = $statement->fetchColumn(); |
|
64 | 64 | |
65 | - // prep the correct OK response based on how many factors are ahead of this one |
|
66 | - $success = self::AUTH_OK; |
|
67 | - if ($requiredFactors > 0) { |
|
68 | - $success = self::AUTH_REQUIRE_NEXT_STAGE; |
|
69 | - } |
|
65 | + // prep the correct OK response based on how many factors are ahead of this one |
|
66 | + $success = self::AUTH_OK; |
|
67 | + if ($requiredFactors > 0) { |
|
68 | + $success = self::AUTH_REQUIRE_NEXT_STAGE; |
|
69 | + } |
|
70 | 70 | |
71 | - foreach ($options as $type) { |
|
72 | - if (!isset($this->typeMap[$type])) { |
|
73 | - // does this type have a credentialProvider registered? |
|
74 | - continue; |
|
75 | - } |
|
71 | + foreach ($options as $type) { |
|
72 | + if (!isset($this->typeMap[$type])) { |
|
73 | + // does this type have a credentialProvider registered? |
|
74 | + continue; |
|
75 | + } |
|
76 | 76 | |
77 | - /** @var ICredentialProvider $credentialProvider */ |
|
78 | - $credentialProvider = $this->typeMap[$type]; |
|
79 | - if ($credentialProvider->authenticate($user, $data)) { |
|
80 | - return $success; |
|
81 | - } |
|
82 | - } |
|
77 | + /** @var ICredentialProvider $credentialProvider */ |
|
78 | + $credentialProvider = $this->typeMap[$type]; |
|
79 | + if ($credentialProvider->authenticate($user, $data)) { |
|
80 | + return $success; |
|
81 | + } |
|
82 | + } |
|
83 | 83 | |
84 | - // We've iterated over all the available providers for this stage. |
|
85 | - // They all hate you. |
|
86 | - return self::AUTH_FAIL; |
|
87 | - } |
|
84 | + // We've iterated over all the available providers for this stage. |
|
85 | + // They all hate you. |
|
86 | + return self::AUTH_FAIL; |
|
87 | + } |
|
88 | 88 | } |
89 | 89 | \ No newline at end of file |
@@ -18,43 +18,43 @@ |
||
18 | 18 | */ |
19 | 19 | class GlobalStateProvider implements IGlobalStateProvider |
20 | 20 | { |
21 | - /** |
|
22 | - * @return array |
|
23 | - */ |
|
24 | - public function &getServerSuperGlobal() |
|
25 | - { |
|
26 | - return $_SERVER; |
|
27 | - } |
|
21 | + /** |
|
22 | + * @return array |
|
23 | + */ |
|
24 | + public function &getServerSuperGlobal() |
|
25 | + { |
|
26 | + return $_SERVER; |
|
27 | + } |
|
28 | 28 | |
29 | - /** |
|
30 | - * @return array |
|
31 | - */ |
|
32 | - public function &getGetSuperGlobal() |
|
33 | - { |
|
34 | - return $_GET; |
|
35 | - } |
|
29 | + /** |
|
30 | + * @return array |
|
31 | + */ |
|
32 | + public function &getGetSuperGlobal() |
|
33 | + { |
|
34 | + return $_GET; |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * @return array |
|
39 | - */ |
|
40 | - public function &getPostSuperGlobal() |
|
41 | - { |
|
42 | - return $_POST; |
|
43 | - } |
|
37 | + /** |
|
38 | + * @return array |
|
39 | + */ |
|
40 | + public function &getPostSuperGlobal() |
|
41 | + { |
|
42 | + return $_POST; |
|
43 | + } |
|
44 | 44 | |
45 | - /** |
|
46 | - * @return array |
|
47 | - */ |
|
48 | - public function &getSessionSuperGlobal() |
|
49 | - { |
|
50 | - return $_SESSION; |
|
51 | - } |
|
45 | + /** |
|
46 | + * @return array |
|
47 | + */ |
|
48 | + public function &getSessionSuperGlobal() |
|
49 | + { |
|
50 | + return $_SESSION; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * @return array |
|
55 | - */ |
|
56 | - public function &getCookieSuperGlobal() |
|
57 | - { |
|
58 | - return $_COOKIE; |
|
59 | - } |
|
53 | + /** |
|
54 | + * @return array |
|
55 | + */ |
|
56 | + public function &getCookieSuperGlobal() |
|
57 | + { |
|
58 | + return $_COOKIE; |
|
59 | + } |
|
60 | 60 | } |
61 | 61 | \ No newline at end of file |
@@ -14,28 +14,28 @@ |
||
14 | 14 | */ |
15 | 15 | interface IGlobalStateProvider |
16 | 16 | { |
17 | - /** |
|
18 | - * @return array |
|
19 | - */ |
|
20 | - public function getServerSuperGlobal(); |
|
17 | + /** |
|
18 | + * @return array |
|
19 | + */ |
|
20 | + public function getServerSuperGlobal(); |
|
21 | 21 | |
22 | - /** |
|
23 | - * @return array |
|
24 | - */ |
|
25 | - public function getGetSuperGlobal(); |
|
22 | + /** |
|
23 | + * @return array |
|
24 | + */ |
|
25 | + public function getGetSuperGlobal(); |
|
26 | 26 | |
27 | - /** |
|
28 | - * @return array |
|
29 | - */ |
|
30 | - public function getPostSuperGlobal(); |
|
27 | + /** |
|
28 | + * @return array |
|
29 | + */ |
|
30 | + public function getPostSuperGlobal(); |
|
31 | 31 | |
32 | - /** |
|
33 | - * @return array |
|
34 | - */ |
|
35 | - public function getSessionSuperGlobal(); |
|
32 | + /** |
|
33 | + * @return array |
|
34 | + */ |
|
35 | + public function getSessionSuperGlobal(); |
|
36 | 36 | |
37 | - /** |
|
38 | - * @return array |
|
39 | - */ |
|
40 | - public function getCookieSuperGlobal(); |
|
37 | + /** |
|
38 | + * @return array |
|
39 | + */ |
|
40 | + public function getCookieSuperGlobal(); |
|
41 | 41 | } |
42 | 42 | \ No newline at end of file |
@@ -18,34 +18,34 @@ |
||
18 | 18 | */ |
19 | 19 | class FakeGlobalStateProvider extends GlobalStateProvider implements IGlobalStateProvider |
20 | 20 | { |
21 | - var $server = array(); |
|
22 | - var $get = array(); |
|
23 | - var $post = array(); |
|
24 | - var $session = array(); |
|
25 | - var $cookie = array(); |
|
21 | + var $server = array(); |
|
22 | + var $get = array(); |
|
23 | + var $post = array(); |
|
24 | + var $session = array(); |
|
25 | + var $cookie = array(); |
|
26 | 26 | |
27 | - public function &getServerSuperGlobal() |
|
28 | - { |
|
29 | - return $this->server; |
|
30 | - } |
|
27 | + public function &getServerSuperGlobal() |
|
28 | + { |
|
29 | + return $this->server; |
|
30 | + } |
|
31 | 31 | |
32 | - public function &getGetSuperGlobal() |
|
33 | - { |
|
34 | - return $this->get; |
|
35 | - } |
|
32 | + public function &getGetSuperGlobal() |
|
33 | + { |
|
34 | + return $this->get; |
|
35 | + } |
|
36 | 36 | |
37 | - public function &getPostSuperGlobal() |
|
38 | - { |
|
39 | - return $this->post; |
|
40 | - } |
|
37 | + public function &getPostSuperGlobal() |
|
38 | + { |
|
39 | + return $this->post; |
|
40 | + } |
|
41 | 41 | |
42 | - public function &getSessionSuperGlobal() |
|
43 | - { |
|
44 | - return $this->session; |
|
45 | - } |
|
42 | + public function &getSessionSuperGlobal() |
|
43 | + { |
|
44 | + return $this->session; |
|
45 | + } |
|
46 | 46 | |
47 | - public function &getCookieSuperGlobal() |
|
48 | - { |
|
49 | - return $this->cookie; |
|
50 | - } |
|
47 | + public function &getCookieSuperGlobal() |
|
48 | + { |
|
49 | + return $this->cookie; |
|
50 | + } |
|
51 | 51 | } |
52 | 52 | \ No newline at end of file |
@@ -21,107 +21,107 @@ |
||
21 | 21 | */ |
22 | 22 | class IpLocationProvider implements ILocationProvider |
23 | 23 | { |
24 | - /** @var string */ |
|
25 | - private $apiKey; |
|
26 | - /** @var PdoDatabase */ |
|
27 | - private $database; |
|
28 | - /** @var HttpHelper */ |
|
29 | - private $httpHelper; |
|
30 | - |
|
31 | - /** |
|
32 | - * IpLocationProvider constructor. |
|
33 | - * |
|
34 | - * @param PdoDatabase $database |
|
35 | - * @param string $apiKey |
|
36 | - * @param HttpHelper $httpHelper |
|
37 | - */ |
|
38 | - public function __construct(PdoDatabase $database, $apiKey, HttpHelper $httpHelper) |
|
39 | - { |
|
40 | - $this->database = $database; |
|
41 | - $this->apiKey = $apiKey; |
|
42 | - $this->httpHelper = $httpHelper; |
|
43 | - } |
|
44 | - |
|
45 | - /** |
|
46 | - * @param string $address |
|
47 | - * |
|
48 | - * @return array|null |
|
49 | - * @throws Exception |
|
50 | - * @throws OptimisticLockFailedException |
|
51 | - */ |
|
52 | - public function getIpLocation($address) |
|
53 | - { |
|
54 | - $address = trim($address); |
|
55 | - |
|
56 | - // lets look in our database first. |
|
57 | - $location = GeoLocation::getByAddress($address, $this->database, true); |
|
58 | - |
|
59 | - if ($location != null) { |
|
60 | - // touch cache timer |
|
61 | - $location->save(); |
|
62 | - |
|
63 | - return $location->getData(); |
|
64 | - } |
|
65 | - |
|
66 | - // OK, it's not there, let's do an IP2Location lookup. |
|
67 | - $result = $this->getResult($address); |
|
68 | - |
|
69 | - if ($result != null) { |
|
70 | - $location = new GeoLocation(); |
|
71 | - $location->setDatabase($this->database); |
|
72 | - $location->setAddress($address); |
|
73 | - $location->setData($result); |
|
74 | - $location->save(); |
|
75 | - |
|
76 | - return $result; |
|
77 | - } |
|
78 | - |
|
79 | - return null; |
|
80 | - } |
|
81 | - |
|
82 | - // adapted from http://www.ipinfodb.com/ip_location_api.php |
|
83 | - |
|
84 | - /** |
|
85 | - * @param string $ip |
|
86 | - * |
|
87 | - * @return array|null |
|
88 | - */ |
|
89 | - private function getResult($ip) |
|
90 | - { |
|
91 | - try { |
|
92 | - if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
93 | - $xml = $this->httpHelper->get($this->getApiBase(), array( |
|
94 | - 'key' => $this->apiKey, |
|
95 | - 'ip' => $ip, |
|
96 | - 'format' => 'xml', |
|
97 | - )); |
|
98 | - |
|
99 | - $response = @new SimpleXMLElement($xml); |
|
100 | - |
|
101 | - $result = array(); |
|
102 | - |
|
103 | - foreach ($response as $field => $value) { |
|
104 | - $result[(string)$field] = (string)$value; |
|
105 | - } |
|
106 | - |
|
107 | - return $result; |
|
108 | - } |
|
109 | - } |
|
110 | - catch (Exception $ex) { |
|
111 | - return null; |
|
112 | - |
|
113 | - // LOGME: do something smart here, or wherever we use this value. |
|
114 | - // This is just a temp hack to squash errors on the UI for now. |
|
115 | - } |
|
116 | - |
|
117 | - return null; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * @return string |
|
122 | - */ |
|
123 | - protected function getApiBase() |
|
124 | - { |
|
125 | - return "http://api.ipinfodb.com/v3/ip-city/"; |
|
126 | - } |
|
24 | + /** @var string */ |
|
25 | + private $apiKey; |
|
26 | + /** @var PdoDatabase */ |
|
27 | + private $database; |
|
28 | + /** @var HttpHelper */ |
|
29 | + private $httpHelper; |
|
30 | + |
|
31 | + /** |
|
32 | + * IpLocationProvider constructor. |
|
33 | + * |
|
34 | + * @param PdoDatabase $database |
|
35 | + * @param string $apiKey |
|
36 | + * @param HttpHelper $httpHelper |
|
37 | + */ |
|
38 | + public function __construct(PdoDatabase $database, $apiKey, HttpHelper $httpHelper) |
|
39 | + { |
|
40 | + $this->database = $database; |
|
41 | + $this->apiKey = $apiKey; |
|
42 | + $this->httpHelper = $httpHelper; |
|
43 | + } |
|
44 | + |
|
45 | + /** |
|
46 | + * @param string $address |
|
47 | + * |
|
48 | + * @return array|null |
|
49 | + * @throws Exception |
|
50 | + * @throws OptimisticLockFailedException |
|
51 | + */ |
|
52 | + public function getIpLocation($address) |
|
53 | + { |
|
54 | + $address = trim($address); |
|
55 | + |
|
56 | + // lets look in our database first. |
|
57 | + $location = GeoLocation::getByAddress($address, $this->database, true); |
|
58 | + |
|
59 | + if ($location != null) { |
|
60 | + // touch cache timer |
|
61 | + $location->save(); |
|
62 | + |
|
63 | + return $location->getData(); |
|
64 | + } |
|
65 | + |
|
66 | + // OK, it's not there, let's do an IP2Location lookup. |
|
67 | + $result = $this->getResult($address); |
|
68 | + |
|
69 | + if ($result != null) { |
|
70 | + $location = new GeoLocation(); |
|
71 | + $location->setDatabase($this->database); |
|
72 | + $location->setAddress($address); |
|
73 | + $location->setData($result); |
|
74 | + $location->save(); |
|
75 | + |
|
76 | + return $result; |
|
77 | + } |
|
78 | + |
|
79 | + return null; |
|
80 | + } |
|
81 | + |
|
82 | + // adapted from http://www.ipinfodb.com/ip_location_api.php |
|
83 | + |
|
84 | + /** |
|
85 | + * @param string $ip |
|
86 | + * |
|
87 | + * @return array|null |
|
88 | + */ |
|
89 | + private function getResult($ip) |
|
90 | + { |
|
91 | + try { |
|
92 | + if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { |
|
93 | + $xml = $this->httpHelper->get($this->getApiBase(), array( |
|
94 | + 'key' => $this->apiKey, |
|
95 | + 'ip' => $ip, |
|
96 | + 'format' => 'xml', |
|
97 | + )); |
|
98 | + |
|
99 | + $response = @new SimpleXMLElement($xml); |
|
100 | + |
|
101 | + $result = array(); |
|
102 | + |
|
103 | + foreach ($response as $field => $value) { |
|
104 | + $result[(string)$field] = (string)$value; |
|
105 | + } |
|
106 | + |
|
107 | + return $result; |
|
108 | + } |
|
109 | + } |
|
110 | + catch (Exception $ex) { |
|
111 | + return null; |
|
112 | + |
|
113 | + // LOGME: do something smart here, or wherever we use this value. |
|
114 | + // This is just a temp hack to squash errors on the UI for now. |
|
115 | + } |
|
116 | + |
|
117 | + return null; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * @return string |
|
122 | + */ |
|
123 | + protected function getApiBase() |
|
124 | + { |
|
125 | + return "http://api.ipinfodb.com/v3/ip-city/"; |
|
126 | + } |
|
127 | 127 | } |
@@ -26,133 +26,133 @@ discard block |
||
26 | 26 | */ |
27 | 27 | class IdentificationVerifier |
28 | 28 | { |
29 | - /** |
|
30 | - * This field is an array of parameters, in key => value format, that should be appended to the Meta Wikimedia |
|
31 | - * Web Service Endpoint URL to query if a user is listed on the Identification Noticeboard. Note that URL encoding |
|
32 | - * of these values is *not* necessary; this is done automatically. |
|
33 | - * |
|
34 | - * @var string[] |
|
35 | - * @category Security-Critical |
|
36 | - */ |
|
37 | - private static $apiQueryParameters = array( |
|
38 | - 'action' => 'query', |
|
39 | - 'format' => 'json', |
|
40 | - 'prop' => 'links', |
|
41 | - // Populated from SiteConfiguration->getIdentificationNoticeboardPage |
|
42 | - 'titles' => '', |
|
43 | - // Username of the user to be checked, with User: prefix, goes here! Set in isIdentifiedOnWiki() |
|
44 | - 'pltitles' => '', |
|
45 | - ); |
|
46 | - /** @var HttpHelper */ |
|
47 | - private $httpHelper; |
|
48 | - /** @var SiteConfiguration */ |
|
49 | - private $siteConfiguration; |
|
50 | - /** @var PdoDatabase */ |
|
51 | - private $dbObject; |
|
52 | - |
|
53 | - /** |
|
54 | - * IdentificationVerifier constructor. |
|
55 | - * |
|
56 | - * @param HttpHelper $httpHelper |
|
57 | - * @param SiteConfiguration $siteConfiguration |
|
58 | - * @param PdoDatabase $dbObject |
|
59 | - */ |
|
60 | - public function __construct(HttpHelper $httpHelper, SiteConfiguration $siteConfiguration, PdoDatabase $dbObject) |
|
61 | - { |
|
62 | - $this->httpHelper = $httpHelper; |
|
63 | - $this->siteConfiguration = $siteConfiguration; |
|
64 | - $this->dbObject = $dbObject; |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * Checks if the given user is identified to the Wikimedia Foundation. |
|
69 | - * |
|
70 | - * @param string $onWikiName The Wikipedia username of the user |
|
71 | - * |
|
72 | - * @return bool |
|
73 | - * @category Security-Critical |
|
74 | - * @throws EnvironmentException |
|
75 | - */ |
|
76 | - public function isUserIdentified($onWikiName) |
|
77 | - { |
|
78 | - if ($this->checkIdentificationCache($onWikiName)) { |
|
79 | - return true; |
|
80 | - } |
|
81 | - else { |
|
82 | - if ($this->isIdentifiedOnWiki($onWikiName)) { |
|
83 | - $this->cacheIdentificationStatus($onWikiName); |
|
84 | - |
|
85 | - return true; |
|
86 | - } |
|
87 | - else { |
|
88 | - return false; |
|
89 | - } |
|
90 | - } |
|
91 | - } |
|
92 | - |
|
93 | - /** |
|
94 | - * Checks if the given user has a valid entry in the idcache table. |
|
95 | - * |
|
96 | - * @param string $onWikiName The Wikipedia username of the user |
|
97 | - * |
|
98 | - * @return bool |
|
99 | - * @category Security-Critical |
|
100 | - */ |
|
101 | - private function checkIdentificationCache($onWikiName) |
|
102 | - { |
|
103 | - $interval = $this->siteConfiguration->getIdentificationCacheExpiry(); |
|
104 | - |
|
105 | - $query = <<<SQL |
|
29 | + /** |
|
30 | + * This field is an array of parameters, in key => value format, that should be appended to the Meta Wikimedia |
|
31 | + * Web Service Endpoint URL to query if a user is listed on the Identification Noticeboard. Note that URL encoding |
|
32 | + * of these values is *not* necessary; this is done automatically. |
|
33 | + * |
|
34 | + * @var string[] |
|
35 | + * @category Security-Critical |
|
36 | + */ |
|
37 | + private static $apiQueryParameters = array( |
|
38 | + 'action' => 'query', |
|
39 | + 'format' => 'json', |
|
40 | + 'prop' => 'links', |
|
41 | + // Populated from SiteConfiguration->getIdentificationNoticeboardPage |
|
42 | + 'titles' => '', |
|
43 | + // Username of the user to be checked, with User: prefix, goes here! Set in isIdentifiedOnWiki() |
|
44 | + 'pltitles' => '', |
|
45 | + ); |
|
46 | + /** @var HttpHelper */ |
|
47 | + private $httpHelper; |
|
48 | + /** @var SiteConfiguration */ |
|
49 | + private $siteConfiguration; |
|
50 | + /** @var PdoDatabase */ |
|
51 | + private $dbObject; |
|
52 | + |
|
53 | + /** |
|
54 | + * IdentificationVerifier constructor. |
|
55 | + * |
|
56 | + * @param HttpHelper $httpHelper |
|
57 | + * @param SiteConfiguration $siteConfiguration |
|
58 | + * @param PdoDatabase $dbObject |
|
59 | + */ |
|
60 | + public function __construct(HttpHelper $httpHelper, SiteConfiguration $siteConfiguration, PdoDatabase $dbObject) |
|
61 | + { |
|
62 | + $this->httpHelper = $httpHelper; |
|
63 | + $this->siteConfiguration = $siteConfiguration; |
|
64 | + $this->dbObject = $dbObject; |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * Checks if the given user is identified to the Wikimedia Foundation. |
|
69 | + * |
|
70 | + * @param string $onWikiName The Wikipedia username of the user |
|
71 | + * |
|
72 | + * @return bool |
|
73 | + * @category Security-Critical |
|
74 | + * @throws EnvironmentException |
|
75 | + */ |
|
76 | + public function isUserIdentified($onWikiName) |
|
77 | + { |
|
78 | + if ($this->checkIdentificationCache($onWikiName)) { |
|
79 | + return true; |
|
80 | + } |
|
81 | + else { |
|
82 | + if ($this->isIdentifiedOnWiki($onWikiName)) { |
|
83 | + $this->cacheIdentificationStatus($onWikiName); |
|
84 | + |
|
85 | + return true; |
|
86 | + } |
|
87 | + else { |
|
88 | + return false; |
|
89 | + } |
|
90 | + } |
|
91 | + } |
|
92 | + |
|
93 | + /** |
|
94 | + * Checks if the given user has a valid entry in the idcache table. |
|
95 | + * |
|
96 | + * @param string $onWikiName The Wikipedia username of the user |
|
97 | + * |
|
98 | + * @return bool |
|
99 | + * @category Security-Critical |
|
100 | + */ |
|
101 | + private function checkIdentificationCache($onWikiName) |
|
102 | + { |
|
103 | + $interval = $this->siteConfiguration->getIdentificationCacheExpiry(); |
|
104 | + |
|
105 | + $query = <<<SQL |
|
106 | 106 | SELECT COUNT(`id`) |
107 | 107 | FROM `idcache` |
108 | 108 | WHERE `onwikiusername` = :onwikiname |
109 | 109 | AND DATE_ADD(`checktime`, INTERVAL {$interval}) >= NOW(); |
110 | 110 | SQL; |
111 | - $stmt = $this->dbObject->prepare($query); |
|
112 | - $stmt->bindValue(':onwikiname', $onWikiName, PDO::PARAM_STR); |
|
113 | - $stmt->execute(); |
|
114 | - |
|
115 | - // Guaranteed by the query to only return a single row with a single column |
|
116 | - $results = $stmt->fetch(PDO::FETCH_NUM); |
|
117 | - |
|
118 | - // I don't expect this to ever be a value other than 0 or 1 since the `onwikiusername` column is declared as a |
|
119 | - // unique key - but meh. |
|
120 | - return $results[0] != 0; |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Does pretty much exactly what it says on the label - this method will clear all expired idcache entries from the |
|
125 | - * idcache table. Meant to be called periodically by a maintenance script. |
|
126 | - * |
|
127 | - * @param SiteConfiguration $siteConfiguration |
|
128 | - * @param PdoDatabase $dbObject |
|
129 | - * |
|
130 | - * @return void |
|
131 | - */ |
|
132 | - public static function clearExpiredCacheEntries(SiteConfiguration $siteConfiguration, PdoDatabase $dbObject) |
|
133 | - { |
|
134 | - $interval = $siteConfiguration->getIdentificationCacheExpiry(); |
|
135 | - |
|
136 | - $query = <<<SQL |
|
111 | + $stmt = $this->dbObject->prepare($query); |
|
112 | + $stmt->bindValue(':onwikiname', $onWikiName, PDO::PARAM_STR); |
|
113 | + $stmt->execute(); |
|
114 | + |
|
115 | + // Guaranteed by the query to only return a single row with a single column |
|
116 | + $results = $stmt->fetch(PDO::FETCH_NUM); |
|
117 | + |
|
118 | + // I don't expect this to ever be a value other than 0 or 1 since the `onwikiusername` column is declared as a |
|
119 | + // unique key - but meh. |
|
120 | + return $results[0] != 0; |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Does pretty much exactly what it says on the label - this method will clear all expired idcache entries from the |
|
125 | + * idcache table. Meant to be called periodically by a maintenance script. |
|
126 | + * |
|
127 | + * @param SiteConfiguration $siteConfiguration |
|
128 | + * @param PdoDatabase $dbObject |
|
129 | + * |
|
130 | + * @return void |
|
131 | + */ |
|
132 | + public static function clearExpiredCacheEntries(SiteConfiguration $siteConfiguration, PdoDatabase $dbObject) |
|
133 | + { |
|
134 | + $interval = $siteConfiguration->getIdentificationCacheExpiry(); |
|
135 | + |
|
136 | + $query = <<<SQL |
|
137 | 137 | DELETE FROM `idcache` |
138 | 138 | WHERE DATE_ADD(`checktime`, INTERVAL {$interval}) < NOW(); |
139 | 139 | SQL; |
140 | - $dbObject->prepare($query)->execute(); |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * This method will add an entry to the idcache that the given Wikipedia user has been verified as identified. This |
|
145 | - * is so we don't have to hit the API every single time we check. The cache entry is valid for as long as specified |
|
146 | - * in the ACC configuration (validity enforced by checkIdentificationCache() and clearExpiredCacheEntries()). |
|
147 | - * |
|
148 | - * @param string $onWikiName The Wikipedia username of the user |
|
149 | - * |
|
150 | - * @return void |
|
151 | - * @category Security-Critical |
|
152 | - */ |
|
153 | - private function cacheIdentificationStatus($onWikiName) |
|
154 | - { |
|
155 | - $query = <<<SQL |
|
140 | + $dbObject->prepare($query)->execute(); |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * This method will add an entry to the idcache that the given Wikipedia user has been verified as identified. This |
|
145 | + * is so we don't have to hit the API every single time we check. The cache entry is valid for as long as specified |
|
146 | + * in the ACC configuration (validity enforced by checkIdentificationCache() and clearExpiredCacheEntries()). |
|
147 | + * |
|
148 | + * @param string $onWikiName The Wikipedia username of the user |
|
149 | + * |
|
150 | + * @return void |
|
151 | + * @category Security-Critical |
|
152 | + */ |
|
153 | + private function cacheIdentificationStatus($onWikiName) |
|
154 | + { |
|
155 | + $query = <<<SQL |
|
156 | 156 | INSERT INTO `idcache` |
157 | 157 | (`onwikiusername`) |
158 | 158 | VALUES |
@@ -161,45 +161,45 @@ discard block |
||
161 | 161 | `onwikiusername` = VALUES(`onwikiusername`), |
162 | 162 | `checktime` = CURRENT_TIMESTAMP; |
163 | 163 | SQL; |
164 | - $stmt = $this->dbObject->prepare($query); |
|
165 | - $stmt->bindValue(':onwikiname', $onWikiName, PDO::PARAM_STR); |
|
166 | - $stmt->execute(); |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Queries the Wikimedia API to determine if the specified user is listed on the identification noticeboard. |
|
171 | - * |
|
172 | - * @param string $onWikiName The Wikipedia username of the user |
|
173 | - * |
|
174 | - * @return bool |
|
175 | - * @throws EnvironmentException |
|
176 | - * @category Security-Critical |
|
177 | - */ |
|
178 | - private function isIdentifiedOnWiki($onWikiName) |
|
179 | - { |
|
180 | - $strings = new StringFunctions(); |
|
181 | - |
|
182 | - // First character of Wikipedia usernames is always capitalized. |
|
183 | - $onWikiName = $strings->upperCaseFirst($onWikiName); |
|
184 | - |
|
185 | - $parameters = self::$apiQueryParameters; |
|
186 | - $parameters['pltitles'] = "User:" . $onWikiName; |
|
187 | - $parameters['titles'] = $this->siteConfiguration->getIdentificationNoticeboardPage(); |
|
188 | - |
|
189 | - try { |
|
190 | - $endpoint = $this->siteConfiguration->getMetaWikimediaWebServiceEndpoint(); |
|
191 | - $response = $this->httpHelper->get($endpoint, $parameters); |
|
192 | - $response = json_decode($response, true); |
|
193 | - } catch (CurlException $ex) { |
|
194 | - // failed getting identification status, so throw a nicer error. |
|
195 | - $message = 'Could not contact metawiki API to determine user\' identification status. ' |
|
196 | - . 'This is probably a transient error, so please try again.'; |
|
197 | - |
|
198 | - throw new EnvironmentException($message); |
|
199 | - } |
|
200 | - |
|
201 | - $page = @array_pop($response['query']['pages']); |
|
202 | - |
|
203 | - return @$page['links'][0]['title'] === "User:" . $onWikiName; |
|
204 | - } |
|
164 | + $stmt = $this->dbObject->prepare($query); |
|
165 | + $stmt->bindValue(':onwikiname', $onWikiName, PDO::PARAM_STR); |
|
166 | + $stmt->execute(); |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Queries the Wikimedia API to determine if the specified user is listed on the identification noticeboard. |
|
171 | + * |
|
172 | + * @param string $onWikiName The Wikipedia username of the user |
|
173 | + * |
|
174 | + * @return bool |
|
175 | + * @throws EnvironmentException |
|
176 | + * @category Security-Critical |
|
177 | + */ |
|
178 | + private function isIdentifiedOnWiki($onWikiName) |
|
179 | + { |
|
180 | + $strings = new StringFunctions(); |
|
181 | + |
|
182 | + // First character of Wikipedia usernames is always capitalized. |
|
183 | + $onWikiName = $strings->upperCaseFirst($onWikiName); |
|
184 | + |
|
185 | + $parameters = self::$apiQueryParameters; |
|
186 | + $parameters['pltitles'] = "User:" . $onWikiName; |
|
187 | + $parameters['titles'] = $this->siteConfiguration->getIdentificationNoticeboardPage(); |
|
188 | + |
|
189 | + try { |
|
190 | + $endpoint = $this->siteConfiguration->getMetaWikimediaWebServiceEndpoint(); |
|
191 | + $response = $this->httpHelper->get($endpoint, $parameters); |
|
192 | + $response = json_decode($response, true); |
|
193 | + } catch (CurlException $ex) { |
|
194 | + // failed getting identification status, so throw a nicer error. |
|
195 | + $message = 'Could not contact metawiki API to determine user\' identification status. ' |
|
196 | + . 'This is probably a transient error, so please try again.'; |
|
197 | + |
|
198 | + throw new EnvironmentException($message); |
|
199 | + } |
|
200 | + |
|
201 | + $page = @array_pop($response['query']['pages']); |
|
202 | + |
|
203 | + return @$page['links'][0]['title'] === "User:" . $onWikiName; |
|
204 | + } |
|
205 | 205 | } |
@@ -77,14 +77,12 @@ discard block |
||
77 | 77 | { |
78 | 78 | if ($this->checkIdentificationCache($onWikiName)) { |
79 | 79 | return true; |
80 | - } |
|
81 | - else { |
|
80 | + } else { |
|
82 | 81 | if ($this->isIdentifiedOnWiki($onWikiName)) { |
83 | 82 | $this->cacheIdentificationStatus($onWikiName); |
84 | 83 | |
85 | 84 | return true; |
86 | - } |
|
87 | - else { |
|
85 | + } else { |
|
88 | 86 | return false; |
89 | 87 | } |
90 | 88 | } |
@@ -190,7 +188,8 @@ discard block |
||
190 | 188 | $endpoint = $this->siteConfiguration->getMetaWikimediaWebServiceEndpoint(); |
191 | 189 | $response = $this->httpHelper->get($endpoint, $parameters); |
192 | 190 | $response = json_decode($response, true); |
193 | - } catch (CurlException $ex) { |
|
191 | + } |
|
192 | + catch (CurlException $ex) { |
|
194 | 193 | // failed getting identification status, so throw a nicer error. |
195 | 194 | $message = 'Could not contact metawiki API to determine user\' identification status. ' |
196 | 195 | . 'This is probably a transient error, so please try again.'; |