@@ -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 | } |
@@ -14,199 +14,199 @@ |
||
| 14 | 14 | |
| 15 | 15 | final class SecurityManager |
| 16 | 16 | { |
| 17 | - const ALLOWED = 1; |
|
| 18 | - const ERROR_NOT_IDENTIFIED = 2; |
|
| 19 | - const ERROR_DENIED = 3; |
|
| 20 | - /** @var IdentificationVerifier */ |
|
| 21 | - private $identificationVerifier; |
|
| 22 | - /** |
|
| 23 | - * @var RoleConfiguration |
|
| 24 | - */ |
|
| 25 | - private $roleConfiguration; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * SecurityManager constructor. |
|
| 29 | - * |
|
| 30 | - * @param IdentificationVerifier $identificationVerifier |
|
| 31 | - * @param RoleConfiguration $roleConfiguration |
|
| 32 | - */ |
|
| 33 | - public function __construct( |
|
| 34 | - IdentificationVerifier $identificationVerifier, |
|
| 35 | - RoleConfiguration $roleConfiguration |
|
| 36 | - ) { |
|
| 37 | - $this->identificationVerifier = $identificationVerifier; |
|
| 38 | - $this->roleConfiguration = $roleConfiguration; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Tests if a user is allowed to perform an action. |
|
| 43 | - * |
|
| 44 | - * This method should form a hard, deterministic security barrier, and only return true if it is absolutely sure |
|
| 45 | - * that a user should have access to something. |
|
| 46 | - * |
|
| 47 | - * @param string $page |
|
| 48 | - * @param string $route |
|
| 49 | - * @param User $user |
|
| 50 | - * |
|
| 51 | - * @return int |
|
| 52 | - * |
|
| 53 | - * @category Security-Critical |
|
| 54 | - */ |
|
| 55 | - public function allows($page, $route, User $user) |
|
| 56 | - { |
|
| 57 | - $this->getActiveRoles($user, $activeRoles, $inactiveRoles); |
|
| 58 | - |
|
| 59 | - $availableRights = $this->flattenRoles($activeRoles); |
|
| 60 | - $testResult = $this->findResult($availableRights, $page, $route); |
|
| 61 | - |
|
| 62 | - if ($testResult !== null) { |
|
| 63 | - // We got a firm result here, so just return it. |
|
| 64 | - return $testResult; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - // No firm result yet, so continue testing the inactive roles so we can give a better error. |
|
| 68 | - $inactiveRights = $this->flattenRoles($inactiveRoles); |
|
| 69 | - $testResult = $this->findResult($inactiveRights, $page, $route); |
|
| 70 | - |
|
| 71 | - if ($testResult === self::ALLOWED) { |
|
| 72 | - // The user is allowed to access this, but their role is inactive. |
|
| 73 | - return self::ERROR_NOT_IDENTIFIED; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - // Other options from the secondary test are denied and inconclusive, which at this point defaults to denied. |
|
| 77 | - return self::ERROR_DENIED; |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @param array $pseudoRole The role (flattened) to check |
|
| 82 | - * @param string $page The page class to check |
|
| 83 | - * @param string $route The page route to check |
|
| 84 | - * |
|
| 85 | - * @return int|null |
|
| 86 | - */ |
|
| 87 | - private function findResult($pseudoRole, $page, $route) |
|
| 88 | - { |
|
| 89 | - if (isset($pseudoRole[$page])) { |
|
| 90 | - // check for deny on catch-all route |
|
| 91 | - if (isset($pseudoRole[$page][RoleConfiguration::ALL])) { |
|
| 92 | - if ($pseudoRole[$page][RoleConfiguration::ALL] === RoleConfiguration::ACCESS_DENY) { |
|
| 93 | - return self::ERROR_DENIED; |
|
| 94 | - } |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - // check normal route |
|
| 98 | - if (isset($pseudoRole[$page][$route])) { |
|
| 99 | - if ($pseudoRole[$page][$route] === RoleConfiguration::ACCESS_DENY) { |
|
| 100 | - return self::ERROR_DENIED; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - if ($pseudoRole[$page][$route] === RoleConfiguration::ACCESS_ALLOW) { |
|
| 104 | - return self::ALLOWED; |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - // check for allowed on catch-all route |
|
| 109 | - if (isset($pseudoRole[$page][RoleConfiguration::ALL])) { |
|
| 110 | - if ($pseudoRole[$page][RoleConfiguration::ALL] === RoleConfiguration::ACCESS_ALLOW) { |
|
| 111 | - return self::ALLOWED; |
|
| 112 | - } |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - // return indeterminate result |
|
| 117 | - return null; |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - /** |
|
| 121 | - * Takes an array of roles and flattens the values to a single set. |
|
| 122 | - * |
|
| 123 | - * @param array $activeRoles |
|
| 124 | - * |
|
| 125 | - * @return array |
|
| 126 | - */ |
|
| 127 | - private function flattenRoles($activeRoles) |
|
| 128 | - { |
|
| 129 | - $result = array(); |
|
| 130 | - |
|
| 131 | - $roleConfig = $this->roleConfiguration->getApplicableRoles($activeRoles); |
|
| 132 | - |
|
| 133 | - // Iterate over every page in every role |
|
| 134 | - foreach ($roleConfig as $role) { |
|
| 135 | - foreach ($role as $page => $pageRights) { |
|
| 136 | - // Create holder in result for this page |
|
| 137 | - if (!isset($result[$page])) { |
|
| 138 | - $result[$page] = array(); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - foreach ($pageRights as $action => $permission) { |
|
| 142 | - // Deny takes precedence, so if it's set, don't change it. |
|
| 143 | - if (isset($result[$page][$action])) { |
|
| 144 | - if ($result[$page][$action] === RoleConfiguration::ACCESS_DENY) { |
|
| 145 | - continue; |
|
| 146 | - } |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - if ($permission === RoleConfiguration::ACCESS_DEFAULT) { |
|
| 150 | - // Configured to do precisely nothing. |
|
| 151 | - continue; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - $result[$page][$action] = $permission; |
|
| 155 | - } |
|
| 156 | - } |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - return $result; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * @param User $user |
|
| 164 | - * @param array $activeRoles |
|
| 165 | - * @param array $inactiveRoles |
|
| 166 | - */ |
|
| 167 | - public function getActiveRoles(User $user, &$activeRoles, &$inactiveRoles) |
|
| 168 | - { |
|
| 169 | - // Default to the community user here, because the main user is logged out |
|
| 170 | - $identified = false; |
|
| 171 | - $userRoles = array('public'); |
|
| 172 | - |
|
| 173 | - // if we're not the community user, get our real rights. |
|
| 174 | - if (!$user->isCommunityUser()) { |
|
| 175 | - // Check the user's status - only active users are allowed the effects of roles |
|
| 176 | - |
|
| 177 | - $userRoles[] = 'loggedIn'; |
|
| 178 | - |
|
| 179 | - if ($user->isActive()) { |
|
| 180 | - $ur = UserRole::getForUser($user->getId(), $user->getDatabase()); |
|
| 181 | - |
|
| 182 | - // NOTE: public is still in this array. |
|
| 183 | - foreach ($ur as $r) { |
|
| 184 | - $userRoles[] = $r->getRole(); |
|
| 185 | - } |
|
| 186 | - |
|
| 187 | - $identified = $user->isIdentified($this->identificationVerifier); |
|
| 188 | - } |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - $activeRoles = array(); |
|
| 192 | - $inactiveRoles = array(); |
|
| 193 | - |
|
| 194 | - foreach ($userRoles as $v) { |
|
| 195 | - if ($this->roleConfiguration->roleNeedsIdentification($v)) { |
|
| 196 | - if ($identified) { |
|
| 197 | - $activeRoles[] = $v; |
|
| 198 | - } |
|
| 199 | - else { |
|
| 200 | - $inactiveRoles[] = $v; |
|
| 201 | - } |
|
| 202 | - } |
|
| 203 | - else { |
|
| 204 | - $activeRoles[] = $v; |
|
| 205 | - } |
|
| 206 | - } |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - public function getRoleConfiguration(){ |
|
| 210 | - return $this->roleConfiguration; |
|
| 211 | - } |
|
| 17 | + const ALLOWED = 1; |
|
| 18 | + const ERROR_NOT_IDENTIFIED = 2; |
|
| 19 | + const ERROR_DENIED = 3; |
|
| 20 | + /** @var IdentificationVerifier */ |
|
| 21 | + private $identificationVerifier; |
|
| 22 | + /** |
|
| 23 | + * @var RoleConfiguration |
|
| 24 | + */ |
|
| 25 | + private $roleConfiguration; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * SecurityManager constructor. |
|
| 29 | + * |
|
| 30 | + * @param IdentificationVerifier $identificationVerifier |
|
| 31 | + * @param RoleConfiguration $roleConfiguration |
|
| 32 | + */ |
|
| 33 | + public function __construct( |
|
| 34 | + IdentificationVerifier $identificationVerifier, |
|
| 35 | + RoleConfiguration $roleConfiguration |
|
| 36 | + ) { |
|
| 37 | + $this->identificationVerifier = $identificationVerifier; |
|
| 38 | + $this->roleConfiguration = $roleConfiguration; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Tests if a user is allowed to perform an action. |
|
| 43 | + * |
|
| 44 | + * This method should form a hard, deterministic security barrier, and only return true if it is absolutely sure |
|
| 45 | + * that a user should have access to something. |
|
| 46 | + * |
|
| 47 | + * @param string $page |
|
| 48 | + * @param string $route |
|
| 49 | + * @param User $user |
|
| 50 | + * |
|
| 51 | + * @return int |
|
| 52 | + * |
|
| 53 | + * @category Security-Critical |
|
| 54 | + */ |
|
| 55 | + public function allows($page, $route, User $user) |
|
| 56 | + { |
|
| 57 | + $this->getActiveRoles($user, $activeRoles, $inactiveRoles); |
|
| 58 | + |
|
| 59 | + $availableRights = $this->flattenRoles($activeRoles); |
|
| 60 | + $testResult = $this->findResult($availableRights, $page, $route); |
|
| 61 | + |
|
| 62 | + if ($testResult !== null) { |
|
| 63 | + // We got a firm result here, so just return it. |
|
| 64 | + return $testResult; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + // No firm result yet, so continue testing the inactive roles so we can give a better error. |
|
| 68 | + $inactiveRights = $this->flattenRoles($inactiveRoles); |
|
| 69 | + $testResult = $this->findResult($inactiveRights, $page, $route); |
|
| 70 | + |
|
| 71 | + if ($testResult === self::ALLOWED) { |
|
| 72 | + // The user is allowed to access this, but their role is inactive. |
|
| 73 | + return self::ERROR_NOT_IDENTIFIED; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + // Other options from the secondary test are denied and inconclusive, which at this point defaults to denied. |
|
| 77 | + return self::ERROR_DENIED; |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @param array $pseudoRole The role (flattened) to check |
|
| 82 | + * @param string $page The page class to check |
|
| 83 | + * @param string $route The page route to check |
|
| 84 | + * |
|
| 85 | + * @return int|null |
|
| 86 | + */ |
|
| 87 | + private function findResult($pseudoRole, $page, $route) |
|
| 88 | + { |
|
| 89 | + if (isset($pseudoRole[$page])) { |
|
| 90 | + // check for deny on catch-all route |
|
| 91 | + if (isset($pseudoRole[$page][RoleConfiguration::ALL])) { |
|
| 92 | + if ($pseudoRole[$page][RoleConfiguration::ALL] === RoleConfiguration::ACCESS_DENY) { |
|
| 93 | + return self::ERROR_DENIED; |
|
| 94 | + } |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + // check normal route |
|
| 98 | + if (isset($pseudoRole[$page][$route])) { |
|
| 99 | + if ($pseudoRole[$page][$route] === RoleConfiguration::ACCESS_DENY) { |
|
| 100 | + return self::ERROR_DENIED; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + if ($pseudoRole[$page][$route] === RoleConfiguration::ACCESS_ALLOW) { |
|
| 104 | + return self::ALLOWED; |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + // check for allowed on catch-all route |
|
| 109 | + if (isset($pseudoRole[$page][RoleConfiguration::ALL])) { |
|
| 110 | + if ($pseudoRole[$page][RoleConfiguration::ALL] === RoleConfiguration::ACCESS_ALLOW) { |
|
| 111 | + return self::ALLOWED; |
|
| 112 | + } |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + // return indeterminate result |
|
| 117 | + return null; |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + /** |
|
| 121 | + * Takes an array of roles and flattens the values to a single set. |
|
| 122 | + * |
|
| 123 | + * @param array $activeRoles |
|
| 124 | + * |
|
| 125 | + * @return array |
|
| 126 | + */ |
|
| 127 | + private function flattenRoles($activeRoles) |
|
| 128 | + { |
|
| 129 | + $result = array(); |
|
| 130 | + |
|
| 131 | + $roleConfig = $this->roleConfiguration->getApplicableRoles($activeRoles); |
|
| 132 | + |
|
| 133 | + // Iterate over every page in every role |
|
| 134 | + foreach ($roleConfig as $role) { |
|
| 135 | + foreach ($role as $page => $pageRights) { |
|
| 136 | + // Create holder in result for this page |
|
| 137 | + if (!isset($result[$page])) { |
|
| 138 | + $result[$page] = array(); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + foreach ($pageRights as $action => $permission) { |
|
| 142 | + // Deny takes precedence, so if it's set, don't change it. |
|
| 143 | + if (isset($result[$page][$action])) { |
|
| 144 | + if ($result[$page][$action] === RoleConfiguration::ACCESS_DENY) { |
|
| 145 | + continue; |
|
| 146 | + } |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + if ($permission === RoleConfiguration::ACCESS_DEFAULT) { |
|
| 150 | + // Configured to do precisely nothing. |
|
| 151 | + continue; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + $result[$page][$action] = $permission; |
|
| 155 | + } |
|
| 156 | + } |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + return $result; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * @param User $user |
|
| 164 | + * @param array $activeRoles |
|
| 165 | + * @param array $inactiveRoles |
|
| 166 | + */ |
|
| 167 | + public function getActiveRoles(User $user, &$activeRoles, &$inactiveRoles) |
|
| 168 | + { |
|
| 169 | + // Default to the community user here, because the main user is logged out |
|
| 170 | + $identified = false; |
|
| 171 | + $userRoles = array('public'); |
|
| 172 | + |
|
| 173 | + // if we're not the community user, get our real rights. |
|
| 174 | + if (!$user->isCommunityUser()) { |
|
| 175 | + // Check the user's status - only active users are allowed the effects of roles |
|
| 176 | + |
|
| 177 | + $userRoles[] = 'loggedIn'; |
|
| 178 | + |
|
| 179 | + if ($user->isActive()) { |
|
| 180 | + $ur = UserRole::getForUser($user->getId(), $user->getDatabase()); |
|
| 181 | + |
|
| 182 | + // NOTE: public is still in this array. |
|
| 183 | + foreach ($ur as $r) { |
|
| 184 | + $userRoles[] = $r->getRole(); |
|
| 185 | + } |
|
| 186 | + |
|
| 187 | + $identified = $user->isIdentified($this->identificationVerifier); |
|
| 188 | + } |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + $activeRoles = array(); |
|
| 192 | + $inactiveRoles = array(); |
|
| 193 | + |
|
| 194 | + foreach ($userRoles as $v) { |
|
| 195 | + if ($this->roleConfiguration->roleNeedsIdentification($v)) { |
|
| 196 | + if ($identified) { |
|
| 197 | + $activeRoles[] = $v; |
|
| 198 | + } |
|
| 199 | + else { |
|
| 200 | + $inactiveRoles[] = $v; |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | + else { |
|
| 204 | + $activeRoles[] = $v; |
|
| 205 | + } |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + public function getRoleConfiguration(){ |
|
| 210 | + return $this->roleConfiguration; |
|
| 211 | + } |
|
| 212 | 212 | } |
@@ -47,391 +47,391 @@ |
||
| 47 | 47 | |
| 48 | 48 | class RoleConfiguration |
| 49 | 49 | { |
| 50 | - const ACCESS_ALLOW = 1; |
|
| 51 | - const ACCESS_DENY = -1; |
|
| 52 | - const ACCESS_DEFAULT = 0; |
|
| 53 | - const MAIN = 'main'; |
|
| 54 | - const ALL = '*'; |
|
| 55 | - /** |
|
| 56 | - * A map of roles to rights |
|
| 57 | - * |
|
| 58 | - * For example: |
|
| 59 | - * |
|
| 60 | - * array( |
|
| 61 | - * 'myrole' => array( |
|
| 62 | - * PageMyPage::class => array( |
|
| 63 | - * 'edit' => self::ACCESS_ALLOW, |
|
| 64 | - * 'create' => self::ACCESS_DENY, |
|
| 65 | - * ) |
|
| 66 | - * ) |
|
| 67 | - * ) |
|
| 68 | - * |
|
| 69 | - * Note that DENY takes precedence over everything else when roles are combined, followed by ALLOW, followed by |
|
| 70 | - * DEFAULT. Thus, if you have the following ([A]llow, [D]eny, [-] (default)) grants in different roles, this should |
|
| 71 | - * be the expected result: |
|
| 72 | - * |
|
| 73 | - * - (-,-,-) = - (default because nothing to explicitly say allowed or denied equates to a denial) |
|
| 74 | - * - (A,-,-) = A |
|
| 75 | - * - (D,-,-) = D |
|
| 76 | - * - (A,D,-) = D (deny takes precedence over allow) |
|
| 77 | - * - (A,A,A) = A (repetition has no effect) |
|
| 78 | - * |
|
| 79 | - * The public role is special, and is applied to all users automatically. Avoid using deny on this role. |
|
| 80 | - * |
|
| 81 | - * @var array |
|
| 82 | - */ |
|
| 83 | - private $roleConfig = array( |
|
| 84 | - 'public' => array( |
|
| 85 | - /* |
|
| 50 | + const ACCESS_ALLOW = 1; |
|
| 51 | + const ACCESS_DENY = -1; |
|
| 52 | + const ACCESS_DEFAULT = 0; |
|
| 53 | + const MAIN = 'main'; |
|
| 54 | + const ALL = '*'; |
|
| 55 | + /** |
|
| 56 | + * A map of roles to rights |
|
| 57 | + * |
|
| 58 | + * For example: |
|
| 59 | + * |
|
| 60 | + * array( |
|
| 61 | + * 'myrole' => array( |
|
| 62 | + * PageMyPage::class => array( |
|
| 63 | + * 'edit' => self::ACCESS_ALLOW, |
|
| 64 | + * 'create' => self::ACCESS_DENY, |
|
| 65 | + * ) |
|
| 66 | + * ) |
|
| 67 | + * ) |
|
| 68 | + * |
|
| 69 | + * Note that DENY takes precedence over everything else when roles are combined, followed by ALLOW, followed by |
|
| 70 | + * DEFAULT. Thus, if you have the following ([A]llow, [D]eny, [-] (default)) grants in different roles, this should |
|
| 71 | + * be the expected result: |
|
| 72 | + * |
|
| 73 | + * - (-,-,-) = - (default because nothing to explicitly say allowed or denied equates to a denial) |
|
| 74 | + * - (A,-,-) = A |
|
| 75 | + * - (D,-,-) = D |
|
| 76 | + * - (A,D,-) = D (deny takes precedence over allow) |
|
| 77 | + * - (A,A,A) = A (repetition has no effect) |
|
| 78 | + * |
|
| 79 | + * The public role is special, and is applied to all users automatically. Avoid using deny on this role. |
|
| 80 | + * |
|
| 81 | + * @var array |
|
| 82 | + */ |
|
| 83 | + private $roleConfig = array( |
|
| 84 | + 'public' => array( |
|
| 85 | + /* |
|
| 86 | 86 | * THIS ROLE IS GRANTED TO ALL LOGGED *OUT* USERS IMPLICITLY. |
| 87 | 87 | * |
| 88 | 88 | * USERS IN THIS ROLE DO NOT HAVE TO BE IDENTIFIED TO GET THE RIGHTS CONFERRED HERE. |
| 89 | 89 | * DO NOT ADD ANY SECURITY-SENSITIVE RIGHTS HERE. |
| 90 | 90 | */ |
| 91 | - '_childRoles' => array( |
|
| 92 | - 'publicStats', |
|
| 93 | - ), |
|
| 94 | - PageTeam::class => array( |
|
| 95 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 96 | - ), |
|
| 97 | - PageXffDemo::class => array( |
|
| 98 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 99 | - ) |
|
| 100 | - ), |
|
| 101 | - 'loggedIn' => array( |
|
| 102 | - /* |
|
| 91 | + '_childRoles' => array( |
|
| 92 | + 'publicStats', |
|
| 93 | + ), |
|
| 94 | + PageTeam::class => array( |
|
| 95 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 96 | + ), |
|
| 97 | + PageXffDemo::class => array( |
|
| 98 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 99 | + ) |
|
| 100 | + ), |
|
| 101 | + 'loggedIn' => array( |
|
| 102 | + /* |
|
| 103 | 103 | * THIS ROLE IS GRANTED TO ALL LOGGED IN USERS IMPLICITLY. |
| 104 | 104 | * |
| 105 | 105 | * USERS IN THIS ROLE DO NOT HAVE TO BE IDENTIFIED TO GET THE RIGHTS CONFERRED HERE. |
| 106 | 106 | * DO NOT ADD ANY SECURITY-SENSITIVE RIGHTS HERE. |
| 107 | 107 | */ |
| 108 | - '_childRoles' => array( |
|
| 109 | - 'public', |
|
| 110 | - ), |
|
| 111 | - PagePreferences::class => array( |
|
| 112 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 113 | - 'refreshOAuth' => self::ACCESS_ALLOW, |
|
| 114 | - ), |
|
| 115 | - PageChangePassword::class => array( |
|
| 116 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 117 | - ), |
|
| 118 | - PageMultiFactor::class => array( |
|
| 119 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 120 | - 'scratch' => self::ACCESS_ALLOW, |
|
| 121 | - 'enableYubikeyOtp' => self::ACCESS_ALLOW, |
|
| 122 | - 'disableYubikeyOtp' => self::ACCESS_ALLOW, |
|
| 123 | - 'enableTotp' => self::ACCESS_ALLOW, |
|
| 124 | - 'disableTotp' => self::ACCESS_ALLOW, |
|
| 125 | - ), |
|
| 126 | - PageOAuth::class => array( |
|
| 127 | - 'attach' => self::ACCESS_ALLOW, |
|
| 128 | - 'detach' => self::ACCESS_ALLOW, |
|
| 129 | - ), |
|
| 130 | - ), |
|
| 131 | - 'user' => array( |
|
| 132 | - '_description' => 'A standard tool user.', |
|
| 133 | - '_editableBy' => array('admin', 'toolRoot'), |
|
| 134 | - '_childRoles' => array( |
|
| 135 | - 'internalStats', |
|
| 136 | - ), |
|
| 137 | - PageMain::class => array( |
|
| 138 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 139 | - ), |
|
| 140 | - PageBan::class => array( |
|
| 141 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 142 | - ), |
|
| 143 | - PageEditComment::class => array( |
|
| 144 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 145 | - ), |
|
| 146 | - PageEmailManagement::class => array( |
|
| 147 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 148 | - 'view' => self::ACCESS_ALLOW, |
|
| 149 | - ), |
|
| 150 | - PageExpandedRequestList::class => array( |
|
| 151 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 152 | - ), |
|
| 153 | - PageLog::class => array( |
|
| 154 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 155 | - ), |
|
| 156 | - PageSearch::class => array( |
|
| 157 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 158 | - ), |
|
| 159 | - PageWelcomeTemplateManagement::class => array( |
|
| 160 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 161 | - 'select' => self::ACCESS_ALLOW, |
|
| 162 | - 'view' => self::ACCESS_ALLOW, |
|
| 163 | - ), |
|
| 164 | - PageViewRequest::class => array( |
|
| 165 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 166 | - 'seeAllRequests' => self::ACCESS_ALLOW, |
|
| 167 | - ), |
|
| 168 | - 'RequestData' => array( |
|
| 169 | - 'seePrivateDataWhenReserved' => self::ACCESS_ALLOW, |
|
| 170 | - 'seePrivateDataWithHash' => self::ACCESS_ALLOW, |
|
| 171 | - ), |
|
| 172 | - PageCustomClose::class => array( |
|
| 173 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 174 | - ), |
|
| 175 | - PageComment::class => array( |
|
| 176 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 177 | - ), |
|
| 178 | - PageCloseRequest::class => array( |
|
| 179 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 180 | - ), |
|
| 181 | - PageCreateRequest::class => array( |
|
| 182 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 183 | - ), |
|
| 184 | - PageDeferRequest::class => array( |
|
| 185 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 186 | - ), |
|
| 187 | - PageDropRequest::class => array( |
|
| 188 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 189 | - ), |
|
| 190 | - PageReservation::class => array( |
|
| 191 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 192 | - ), |
|
| 193 | - PageSendToUser::class => array( |
|
| 194 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 195 | - ), |
|
| 196 | - PageBreakReservation::class => array( |
|
| 197 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 198 | - ), |
|
| 199 | - PageJobQueue::class => array( |
|
| 200 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 201 | - 'view' => self::ACCESS_ALLOW, |
|
| 202 | - 'all' => self::ACCESS_ALLOW, |
|
| 203 | - ), |
|
| 204 | - 'RequestCreation' => array( |
|
| 205 | - User::CREATION_MANUAL => self::ACCESS_ALLOW, |
|
| 206 | - ), |
|
| 207 | - 'GlobalInfo' => array( |
|
| 208 | - 'viewSiteNotice' => self::ACCESS_ALLOW, |
|
| 209 | - 'viewOnlineUsers' => self::ACCESS_ALLOW, |
|
| 210 | - ), |
|
| 211 | - ), |
|
| 212 | - 'admin' => array( |
|
| 213 | - '_description' => 'A tool administrator.', |
|
| 214 | - '_editableBy' => array('admin', 'toolRoot'), |
|
| 215 | - '_childRoles' => array( |
|
| 216 | - 'user', |
|
| 217 | - 'requestAdminTools', |
|
| 218 | - ), |
|
| 219 | - PageEmailManagement::class => array( |
|
| 220 | - 'edit' => self::ACCESS_ALLOW, |
|
| 221 | - 'create' => self::ACCESS_ALLOW, |
|
| 222 | - ), |
|
| 223 | - PageSiteNotice::class => array( |
|
| 224 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 225 | - ), |
|
| 226 | - PageUserManagement::class => array( |
|
| 227 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 228 | - 'approve' => self::ACCESS_ALLOW, |
|
| 229 | - 'decline' => self::ACCESS_ALLOW, |
|
| 230 | - 'rename' => self::ACCESS_ALLOW, |
|
| 231 | - 'editUser' => self::ACCESS_ALLOW, |
|
| 232 | - 'suspend' => self::ACCESS_ALLOW, |
|
| 233 | - 'editRoles' => self::ACCESS_ALLOW, |
|
| 234 | - ), |
|
| 235 | - PageWelcomeTemplateManagement::class => array( |
|
| 236 | - 'edit' => self::ACCESS_ALLOW, |
|
| 237 | - 'delete' => self::ACCESS_ALLOW, |
|
| 238 | - 'add' => self::ACCESS_ALLOW, |
|
| 239 | - ), |
|
| 240 | - PageJobQueue::class => array( |
|
| 241 | - 'acknowledge' => self::ACCESS_ALLOW, |
|
| 242 | - 'requeue' => self::ACCESS_ALLOW, |
|
| 243 | - ), |
|
| 244 | - ), |
|
| 245 | - 'checkuser' => array( |
|
| 246 | - '_description' => 'A user with CheckUser access', |
|
| 247 | - '_editableBy' => array('checkuser', 'toolRoot'), |
|
| 248 | - '_childRoles' => array( |
|
| 249 | - 'user', |
|
| 250 | - 'requestAdminTools', |
|
| 251 | - ), |
|
| 252 | - PageUserManagement::class => array( |
|
| 253 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 254 | - 'suspend' => self::ACCESS_ALLOW, |
|
| 255 | - 'editRoles' => self::ACCESS_ALLOW, |
|
| 256 | - ), |
|
| 257 | - 'RequestData' => array( |
|
| 258 | - 'seeUserAgentData' => self::ACCESS_ALLOW, |
|
| 259 | - ), |
|
| 260 | - ), |
|
| 261 | - 'toolRoot' => array( |
|
| 262 | - '_description' => 'A user with shell access to the servers running the tool', |
|
| 263 | - '_editableBy' => array('toolRoot'), |
|
| 264 | - '_childRoles' => array( |
|
| 265 | - 'admin', |
|
| 266 | - ), |
|
| 267 | - PageMultiFactor::class => array( |
|
| 268 | - 'enableU2F' => self::ACCESS_ALLOW, |
|
| 269 | - 'disableU2F' => self::ACCESS_ALLOW, |
|
| 270 | - ) |
|
| 271 | - ), |
|
| 272 | - 'botCreation' => array( |
|
| 273 | - '_description' => 'A user allowed to use the bot to perform account creations', |
|
| 274 | - '_editableBy' => array('admin', 'toolRoot'), |
|
| 275 | - '_childRoles' => array(), |
|
| 276 | - 'RequestCreation' => array( |
|
| 277 | - User::CREATION_BOT => self::ACCESS_ALLOW, |
|
| 278 | - ), |
|
| 279 | - ), |
|
| 280 | - 'oauthCreation' => array( |
|
| 281 | - '_description' => 'A user allowed to use the OAuth to perform account creations', |
|
| 282 | - '_editableBy' => array('admin', 'toolRoot'), |
|
| 283 | - '_childRoles' => array(), |
|
| 284 | - 'RequestCreation' => array( |
|
| 285 | - User::CREATION_OAUTH => self::ACCESS_ALLOW, |
|
| 286 | - ), |
|
| 287 | - ), |
|
| 108 | + '_childRoles' => array( |
|
| 109 | + 'public', |
|
| 110 | + ), |
|
| 111 | + PagePreferences::class => array( |
|
| 112 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 113 | + 'refreshOAuth' => self::ACCESS_ALLOW, |
|
| 114 | + ), |
|
| 115 | + PageChangePassword::class => array( |
|
| 116 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 117 | + ), |
|
| 118 | + PageMultiFactor::class => array( |
|
| 119 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 120 | + 'scratch' => self::ACCESS_ALLOW, |
|
| 121 | + 'enableYubikeyOtp' => self::ACCESS_ALLOW, |
|
| 122 | + 'disableYubikeyOtp' => self::ACCESS_ALLOW, |
|
| 123 | + 'enableTotp' => self::ACCESS_ALLOW, |
|
| 124 | + 'disableTotp' => self::ACCESS_ALLOW, |
|
| 125 | + ), |
|
| 126 | + PageOAuth::class => array( |
|
| 127 | + 'attach' => self::ACCESS_ALLOW, |
|
| 128 | + 'detach' => self::ACCESS_ALLOW, |
|
| 129 | + ), |
|
| 130 | + ), |
|
| 131 | + 'user' => array( |
|
| 132 | + '_description' => 'A standard tool user.', |
|
| 133 | + '_editableBy' => array('admin', 'toolRoot'), |
|
| 134 | + '_childRoles' => array( |
|
| 135 | + 'internalStats', |
|
| 136 | + ), |
|
| 137 | + PageMain::class => array( |
|
| 138 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 139 | + ), |
|
| 140 | + PageBan::class => array( |
|
| 141 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 142 | + ), |
|
| 143 | + PageEditComment::class => array( |
|
| 144 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 145 | + ), |
|
| 146 | + PageEmailManagement::class => array( |
|
| 147 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 148 | + 'view' => self::ACCESS_ALLOW, |
|
| 149 | + ), |
|
| 150 | + PageExpandedRequestList::class => array( |
|
| 151 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 152 | + ), |
|
| 153 | + PageLog::class => array( |
|
| 154 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 155 | + ), |
|
| 156 | + PageSearch::class => array( |
|
| 157 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 158 | + ), |
|
| 159 | + PageWelcomeTemplateManagement::class => array( |
|
| 160 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 161 | + 'select' => self::ACCESS_ALLOW, |
|
| 162 | + 'view' => self::ACCESS_ALLOW, |
|
| 163 | + ), |
|
| 164 | + PageViewRequest::class => array( |
|
| 165 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 166 | + 'seeAllRequests' => self::ACCESS_ALLOW, |
|
| 167 | + ), |
|
| 168 | + 'RequestData' => array( |
|
| 169 | + 'seePrivateDataWhenReserved' => self::ACCESS_ALLOW, |
|
| 170 | + 'seePrivateDataWithHash' => self::ACCESS_ALLOW, |
|
| 171 | + ), |
|
| 172 | + PageCustomClose::class => array( |
|
| 173 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 174 | + ), |
|
| 175 | + PageComment::class => array( |
|
| 176 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 177 | + ), |
|
| 178 | + PageCloseRequest::class => array( |
|
| 179 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 180 | + ), |
|
| 181 | + PageCreateRequest::class => array( |
|
| 182 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 183 | + ), |
|
| 184 | + PageDeferRequest::class => array( |
|
| 185 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 186 | + ), |
|
| 187 | + PageDropRequest::class => array( |
|
| 188 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 189 | + ), |
|
| 190 | + PageReservation::class => array( |
|
| 191 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 192 | + ), |
|
| 193 | + PageSendToUser::class => array( |
|
| 194 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 195 | + ), |
|
| 196 | + PageBreakReservation::class => array( |
|
| 197 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 198 | + ), |
|
| 199 | + PageJobQueue::class => array( |
|
| 200 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 201 | + 'view' => self::ACCESS_ALLOW, |
|
| 202 | + 'all' => self::ACCESS_ALLOW, |
|
| 203 | + ), |
|
| 204 | + 'RequestCreation' => array( |
|
| 205 | + User::CREATION_MANUAL => self::ACCESS_ALLOW, |
|
| 206 | + ), |
|
| 207 | + 'GlobalInfo' => array( |
|
| 208 | + 'viewSiteNotice' => self::ACCESS_ALLOW, |
|
| 209 | + 'viewOnlineUsers' => self::ACCESS_ALLOW, |
|
| 210 | + ), |
|
| 211 | + ), |
|
| 212 | + 'admin' => array( |
|
| 213 | + '_description' => 'A tool administrator.', |
|
| 214 | + '_editableBy' => array('admin', 'toolRoot'), |
|
| 215 | + '_childRoles' => array( |
|
| 216 | + 'user', |
|
| 217 | + 'requestAdminTools', |
|
| 218 | + ), |
|
| 219 | + PageEmailManagement::class => array( |
|
| 220 | + 'edit' => self::ACCESS_ALLOW, |
|
| 221 | + 'create' => self::ACCESS_ALLOW, |
|
| 222 | + ), |
|
| 223 | + PageSiteNotice::class => array( |
|
| 224 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 225 | + ), |
|
| 226 | + PageUserManagement::class => array( |
|
| 227 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 228 | + 'approve' => self::ACCESS_ALLOW, |
|
| 229 | + 'decline' => self::ACCESS_ALLOW, |
|
| 230 | + 'rename' => self::ACCESS_ALLOW, |
|
| 231 | + 'editUser' => self::ACCESS_ALLOW, |
|
| 232 | + 'suspend' => self::ACCESS_ALLOW, |
|
| 233 | + 'editRoles' => self::ACCESS_ALLOW, |
|
| 234 | + ), |
|
| 235 | + PageWelcomeTemplateManagement::class => array( |
|
| 236 | + 'edit' => self::ACCESS_ALLOW, |
|
| 237 | + 'delete' => self::ACCESS_ALLOW, |
|
| 238 | + 'add' => self::ACCESS_ALLOW, |
|
| 239 | + ), |
|
| 240 | + PageJobQueue::class => array( |
|
| 241 | + 'acknowledge' => self::ACCESS_ALLOW, |
|
| 242 | + 'requeue' => self::ACCESS_ALLOW, |
|
| 243 | + ), |
|
| 244 | + ), |
|
| 245 | + 'checkuser' => array( |
|
| 246 | + '_description' => 'A user with CheckUser access', |
|
| 247 | + '_editableBy' => array('checkuser', 'toolRoot'), |
|
| 248 | + '_childRoles' => array( |
|
| 249 | + 'user', |
|
| 250 | + 'requestAdminTools', |
|
| 251 | + ), |
|
| 252 | + PageUserManagement::class => array( |
|
| 253 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 254 | + 'suspend' => self::ACCESS_ALLOW, |
|
| 255 | + 'editRoles' => self::ACCESS_ALLOW, |
|
| 256 | + ), |
|
| 257 | + 'RequestData' => array( |
|
| 258 | + 'seeUserAgentData' => self::ACCESS_ALLOW, |
|
| 259 | + ), |
|
| 260 | + ), |
|
| 261 | + 'toolRoot' => array( |
|
| 262 | + '_description' => 'A user with shell access to the servers running the tool', |
|
| 263 | + '_editableBy' => array('toolRoot'), |
|
| 264 | + '_childRoles' => array( |
|
| 265 | + 'admin', |
|
| 266 | + ), |
|
| 267 | + PageMultiFactor::class => array( |
|
| 268 | + 'enableU2F' => self::ACCESS_ALLOW, |
|
| 269 | + 'disableU2F' => self::ACCESS_ALLOW, |
|
| 270 | + ) |
|
| 271 | + ), |
|
| 272 | + 'botCreation' => array( |
|
| 273 | + '_description' => 'A user allowed to use the bot to perform account creations', |
|
| 274 | + '_editableBy' => array('admin', 'toolRoot'), |
|
| 275 | + '_childRoles' => array(), |
|
| 276 | + 'RequestCreation' => array( |
|
| 277 | + User::CREATION_BOT => self::ACCESS_ALLOW, |
|
| 278 | + ), |
|
| 279 | + ), |
|
| 280 | + 'oauthCreation' => array( |
|
| 281 | + '_description' => 'A user allowed to use the OAuth to perform account creations', |
|
| 282 | + '_editableBy' => array('admin', 'toolRoot'), |
|
| 283 | + '_childRoles' => array(), |
|
| 284 | + 'RequestCreation' => array( |
|
| 285 | + User::CREATION_OAUTH => self::ACCESS_ALLOW, |
|
| 286 | + ), |
|
| 287 | + ), |
|
| 288 | 288 | |
| 289 | 289 | |
| 290 | - // Child roles go below this point |
|
| 291 | - 'publicStats' => array( |
|
| 292 | - '_hidden' => true, |
|
| 293 | - StatsUsers::class => array( |
|
| 294 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 295 | - 'detail' => self::ACCESS_ALLOW, |
|
| 296 | - ), |
|
| 297 | - StatsTopCreators::class => array( |
|
| 298 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 299 | - ), |
|
| 300 | - ), |
|
| 301 | - 'internalStats' => array( |
|
| 302 | - '_hidden' => true, |
|
| 303 | - StatsMain::class => array( |
|
| 304 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 305 | - ), |
|
| 306 | - StatsFastCloses::class => array( |
|
| 307 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 308 | - ), |
|
| 309 | - StatsInactiveUsers::class => array( |
|
| 310 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 311 | - ), |
|
| 312 | - StatsMonthlyStats::class => array( |
|
| 313 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 314 | - ), |
|
| 315 | - StatsReservedRequests::class => array( |
|
| 316 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 317 | - ), |
|
| 318 | - StatsTemplateStats::class => array( |
|
| 319 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 320 | - ), |
|
| 321 | - ), |
|
| 322 | - 'requestAdminTools' => array( |
|
| 323 | - '_hidden' => true, |
|
| 324 | - PageBan::class => array( |
|
| 325 | - self::MAIN => self::ACCESS_ALLOW, |
|
| 326 | - 'set' => self::ACCESS_ALLOW, |
|
| 327 | - 'remove' => self::ACCESS_ALLOW, |
|
| 328 | - ), |
|
| 329 | - PageEditComment::class => array( |
|
| 330 | - 'editOthers' => self::ACCESS_ALLOW, |
|
| 331 | - ), |
|
| 332 | - PageBreakReservation::class => array( |
|
| 333 | - 'force' => self::ACCESS_ALLOW, |
|
| 334 | - ), |
|
| 335 | - PageCustomClose::class => array( |
|
| 336 | - 'skipCcMailingList' => self::ACCESS_ALLOW, |
|
| 337 | - ), |
|
| 338 | - 'RequestData' => array( |
|
| 339 | - 'reopenOldRequest' => self::ACCESS_ALLOW, |
|
| 340 | - 'alwaysSeePrivateData' => self::ACCESS_ALLOW, |
|
| 341 | - 'alwaysSeeHash' => self::ACCESS_ALLOW, |
|
| 342 | - 'seeRestrictedComments' => self::ACCESS_ALLOW, |
|
| 343 | - ), |
|
| 344 | - ), |
|
| 345 | - ); |
|
| 346 | - /** @var array |
|
| 347 | - * List of roles which are *exempt* from the identification requirements |
|
| 348 | - * |
|
| 349 | - * Think twice about adding roles to this list. |
|
| 350 | - * |
|
| 351 | - * @category Security-Critical |
|
| 352 | - */ |
|
| 353 | - private $identificationExempt = array('public', 'loggedIn'); |
|
| 290 | + // Child roles go below this point |
|
| 291 | + 'publicStats' => array( |
|
| 292 | + '_hidden' => true, |
|
| 293 | + StatsUsers::class => array( |
|
| 294 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 295 | + 'detail' => self::ACCESS_ALLOW, |
|
| 296 | + ), |
|
| 297 | + StatsTopCreators::class => array( |
|
| 298 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 299 | + ), |
|
| 300 | + ), |
|
| 301 | + 'internalStats' => array( |
|
| 302 | + '_hidden' => true, |
|
| 303 | + StatsMain::class => array( |
|
| 304 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 305 | + ), |
|
| 306 | + StatsFastCloses::class => array( |
|
| 307 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 308 | + ), |
|
| 309 | + StatsInactiveUsers::class => array( |
|
| 310 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 311 | + ), |
|
| 312 | + StatsMonthlyStats::class => array( |
|
| 313 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 314 | + ), |
|
| 315 | + StatsReservedRequests::class => array( |
|
| 316 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 317 | + ), |
|
| 318 | + StatsTemplateStats::class => array( |
|
| 319 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 320 | + ), |
|
| 321 | + ), |
|
| 322 | + 'requestAdminTools' => array( |
|
| 323 | + '_hidden' => true, |
|
| 324 | + PageBan::class => array( |
|
| 325 | + self::MAIN => self::ACCESS_ALLOW, |
|
| 326 | + 'set' => self::ACCESS_ALLOW, |
|
| 327 | + 'remove' => self::ACCESS_ALLOW, |
|
| 328 | + ), |
|
| 329 | + PageEditComment::class => array( |
|
| 330 | + 'editOthers' => self::ACCESS_ALLOW, |
|
| 331 | + ), |
|
| 332 | + PageBreakReservation::class => array( |
|
| 333 | + 'force' => self::ACCESS_ALLOW, |
|
| 334 | + ), |
|
| 335 | + PageCustomClose::class => array( |
|
| 336 | + 'skipCcMailingList' => self::ACCESS_ALLOW, |
|
| 337 | + ), |
|
| 338 | + 'RequestData' => array( |
|
| 339 | + 'reopenOldRequest' => self::ACCESS_ALLOW, |
|
| 340 | + 'alwaysSeePrivateData' => self::ACCESS_ALLOW, |
|
| 341 | + 'alwaysSeeHash' => self::ACCESS_ALLOW, |
|
| 342 | + 'seeRestrictedComments' => self::ACCESS_ALLOW, |
|
| 343 | + ), |
|
| 344 | + ), |
|
| 345 | + ); |
|
| 346 | + /** @var array |
|
| 347 | + * List of roles which are *exempt* from the identification requirements |
|
| 348 | + * |
|
| 349 | + * Think twice about adding roles to this list. |
|
| 350 | + * |
|
| 351 | + * @category Security-Critical |
|
| 352 | + */ |
|
| 353 | + private $identificationExempt = array('public', 'loggedIn'); |
|
| 354 | 354 | |
| 355 | - /** |
|
| 356 | - * RoleConfiguration constructor. |
|
| 357 | - * |
|
| 358 | - * @param array $roleConfig Set to non-null to override the default configuration. |
|
| 359 | - * @param array $identificationExempt Set to non-null to override the default configuration. |
|
| 360 | - */ |
|
| 361 | - public function __construct(array $roleConfig = null, array $identificationExempt = null) |
|
| 362 | - { |
|
| 363 | - if ($roleConfig !== null) { |
|
| 364 | - $this->roleConfig = $roleConfig; |
|
| 365 | - } |
|
| 355 | + /** |
|
| 356 | + * RoleConfiguration constructor. |
|
| 357 | + * |
|
| 358 | + * @param array $roleConfig Set to non-null to override the default configuration. |
|
| 359 | + * @param array $identificationExempt Set to non-null to override the default configuration. |
|
| 360 | + */ |
|
| 361 | + public function __construct(array $roleConfig = null, array $identificationExempt = null) |
|
| 362 | + { |
|
| 363 | + if ($roleConfig !== null) { |
|
| 364 | + $this->roleConfig = $roleConfig; |
|
| 365 | + } |
|
| 366 | 366 | |
| 367 | - if ($identificationExempt !== null) { |
|
| 368 | - $this->identificationExempt = $identificationExempt; |
|
| 369 | - } |
|
| 370 | - } |
|
| 367 | + if ($identificationExempt !== null) { |
|
| 368 | + $this->identificationExempt = $identificationExempt; |
|
| 369 | + } |
|
| 370 | + } |
|
| 371 | 371 | |
| 372 | - /** |
|
| 373 | - * @param array $roles The roles to check |
|
| 374 | - * |
|
| 375 | - * @return array |
|
| 376 | - */ |
|
| 377 | - public function getApplicableRoles(array $roles) |
|
| 378 | - { |
|
| 379 | - $available = array(); |
|
| 372 | + /** |
|
| 373 | + * @param array $roles The roles to check |
|
| 374 | + * |
|
| 375 | + * @return array |
|
| 376 | + */ |
|
| 377 | + public function getApplicableRoles(array $roles) |
|
| 378 | + { |
|
| 379 | + $available = array(); |
|
| 380 | 380 | |
| 381 | - foreach ($roles as $role) { |
|
| 382 | - if (!isset($this->roleConfig[$role])) { |
|
| 383 | - // wat |
|
| 384 | - continue; |
|
| 385 | - } |
|
| 381 | + foreach ($roles as $role) { |
|
| 382 | + if (!isset($this->roleConfig[$role])) { |
|
| 383 | + // wat |
|
| 384 | + continue; |
|
| 385 | + } |
|
| 386 | 386 | |
| 387 | - $available[$role] = $this->roleConfig[$role]; |
|
| 387 | + $available[$role] = $this->roleConfig[$role]; |
|
| 388 | 388 | |
| 389 | - if (isset($available[$role]['_childRoles'])) { |
|
| 390 | - $childRoles = $this->getApplicableRoles($available[$role]['_childRoles']); |
|
| 391 | - $available = array_merge($available, $childRoles); |
|
| 389 | + if (isset($available[$role]['_childRoles'])) { |
|
| 390 | + $childRoles = $this->getApplicableRoles($available[$role]['_childRoles']); |
|
| 391 | + $available = array_merge($available, $childRoles); |
|
| 392 | 392 | |
| 393 | - unset($available[$role]['_childRoles']); |
|
| 394 | - } |
|
| 393 | + unset($available[$role]['_childRoles']); |
|
| 394 | + } |
|
| 395 | 395 | |
| 396 | - foreach (array('_hidden', '_editableBy', '_description') as $item) { |
|
| 397 | - if (isset($available[$role][$item])) { |
|
| 398 | - unset($available[$role][$item]); |
|
| 399 | - } |
|
| 400 | - } |
|
| 401 | - } |
|
| 396 | + foreach (array('_hidden', '_editableBy', '_description') as $item) { |
|
| 397 | + if (isset($available[$role][$item])) { |
|
| 398 | + unset($available[$role][$item]); |
|
| 399 | + } |
|
| 400 | + } |
|
| 401 | + } |
|
| 402 | 402 | |
| 403 | - return $available; |
|
| 404 | - } |
|
| 403 | + return $available; |
|
| 404 | + } |
|
| 405 | 405 | |
| 406 | - public function getAvailableRoles() |
|
| 407 | - { |
|
| 408 | - $possible = array_diff(array_keys($this->roleConfig), array('public', 'loggedIn')); |
|
| 406 | + public function getAvailableRoles() |
|
| 407 | + { |
|
| 408 | + $possible = array_diff(array_keys($this->roleConfig), array('public', 'loggedIn')); |
|
| 409 | 409 | |
| 410 | - $actual = array(); |
|
| 410 | + $actual = array(); |
|
| 411 | 411 | |
| 412 | - foreach ($possible as $role) { |
|
| 413 | - if (!isset($this->roleConfig[$role]['_hidden'])) { |
|
| 414 | - $actual[$role] = array( |
|
| 415 | - 'description' => $this->roleConfig[$role]['_description'], |
|
| 416 | - 'editableBy' => $this->roleConfig[$role]['_editableBy'], |
|
| 417 | - ); |
|
| 418 | - } |
|
| 419 | - } |
|
| 412 | + foreach ($possible as $role) { |
|
| 413 | + if (!isset($this->roleConfig[$role]['_hidden'])) { |
|
| 414 | + $actual[$role] = array( |
|
| 415 | + 'description' => $this->roleConfig[$role]['_description'], |
|
| 416 | + 'editableBy' => $this->roleConfig[$role]['_editableBy'], |
|
| 417 | + ); |
|
| 418 | + } |
|
| 419 | + } |
|
| 420 | 420 | |
| 421 | - return $actual; |
|
| 422 | - } |
|
| 421 | + return $actual; |
|
| 422 | + } |
|
| 423 | 423 | |
| 424 | - /** |
|
| 425 | - * @param string $role |
|
| 426 | - * |
|
| 427 | - * @return bool |
|
| 428 | - */ |
|
| 429 | - public function roleNeedsIdentification($role) |
|
| 430 | - { |
|
| 431 | - if (in_array($role, $this->identificationExempt)) { |
|
| 432 | - return false; |
|
| 433 | - } |
|
| 424 | + /** |
|
| 425 | + * @param string $role |
|
| 426 | + * |
|
| 427 | + * @return bool |
|
| 428 | + */ |
|
| 429 | + public function roleNeedsIdentification($role) |
|
| 430 | + { |
|
| 431 | + if (in_array($role, $this->identificationExempt)) { |
|
| 432 | + return false; |
|
| 433 | + } |
|
| 434 | 434 | |
| 435 | - return true; |
|
| 436 | - } |
|
| 435 | + return true; |
|
| 436 | + } |
|
| 437 | 437 | } |
@@ -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 |
@@ -15,74 +15,74 @@ |
||
| 15 | 15 | |
| 16 | 16 | class ConsoleStart extends ApplicationBase |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * @var ConsoleTaskBase |
|
| 20 | - */ |
|
| 21 | - private $consoleTask; |
|
| 18 | + /** |
|
| 19 | + * @var ConsoleTaskBase |
|
| 20 | + */ |
|
| 21 | + private $consoleTask; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * ConsoleStart constructor. |
|
| 25 | - * |
|
| 26 | - * @param SiteConfiguration $configuration |
|
| 27 | - * @param ConsoleTaskBase $consoleTask |
|
| 28 | - */ |
|
| 29 | - public function __construct(SiteConfiguration $configuration, ConsoleTaskBase $consoleTask) |
|
| 30 | - { |
|
| 31 | - parent::__construct($configuration); |
|
| 32 | - $this->consoleTask = $consoleTask; |
|
| 33 | - } |
|
| 23 | + /** |
|
| 24 | + * ConsoleStart constructor. |
|
| 25 | + * |
|
| 26 | + * @param SiteConfiguration $configuration |
|
| 27 | + * @param ConsoleTaskBase $consoleTask |
|
| 28 | + */ |
|
| 29 | + public function __construct(SiteConfiguration $configuration, ConsoleTaskBase $consoleTask) |
|
| 30 | + { |
|
| 31 | + parent::__construct($configuration); |
|
| 32 | + $this->consoleTask = $consoleTask; |
|
| 33 | + } |
|
| 34 | 34 | |
| 35 | - protected function setupEnvironment() |
|
| 36 | - { |
|
| 37 | - // initialise super-global providers |
|
| 38 | - WebRequest::setGlobalStateProvider(new FakeGlobalStateProvider()); |
|
| 35 | + protected function setupEnvironment() |
|
| 36 | + { |
|
| 37 | + // initialise super-global providers |
|
| 38 | + WebRequest::setGlobalStateProvider(new FakeGlobalStateProvider()); |
|
| 39 | 39 | |
| 40 | - if (WebRequest::method() !== null) { |
|
| 41 | - throw new EnvironmentException('This is a console task, which cannot be executed via the web.'); |
|
| 42 | - } |
|
| 40 | + if (WebRequest::method() !== null) { |
|
| 41 | + throw new EnvironmentException('This is a console task, which cannot be executed via the web.'); |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | - return parent::setupEnvironment(); |
|
| 45 | - } |
|
| 44 | + return parent::setupEnvironment(); |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - protected function cleanupEnvironment() |
|
| 48 | - { |
|
| 49 | - } |
|
| 47 | + protected function cleanupEnvironment() |
|
| 48 | + { |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * Main application logic |
|
| 53 | - */ |
|
| 54 | - protected function main() |
|
| 55 | - { |
|
| 56 | - $database = PdoDatabase::getDatabaseConnection('acc'); |
|
| 51 | + /** |
|
| 52 | + * Main application logic |
|
| 53 | + */ |
|
| 54 | + protected function main() |
|
| 55 | + { |
|
| 56 | + $database = PdoDatabase::getDatabaseConnection('acc'); |
|
| 57 | 57 | |
| 58 | - if ($this->getConfiguration()->getIrcNotificationsEnabled()) { |
|
| 59 | - $notificationsDatabase = PdoDatabase::getDatabaseConnection('notifications'); |
|
| 60 | - } |
|
| 61 | - else { |
|
| 62 | - // pass through null |
|
| 63 | - $notificationsDatabase = null; |
|
| 64 | - } |
|
| 58 | + if ($this->getConfiguration()->getIrcNotificationsEnabled()) { |
|
| 59 | + $notificationsDatabase = PdoDatabase::getDatabaseConnection('notifications'); |
|
| 60 | + } |
|
| 61 | + else { |
|
| 62 | + // pass through null |
|
| 63 | + $notificationsDatabase = null; |
|
| 64 | + } |
|
| 65 | 65 | |
| 66 | - $this->setupHelpers($this->consoleTask, $this->getConfiguration(), $database, $notificationsDatabase); |
|
| 66 | + $this->setupHelpers($this->consoleTask, $this->getConfiguration(), $database, $notificationsDatabase); |
|
| 67 | 67 | |
| 68 | - // initialise a database transaction |
|
| 69 | - if (!$database->beginTransaction()) { |
|
| 70 | - throw new Exception('Failed to start transaction on primary database.'); |
|
| 71 | - } |
|
| 68 | + // initialise a database transaction |
|
| 69 | + if (!$database->beginTransaction()) { |
|
| 70 | + throw new Exception('Failed to start transaction on primary database.'); |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - try { |
|
| 74 | - // run the task |
|
| 75 | - $this->consoleTask->execute(); |
|
| 73 | + try { |
|
| 74 | + // run the task |
|
| 75 | + $this->consoleTask->execute(); |
|
| 76 | 76 | |
| 77 | - if ($database->hasActiveTransaction()) { |
|
| 78 | - $database->commit(); |
|
| 79 | - } |
|
| 80 | - } |
|
| 81 | - finally { |
|
| 82 | - // Catch any hanging on transactions |
|
| 83 | - if ($database->hasActiveTransaction()) { |
|
| 84 | - $database->rollBack(); |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - } |
|
| 77 | + if ($database->hasActiveTransaction()) { |
|
| 78 | + $database->commit(); |
|
| 79 | + } |
|
| 80 | + } |
|
| 81 | + finally { |
|
| 82 | + // Catch any hanging on transactions |
|
| 83 | + if ($database->hasActiveTransaction()) { |
|
| 84 | + $database->rollBack(); |
|
| 85 | + } |
|
| 86 | + } |
|
| 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 |