@@ -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 | } |
@@ -15,137 +15,137 @@ |
||
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 | - // get this factor |
|
98 | - $statement = $this->database->prepare('SELECT * FROM credential WHERE user = :user AND type = :type'); |
|
99 | - $statement->execute(array(':user' => $user->getId(), ':type' => $this->type)); |
|
100 | - /** @var Credential $credential */ |
|
101 | - $credential = $statement->fetchObject(Credential::class); |
|
102 | - $credential->setDatabase($this->database); |
|
103 | - $statement->closeCursor(); |
|
104 | - |
|
105 | - $stage = $credential->getFactor(); |
|
106 | - |
|
107 | - $statement = $this->database->prepare('SELECT COUNT(*) FROM credential WHERE user = :user AND factor = :factor'); |
|
108 | - $statement->execute(array(':user' => $user->getId(), ':factor' => $stage)); |
|
109 | - $alternates = $statement->fetchColumn(); |
|
110 | - $statement->closeCursor(); |
|
111 | - |
|
112 | - if($alternates <= 1) { |
|
113 | - // decrement the factor for every stage above this |
|
114 | - $sql = 'UPDATE credential SET factor = factor - 1 WHERE user = :user AND factor > :factor'; |
|
115 | - $statement = $this->database->prepare($sql); |
|
116 | - $statement->execute(array(':user' => $user->getId(), ':factor' => $stage)); |
|
117 | - } |
|
118 | - else { |
|
119 | - // There are other auth factors at this point. Don't renumber the factors just yet. |
|
120 | - } |
|
121 | - |
|
122 | - // delete this credential. |
|
123 | - $credential->delete(); |
|
124 | - } |
|
125 | - |
|
126 | - /** |
|
127 | - * @param User $user |
|
128 | - * |
|
129 | - * @return Credential |
|
130 | - */ |
|
131 | - protected function createNewCredential(User $user) |
|
132 | - { |
|
133 | - $credential = new Credential(); |
|
134 | - $credential->setDatabase($this->getDatabase()); |
|
135 | - $credential->setUserId($user->getId()); |
|
136 | - $credential->setType($this->type); |
|
137 | - |
|
138 | - return $credential; |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * @param int $userId |
|
143 | - * |
|
144 | - * @return bool |
|
145 | - */ |
|
146 | - public function userIsEnrolled($userId) { |
|
147 | - $cred = $this->getCredentialData($userId); |
|
148 | - |
|
149 | - return $cred !== null; |
|
150 | - } |
|
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 | + // get this factor |
|
98 | + $statement = $this->database->prepare('SELECT * FROM credential WHERE user = :user AND type = :type'); |
|
99 | + $statement->execute(array(':user' => $user->getId(), ':type' => $this->type)); |
|
100 | + /** @var Credential $credential */ |
|
101 | + $credential = $statement->fetchObject(Credential::class); |
|
102 | + $credential->setDatabase($this->database); |
|
103 | + $statement->closeCursor(); |
|
104 | + |
|
105 | + $stage = $credential->getFactor(); |
|
106 | + |
|
107 | + $statement = $this->database->prepare('SELECT COUNT(*) FROM credential WHERE user = :user AND factor = :factor'); |
|
108 | + $statement->execute(array(':user' => $user->getId(), ':factor' => $stage)); |
|
109 | + $alternates = $statement->fetchColumn(); |
|
110 | + $statement->closeCursor(); |
|
111 | + |
|
112 | + if($alternates <= 1) { |
|
113 | + // decrement the factor for every stage above this |
|
114 | + $sql = 'UPDATE credential SET factor = factor - 1 WHERE user = :user AND factor > :factor'; |
|
115 | + $statement = $this->database->prepare($sql); |
|
116 | + $statement->execute(array(':user' => $user->getId(), ':factor' => $stage)); |
|
117 | + } |
|
118 | + else { |
|
119 | + // There are other auth factors at this point. Don't renumber the factors just yet. |
|
120 | + } |
|
121 | + |
|
122 | + // delete this credential. |
|
123 | + $credential->delete(); |
|
124 | + } |
|
125 | + |
|
126 | + /** |
|
127 | + * @param User $user |
|
128 | + * |
|
129 | + * @return Credential |
|
130 | + */ |
|
131 | + protected function createNewCredential(User $user) |
|
132 | + { |
|
133 | + $credential = new Credential(); |
|
134 | + $credential->setDatabase($this->getDatabase()); |
|
135 | + $credential->setUserId($user->getId()); |
|
136 | + $credential->setType($this->type); |
|
137 | + |
|
138 | + return $credential; |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * @param int $userId |
|
143 | + * |
|
144 | + * @return bool |
|
145 | + */ |
|
146 | + public function userIsEnrolled($userId) { |
|
147 | + $cred = $this->getCredentialData($userId); |
|
148 | + |
|
149 | + return $cred !== null; |
|
150 | + } |
|
151 | 151 | } |
152 | 152 | \ No newline at end of file |
@@ -19,136 +19,136 @@ |
||
19 | 19 | |
20 | 20 | class TotpCredentialProvider extends CredentialProviderBase |
21 | 21 | { |
22 | - /** @var EncryptionHelper */ |
|
23 | - private $encryptionHelper; |
|
24 | - |
|
25 | - /** |
|
26 | - * TotpCredentialProvider constructor. |
|
27 | - * |
|
28 | - * @param PdoDatabase $database |
|
29 | - * @param SiteConfiguration $configuration |
|
30 | - */ |
|
31 | - public function __construct(PdoDatabase $database, SiteConfiguration $configuration) |
|
32 | - { |
|
33 | - parent::__construct($database, $configuration, 'totp'); |
|
34 | - $this->encryptionHelper = new EncryptionHelper($configuration); |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Validates a user-provided credential |
|
39 | - * |
|
40 | - * @param User $user The user to test the authentication against |
|
41 | - * @param string $data The raw credential data to be validated |
|
42 | - * |
|
43 | - * @return bool |
|
44 | - * @throws ApplicationLogicException |
|
45 | - */ |
|
46 | - public function authenticate(User $user, $data) |
|
47 | - { |
|
48 | - if (is_array($data)) { |
|
49 | - return false; |
|
50 | - } |
|
51 | - |
|
52 | - $storedData = $this->getCredentialData($user->getId()); |
|
53 | - |
|
54 | - if ($storedData === null) { |
|
55 | - throw new ApplicationLogicException('Credential data not found'); |
|
56 | - } |
|
57 | - |
|
58 | - $provisioningUrl = $this->encryptionHelper->decryptData($storedData->getData()); |
|
59 | - $totp = Factory::loadFromProvisioningUri($provisioningUrl); |
|
60 | - |
|
61 | - return $totp->verify($data, null, 2); |
|
62 | - } |
|
63 | - |
|
64 | - public function verifyEnable(User $user, $data) |
|
65 | - { |
|
66 | - $storedData = $this->getCredentialData($user->getId(), true); |
|
67 | - |
|
68 | - if ($storedData === null) { |
|
69 | - throw new ApplicationLogicException('Credential data not found'); |
|
70 | - } |
|
71 | - |
|
72 | - $provisioningUrl = $this->encryptionHelper->decryptData($storedData->getData()); |
|
73 | - $totp = Factory::loadFromProvisioningUri($provisioningUrl); |
|
74 | - |
|
75 | - $result = $totp->verify($data, null, 2); |
|
76 | - |
|
77 | - if ($result && $storedData->getTimeout() > new DateTimeImmutable()) { |
|
78 | - $storedData->setDisabled(0); |
|
79 | - $storedData->setPriority(5); |
|
80 | - $storedData->setTimeout(null); |
|
81 | - $storedData->save(); |
|
82 | - } |
|
83 | - |
|
84 | - return $result; |
|
85 | - } |
|
86 | - |
|
87 | - /** |
|
88 | - * @param User $user The user the credential belongs to |
|
89 | - * @param int $factor The factor this credential provides |
|
90 | - * @param string $data Unused here, due to there being no user-provided data. We provide the user with the secret. |
|
91 | - */ |
|
92 | - public function setCredential(User $user, $factor, $data) |
|
93 | - { |
|
94 | - $issuer = 'ACC - ' . $this->getConfiguration()->getIrcNotificationsInstance(); |
|
95 | - $totp = new TOTP($user->getUsername()); |
|
96 | - $totp->setIssuer($issuer); |
|
97 | - |
|
98 | - $storedData = $this->getCredentialData($user->getId(), null); |
|
99 | - |
|
100 | - if ($storedData !== null) { |
|
101 | - $storedData->delete(); |
|
102 | - } |
|
103 | - |
|
104 | - $storedData = $this->createNewCredential($user); |
|
105 | - |
|
106 | - $storedData->setData($this->encryptionHelper->encryptData($totp->getProvisioningUri())); |
|
107 | - $storedData->setFactor($factor); |
|
108 | - $storedData->setTimeout(new DateTimeImmutable('+ 1 hour')); |
|
109 | - $storedData->setDisabled(1); |
|
110 | - $storedData->setVersion(1); |
|
111 | - |
|
112 | - $storedData->save(); |
|
113 | - } |
|
114 | - |
|
115 | - public function getProvisioningUrl(User $user) |
|
116 | - { |
|
117 | - $storedData = $this->getCredentialData($user->getId(), true); |
|
118 | - |
|
119 | - if ($storedData->getTimeout() < new DateTimeImmutable()) { |
|
120 | - $storedData->delete(); |
|
121 | - $storedData = null; |
|
122 | - } |
|
123 | - |
|
124 | - if ($storedData === null) { |
|
125 | - throw new ApplicationLogicException('Credential data not found'); |
|
126 | - } |
|
127 | - |
|
128 | - return $this->encryptionHelper->decryptData($storedData->getData()); |
|
129 | - } |
|
130 | - |
|
131 | - public function isPartiallyEnrolled(User $user) |
|
132 | - { |
|
133 | - $storedData = $this->getCredentialData($user->getId(), true); |
|
134 | - |
|
135 | - if ($storedData->getTimeout() < new DateTimeImmutable()) { |
|
136 | - $storedData->delete(); |
|
137 | - |
|
138 | - return false; |
|
139 | - } |
|
140 | - |
|
141 | - if ($storedData === null) { |
|
142 | - return false; |
|
143 | - } |
|
22 | + /** @var EncryptionHelper */ |
|
23 | + private $encryptionHelper; |
|
24 | + |
|
25 | + /** |
|
26 | + * TotpCredentialProvider constructor. |
|
27 | + * |
|
28 | + * @param PdoDatabase $database |
|
29 | + * @param SiteConfiguration $configuration |
|
30 | + */ |
|
31 | + public function __construct(PdoDatabase $database, SiteConfiguration $configuration) |
|
32 | + { |
|
33 | + parent::__construct($database, $configuration, 'totp'); |
|
34 | + $this->encryptionHelper = new EncryptionHelper($configuration); |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Validates a user-provided credential |
|
39 | + * |
|
40 | + * @param User $user The user to test the authentication against |
|
41 | + * @param string $data The raw credential data to be validated |
|
42 | + * |
|
43 | + * @return bool |
|
44 | + * @throws ApplicationLogicException |
|
45 | + */ |
|
46 | + public function authenticate(User $user, $data) |
|
47 | + { |
|
48 | + if (is_array($data)) { |
|
49 | + return false; |
|
50 | + } |
|
51 | + |
|
52 | + $storedData = $this->getCredentialData($user->getId()); |
|
53 | + |
|
54 | + if ($storedData === null) { |
|
55 | + throw new ApplicationLogicException('Credential data not found'); |
|
56 | + } |
|
57 | + |
|
58 | + $provisioningUrl = $this->encryptionHelper->decryptData($storedData->getData()); |
|
59 | + $totp = Factory::loadFromProvisioningUri($provisioningUrl); |
|
60 | + |
|
61 | + return $totp->verify($data, null, 2); |
|
62 | + } |
|
63 | + |
|
64 | + public function verifyEnable(User $user, $data) |
|
65 | + { |
|
66 | + $storedData = $this->getCredentialData($user->getId(), true); |
|
67 | + |
|
68 | + if ($storedData === null) { |
|
69 | + throw new ApplicationLogicException('Credential data not found'); |
|
70 | + } |
|
71 | + |
|
72 | + $provisioningUrl = $this->encryptionHelper->decryptData($storedData->getData()); |
|
73 | + $totp = Factory::loadFromProvisioningUri($provisioningUrl); |
|
74 | + |
|
75 | + $result = $totp->verify($data, null, 2); |
|
76 | + |
|
77 | + if ($result && $storedData->getTimeout() > new DateTimeImmutable()) { |
|
78 | + $storedData->setDisabled(0); |
|
79 | + $storedData->setPriority(5); |
|
80 | + $storedData->setTimeout(null); |
|
81 | + $storedData->save(); |
|
82 | + } |
|
83 | + |
|
84 | + return $result; |
|
85 | + } |
|
86 | + |
|
87 | + /** |
|
88 | + * @param User $user The user the credential belongs to |
|
89 | + * @param int $factor The factor this credential provides |
|
90 | + * @param string $data Unused here, due to there being no user-provided data. We provide the user with the secret. |
|
91 | + */ |
|
92 | + public function setCredential(User $user, $factor, $data) |
|
93 | + { |
|
94 | + $issuer = 'ACC - ' . $this->getConfiguration()->getIrcNotificationsInstance(); |
|
95 | + $totp = new TOTP($user->getUsername()); |
|
96 | + $totp->setIssuer($issuer); |
|
97 | + |
|
98 | + $storedData = $this->getCredentialData($user->getId(), null); |
|
99 | + |
|
100 | + if ($storedData !== null) { |
|
101 | + $storedData->delete(); |
|
102 | + } |
|
103 | + |
|
104 | + $storedData = $this->createNewCredential($user); |
|
105 | + |
|
106 | + $storedData->setData($this->encryptionHelper->encryptData($totp->getProvisioningUri())); |
|
107 | + $storedData->setFactor($factor); |
|
108 | + $storedData->setTimeout(new DateTimeImmutable('+ 1 hour')); |
|
109 | + $storedData->setDisabled(1); |
|
110 | + $storedData->setVersion(1); |
|
111 | + |
|
112 | + $storedData->save(); |
|
113 | + } |
|
114 | + |
|
115 | + public function getProvisioningUrl(User $user) |
|
116 | + { |
|
117 | + $storedData = $this->getCredentialData($user->getId(), true); |
|
118 | + |
|
119 | + if ($storedData->getTimeout() < new DateTimeImmutable()) { |
|
120 | + $storedData->delete(); |
|
121 | + $storedData = null; |
|
122 | + } |
|
123 | + |
|
124 | + if ($storedData === null) { |
|
125 | + throw new ApplicationLogicException('Credential data not found'); |
|
126 | + } |
|
127 | + |
|
128 | + return $this->encryptionHelper->decryptData($storedData->getData()); |
|
129 | + } |
|
130 | + |
|
131 | + public function isPartiallyEnrolled(User $user) |
|
132 | + { |
|
133 | + $storedData = $this->getCredentialData($user->getId(), true); |
|
134 | + |
|
135 | + if ($storedData->getTimeout() < new DateTimeImmutable()) { |
|
136 | + $storedData->delete(); |
|
137 | + |
|
138 | + return false; |
|
139 | + } |
|
140 | + |
|
141 | + if ($storedData === null) { |
|
142 | + return false; |
|
143 | + } |
|
144 | 144 | |
145 | - return true; |
|
146 | - } |
|
145 | + return true; |
|
146 | + } |
|
147 | 147 | |
148 | - public function getSecret(User $user) |
|
149 | - { |
|
150 | - $totp = Factory::loadFromProvisioningUri($this->getProvisioningUrl($user)); |
|
148 | + public function getSecret(User $user) |
|
149 | + { |
|
150 | + $totp = Factory::loadFromProvisioningUri($this->getProvisioningUrl($user)); |
|
151 | 151 | |
152 | - return $totp->getSecret(); |
|
153 | - } |
|
152 | + return $totp->getSecret(); |
|
153 | + } |
|
154 | 154 | } |
155 | 155 | \ No newline at end of file |
@@ -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 |
@@ -12,48 +12,48 @@ |
||
12 | 12 | |
13 | 13 | class EncryptionHelper |
14 | 14 | { |
15 | - /** |
|
16 | - * @var SiteConfiguration |
|
17 | - */ |
|
18 | - private $configuration; |
|
19 | - |
|
20 | - /** |
|
21 | - * EncryptionHelper constructor. |
|
22 | - * |
|
23 | - * @param SiteConfiguration $configuration |
|
24 | - */ |
|
25 | - public function __construct(SiteConfiguration $configuration) |
|
26 | - { |
|
27 | - $this->configuration = $configuration; |
|
28 | - } |
|
29 | - |
|
30 | - public function encryptData($secret) |
|
31 | - { |
|
32 | - $iv = openssl_random_pseudo_bytes(16); |
|
33 | - $password = $this->getEncryptionKey(); |
|
34 | - $encryptedKey = openssl_encrypt($secret, 'aes-256-ctr', $password, OPENSSL_RAW_DATA, $iv); |
|
35 | - |
|
36 | - $data = base64_encode($iv) . '|' . base64_encode($encryptedKey); |
|
37 | - |
|
38 | - return $data; |
|
39 | - } |
|
40 | - |
|
41 | - public function decryptData($data) |
|
42 | - { |
|
43 | - list($iv, $encryptedKey) = array_map('base64_decode', explode('|', $data)); |
|
44 | - |
|
45 | - $password = $this->getEncryptionKey(); |
|
46 | - |
|
47 | - $secret = openssl_decrypt($encryptedKey, 'aes-256-ctr', $password, OPENSSL_RAW_DATA, $iv); |
|
48 | - |
|
49 | - return $secret; |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * @return string |
|
54 | - */ |
|
55 | - private function getEncryptionKey() |
|
56 | - { |
|
57 | - return openssl_digest($this->configuration->getTotpEncryptionKey(), 'sha256'); |
|
58 | - } |
|
15 | + /** |
|
16 | + * @var SiteConfiguration |
|
17 | + */ |
|
18 | + private $configuration; |
|
19 | + |
|
20 | + /** |
|
21 | + * EncryptionHelper constructor. |
|
22 | + * |
|
23 | + * @param SiteConfiguration $configuration |
|
24 | + */ |
|
25 | + public function __construct(SiteConfiguration $configuration) |
|
26 | + { |
|
27 | + $this->configuration = $configuration; |
|
28 | + } |
|
29 | + |
|
30 | + public function encryptData($secret) |
|
31 | + { |
|
32 | + $iv = openssl_random_pseudo_bytes(16); |
|
33 | + $password = $this->getEncryptionKey(); |
|
34 | + $encryptedKey = openssl_encrypt($secret, 'aes-256-ctr', $password, OPENSSL_RAW_DATA, $iv); |
|
35 | + |
|
36 | + $data = base64_encode($iv) . '|' . base64_encode($encryptedKey); |
|
37 | + |
|
38 | + return $data; |
|
39 | + } |
|
40 | + |
|
41 | + public function decryptData($data) |
|
42 | + { |
|
43 | + list($iv, $encryptedKey) = array_map('base64_decode', explode('|', $data)); |
|
44 | + |
|
45 | + $password = $this->getEncryptionKey(); |
|
46 | + |
|
47 | + $secret = openssl_decrypt($encryptedKey, 'aes-256-ctr', $password, OPENSSL_RAW_DATA, $iv); |
|
48 | + |
|
49 | + return $secret; |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * @return string |
|
54 | + */ |
|
55 | + private function getEncryptionKey() |
|
56 | + { |
|
57 | + return openssl_digest($this->configuration->getTotpEncryptionKey(), 'sha256'); |
|
58 | + } |
|
59 | 59 | } |
60 | 60 | \ No newline at end of file |
@@ -46,374 +46,374 @@ |
||
46 | 46 | |
47 | 47 | class RoleConfiguration |
48 | 48 | { |
49 | - const ACCESS_ALLOW = 1; |
|
50 | - const ACCESS_DENY = -1; |
|
51 | - const ACCESS_DEFAULT = 0; |
|
52 | - const MAIN = 'main'; |
|
53 | - const ALL = '*'; |
|
54 | - /** |
|
55 | - * A map of roles to rights |
|
56 | - * |
|
57 | - * For example: |
|
58 | - * |
|
59 | - * array( |
|
60 | - * 'myrole' => array( |
|
61 | - * PageMyPage::class => array( |
|
62 | - * 'edit' => self::ACCESS_ALLOW, |
|
63 | - * 'create' => self::ACCESS_DENY, |
|
64 | - * ) |
|
65 | - * ) |
|
66 | - * ) |
|
67 | - * |
|
68 | - * Note that DENY takes precedence over everything else when roles are combined, followed by ALLOW, followed by |
|
69 | - * DEFAULT. Thus, if you have the following ([A]llow, [D]eny, [-] (default)) grants in different roles, this should |
|
70 | - * be the expected result: |
|
71 | - * |
|
72 | - * - (-,-,-) = - (default because nothing to explicitly say allowed or denied equates to a denial) |
|
73 | - * - (A,-,-) = A |
|
74 | - * - (D,-,-) = D |
|
75 | - * - (A,D,-) = D (deny takes precedence over allow) |
|
76 | - * - (A,A,A) = A (repetition has no effect) |
|
77 | - * |
|
78 | - * The public role is special, and is applied to all users automatically. Avoid using deny on this role. |
|
79 | - * |
|
80 | - * @var array |
|
81 | - */ |
|
82 | - private $roleConfig = array( |
|
83 | - 'public' => array( |
|
84 | - /* |
|
49 | + const ACCESS_ALLOW = 1; |
|
50 | + const ACCESS_DENY = -1; |
|
51 | + const ACCESS_DEFAULT = 0; |
|
52 | + const MAIN = 'main'; |
|
53 | + const ALL = '*'; |
|
54 | + /** |
|
55 | + * A map of roles to rights |
|
56 | + * |
|
57 | + * For example: |
|
58 | + * |
|
59 | + * array( |
|
60 | + * 'myrole' => array( |
|
61 | + * PageMyPage::class => array( |
|
62 | + * 'edit' => self::ACCESS_ALLOW, |
|
63 | + * 'create' => self::ACCESS_DENY, |
|
64 | + * ) |
|
65 | + * ) |
|
66 | + * ) |
|
67 | + * |
|
68 | + * Note that DENY takes precedence over everything else when roles are combined, followed by ALLOW, followed by |
|
69 | + * DEFAULT. Thus, if you have the following ([A]llow, [D]eny, [-] (default)) grants in different roles, this should |
|
70 | + * be the expected result: |
|
71 | + * |
|
72 | + * - (-,-,-) = - (default because nothing to explicitly say allowed or denied equates to a denial) |
|
73 | + * - (A,-,-) = A |
|
74 | + * - (D,-,-) = D |
|
75 | + * - (A,D,-) = D (deny takes precedence over allow) |
|
76 | + * - (A,A,A) = A (repetition has no effect) |
|
77 | + * |
|
78 | + * The public role is special, and is applied to all users automatically. Avoid using deny on this role. |
|
79 | + * |
|
80 | + * @var array |
|
81 | + */ |
|
82 | + private $roleConfig = array( |
|
83 | + 'public' => array( |
|
84 | + /* |
|
85 | 85 | * THIS ROLE IS GRANTED TO ALL LOGGED *OUT* USERS IMPLICITLY. |
86 | 86 | * |
87 | 87 | * USERS IN THIS ROLE DO NOT HAVE TO BE IDENTIFIED TO GET THE RIGHTS CONFERRED HERE. |
88 | 88 | * DO NOT ADD ANY SECURITY-SENSITIVE RIGHTS HERE. |
89 | 89 | */ |
90 | - '_childRoles' => array( |
|
91 | - 'publicStats', |
|
92 | - ), |
|
93 | - PageTeam::class => array( |
|
94 | - self::MAIN => self::ACCESS_ALLOW, |
|
95 | - ), |
|
96 | - ), |
|
97 | - 'loggedIn' => array( |
|
98 | - /* |
|
90 | + '_childRoles' => array( |
|
91 | + 'publicStats', |
|
92 | + ), |
|
93 | + PageTeam::class => array( |
|
94 | + self::MAIN => self::ACCESS_ALLOW, |
|
95 | + ), |
|
96 | + ), |
|
97 | + 'loggedIn' => array( |
|
98 | + /* |
|
99 | 99 | * THIS ROLE IS GRANTED TO ALL LOGGED IN USERS IMPLICITLY. |
100 | 100 | * |
101 | 101 | * USERS IN THIS ROLE DO NOT HAVE TO BE IDENTIFIED TO GET THE RIGHTS CONFERRED HERE. |
102 | 102 | * DO NOT ADD ANY SECURITY-SENSITIVE RIGHTS HERE. |
103 | 103 | */ |
104 | - '_childRoles' => array( |
|
105 | - 'public', |
|
106 | - ), |
|
107 | - PagePreferences::class => array( |
|
108 | - self::MAIN => self::ACCESS_ALLOW, |
|
109 | - ), |
|
110 | - PageChangePassword::class => array( |
|
111 | - self::MAIN => self::ACCESS_ALLOW, |
|
112 | - ), |
|
113 | - PageMultiFactor::class => array( |
|
114 | - self::MAIN => self::ACCESS_ALLOW, |
|
115 | - 'scratch' => self::ACCESS_ALLOW, |
|
116 | - 'enableYubikeyOtp' => self::ACCESS_ALLOW, |
|
117 | - 'disableYubikeyOtp' => self::ACCESS_ALLOW, |
|
118 | - 'enableTotp' => self::ACCESS_ALLOW, |
|
119 | - 'disableTotp' => self::ACCESS_ALLOW, |
|
120 | - 'enableU2F' => self::ACCESS_ALLOW, |
|
121 | - 'disableU2F' => self::ACCESS_ALLOW, |
|
122 | - ), |
|
123 | - PageOAuth::class => array( |
|
124 | - 'attach' => self::ACCESS_ALLOW, |
|
125 | - 'detach' => self::ACCESS_ALLOW, |
|
126 | - ), |
|
127 | - ), |
|
128 | - 'user' => array( |
|
129 | - '_description' => 'A standard tool user.', |
|
130 | - '_editableBy' => array('admin', 'toolRoot'), |
|
131 | - '_childRoles' => array( |
|
132 | - 'internalStats', |
|
133 | - ), |
|
134 | - PageMain::class => array( |
|
135 | - self::MAIN => self::ACCESS_ALLOW, |
|
136 | - ), |
|
137 | - PageBan::class => array( |
|
138 | - self::MAIN => self::ACCESS_ALLOW, |
|
139 | - ), |
|
140 | - PageEditComment::class => array( |
|
141 | - self::MAIN => self::ACCESS_ALLOW, |
|
142 | - ), |
|
143 | - PageEmailManagement::class => array( |
|
144 | - self::MAIN => self::ACCESS_ALLOW, |
|
145 | - 'view' => self::ACCESS_ALLOW, |
|
146 | - ), |
|
147 | - PageExpandedRequestList::class => array( |
|
148 | - self::MAIN => self::ACCESS_ALLOW, |
|
149 | - ), |
|
150 | - PageLog::class => array( |
|
151 | - self::MAIN => self::ACCESS_ALLOW, |
|
152 | - ), |
|
153 | - PageSearch::class => array( |
|
154 | - self::MAIN => self::ACCESS_ALLOW, |
|
155 | - ), |
|
156 | - PageWelcomeTemplateManagement::class => array( |
|
157 | - self::MAIN => self::ACCESS_ALLOW, |
|
158 | - 'select' => self::ACCESS_ALLOW, |
|
159 | - 'view' => self::ACCESS_ALLOW, |
|
160 | - ), |
|
161 | - PageViewRequest::class => array( |
|
162 | - self::MAIN => self::ACCESS_ALLOW, |
|
163 | - 'seeAllRequests' => self::ACCESS_ALLOW, |
|
164 | - ), |
|
165 | - 'RequestData' => array( |
|
166 | - 'seePrivateDataWhenReserved' => self::ACCESS_ALLOW, |
|
167 | - 'seePrivateDataWithHash' => self::ACCESS_ALLOW, |
|
168 | - ), |
|
169 | - PageCustomClose::class => array( |
|
170 | - self::MAIN => self::ACCESS_ALLOW, |
|
171 | - ), |
|
172 | - PageComment::class => array( |
|
173 | - self::MAIN => self::ACCESS_ALLOW, |
|
174 | - ), |
|
175 | - PageCloseRequest::class => array( |
|
176 | - self::MAIN => self::ACCESS_ALLOW, |
|
177 | - ), |
|
178 | - PageCreateRequest::class => array( |
|
179 | - self::MAIN => self::ACCESS_ALLOW, |
|
180 | - ), |
|
181 | - PageDeferRequest::class => array( |
|
182 | - self::MAIN => self::ACCESS_ALLOW, |
|
183 | - ), |
|
184 | - PageDropRequest::class => array( |
|
185 | - self::MAIN => self::ACCESS_ALLOW, |
|
186 | - ), |
|
187 | - PageReservation::class => array( |
|
188 | - self::MAIN => self::ACCESS_ALLOW, |
|
189 | - ), |
|
190 | - PageSendToUser::class => array( |
|
191 | - self::MAIN => self::ACCESS_ALLOW, |
|
192 | - ), |
|
193 | - PageBreakReservation::class => array( |
|
194 | - self::MAIN => self::ACCESS_ALLOW, |
|
195 | - ), |
|
196 | - PageJobQueue::class => array( |
|
197 | - self::MAIN => self::ACCESS_ALLOW, |
|
198 | - 'view' => self::ACCESS_ALLOW, |
|
199 | - 'all' => self::ACCESS_ALLOW, |
|
200 | - ), |
|
201 | - 'RequestCreation' => array( |
|
202 | - User::CREATION_MANUAL => self::ACCESS_ALLOW, |
|
203 | - User::CREATION_OAUTH => self::ACCESS_ALLOW, |
|
204 | - ), |
|
205 | - ), |
|
206 | - 'admin' => array( |
|
207 | - '_description' => 'A tool administrator.', |
|
208 | - '_editableBy' => array('admin', 'toolRoot'), |
|
209 | - '_childRoles' => array( |
|
210 | - 'user', |
|
211 | - 'requestAdminTools', |
|
212 | - ), |
|
213 | - PageEmailManagement::class => array( |
|
214 | - 'edit' => self::ACCESS_ALLOW, |
|
215 | - 'create' => self::ACCESS_ALLOW, |
|
216 | - ), |
|
217 | - PageSiteNotice::class => array( |
|
218 | - self::MAIN => self::ACCESS_ALLOW, |
|
219 | - ), |
|
220 | - PageUserManagement::class => array( |
|
221 | - self::MAIN => self::ACCESS_ALLOW, |
|
222 | - 'approve' => self::ACCESS_ALLOW, |
|
223 | - 'decline' => self::ACCESS_ALLOW, |
|
224 | - 'rename' => self::ACCESS_ALLOW, |
|
225 | - 'editUser' => self::ACCESS_ALLOW, |
|
226 | - 'suspend' => self::ACCESS_ALLOW, |
|
227 | - 'editRoles' => self::ACCESS_ALLOW, |
|
228 | - ), |
|
229 | - PageWelcomeTemplateManagement::class => array( |
|
230 | - 'edit' => self::ACCESS_ALLOW, |
|
231 | - 'delete' => self::ACCESS_ALLOW, |
|
232 | - 'add' => self::ACCESS_ALLOW, |
|
233 | - ), |
|
234 | - PageJobQueue::class => array( |
|
235 | - 'acknowledge' => self::ACCESS_ALLOW, |
|
236 | - 'requeue' => self::ACCESS_ALLOW, |
|
237 | - ), |
|
238 | - ), |
|
239 | - 'checkuser' => array( |
|
240 | - '_description' => 'A user with CheckUser access', |
|
241 | - '_editableBy' => array('checkuser', 'toolRoot'), |
|
242 | - '_childRoles' => array( |
|
243 | - 'user', |
|
244 | - 'requestAdminTools', |
|
245 | - ), |
|
246 | - PageUserManagement::class => array( |
|
247 | - self::MAIN => self::ACCESS_ALLOW, |
|
248 | - 'suspend' => self::ACCESS_ALLOW, |
|
249 | - 'editRoles' => self::ACCESS_ALLOW, |
|
250 | - ), |
|
251 | - 'RequestData' => array( |
|
252 | - 'seeUserAgentData' => self::ACCESS_ALLOW, |
|
253 | - ), |
|
254 | - ), |
|
255 | - 'toolRoot' => array( |
|
256 | - '_description' => 'A user with shell access to the servers running the tool', |
|
257 | - '_editableBy' => array('toolRoot'), |
|
258 | - '_childRoles' => array( |
|
259 | - 'admin', |
|
260 | - 'checkuser', |
|
261 | - ), |
|
262 | - ), |
|
263 | - 'botCreation' => array( |
|
264 | - '_description' => 'A user allowed to use the bot to perform account creations', |
|
265 | - '_editableBy' => array('admin', 'toolRoot'), |
|
266 | - '_childRoles' => array(), |
|
267 | - 'RequestCreation' => array( |
|
268 | - User::CREATION_BOT => self::ACCESS_ALLOW, |
|
269 | - ), |
|
270 | - ), |
|
104 | + '_childRoles' => array( |
|
105 | + 'public', |
|
106 | + ), |
|
107 | + PagePreferences::class => array( |
|
108 | + self::MAIN => self::ACCESS_ALLOW, |
|
109 | + ), |
|
110 | + PageChangePassword::class => array( |
|
111 | + self::MAIN => self::ACCESS_ALLOW, |
|
112 | + ), |
|
113 | + PageMultiFactor::class => array( |
|
114 | + self::MAIN => self::ACCESS_ALLOW, |
|
115 | + 'scratch' => self::ACCESS_ALLOW, |
|
116 | + 'enableYubikeyOtp' => self::ACCESS_ALLOW, |
|
117 | + 'disableYubikeyOtp' => self::ACCESS_ALLOW, |
|
118 | + 'enableTotp' => self::ACCESS_ALLOW, |
|
119 | + 'disableTotp' => self::ACCESS_ALLOW, |
|
120 | + 'enableU2F' => self::ACCESS_ALLOW, |
|
121 | + 'disableU2F' => self::ACCESS_ALLOW, |
|
122 | + ), |
|
123 | + PageOAuth::class => array( |
|
124 | + 'attach' => self::ACCESS_ALLOW, |
|
125 | + 'detach' => self::ACCESS_ALLOW, |
|
126 | + ), |
|
127 | + ), |
|
128 | + 'user' => array( |
|
129 | + '_description' => 'A standard tool user.', |
|
130 | + '_editableBy' => array('admin', 'toolRoot'), |
|
131 | + '_childRoles' => array( |
|
132 | + 'internalStats', |
|
133 | + ), |
|
134 | + PageMain::class => array( |
|
135 | + self::MAIN => self::ACCESS_ALLOW, |
|
136 | + ), |
|
137 | + PageBan::class => array( |
|
138 | + self::MAIN => self::ACCESS_ALLOW, |
|
139 | + ), |
|
140 | + PageEditComment::class => array( |
|
141 | + self::MAIN => self::ACCESS_ALLOW, |
|
142 | + ), |
|
143 | + PageEmailManagement::class => array( |
|
144 | + self::MAIN => self::ACCESS_ALLOW, |
|
145 | + 'view' => self::ACCESS_ALLOW, |
|
146 | + ), |
|
147 | + PageExpandedRequestList::class => array( |
|
148 | + self::MAIN => self::ACCESS_ALLOW, |
|
149 | + ), |
|
150 | + PageLog::class => array( |
|
151 | + self::MAIN => self::ACCESS_ALLOW, |
|
152 | + ), |
|
153 | + PageSearch::class => array( |
|
154 | + self::MAIN => self::ACCESS_ALLOW, |
|
155 | + ), |
|
156 | + PageWelcomeTemplateManagement::class => array( |
|
157 | + self::MAIN => self::ACCESS_ALLOW, |
|
158 | + 'select' => self::ACCESS_ALLOW, |
|
159 | + 'view' => self::ACCESS_ALLOW, |
|
160 | + ), |
|
161 | + PageViewRequest::class => array( |
|
162 | + self::MAIN => self::ACCESS_ALLOW, |
|
163 | + 'seeAllRequests' => self::ACCESS_ALLOW, |
|
164 | + ), |
|
165 | + 'RequestData' => array( |
|
166 | + 'seePrivateDataWhenReserved' => self::ACCESS_ALLOW, |
|
167 | + 'seePrivateDataWithHash' => self::ACCESS_ALLOW, |
|
168 | + ), |
|
169 | + PageCustomClose::class => array( |
|
170 | + self::MAIN => self::ACCESS_ALLOW, |
|
171 | + ), |
|
172 | + PageComment::class => array( |
|
173 | + self::MAIN => self::ACCESS_ALLOW, |
|
174 | + ), |
|
175 | + PageCloseRequest::class => array( |
|
176 | + self::MAIN => self::ACCESS_ALLOW, |
|
177 | + ), |
|
178 | + PageCreateRequest::class => array( |
|
179 | + self::MAIN => self::ACCESS_ALLOW, |
|
180 | + ), |
|
181 | + PageDeferRequest::class => array( |
|
182 | + self::MAIN => self::ACCESS_ALLOW, |
|
183 | + ), |
|
184 | + PageDropRequest::class => array( |
|
185 | + self::MAIN => self::ACCESS_ALLOW, |
|
186 | + ), |
|
187 | + PageReservation::class => array( |
|
188 | + self::MAIN => self::ACCESS_ALLOW, |
|
189 | + ), |
|
190 | + PageSendToUser::class => array( |
|
191 | + self::MAIN => self::ACCESS_ALLOW, |
|
192 | + ), |
|
193 | + PageBreakReservation::class => array( |
|
194 | + self::MAIN => self::ACCESS_ALLOW, |
|
195 | + ), |
|
196 | + PageJobQueue::class => array( |
|
197 | + self::MAIN => self::ACCESS_ALLOW, |
|
198 | + 'view' => self::ACCESS_ALLOW, |
|
199 | + 'all' => self::ACCESS_ALLOW, |
|
200 | + ), |
|
201 | + 'RequestCreation' => array( |
|
202 | + User::CREATION_MANUAL => self::ACCESS_ALLOW, |
|
203 | + User::CREATION_OAUTH => self::ACCESS_ALLOW, |
|
204 | + ), |
|
205 | + ), |
|
206 | + 'admin' => array( |
|
207 | + '_description' => 'A tool administrator.', |
|
208 | + '_editableBy' => array('admin', 'toolRoot'), |
|
209 | + '_childRoles' => array( |
|
210 | + 'user', |
|
211 | + 'requestAdminTools', |
|
212 | + ), |
|
213 | + PageEmailManagement::class => array( |
|
214 | + 'edit' => self::ACCESS_ALLOW, |
|
215 | + 'create' => self::ACCESS_ALLOW, |
|
216 | + ), |
|
217 | + PageSiteNotice::class => array( |
|
218 | + self::MAIN => self::ACCESS_ALLOW, |
|
219 | + ), |
|
220 | + PageUserManagement::class => array( |
|
221 | + self::MAIN => self::ACCESS_ALLOW, |
|
222 | + 'approve' => self::ACCESS_ALLOW, |
|
223 | + 'decline' => self::ACCESS_ALLOW, |
|
224 | + 'rename' => self::ACCESS_ALLOW, |
|
225 | + 'editUser' => self::ACCESS_ALLOW, |
|
226 | + 'suspend' => self::ACCESS_ALLOW, |
|
227 | + 'editRoles' => self::ACCESS_ALLOW, |
|
228 | + ), |
|
229 | + PageWelcomeTemplateManagement::class => array( |
|
230 | + 'edit' => self::ACCESS_ALLOW, |
|
231 | + 'delete' => self::ACCESS_ALLOW, |
|
232 | + 'add' => self::ACCESS_ALLOW, |
|
233 | + ), |
|
234 | + PageJobQueue::class => array( |
|
235 | + 'acknowledge' => self::ACCESS_ALLOW, |
|
236 | + 'requeue' => self::ACCESS_ALLOW, |
|
237 | + ), |
|
238 | + ), |
|
239 | + 'checkuser' => array( |
|
240 | + '_description' => 'A user with CheckUser access', |
|
241 | + '_editableBy' => array('checkuser', 'toolRoot'), |
|
242 | + '_childRoles' => array( |
|
243 | + 'user', |
|
244 | + 'requestAdminTools', |
|
245 | + ), |
|
246 | + PageUserManagement::class => array( |
|
247 | + self::MAIN => self::ACCESS_ALLOW, |
|
248 | + 'suspend' => self::ACCESS_ALLOW, |
|
249 | + 'editRoles' => self::ACCESS_ALLOW, |
|
250 | + ), |
|
251 | + 'RequestData' => array( |
|
252 | + 'seeUserAgentData' => self::ACCESS_ALLOW, |
|
253 | + ), |
|
254 | + ), |
|
255 | + 'toolRoot' => array( |
|
256 | + '_description' => 'A user with shell access to the servers running the tool', |
|
257 | + '_editableBy' => array('toolRoot'), |
|
258 | + '_childRoles' => array( |
|
259 | + 'admin', |
|
260 | + 'checkuser', |
|
261 | + ), |
|
262 | + ), |
|
263 | + 'botCreation' => array( |
|
264 | + '_description' => 'A user allowed to use the bot to perform account creations', |
|
265 | + '_editableBy' => array('admin', 'toolRoot'), |
|
266 | + '_childRoles' => array(), |
|
267 | + 'RequestCreation' => array( |
|
268 | + User::CREATION_BOT => self::ACCESS_ALLOW, |
|
269 | + ), |
|
270 | + ), |
|
271 | 271 | |
272 | - // Child roles go below this point |
|
273 | - 'publicStats' => array( |
|
274 | - '_hidden' => true, |
|
275 | - StatsUsers::class => array( |
|
276 | - self::MAIN => self::ACCESS_ALLOW, |
|
277 | - 'detail' => self::ACCESS_ALLOW, |
|
278 | - ), |
|
279 | - StatsTopCreators::class => array( |
|
280 | - self::MAIN => self::ACCESS_ALLOW, |
|
281 | - ), |
|
282 | - ), |
|
283 | - 'internalStats' => array( |
|
284 | - '_hidden' => true, |
|
285 | - StatsMain::class => array( |
|
286 | - self::MAIN => self::ACCESS_ALLOW, |
|
287 | - ), |
|
288 | - StatsFastCloses::class => array( |
|
289 | - self::MAIN => self::ACCESS_ALLOW, |
|
290 | - ), |
|
291 | - StatsInactiveUsers::class => array( |
|
292 | - self::MAIN => self::ACCESS_ALLOW, |
|
293 | - ), |
|
294 | - StatsMonthlyStats::class => array( |
|
295 | - self::MAIN => self::ACCESS_ALLOW, |
|
296 | - ), |
|
297 | - StatsReservedRequests::class => array( |
|
298 | - self::MAIN => self::ACCESS_ALLOW, |
|
299 | - ), |
|
300 | - StatsTemplateStats::class => array( |
|
301 | - self::MAIN => self::ACCESS_ALLOW, |
|
302 | - ), |
|
303 | - ), |
|
304 | - 'requestAdminTools' => array( |
|
305 | - '_hidden' => true, |
|
306 | - PageBan::class => array( |
|
307 | - self::MAIN => self::ACCESS_ALLOW, |
|
308 | - 'set' => self::ACCESS_ALLOW, |
|
309 | - 'remove' => self::ACCESS_ALLOW, |
|
310 | - ), |
|
311 | - PageEditComment::class => array( |
|
312 | - 'editOthers' => self::ACCESS_ALLOW, |
|
313 | - ), |
|
314 | - PageBreakReservation::class => array( |
|
315 | - 'force' => self::ACCESS_ALLOW, |
|
316 | - ), |
|
317 | - PageCustomClose::class => array( |
|
318 | - 'skipCcMailingList' => self::ACCESS_ALLOW, |
|
319 | - ), |
|
320 | - 'RequestData' => array( |
|
321 | - 'reopenOldRequest' => self::ACCESS_ALLOW, |
|
322 | - 'alwaysSeePrivateData' => self::ACCESS_ALLOW, |
|
323 | - 'alwaysSeeHash' => self::ACCESS_ALLOW, |
|
324 | - 'seeRestrictedComments' => self::ACCESS_ALLOW, |
|
325 | - ), |
|
326 | - ), |
|
327 | - ); |
|
328 | - /** @var array |
|
329 | - * List of roles which are *exempt* from the identification requirements |
|
330 | - * |
|
331 | - * Think twice about adding roles to this list. |
|
332 | - * |
|
333 | - * @category Security-Critical |
|
334 | - */ |
|
335 | - private $identificationExempt = array('public', 'loggedIn'); |
|
272 | + // Child roles go below this point |
|
273 | + 'publicStats' => array( |
|
274 | + '_hidden' => true, |
|
275 | + StatsUsers::class => array( |
|
276 | + self::MAIN => self::ACCESS_ALLOW, |
|
277 | + 'detail' => self::ACCESS_ALLOW, |
|
278 | + ), |
|
279 | + StatsTopCreators::class => array( |
|
280 | + self::MAIN => self::ACCESS_ALLOW, |
|
281 | + ), |
|
282 | + ), |
|
283 | + 'internalStats' => array( |
|
284 | + '_hidden' => true, |
|
285 | + StatsMain::class => array( |
|
286 | + self::MAIN => self::ACCESS_ALLOW, |
|
287 | + ), |
|
288 | + StatsFastCloses::class => array( |
|
289 | + self::MAIN => self::ACCESS_ALLOW, |
|
290 | + ), |
|
291 | + StatsInactiveUsers::class => array( |
|
292 | + self::MAIN => self::ACCESS_ALLOW, |
|
293 | + ), |
|
294 | + StatsMonthlyStats::class => array( |
|
295 | + self::MAIN => self::ACCESS_ALLOW, |
|
296 | + ), |
|
297 | + StatsReservedRequests::class => array( |
|
298 | + self::MAIN => self::ACCESS_ALLOW, |
|
299 | + ), |
|
300 | + StatsTemplateStats::class => array( |
|
301 | + self::MAIN => self::ACCESS_ALLOW, |
|
302 | + ), |
|
303 | + ), |
|
304 | + 'requestAdminTools' => array( |
|
305 | + '_hidden' => true, |
|
306 | + PageBan::class => array( |
|
307 | + self::MAIN => self::ACCESS_ALLOW, |
|
308 | + 'set' => self::ACCESS_ALLOW, |
|
309 | + 'remove' => self::ACCESS_ALLOW, |
|
310 | + ), |
|
311 | + PageEditComment::class => array( |
|
312 | + 'editOthers' => self::ACCESS_ALLOW, |
|
313 | + ), |
|
314 | + PageBreakReservation::class => array( |
|
315 | + 'force' => self::ACCESS_ALLOW, |
|
316 | + ), |
|
317 | + PageCustomClose::class => array( |
|
318 | + 'skipCcMailingList' => self::ACCESS_ALLOW, |
|
319 | + ), |
|
320 | + 'RequestData' => array( |
|
321 | + 'reopenOldRequest' => self::ACCESS_ALLOW, |
|
322 | + 'alwaysSeePrivateData' => self::ACCESS_ALLOW, |
|
323 | + 'alwaysSeeHash' => self::ACCESS_ALLOW, |
|
324 | + 'seeRestrictedComments' => self::ACCESS_ALLOW, |
|
325 | + ), |
|
326 | + ), |
|
327 | + ); |
|
328 | + /** @var array |
|
329 | + * List of roles which are *exempt* from the identification requirements |
|
330 | + * |
|
331 | + * Think twice about adding roles to this list. |
|
332 | + * |
|
333 | + * @category Security-Critical |
|
334 | + */ |
|
335 | + private $identificationExempt = array('public', 'loggedIn'); |
|
336 | 336 | |
337 | - /** |
|
338 | - * RoleConfiguration constructor. |
|
339 | - * |
|
340 | - * @param array $roleConfig Set to non-null to override the default configuration. |
|
341 | - * @param array $identificationExempt Set to non-null to override the default configuration. |
|
342 | - */ |
|
343 | - public function __construct(array $roleConfig = null, array $identificationExempt = null) |
|
344 | - { |
|
345 | - if ($roleConfig !== null) { |
|
346 | - $this->roleConfig = $roleConfig; |
|
347 | - } |
|
337 | + /** |
|
338 | + * RoleConfiguration constructor. |
|
339 | + * |
|
340 | + * @param array $roleConfig Set to non-null to override the default configuration. |
|
341 | + * @param array $identificationExempt Set to non-null to override the default configuration. |
|
342 | + */ |
|
343 | + public function __construct(array $roleConfig = null, array $identificationExempt = null) |
|
344 | + { |
|
345 | + if ($roleConfig !== null) { |
|
346 | + $this->roleConfig = $roleConfig; |
|
347 | + } |
|
348 | 348 | |
349 | - if ($identificationExempt !== null) { |
|
350 | - $this->identificationExempt = $identificationExempt; |
|
351 | - } |
|
352 | - } |
|
349 | + if ($identificationExempt !== null) { |
|
350 | + $this->identificationExempt = $identificationExempt; |
|
351 | + } |
|
352 | + } |
|
353 | 353 | |
354 | - /** |
|
355 | - * @param array $roles The roles to check |
|
356 | - * |
|
357 | - * @return array |
|
358 | - */ |
|
359 | - public function getApplicableRoles(array $roles) |
|
360 | - { |
|
361 | - $available = array(); |
|
354 | + /** |
|
355 | + * @param array $roles The roles to check |
|
356 | + * |
|
357 | + * @return array |
|
358 | + */ |
|
359 | + public function getApplicableRoles(array $roles) |
|
360 | + { |
|
361 | + $available = array(); |
|
362 | 362 | |
363 | - foreach ($roles as $role) { |
|
364 | - if (!isset($this->roleConfig[$role])) { |
|
365 | - // wat |
|
366 | - continue; |
|
367 | - } |
|
363 | + foreach ($roles as $role) { |
|
364 | + if (!isset($this->roleConfig[$role])) { |
|
365 | + // wat |
|
366 | + continue; |
|
367 | + } |
|
368 | 368 | |
369 | - $available[$role] = $this->roleConfig[$role]; |
|
369 | + $available[$role] = $this->roleConfig[$role]; |
|
370 | 370 | |
371 | - if (isset($available[$role]['_childRoles'])) { |
|
372 | - $childRoles = self::getApplicableRoles($available[$role]['_childRoles']); |
|
373 | - $available = array_merge($available, $childRoles); |
|
371 | + if (isset($available[$role]['_childRoles'])) { |
|
372 | + $childRoles = self::getApplicableRoles($available[$role]['_childRoles']); |
|
373 | + $available = array_merge($available, $childRoles); |
|
374 | 374 | |
375 | - unset($available[$role]['_childRoles']); |
|
376 | - } |
|
375 | + unset($available[$role]['_childRoles']); |
|
376 | + } |
|
377 | 377 | |
378 | - foreach (array('_hidden', '_editableBy', '_description') as $item) { |
|
379 | - if (isset($available[$role][$item])) { |
|
380 | - unset($available[$role][$item]); |
|
381 | - } |
|
382 | - } |
|
383 | - } |
|
378 | + foreach (array('_hidden', '_editableBy', '_description') as $item) { |
|
379 | + if (isset($available[$role][$item])) { |
|
380 | + unset($available[$role][$item]); |
|
381 | + } |
|
382 | + } |
|
383 | + } |
|
384 | 384 | |
385 | - return $available; |
|
386 | - } |
|
385 | + return $available; |
|
386 | + } |
|
387 | 387 | |
388 | - public function getAvailableRoles() |
|
389 | - { |
|
390 | - $possible = array_diff(array_keys($this->roleConfig), array('public', 'loggedIn')); |
|
388 | + public function getAvailableRoles() |
|
389 | + { |
|
390 | + $possible = array_diff(array_keys($this->roleConfig), array('public', 'loggedIn')); |
|
391 | 391 | |
392 | - $actual = array(); |
|
392 | + $actual = array(); |
|
393 | 393 | |
394 | - foreach ($possible as $role) { |
|
395 | - if (!isset($this->roleConfig[$role]['_hidden'])) { |
|
396 | - $actual[$role] = array( |
|
397 | - 'description' => $this->roleConfig[$role]['_description'], |
|
398 | - 'editableBy' => $this->roleConfig[$role]['_editableBy'], |
|
399 | - ); |
|
400 | - } |
|
401 | - } |
|
394 | + foreach ($possible as $role) { |
|
395 | + if (!isset($this->roleConfig[$role]['_hidden'])) { |
|
396 | + $actual[$role] = array( |
|
397 | + 'description' => $this->roleConfig[$role]['_description'], |
|
398 | + 'editableBy' => $this->roleConfig[$role]['_editableBy'], |
|
399 | + ); |
|
400 | + } |
|
401 | + } |
|
402 | 402 | |
403 | - return $actual; |
|
404 | - } |
|
403 | + return $actual; |
|
404 | + } |
|
405 | 405 | |
406 | - /** |
|
407 | - * @param string $role |
|
408 | - * |
|
409 | - * @return bool |
|
410 | - */ |
|
411 | - public function roleNeedsIdentification($role) |
|
412 | - { |
|
413 | - if (in_array($role, $this->identificationExempt)) { |
|
414 | - return false; |
|
415 | - } |
|
406 | + /** |
|
407 | + * @param string $role |
|
408 | + * |
|
409 | + * @return bool |
|
410 | + */ |
|
411 | + public function roleNeedsIdentification($role) |
|
412 | + { |
|
413 | + if (in_array($role, $this->identificationExempt)) { |
|
414 | + return false; |
|
415 | + } |
|
416 | 416 | |
417 | - return true; |
|
418 | - } |
|
417 | + return true; |
|
418 | + } |
|
419 | 419 | } |
@@ -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 |
@@ -14,9 +14,9 @@ |
||
14 | 14 | |
15 | 15 | function smarty_modifier_nlimplode($list, $conjunction = 'or') |
16 | 16 | { |
17 | - $last = array_pop($list); |
|
18 | - if ($list) { |
|
19 | - return implode(', ', $list) . ', ' . $conjunction . ' ' . $last; |
|
20 | - } |
|
21 | - return $last; |
|
17 | + $last = array_pop($list); |
|
18 | + if ($list) { |
|
19 | + return implode(', ', $list) . ', ' . $conjunction . ' ' . $last; |
|
20 | + } |
|
21 | + return $last; |
|
22 | 22 | } |
23 | 23 | \ No newline at end of file |
@@ -8,10 +8,10 @@ |
||
8 | 8 | |
9 | 9 | function smarty_modifier_demodhex($input) |
10 | 10 | { |
11 | - $hex = preg_replace( |
|
12 | - array('/c/', '/b/', '/d/', '/e/', '/f/', '/g/', '/h/', '/i/', '/j/', '/k/', '/l/', '/n/', '/r/', '/t/', '/u/', '/v/'), |
|
13 | - array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'), |
|
14 | - $input); |
|
11 | + $hex = preg_replace( |
|
12 | + array('/c/', '/b/', '/d/', '/e/', '/f/', '/g/', '/h/', '/i/', '/j/', '/k/', '/l/', '/n/', '/r/', '/t/', '/u/', '/v/'), |
|
13 | + array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'), |
|
14 | + $input); |
|
15 | 15 | |
16 | - return hexdec($hex); |
|
16 | + return hexdec($hex); |
|
17 | 17 | } |
18 | 18 | \ No newline at end of file |