@@ -200,24 +200,24 @@ discard block |
||
| 200 | 200 | |
| 201 | 201 | // request states |
| 202 | 202 | $availableRequestStates = array( |
| 203 | - 'Open' => array( |
|
| 204 | - 'defertolog' => 'users', // don't change or you'll break old logs |
|
| 205 | - 'deferto' => 'users', |
|
| 206 | - 'header' => 'Open requests', |
|
| 207 | - 'api' => "open", |
|
| 208 | - ), |
|
| 209 | - 'Flagged users' => array( |
|
| 210 | - 'defertolog' => 'flagged users', // don't change or you'll break old logs |
|
| 211 | - 'deferto' => 'flagged users', |
|
| 212 | - 'header' => 'Flagged user needed', |
|
| 213 | - 'api' => "admin", |
|
| 214 | - ), |
|
| 215 | - 'Checkuser' => array( |
|
| 216 | - 'defertolog' => 'checkusers', // don't change or you'll break old logs |
|
| 217 | - 'deferto' => 'checkusers', |
|
| 218 | - 'header' => 'Checkuser needed', |
|
| 219 | - 'api' => "checkuser", |
|
| 220 | - ), |
|
| 203 | + 'Open' => array( |
|
| 204 | + 'defertolog' => 'users', // don't change or you'll break old logs |
|
| 205 | + 'deferto' => 'users', |
|
| 206 | + 'header' => 'Open requests', |
|
| 207 | + 'api' => "open", |
|
| 208 | + ), |
|
| 209 | + 'Flagged users' => array( |
|
| 210 | + 'defertolog' => 'flagged users', // don't change or you'll break old logs |
|
| 211 | + 'deferto' => 'flagged users', |
|
| 212 | + 'header' => 'Flagged user needed', |
|
| 213 | + 'api' => "admin", |
|
| 214 | + ), |
|
| 215 | + 'Checkuser' => array( |
|
| 216 | + 'defertolog' => 'checkusers', // don't change or you'll break old logs |
|
| 217 | + 'deferto' => 'checkusers', |
|
| 218 | + 'header' => 'Checkuser needed', |
|
| 219 | + 'api' => "checkuser", |
|
| 220 | + ), |
|
| 221 | 221 | ); |
| 222 | 222 | |
| 223 | 223 | $defaultRequestStateKey = 'Open'; |
@@ -264,21 +264,21 @@ discard block |
||
| 264 | 264 | require_once('config.local.inc.php'); |
| 265 | 265 | |
| 266 | 266 | $cDatabaseConfig = array( |
| 267 | - "acc" => array( |
|
| 268 | - "dsrcname" => "mysql:host=" . $toolserver_host . ";dbname=" . $toolserver_database, |
|
| 269 | - "username" => $toolserver_username, |
|
| 270 | - "password" => $toolserver_password, |
|
| 271 | - ), |
|
| 272 | - "wikipedia" => array( |
|
| 273 | - "dsrcname" => "mysql:host=" . $antispoof_host . ";dbname=" . $antispoof_db, |
|
| 274 | - "username" => $toolserver_username, |
|
| 275 | - "password" => $toolserver_password, |
|
| 276 | - ), |
|
| 277 | - "notifications" => array( |
|
| 278 | - "dsrcname" => "mysql:host=" . $toolserver_notification_dbhost . ";dbname=" . $toolserver_notification_database, |
|
| 279 | - "username" => $notifications_username, |
|
| 280 | - "password" => $notifications_password, |
|
| 281 | - ), |
|
| 267 | + "acc" => array( |
|
| 268 | + "dsrcname" => "mysql:host=" . $toolserver_host . ";dbname=" . $toolserver_database, |
|
| 269 | + "username" => $toolserver_username, |
|
| 270 | + "password" => $toolserver_password, |
|
| 271 | + ), |
|
| 272 | + "wikipedia" => array( |
|
| 273 | + "dsrcname" => "mysql:host=" . $antispoof_host . ";dbname=" . $antispoof_db, |
|
| 274 | + "username" => $toolserver_username, |
|
| 275 | + "password" => $toolserver_password, |
|
| 276 | + ), |
|
| 277 | + "notifications" => array( |
|
| 278 | + "dsrcname" => "mysql:host=" . $toolserver_notification_dbhost . ";dbname=" . $toolserver_notification_database, |
|
| 279 | + "username" => $notifications_username, |
|
| 280 | + "password" => $notifications_password, |
|
| 281 | + ), |
|
| 282 | 282 | ); |
| 283 | 283 | |
| 284 | 284 | // //Keep the included files from being executed. |
@@ -290,18 +290,18 @@ discard block |
||
| 290 | 290 | ini_set('user_agent', $toolUserAgent); |
| 291 | 291 | |
| 292 | 292 | foreach (array( |
| 293 | - "mbstring", // unicode and stuff |
|
| 294 | - "pdo", |
|
| 295 | - "pdo_mysql", // new database module |
|
| 296 | - "session", |
|
| 297 | - "date", |
|
| 298 | - "pcre", // core stuff |
|
| 299 | - "curl", // mediawiki api access etc |
|
| 300 | - "openssl", // token generation |
|
| 293 | + "mbstring", // unicode and stuff |
|
| 294 | + "pdo", |
|
| 295 | + "pdo_mysql", // new database module |
|
| 296 | + "session", |
|
| 297 | + "date", |
|
| 298 | + "pcre", // core stuff |
|
| 299 | + "curl", // mediawiki api access etc |
|
| 300 | + "openssl", // token generation |
|
| 301 | 301 | ) as $x) { |
| 302 | - if (!extension_loaded($x)) { |
|
| 303 | - die("extension $x is required."); |
|
| 304 | - } |
|
| 302 | + if (!extension_loaded($x)) { |
|
| 303 | + die("extension $x is required."); |
|
| 304 | + } |
|
| 305 | 305 | } |
| 306 | 306 | |
| 307 | 307 | // Set up the AutoLoader |
@@ -328,39 +328,39 @@ discard block |
||
| 328 | 328 | $siteConfiguration = new \Waca\SiteConfiguration(); |
| 329 | 329 | |
| 330 | 330 | $siteConfiguration->setBaseUrl($baseurl) |
| 331 | - ->setFilePath(__DIR__) |
|
| 332 | - ->setDebuggingTraceEnabled($enableErrorTrace) |
|
| 333 | - ->setForceIdentification($forceIdentification) |
|
| 334 | - ->setIdentificationCacheExpiry($identificationCacheExpiry) |
|
| 335 | - ->setMediawikiScriptPath($mediawikiScriptPath) |
|
| 336 | - ->setMediawikiWebServiceEndpoint($mediawikiWebServiceEndpoint) |
|
| 337 | - ->setMetaWikimediaWebServiceEndpoint($metaWikimediaWebServiceEndpoint) |
|
| 338 | - ->setEnforceOAuth($enforceOAuth) |
|
| 339 | - ->setEmailConfirmationEnabled($enableEmailConfirm == 1) |
|
| 340 | - ->setEmailConfirmationExpiryDays($emailConfirmationExpiryDays) |
|
| 341 | - ->setMiserModeLimit($requestLimitShowOnly) |
|
| 342 | - ->setRequestStates($availableRequestStates) |
|
| 343 | - ->setSquidList($squidIpList) |
|
| 344 | - ->setDefaultCreatedTemplateId($createdid) |
|
| 345 | - ->setDefaultRequestStateKey($defaultRequestStateKey) |
|
| 346 | - ->setUseStrictTransportSecurity($strictTransportSecurityExpiry) |
|
| 347 | - ->setUserAgent($toolUserAgent) |
|
| 348 | - ->setCurlDisableVerifyPeer($curlDisableSSLVerifyPeer) |
|
| 349 | - ->setUseOAuthSignup($useOauthSignup) |
|
| 350 | - ->setOAuthBaseUrl($oauthBaseUrl) |
|
| 351 | - ->setOAuthConsumerToken($oauthConsumerToken) |
|
| 352 | - ->setOAuthConsumerSecret($oauthSecretToken) |
|
| 353 | - ->setOauthMediaWikiCanonicalServer($oauthMediaWikiCanonicalServer) |
|
| 354 | - ->setDataClearInterval($dataclear_interval) |
|
| 355 | - ->setXffTrustedHostsFile($xff_trusted_hosts_file) |
|
| 356 | - ->setIrcNotificationsEnabled($ircBotNotificationsEnabled == 1) |
|
| 357 | - ->setIrcNotificationType($ircBotNotificationType) |
|
| 358 | - ->setIrcNotificationsInstance($whichami) |
|
| 359 | - ->setTitleBlacklistEnabled($enableTitleblacklist == 1) |
|
| 360 | - ->setTorExitPaths(array_merge(gethostbynamel('en.wikipedia.org'), gethostbynamel('accounts.wmflabs.org'))) |
|
| 361 | - ->setCreationBotUsername($creationBotUsername) |
|
| 362 | - ->setCreationBotPassword($creationBotPassword) |
|
| 363 | - ->setCurlCookieJar($curlCookieJar) |
|
| 364 | - ->setYubicoApiId($yubicoApiId) |
|
| 365 | - ->setYubicoApiKey($yubicoApiKey) |
|
| 366 | - ->setTotpEncryptionKey($totpEncryptionKey); |
|
| 331 | + ->setFilePath(__DIR__) |
|
| 332 | + ->setDebuggingTraceEnabled($enableErrorTrace) |
|
| 333 | + ->setForceIdentification($forceIdentification) |
|
| 334 | + ->setIdentificationCacheExpiry($identificationCacheExpiry) |
|
| 335 | + ->setMediawikiScriptPath($mediawikiScriptPath) |
|
| 336 | + ->setMediawikiWebServiceEndpoint($mediawikiWebServiceEndpoint) |
|
| 337 | + ->setMetaWikimediaWebServiceEndpoint($metaWikimediaWebServiceEndpoint) |
|
| 338 | + ->setEnforceOAuth($enforceOAuth) |
|
| 339 | + ->setEmailConfirmationEnabled($enableEmailConfirm == 1) |
|
| 340 | + ->setEmailConfirmationExpiryDays($emailConfirmationExpiryDays) |
|
| 341 | + ->setMiserModeLimit($requestLimitShowOnly) |
|
| 342 | + ->setRequestStates($availableRequestStates) |
|
| 343 | + ->setSquidList($squidIpList) |
|
| 344 | + ->setDefaultCreatedTemplateId($createdid) |
|
| 345 | + ->setDefaultRequestStateKey($defaultRequestStateKey) |
|
| 346 | + ->setUseStrictTransportSecurity($strictTransportSecurityExpiry) |
|
| 347 | + ->setUserAgent($toolUserAgent) |
|
| 348 | + ->setCurlDisableVerifyPeer($curlDisableSSLVerifyPeer) |
|
| 349 | + ->setUseOAuthSignup($useOauthSignup) |
|
| 350 | + ->setOAuthBaseUrl($oauthBaseUrl) |
|
| 351 | + ->setOAuthConsumerToken($oauthConsumerToken) |
|
| 352 | + ->setOAuthConsumerSecret($oauthSecretToken) |
|
| 353 | + ->setOauthMediaWikiCanonicalServer($oauthMediaWikiCanonicalServer) |
|
| 354 | + ->setDataClearInterval($dataclear_interval) |
|
| 355 | + ->setXffTrustedHostsFile($xff_trusted_hosts_file) |
|
| 356 | + ->setIrcNotificationsEnabled($ircBotNotificationsEnabled == 1) |
|
| 357 | + ->setIrcNotificationType($ircBotNotificationType) |
|
| 358 | + ->setIrcNotificationsInstance($whichami) |
|
| 359 | + ->setTitleBlacklistEnabled($enableTitleblacklist == 1) |
|
| 360 | + ->setTorExitPaths(array_merge(gethostbynamel('en.wikipedia.org'), gethostbynamel('accounts.wmflabs.org'))) |
|
| 361 | + ->setCreationBotUsername($creationBotUsername) |
|
| 362 | + ->setCreationBotPassword($creationBotPassword) |
|
| 363 | + ->setCurlCookieJar($curlCookieJar) |
|
| 364 | + ->setYubicoApiId($yubicoApiId) |
|
| 365 | + ->setYubicoApiKey($yubicoApiKey) |
|
| 366 | + ->setTotpEncryptionKey($totpEncryptionKey); |
|
@@ -15,187 +15,187 @@ discard block |
||
| 15 | 15 | |
| 16 | 16 | class Credential extends DataObject |
| 17 | 17 | { |
| 18 | - /** @var int */ |
|
| 19 | - private $user; |
|
| 20 | - /** @var int */ |
|
| 21 | - private $factor; |
|
| 22 | - /** @var string */ |
|
| 23 | - private $type; |
|
| 24 | - /** @var string */ |
|
| 25 | - private $data; |
|
| 26 | - /** @var int */ |
|
| 27 | - private $version; |
|
| 28 | - private $timeout; |
|
| 29 | - /** @var int */ |
|
| 30 | - private $disabled = 0; |
|
| 31 | - /** @var int */ |
|
| 32 | - private $priority; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * @return int |
|
| 36 | - */ |
|
| 37 | - public function getUserId() |
|
| 38 | - { |
|
| 39 | - return $this->user; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @param int $user |
|
| 44 | - */ |
|
| 45 | - public function setUserId($user) |
|
| 46 | - { |
|
| 47 | - $this->user = $user; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - /** |
|
| 51 | - * @return int |
|
| 52 | - */ |
|
| 53 | - public function getFactor() |
|
| 54 | - { |
|
| 55 | - return $this->factor; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * @param int $factor |
|
| 60 | - */ |
|
| 61 | - public function setFactor($factor) |
|
| 62 | - { |
|
| 63 | - $this->factor = $factor; |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * @return string |
|
| 68 | - */ |
|
| 69 | - public function getType() |
|
| 70 | - { |
|
| 71 | - return $this->type; |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @param string $type |
|
| 76 | - */ |
|
| 77 | - public function setType($type) |
|
| 78 | - { |
|
| 79 | - $this->type = $type; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @return string |
|
| 84 | - */ |
|
| 85 | - public function getData() |
|
| 86 | - { |
|
| 87 | - return $this->data; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * @param string $data |
|
| 92 | - */ |
|
| 93 | - public function setData($data) |
|
| 94 | - { |
|
| 95 | - $this->data = $data; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @return int |
|
| 100 | - */ |
|
| 101 | - public function getVersion() |
|
| 102 | - { |
|
| 103 | - return $this->version; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * @param int $version |
|
| 108 | - */ |
|
| 109 | - public function setVersion($version) |
|
| 110 | - { |
|
| 111 | - $this->version = $version; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * @return mixed |
|
| 116 | - */ |
|
| 117 | - public function getTimeout() |
|
| 118 | - { |
|
| 119 | - if ($this->timeout === null) { |
|
| 120 | - return null; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - return new DateTimeImmutable($this->timeout); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * @param mixed $timeout |
|
| 128 | - */ |
|
| 129 | - public function setTimeout(DateTimeImmutable $timeout = null) |
|
| 130 | - { |
|
| 131 | - if ($timeout === null) { |
|
| 132 | - $this->timeout = null; |
|
| 133 | - } |
|
| 134 | - else { |
|
| 135 | - $this->timeout = $timeout->format('Y-m-d H:i:s'); |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * @return int |
|
| 141 | - */ |
|
| 142 | - public function getDisabled() |
|
| 143 | - { |
|
| 144 | - return $this->disabled; |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * @param int $disabled |
|
| 149 | - */ |
|
| 150 | - public function setDisabled($disabled) |
|
| 151 | - { |
|
| 152 | - $this->disabled = $disabled; |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * @return int |
|
| 157 | - */ |
|
| 158 | - public function getPriority() |
|
| 159 | - { |
|
| 160 | - return $this->priority; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * @param int $priority |
|
| 165 | - */ |
|
| 166 | - public function setPriority($priority) |
|
| 167 | - { |
|
| 168 | - $this->priority = $priority; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - public function save() |
|
| 172 | - { |
|
| 173 | - if ($this->isNew()) { |
|
| 174 | - // insert |
|
| 175 | - $statement = $this->dbObject->prepare(<<<SQL |
|
| 18 | + /** @var int */ |
|
| 19 | + private $user; |
|
| 20 | + /** @var int */ |
|
| 21 | + private $factor; |
|
| 22 | + /** @var string */ |
|
| 23 | + private $type; |
|
| 24 | + /** @var string */ |
|
| 25 | + private $data; |
|
| 26 | + /** @var int */ |
|
| 27 | + private $version; |
|
| 28 | + private $timeout; |
|
| 29 | + /** @var int */ |
|
| 30 | + private $disabled = 0; |
|
| 31 | + /** @var int */ |
|
| 32 | + private $priority; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * @return int |
|
| 36 | + */ |
|
| 37 | + public function getUserId() |
|
| 38 | + { |
|
| 39 | + return $this->user; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @param int $user |
|
| 44 | + */ |
|
| 45 | + public function setUserId($user) |
|
| 46 | + { |
|
| 47 | + $this->user = $user; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + /** |
|
| 51 | + * @return int |
|
| 52 | + */ |
|
| 53 | + public function getFactor() |
|
| 54 | + { |
|
| 55 | + return $this->factor; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * @param int $factor |
|
| 60 | + */ |
|
| 61 | + public function setFactor($factor) |
|
| 62 | + { |
|
| 63 | + $this->factor = $factor; |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * @return string |
|
| 68 | + */ |
|
| 69 | + public function getType() |
|
| 70 | + { |
|
| 71 | + return $this->type; |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @param string $type |
|
| 76 | + */ |
|
| 77 | + public function setType($type) |
|
| 78 | + { |
|
| 79 | + $this->type = $type; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @return string |
|
| 84 | + */ |
|
| 85 | + public function getData() |
|
| 86 | + { |
|
| 87 | + return $this->data; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * @param string $data |
|
| 92 | + */ |
|
| 93 | + public function setData($data) |
|
| 94 | + { |
|
| 95 | + $this->data = $data; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @return int |
|
| 100 | + */ |
|
| 101 | + public function getVersion() |
|
| 102 | + { |
|
| 103 | + return $this->version; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * @param int $version |
|
| 108 | + */ |
|
| 109 | + public function setVersion($version) |
|
| 110 | + { |
|
| 111 | + $this->version = $version; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * @return mixed |
|
| 116 | + */ |
|
| 117 | + public function getTimeout() |
|
| 118 | + { |
|
| 119 | + if ($this->timeout === null) { |
|
| 120 | + return null; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + return new DateTimeImmutable($this->timeout); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * @param mixed $timeout |
|
| 128 | + */ |
|
| 129 | + public function setTimeout(DateTimeImmutable $timeout = null) |
|
| 130 | + { |
|
| 131 | + if ($timeout === null) { |
|
| 132 | + $this->timeout = null; |
|
| 133 | + } |
|
| 134 | + else { |
|
| 135 | + $this->timeout = $timeout->format('Y-m-d H:i:s'); |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * @return int |
|
| 141 | + */ |
|
| 142 | + public function getDisabled() |
|
| 143 | + { |
|
| 144 | + return $this->disabled; |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * @param int $disabled |
|
| 149 | + */ |
|
| 150 | + public function setDisabled($disabled) |
|
| 151 | + { |
|
| 152 | + $this->disabled = $disabled; |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * @return int |
|
| 157 | + */ |
|
| 158 | + public function getPriority() |
|
| 159 | + { |
|
| 160 | + return $this->priority; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * @param int $priority |
|
| 165 | + */ |
|
| 166 | + public function setPriority($priority) |
|
| 167 | + { |
|
| 168 | + $this->priority = $priority; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + public function save() |
|
| 172 | + { |
|
| 173 | + if ($this->isNew()) { |
|
| 174 | + // insert |
|
| 175 | + $statement = $this->dbObject->prepare(<<<SQL |
|
| 176 | 176 | INSERT INTO credential ( updateversion, user, factor, type, data, version, timeout, disabled, priority ) |
| 177 | 177 | VALUES ( 0, :user, :factor, :type, :data, :version, :timeout, :disabled, :priority ); |
| 178 | 178 | SQL |
| 179 | - ); |
|
| 180 | - $statement->bindValue(":user", $this->user); |
|
| 181 | - $statement->bindValue(":factor", $this->factor); |
|
| 182 | - $statement->bindValue(":type", $this->type); |
|
| 183 | - $statement->bindValue(":data", $this->data); |
|
| 184 | - $statement->bindValue(":version", $this->version); |
|
| 185 | - $statement->bindValue(":timeout", $this->timeout); |
|
| 186 | - $statement->bindValue(":disabled", $this->disabled); |
|
| 187 | - $statement->bindValue(":priority", $this->priority); |
|
| 188 | - |
|
| 189 | - if ($statement->execute()) { |
|
| 190 | - $this->id = (int)$this->dbObject->lastInsertId(); |
|
| 191 | - } |
|
| 192 | - else { |
|
| 193 | - throw new Exception($statement->errorInfo()); |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - else { |
|
| 197 | - // update |
|
| 198 | - $statement = $this->dbObject->prepare(<<<SQL |
|
| 179 | + ); |
|
| 180 | + $statement->bindValue(":user", $this->user); |
|
| 181 | + $statement->bindValue(":factor", $this->factor); |
|
| 182 | + $statement->bindValue(":type", $this->type); |
|
| 183 | + $statement->bindValue(":data", $this->data); |
|
| 184 | + $statement->bindValue(":version", $this->version); |
|
| 185 | + $statement->bindValue(":timeout", $this->timeout); |
|
| 186 | + $statement->bindValue(":disabled", $this->disabled); |
|
| 187 | + $statement->bindValue(":priority", $this->priority); |
|
| 188 | + |
|
| 189 | + if ($statement->execute()) { |
|
| 190 | + $this->id = (int)$this->dbObject->lastInsertId(); |
|
| 191 | + } |
|
| 192 | + else { |
|
| 193 | + throw new Exception($statement->errorInfo()); |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + else { |
|
| 197 | + // update |
|
| 198 | + $statement = $this->dbObject->prepare(<<<SQL |
|
| 199 | 199 | UPDATE credential |
| 200 | 200 | SET factor = :factor |
| 201 | 201 | , data = :data |
@@ -206,27 +206,27 @@ discard block |
||
| 206 | 206 | , updateversion = updateversion + 1 |
| 207 | 207 | WHERE id = :id AND updateversion = :updateversion; |
| 208 | 208 | SQL |
| 209 | - ); |
|
| 209 | + ); |
|
| 210 | 210 | |
| 211 | - $statement->bindValue(':id', $this->id); |
|
| 212 | - $statement->bindValue(':updateversion', $this->updateversion); |
|
| 211 | + $statement->bindValue(':id', $this->id); |
|
| 212 | + $statement->bindValue(':updateversion', $this->updateversion); |
|
| 213 | 213 | |
| 214 | - $statement->bindValue(":factor", $this->factor); |
|
| 215 | - $statement->bindValue(":data", $this->data); |
|
| 216 | - $statement->bindValue(":version", $this->version); |
|
| 217 | - $statement->bindValue(":timeout", $this->timeout); |
|
| 218 | - $statement->bindValue(":disabled", $this->disabled); |
|
| 219 | - $statement->bindValue(":priority", $this->priority); |
|
| 214 | + $statement->bindValue(":factor", $this->factor); |
|
| 215 | + $statement->bindValue(":data", $this->data); |
|
| 216 | + $statement->bindValue(":version", $this->version); |
|
| 217 | + $statement->bindValue(":timeout", $this->timeout); |
|
| 218 | + $statement->bindValue(":disabled", $this->disabled); |
|
| 219 | + $statement->bindValue(":priority", $this->priority); |
|
| 220 | 220 | |
| 221 | - if (!$statement->execute()) { |
|
| 222 | - throw new Exception($statement->errorInfo()); |
|
| 223 | - } |
|
| 221 | + if (!$statement->execute()) { |
|
| 222 | + throw new Exception($statement->errorInfo()); |
|
| 223 | + } |
|
| 224 | 224 | |
| 225 | - if ($statement->rowCount() !== 1) { |
|
| 226 | - throw new OptimisticLockFailedException(); |
|
| 227 | - } |
|
| 225 | + if ($statement->rowCount() !== 1) { |
|
| 226 | + throw new OptimisticLockFailedException(); |
|
| 227 | + } |
|
| 228 | 228 | |
| 229 | - $this->updateversion++; |
|
| 230 | - } |
|
| 231 | - } |
|
| 229 | + $this->updateversion++; |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | 232 | } |
| 233 | 233 | \ No newline at end of file |
@@ -22,560 +22,560 @@ |
||
| 22 | 22 | */ |
| 23 | 23 | class WebRequest |
| 24 | 24 | { |
| 25 | - /** |
|
| 26 | - * @var \Waca\Providers\GlobalState\IGlobalStateProvider Provides access to the global state. |
|
| 27 | - */ |
|
| 28 | - private static $globalStateProvider; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Returns a boolean value if the request was submitted with the HTTP POST method. |
|
| 32 | - * @return bool |
|
| 33 | - */ |
|
| 34 | - public static function wasPosted() |
|
| 35 | - { |
|
| 36 | - return self::method() === 'POST'; |
|
| 37 | - } |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Gets the HTTP Method used |
|
| 41 | - * @return string|null |
|
| 42 | - */ |
|
| 43 | - public static function method() |
|
| 44 | - { |
|
| 45 | - $server = &self::$globalStateProvider->getServerSuperGlobal(); |
|
| 46 | - |
|
| 47 | - if (isset($server['REQUEST_METHOD'])) { |
|
| 48 | - return $server['REQUEST_METHOD']; |
|
| 49 | - } |
|
| 50 | - |
|
| 51 | - return null; |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * Gets a boolean value stating whether the request was served over HTTPS or not. |
|
| 56 | - * @return bool |
|
| 57 | - */ |
|
| 58 | - public static function isHttps() |
|
| 59 | - { |
|
| 60 | - $server = &self::$globalStateProvider->getServerSuperGlobal(); |
|
| 61 | - |
|
| 62 | - if (isset($server['HTTP_X_FORWARDED_PROTO'])) { |
|
| 63 | - if ($server['HTTP_X_FORWARDED_PROTO'] === 'https') { |
|
| 64 | - // Client <=> Proxy is encrypted |
|
| 65 | - return true; |
|
| 66 | - } |
|
| 67 | - else { |
|
| 68 | - // Proxy <=> Server link unknown, Client <=> Proxy is not encrypted. |
|
| 69 | - return false; |
|
| 70 | - } |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - if (isset($server['HTTPS'])) { |
|
| 74 | - if ($server['HTTPS'] === 'off') { |
|
| 75 | - // ISAPI on IIS breaks the spec. :( |
|
| 76 | - return false; |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - if ($server['HTTPS'] !== '') { |
|
| 80 | - // Set to a non-empty value |
|
| 81 | - return true; |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - return false; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Gets the path info |
|
| 90 | - * |
|
| 91 | - * @return array Array of path info segments |
|
| 92 | - */ |
|
| 93 | - public static function pathInfo() |
|
| 94 | - { |
|
| 95 | - $server = &self::$globalStateProvider->getServerSuperGlobal(); |
|
| 96 | - if (!isset($server['PATH_INFO'])) { |
|
| 97 | - return array(); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - $exploded = explode('/', $server['PATH_INFO']); |
|
| 101 | - |
|
| 102 | - // filter out empty values, and reindex from zero. Notably, the first element is always zero, since it starts |
|
| 103 | - // with a / |
|
| 104 | - return array_values(array_filter($exploded)); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Gets the remote address of the web request |
|
| 109 | - * @return null|string |
|
| 110 | - */ |
|
| 111 | - public static function remoteAddress() |
|
| 112 | - { |
|
| 113 | - $server = &self::$globalStateProvider->getServerSuperGlobal(); |
|
| 114 | - |
|
| 115 | - if (isset($server['REMOTE_ADDR'])) { |
|
| 116 | - return $server['REMOTE_ADDR']; |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - return null; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Gets the remote address of the web request |
|
| 124 | - * @return null|string |
|
| 125 | - */ |
|
| 126 | - public static function httpHost() |
|
| 127 | - { |
|
| 128 | - $server = &self::$globalStateProvider->getServerSuperGlobal(); |
|
| 129 | - |
|
| 130 | - if (isset($server['HTTP_HOST'])) { |
|
| 131 | - return $server['HTTP_HOST']; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - return null; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Gets the XFF header contents for the web request |
|
| 139 | - * @return null|string |
|
| 140 | - */ |
|
| 141 | - public static function forwardedAddress() |
|
| 142 | - { |
|
| 143 | - $server = &self::$globalStateProvider->getServerSuperGlobal(); |
|
| 144 | - |
|
| 145 | - if (isset($server['HTTP_X_FORWARDED_FOR'])) { |
|
| 146 | - return $server['HTTP_X_FORWARDED_FOR']; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - return null; |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Sets the global state provider. |
|
| 154 | - * |
|
| 155 | - * Almost guaranteed this is not the method you want in production code. |
|
| 156 | - * |
|
| 157 | - * @param \Waca\Providers\GlobalState\IGlobalStateProvider $globalState |
|
| 158 | - */ |
|
| 159 | - public static function setGlobalStateProvider($globalState) |
|
| 160 | - { |
|
| 161 | - self::$globalStateProvider = $globalState; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - #region POST variables |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * @param string $key |
|
| 168 | - * |
|
| 169 | - * @return null|string |
|
| 170 | - */ |
|
| 171 | - public static function postString($key) |
|
| 172 | - { |
|
| 173 | - $post = &self::$globalStateProvider->getPostSuperGlobal(); |
|
| 174 | - if (!array_key_exists($key, $post)) { |
|
| 175 | - return null; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - if ($post[$key] === "") { |
|
| 179 | - return null; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - return (string)$post[$key]; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * @param string $key |
|
| 187 | - * |
|
| 188 | - * @return null|string |
|
| 189 | - */ |
|
| 190 | - public static function postEmail($key) |
|
| 191 | - { |
|
| 192 | - $post = &self::$globalStateProvider->getPostSuperGlobal(); |
|
| 193 | - if (!array_key_exists($key, $post)) { |
|
| 194 | - return null; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - $filteredValue = filter_var($post[$key], FILTER_SANITIZE_EMAIL); |
|
| 198 | - |
|
| 199 | - if ($filteredValue === false) { |
|
| 200 | - return null; |
|
| 201 | - } |
|
| 202 | - |
|
| 203 | - return (string)$filteredValue; |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * @param string $key |
|
| 208 | - * |
|
| 209 | - * @return int|null |
|
| 210 | - */ |
|
| 211 | - public static function postInt($key) |
|
| 212 | - { |
|
| 213 | - $post = &self::$globalStateProvider->getPostSuperGlobal(); |
|
| 214 | - if (!array_key_exists($key, $post)) { |
|
| 215 | - return null; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - $filteredValue = filter_var($post[$key], FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE); |
|
| 219 | - |
|
| 220 | - if ($filteredValue === null) { |
|
| 221 | - return null; |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - return (int)$filteredValue; |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * @param string $key |
|
| 229 | - * |
|
| 230 | - * @return bool |
|
| 231 | - */ |
|
| 232 | - public static function postBoolean($key) |
|
| 233 | - { |
|
| 234 | - $get = &self::$globalStateProvider->getPostSuperGlobal(); |
|
| 235 | - if (!array_key_exists($key, $get)) { |
|
| 236 | - return false; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - // presence of parameter only |
|
| 240 | - if ($get[$key] === "") { |
|
| 241 | - return true; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - if (in_array($get[$key], array(false, 'no', 'off', 0, 'false'), true)) { |
|
| 245 | - return false; |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - return true; |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - #endregion |
|
| 252 | - |
|
| 253 | - #region GET variables |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * @param string $key |
|
| 257 | - * |
|
| 258 | - * @return bool |
|
| 259 | - */ |
|
| 260 | - public static function getBoolean($key) |
|
| 261 | - { |
|
| 262 | - $get = &self::$globalStateProvider->getGetSuperGlobal(); |
|
| 263 | - if (!array_key_exists($key, $get)) { |
|
| 264 | - return false; |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - // presence of parameter only |
|
| 268 | - if ($get[$key] === "") { |
|
| 269 | - return true; |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - if (in_array($get[$key], array(false, 'no', 'off', 0, 'false'), true)) { |
|
| 273 | - return false; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - return true; |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - /** |
|
| 280 | - * @param string $key |
|
| 281 | - * |
|
| 282 | - * @return int|null |
|
| 283 | - */ |
|
| 284 | - public static function getInt($key) |
|
| 285 | - { |
|
| 286 | - $get = &self::$globalStateProvider->getGetSuperGlobal(); |
|
| 287 | - if (!array_key_exists($key, $get)) { |
|
| 288 | - return null; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - $filteredValue = filter_var($get[$key], FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE); |
|
| 292 | - |
|
| 293 | - if ($filteredValue === null) { |
|
| 294 | - return null; |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - return (int)$filteredValue; |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - /** |
|
| 301 | - * @param string $key |
|
| 302 | - * |
|
| 303 | - * @return null|string |
|
| 304 | - */ |
|
| 305 | - public static function getString($key) |
|
| 306 | - { |
|
| 307 | - $get = &self::$globalStateProvider->getGetSuperGlobal(); |
|
| 308 | - if (!array_key_exists($key, $get)) { |
|
| 309 | - return null; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - if ($get[$key] === "") { |
|
| 313 | - return null; |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - return (string)$get[$key]; |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - #endregion |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * Sets the logged-in user to the specified user. |
|
| 323 | - * |
|
| 324 | - * @param User $user |
|
| 325 | - */ |
|
| 326 | - public static function setLoggedInUser(User $user) |
|
| 327 | - { |
|
| 328 | - $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 329 | - |
|
| 330 | - $session['userID'] = $user->getId(); |
|
| 331 | - unset($session['partialLogin']); |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - /** |
|
| 335 | - * Sets the post-login redirect |
|
| 336 | - */ |
|
| 337 | - public static function setPostLoginRedirect() |
|
| 338 | - { |
|
| 339 | - $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 340 | - $session['returnTo'] = self::requestUri(); |
|
| 341 | - } |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * @return string|null |
|
| 345 | - */ |
|
| 346 | - public static function requestUri() |
|
| 347 | - { |
|
| 348 | - $server = &self::$globalStateProvider->getServerSuperGlobal(); |
|
| 349 | - |
|
| 350 | - if (isset($server['REQUEST_URI'])) { |
|
| 351 | - return $server['REQUEST_URI']; |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - return null; |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * Clears the post-login redirect |
|
| 359 | - * @return string |
|
| 360 | - */ |
|
| 361 | - public static function clearPostLoginRedirect() |
|
| 362 | - { |
|
| 363 | - $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 364 | - if (array_key_exists('returnTo', $session)) { |
|
| 365 | - $path = $session['returnTo']; |
|
| 366 | - unset($session['returnTo']); |
|
| 367 | - |
|
| 368 | - return $path; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - return null; |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - /** |
|
| 375 | - * @return string|null |
|
| 376 | - */ |
|
| 377 | - public static function serverName() |
|
| 378 | - { |
|
| 379 | - $server = &self::$globalStateProvider->getServerSuperGlobal(); |
|
| 380 | - |
|
| 381 | - if (isset($server['SERVER_NAME'])) { |
|
| 382 | - return $server['SERVER_NAME']; |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - return null; |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - /** |
|
| 389 | - * You probably only want to deal with this through SessionAlert. |
|
| 390 | - * @return void |
|
| 391 | - */ |
|
| 392 | - public static function clearSessionAlertData() |
|
| 393 | - { |
|
| 394 | - $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 395 | - if (array_key_exists('alerts', $session)) { |
|
| 396 | - unset($session['alerts']); |
|
| 397 | - } |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * You probably only want to deal with this through SessionAlert. |
|
| 402 | - * |
|
| 403 | - * @return string[] |
|
| 404 | - */ |
|
| 405 | - public static function getSessionAlertData() |
|
| 406 | - { |
|
| 407 | - $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 408 | - if (array_key_exists('alerts', $session)) { |
|
| 409 | - return $session['alerts']; |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - return array(); |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - /** |
|
| 416 | - * You probably only want to deal with this through SessionAlert. |
|
| 417 | - * |
|
| 418 | - * @param string[] $data |
|
| 419 | - */ |
|
| 420 | - public static function setSessionAlertData($data) |
|
| 421 | - { |
|
| 422 | - $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 423 | - $session['alerts'] = $data; |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * You probably only want to deal with this through TokenManager. |
|
| 428 | - * |
|
| 429 | - * @return string[] |
|
| 430 | - */ |
|
| 431 | - public static function getSessionTokenData() |
|
| 432 | - { |
|
| 433 | - $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 434 | - if (array_key_exists('tokens', $session)) { |
|
| 435 | - return $session['tokens']; |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - return array(); |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - /** |
|
| 442 | - * You probably only want to deal with this through TokenManager. |
|
| 443 | - * |
|
| 444 | - * @param string[] $data |
|
| 445 | - */ |
|
| 446 | - public static function setSessionTokenData($data) |
|
| 447 | - { |
|
| 448 | - $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 449 | - $session['tokens'] = $data; |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * @param string $key |
|
| 454 | - * |
|
| 455 | - * @return mixed |
|
| 456 | - */ |
|
| 457 | - public static function getSessionContext($key) |
|
| 458 | - { |
|
| 459 | - $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 460 | - |
|
| 461 | - if (!isset($session['context'])) { |
|
| 462 | - $session['context'] = array(); |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - if (!isset($session['context'][$key])) { |
|
| 466 | - return null; |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - return $session['context'][$key]; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * @param string $key |
|
| 474 | - * @param mixed $data |
|
| 475 | - */ |
|
| 476 | - public static function setSessionContext($key, $data) |
|
| 477 | - { |
|
| 478 | - $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 479 | - |
|
| 480 | - if (!isset($session['context'])) { |
|
| 481 | - $session['context'] = array(); |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - $session['context'][$key] = $data; |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - /** |
|
| 488 | - * @return int|null |
|
| 489 | - */ |
|
| 490 | - public static function getSessionUserId() |
|
| 491 | - { |
|
| 492 | - $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 493 | - |
|
| 494 | - return isset($session['userID']) ? (int)$session['userID'] : null; |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - /** |
|
| 498 | - * @param User $user |
|
| 499 | - */ |
|
| 500 | - public static function setOAuthPartialLogin(User $user) |
|
| 501 | - { |
|
| 502 | - $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 503 | - $session['oauthPartialLogin'] = $user->getId(); |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - /** |
|
| 507 | - * @return int|null |
|
| 508 | - */ |
|
| 509 | - public static function getOAuthPartialLogin() |
|
| 510 | - { |
|
| 511 | - $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 512 | - |
|
| 513 | - return isset($session['oauthPartialLogin']) ? (int)$session['oauthPartialLogin'] : null; |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - public static function setAuthPartialLogin($userId, $stage) |
|
| 517 | - { |
|
| 518 | - $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 519 | - $session['authPartialLoginId'] = $userId; |
|
| 520 | - $session['authPartialLoginStage'] = $stage; |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - public static function getAuthPartialLogin() |
|
| 524 | - { |
|
| 525 | - $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 526 | - |
|
| 527 | - $userId = isset($session['authPartialLoginId']) ? (int)$session['authPartialLoginId'] : null; |
|
| 528 | - $stage = isset($session['authPartialLoginStage']) ? (int)$session['authPartialLoginStage'] : null; |
|
| 529 | - |
|
| 530 | - return array($userId, $stage); |
|
| 531 | - } |
|
| 532 | - |
|
| 533 | - public static function clearAuthPartialLogin() |
|
| 534 | - { |
|
| 535 | - $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 536 | - unset($session['authPartialLoginId']); |
|
| 537 | - unset($session['authPartialLoginStage']); |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - /** |
|
| 541 | - * @return null|string |
|
| 542 | - */ |
|
| 543 | - public static function userAgent() |
|
| 544 | - { |
|
| 545 | - $server = &self::$globalStateProvider->getServerSuperGlobal(); |
|
| 546 | - |
|
| 547 | - if (isset($server['HTTP_USER_AGENT'])) { |
|
| 548 | - return $server['HTTP_USER_AGENT']; |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - return null; |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - /** |
|
| 555 | - * @return null|string |
|
| 556 | - */ |
|
| 557 | - public static function scriptName() |
|
| 558 | - { |
|
| 559 | - $server = &self::$globalStateProvider->getServerSuperGlobal(); |
|
| 560 | - |
|
| 561 | - if (isset($server['SCRIPT_NAME'])) { |
|
| 562 | - return $server['SCRIPT_NAME']; |
|
| 563 | - } |
|
| 564 | - |
|
| 565 | - return null; |
|
| 566 | - } |
|
| 567 | - |
|
| 568 | - /** |
|
| 569 | - * @return null|string |
|
| 570 | - */ |
|
| 571 | - public static function origin() |
|
| 572 | - { |
|
| 573 | - $server = &self::$globalStateProvider->getServerSuperGlobal(); |
|
| 574 | - |
|
| 575 | - if (isset($server['HTTP_ORIGIN'])) { |
|
| 576 | - return $server['HTTP_ORIGIN']; |
|
| 577 | - } |
|
| 578 | - |
|
| 579 | - return null; |
|
| 580 | - } |
|
| 25 | + /** |
|
| 26 | + * @var \Waca\Providers\GlobalState\IGlobalStateProvider Provides access to the global state. |
|
| 27 | + */ |
|
| 28 | + private static $globalStateProvider; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Returns a boolean value if the request was submitted with the HTTP POST method. |
|
| 32 | + * @return bool |
|
| 33 | + */ |
|
| 34 | + public static function wasPosted() |
|
| 35 | + { |
|
| 36 | + return self::method() === 'POST'; |
|
| 37 | + } |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Gets the HTTP Method used |
|
| 41 | + * @return string|null |
|
| 42 | + */ |
|
| 43 | + public static function method() |
|
| 44 | + { |
|
| 45 | + $server = &self::$globalStateProvider->getServerSuperGlobal(); |
|
| 46 | + |
|
| 47 | + if (isset($server['REQUEST_METHOD'])) { |
|
| 48 | + return $server['REQUEST_METHOD']; |
|
| 49 | + } |
|
| 50 | + |
|
| 51 | + return null; |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * Gets a boolean value stating whether the request was served over HTTPS or not. |
|
| 56 | + * @return bool |
|
| 57 | + */ |
|
| 58 | + public static function isHttps() |
|
| 59 | + { |
|
| 60 | + $server = &self::$globalStateProvider->getServerSuperGlobal(); |
|
| 61 | + |
|
| 62 | + if (isset($server['HTTP_X_FORWARDED_PROTO'])) { |
|
| 63 | + if ($server['HTTP_X_FORWARDED_PROTO'] === 'https') { |
|
| 64 | + // Client <=> Proxy is encrypted |
|
| 65 | + return true; |
|
| 66 | + } |
|
| 67 | + else { |
|
| 68 | + // Proxy <=> Server link unknown, Client <=> Proxy is not encrypted. |
|
| 69 | + return false; |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + if (isset($server['HTTPS'])) { |
|
| 74 | + if ($server['HTTPS'] === 'off') { |
|
| 75 | + // ISAPI on IIS breaks the spec. :( |
|
| 76 | + return false; |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + if ($server['HTTPS'] !== '') { |
|
| 80 | + // Set to a non-empty value |
|
| 81 | + return true; |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + return false; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Gets the path info |
|
| 90 | + * |
|
| 91 | + * @return array Array of path info segments |
|
| 92 | + */ |
|
| 93 | + public static function pathInfo() |
|
| 94 | + { |
|
| 95 | + $server = &self::$globalStateProvider->getServerSuperGlobal(); |
|
| 96 | + if (!isset($server['PATH_INFO'])) { |
|
| 97 | + return array(); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + $exploded = explode('/', $server['PATH_INFO']); |
|
| 101 | + |
|
| 102 | + // filter out empty values, and reindex from zero. Notably, the first element is always zero, since it starts |
|
| 103 | + // with a / |
|
| 104 | + return array_values(array_filter($exploded)); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Gets the remote address of the web request |
|
| 109 | + * @return null|string |
|
| 110 | + */ |
|
| 111 | + public static function remoteAddress() |
|
| 112 | + { |
|
| 113 | + $server = &self::$globalStateProvider->getServerSuperGlobal(); |
|
| 114 | + |
|
| 115 | + if (isset($server['REMOTE_ADDR'])) { |
|
| 116 | + return $server['REMOTE_ADDR']; |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + return null; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Gets the remote address of the web request |
|
| 124 | + * @return null|string |
|
| 125 | + */ |
|
| 126 | + public static function httpHost() |
|
| 127 | + { |
|
| 128 | + $server = &self::$globalStateProvider->getServerSuperGlobal(); |
|
| 129 | + |
|
| 130 | + if (isset($server['HTTP_HOST'])) { |
|
| 131 | + return $server['HTTP_HOST']; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + return null; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Gets the XFF header contents for the web request |
|
| 139 | + * @return null|string |
|
| 140 | + */ |
|
| 141 | + public static function forwardedAddress() |
|
| 142 | + { |
|
| 143 | + $server = &self::$globalStateProvider->getServerSuperGlobal(); |
|
| 144 | + |
|
| 145 | + if (isset($server['HTTP_X_FORWARDED_FOR'])) { |
|
| 146 | + return $server['HTTP_X_FORWARDED_FOR']; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + return null; |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Sets the global state provider. |
|
| 154 | + * |
|
| 155 | + * Almost guaranteed this is not the method you want in production code. |
|
| 156 | + * |
|
| 157 | + * @param \Waca\Providers\GlobalState\IGlobalStateProvider $globalState |
|
| 158 | + */ |
|
| 159 | + public static function setGlobalStateProvider($globalState) |
|
| 160 | + { |
|
| 161 | + self::$globalStateProvider = $globalState; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + #region POST variables |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * @param string $key |
|
| 168 | + * |
|
| 169 | + * @return null|string |
|
| 170 | + */ |
|
| 171 | + public static function postString($key) |
|
| 172 | + { |
|
| 173 | + $post = &self::$globalStateProvider->getPostSuperGlobal(); |
|
| 174 | + if (!array_key_exists($key, $post)) { |
|
| 175 | + return null; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + if ($post[$key] === "") { |
|
| 179 | + return null; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + return (string)$post[$key]; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * @param string $key |
|
| 187 | + * |
|
| 188 | + * @return null|string |
|
| 189 | + */ |
|
| 190 | + public static function postEmail($key) |
|
| 191 | + { |
|
| 192 | + $post = &self::$globalStateProvider->getPostSuperGlobal(); |
|
| 193 | + if (!array_key_exists($key, $post)) { |
|
| 194 | + return null; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + $filteredValue = filter_var($post[$key], FILTER_SANITIZE_EMAIL); |
|
| 198 | + |
|
| 199 | + if ($filteredValue === false) { |
|
| 200 | + return null; |
|
| 201 | + } |
|
| 202 | + |
|
| 203 | + return (string)$filteredValue; |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * @param string $key |
|
| 208 | + * |
|
| 209 | + * @return int|null |
|
| 210 | + */ |
|
| 211 | + public static function postInt($key) |
|
| 212 | + { |
|
| 213 | + $post = &self::$globalStateProvider->getPostSuperGlobal(); |
|
| 214 | + if (!array_key_exists($key, $post)) { |
|
| 215 | + return null; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + $filteredValue = filter_var($post[$key], FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE); |
|
| 219 | + |
|
| 220 | + if ($filteredValue === null) { |
|
| 221 | + return null; |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + return (int)$filteredValue; |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * @param string $key |
|
| 229 | + * |
|
| 230 | + * @return bool |
|
| 231 | + */ |
|
| 232 | + public static function postBoolean($key) |
|
| 233 | + { |
|
| 234 | + $get = &self::$globalStateProvider->getPostSuperGlobal(); |
|
| 235 | + if (!array_key_exists($key, $get)) { |
|
| 236 | + return false; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + // presence of parameter only |
|
| 240 | + if ($get[$key] === "") { |
|
| 241 | + return true; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + if (in_array($get[$key], array(false, 'no', 'off', 0, 'false'), true)) { |
|
| 245 | + return false; |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + return true; |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + #endregion |
|
| 252 | + |
|
| 253 | + #region GET variables |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * @param string $key |
|
| 257 | + * |
|
| 258 | + * @return bool |
|
| 259 | + */ |
|
| 260 | + public static function getBoolean($key) |
|
| 261 | + { |
|
| 262 | + $get = &self::$globalStateProvider->getGetSuperGlobal(); |
|
| 263 | + if (!array_key_exists($key, $get)) { |
|
| 264 | + return false; |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + // presence of parameter only |
|
| 268 | + if ($get[$key] === "") { |
|
| 269 | + return true; |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + if (in_array($get[$key], array(false, 'no', 'off', 0, 'false'), true)) { |
|
| 273 | + return false; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + return true; |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + /** |
|
| 280 | + * @param string $key |
|
| 281 | + * |
|
| 282 | + * @return int|null |
|
| 283 | + */ |
|
| 284 | + public static function getInt($key) |
|
| 285 | + { |
|
| 286 | + $get = &self::$globalStateProvider->getGetSuperGlobal(); |
|
| 287 | + if (!array_key_exists($key, $get)) { |
|
| 288 | + return null; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + $filteredValue = filter_var($get[$key], FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE); |
|
| 292 | + |
|
| 293 | + if ($filteredValue === null) { |
|
| 294 | + return null; |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + return (int)$filteredValue; |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + /** |
|
| 301 | + * @param string $key |
|
| 302 | + * |
|
| 303 | + * @return null|string |
|
| 304 | + */ |
|
| 305 | + public static function getString($key) |
|
| 306 | + { |
|
| 307 | + $get = &self::$globalStateProvider->getGetSuperGlobal(); |
|
| 308 | + if (!array_key_exists($key, $get)) { |
|
| 309 | + return null; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + if ($get[$key] === "") { |
|
| 313 | + return null; |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + return (string)$get[$key]; |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + #endregion |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * Sets the logged-in user to the specified user. |
|
| 323 | + * |
|
| 324 | + * @param User $user |
|
| 325 | + */ |
|
| 326 | + public static function setLoggedInUser(User $user) |
|
| 327 | + { |
|
| 328 | + $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 329 | + |
|
| 330 | + $session['userID'] = $user->getId(); |
|
| 331 | + unset($session['partialLogin']); |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + /** |
|
| 335 | + * Sets the post-login redirect |
|
| 336 | + */ |
|
| 337 | + public static function setPostLoginRedirect() |
|
| 338 | + { |
|
| 339 | + $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 340 | + $session['returnTo'] = self::requestUri(); |
|
| 341 | + } |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * @return string|null |
|
| 345 | + */ |
|
| 346 | + public static function requestUri() |
|
| 347 | + { |
|
| 348 | + $server = &self::$globalStateProvider->getServerSuperGlobal(); |
|
| 349 | + |
|
| 350 | + if (isset($server['REQUEST_URI'])) { |
|
| 351 | + return $server['REQUEST_URI']; |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + return null; |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * Clears the post-login redirect |
|
| 359 | + * @return string |
|
| 360 | + */ |
|
| 361 | + public static function clearPostLoginRedirect() |
|
| 362 | + { |
|
| 363 | + $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 364 | + if (array_key_exists('returnTo', $session)) { |
|
| 365 | + $path = $session['returnTo']; |
|
| 366 | + unset($session['returnTo']); |
|
| 367 | + |
|
| 368 | + return $path; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + return null; |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + /** |
|
| 375 | + * @return string|null |
|
| 376 | + */ |
|
| 377 | + public static function serverName() |
|
| 378 | + { |
|
| 379 | + $server = &self::$globalStateProvider->getServerSuperGlobal(); |
|
| 380 | + |
|
| 381 | + if (isset($server['SERVER_NAME'])) { |
|
| 382 | + return $server['SERVER_NAME']; |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + return null; |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + /** |
|
| 389 | + * You probably only want to deal with this through SessionAlert. |
|
| 390 | + * @return void |
|
| 391 | + */ |
|
| 392 | + public static function clearSessionAlertData() |
|
| 393 | + { |
|
| 394 | + $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 395 | + if (array_key_exists('alerts', $session)) { |
|
| 396 | + unset($session['alerts']); |
|
| 397 | + } |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * You probably only want to deal with this through SessionAlert. |
|
| 402 | + * |
|
| 403 | + * @return string[] |
|
| 404 | + */ |
|
| 405 | + public static function getSessionAlertData() |
|
| 406 | + { |
|
| 407 | + $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 408 | + if (array_key_exists('alerts', $session)) { |
|
| 409 | + return $session['alerts']; |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + return array(); |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + /** |
|
| 416 | + * You probably only want to deal with this through SessionAlert. |
|
| 417 | + * |
|
| 418 | + * @param string[] $data |
|
| 419 | + */ |
|
| 420 | + public static function setSessionAlertData($data) |
|
| 421 | + { |
|
| 422 | + $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 423 | + $session['alerts'] = $data; |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * You probably only want to deal with this through TokenManager. |
|
| 428 | + * |
|
| 429 | + * @return string[] |
|
| 430 | + */ |
|
| 431 | + public static function getSessionTokenData() |
|
| 432 | + { |
|
| 433 | + $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 434 | + if (array_key_exists('tokens', $session)) { |
|
| 435 | + return $session['tokens']; |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + return array(); |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + /** |
|
| 442 | + * You probably only want to deal with this through TokenManager. |
|
| 443 | + * |
|
| 444 | + * @param string[] $data |
|
| 445 | + */ |
|
| 446 | + public static function setSessionTokenData($data) |
|
| 447 | + { |
|
| 448 | + $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 449 | + $session['tokens'] = $data; |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * @param string $key |
|
| 454 | + * |
|
| 455 | + * @return mixed |
|
| 456 | + */ |
|
| 457 | + public static function getSessionContext($key) |
|
| 458 | + { |
|
| 459 | + $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 460 | + |
|
| 461 | + if (!isset($session['context'])) { |
|
| 462 | + $session['context'] = array(); |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + if (!isset($session['context'][$key])) { |
|
| 466 | + return null; |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + return $session['context'][$key]; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * @param string $key |
|
| 474 | + * @param mixed $data |
|
| 475 | + */ |
|
| 476 | + public static function setSessionContext($key, $data) |
|
| 477 | + { |
|
| 478 | + $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 479 | + |
|
| 480 | + if (!isset($session['context'])) { |
|
| 481 | + $session['context'] = array(); |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + $session['context'][$key] = $data; |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + /** |
|
| 488 | + * @return int|null |
|
| 489 | + */ |
|
| 490 | + public static function getSessionUserId() |
|
| 491 | + { |
|
| 492 | + $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 493 | + |
|
| 494 | + return isset($session['userID']) ? (int)$session['userID'] : null; |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + /** |
|
| 498 | + * @param User $user |
|
| 499 | + */ |
|
| 500 | + public static function setOAuthPartialLogin(User $user) |
|
| 501 | + { |
|
| 502 | + $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 503 | + $session['oauthPartialLogin'] = $user->getId(); |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + /** |
|
| 507 | + * @return int|null |
|
| 508 | + */ |
|
| 509 | + public static function getOAuthPartialLogin() |
|
| 510 | + { |
|
| 511 | + $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 512 | + |
|
| 513 | + return isset($session['oauthPartialLogin']) ? (int)$session['oauthPartialLogin'] : null; |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + public static function setAuthPartialLogin($userId, $stage) |
|
| 517 | + { |
|
| 518 | + $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 519 | + $session['authPartialLoginId'] = $userId; |
|
| 520 | + $session['authPartialLoginStage'] = $stage; |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + public static function getAuthPartialLogin() |
|
| 524 | + { |
|
| 525 | + $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 526 | + |
|
| 527 | + $userId = isset($session['authPartialLoginId']) ? (int)$session['authPartialLoginId'] : null; |
|
| 528 | + $stage = isset($session['authPartialLoginStage']) ? (int)$session['authPartialLoginStage'] : null; |
|
| 529 | + |
|
| 530 | + return array($userId, $stage); |
|
| 531 | + } |
|
| 532 | + |
|
| 533 | + public static function clearAuthPartialLogin() |
|
| 534 | + { |
|
| 535 | + $session = &self::$globalStateProvider->getSessionSuperGlobal(); |
|
| 536 | + unset($session['authPartialLoginId']); |
|
| 537 | + unset($session['authPartialLoginStage']); |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + /** |
|
| 541 | + * @return null|string |
|
| 542 | + */ |
|
| 543 | + public static function userAgent() |
|
| 544 | + { |
|
| 545 | + $server = &self::$globalStateProvider->getServerSuperGlobal(); |
|
| 546 | + |
|
| 547 | + if (isset($server['HTTP_USER_AGENT'])) { |
|
| 548 | + return $server['HTTP_USER_AGENT']; |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + return null; |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + /** |
|
| 555 | + * @return null|string |
|
| 556 | + */ |
|
| 557 | + public static function scriptName() |
|
| 558 | + { |
|
| 559 | + $server = &self::$globalStateProvider->getServerSuperGlobal(); |
|
| 560 | + |
|
| 561 | + if (isset($server['SCRIPT_NAME'])) { |
|
| 562 | + return $server['SCRIPT_NAME']; |
|
| 563 | + } |
|
| 564 | + |
|
| 565 | + return null; |
|
| 566 | + } |
|
| 567 | + |
|
| 568 | + /** |
|
| 569 | + * @return null|string |
|
| 570 | + */ |
|
| 571 | + public static function origin() |
|
| 572 | + { |
|
| 573 | + $server = &self::$globalStateProvider->getServerSuperGlobal(); |
|
| 574 | + |
|
| 575 | + if (isset($server['HTTP_ORIGIN'])) { |
|
| 576 | + return $server['HTTP_ORIGIN']; |
|
| 577 | + } |
|
| 578 | + |
|
| 579 | + return null; |
|
| 580 | + } |
|
| 581 | 581 | } |
| 582 | 582 | \ No newline at end of file |
@@ -229,7 +229,8 @@ discard block |
||
| 229 | 229 | $this->deleteCredential($database, $currentUser, $otpCredentialProvider, $factorType); |
| 230 | 230 | } |
| 231 | 231 | |
| 232 | - protected function enableU2F() { |
|
| 232 | + protected function enableU2F() |
|
| 233 | + { |
|
| 233 | 234 | $database = $this->getDatabase(); |
| 234 | 235 | $currentUser = User::getCurrent($database); |
| 235 | 236 | |
@@ -336,7 +337,8 @@ discard block |
||
| 336 | 337 | } |
| 337 | 338 | } |
| 338 | 339 | |
| 339 | - protected function disableU2F() { |
|
| 340 | + protected function disableU2F() |
|
| 341 | + { |
|
| 340 | 342 | $database = $this->getDatabase(); |
| 341 | 343 | $currentUser = User::getCurrent($database); |
| 342 | 344 | |
@@ -26,239 +26,239 @@ discard block |
||
| 26 | 26 | |
| 27 | 27 | class PageMultiFactor extends InternalPageBase |
| 28 | 28 | { |
| 29 | - /** |
|
| 30 | - * Main function for this page, when no specific actions are called. |
|
| 31 | - * @return void |
|
| 32 | - */ |
|
| 33 | - protected function main() |
|
| 34 | - { |
|
| 35 | - $database = $this->getDatabase(); |
|
| 36 | - $currentUser = User::getCurrent($database); |
|
| 37 | - |
|
| 38 | - $yubikeyOtpCredentialProvider = new YubikeyOtpCredentialProvider($database, $this->getSiteConfiguration(), |
|
| 39 | - $this->getHttpHelper()); |
|
| 40 | - $this->assign('yubikeyOtpIdentity', $yubikeyOtpCredentialProvider->getYubikeyData($currentUser->getId())); |
|
| 41 | - $this->assign('yubikeyOtpEnrolled', $yubikeyOtpCredentialProvider->userIsEnrolled($currentUser->getId())); |
|
| 42 | - |
|
| 43 | - $totpCredentialProvider = new TotpCredentialProvider($database, $this->getSiteConfiguration()); |
|
| 44 | - $this->assign('totpEnrolled', $totpCredentialProvider->userIsEnrolled($currentUser->getId())); |
|
| 45 | - |
|
| 46 | - $u2fCredentialProvider = new U2FCredentialProvider($database, $this->getSiteConfiguration()); |
|
| 47 | - $this->assign('u2fEnrolled', $u2fCredentialProvider->userIsEnrolled($currentUser->getId())); |
|
| 48 | - |
|
| 49 | - $scratchCredentialProvider = new ScratchTokenCredentialProvider($database, $this->getSiteConfiguration()); |
|
| 50 | - $this->assign('scratchEnrolled', $scratchCredentialProvider->userIsEnrolled($currentUser->getId())); |
|
| 51 | - $this->assign('scratchRemaining', $scratchCredentialProvider->getRemaining($currentUser->getId())); |
|
| 52 | - |
|
| 53 | - $this->setTemplate('mfa/mfa.tpl'); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - protected function enableYubikeyOtp() |
|
| 57 | - { |
|
| 58 | - $database = $this->getDatabase(); |
|
| 59 | - $currentUser = User::getCurrent($database); |
|
| 60 | - |
|
| 61 | - $otpCredentialProvider = new YubikeyOtpCredentialProvider($database, |
|
| 62 | - $this->getSiteConfiguration(), $this->getHttpHelper()); |
|
| 63 | - |
|
| 64 | - if (WebRequest::wasPosted()) { |
|
| 65 | - $this->validateCSRFToken(); |
|
| 66 | - |
|
| 67 | - $passwordCredentialProvider = new PasswordCredentialProvider($database, |
|
| 68 | - $this->getSiteConfiguration()); |
|
| 69 | - |
|
| 70 | - $password = WebRequest::postString('password'); |
|
| 71 | - $otp = WebRequest::postString('otp'); |
|
| 72 | - |
|
| 73 | - $result = $passwordCredentialProvider->authenticate($currentUser, $password); |
|
| 74 | - |
|
| 75 | - if ($result) { |
|
| 76 | - try { |
|
| 77 | - $otpCredentialProvider->setCredential($currentUser, 2, $otp); |
|
| 78 | - SessionAlert::success('Enabled YubiKey OTP.'); |
|
| 79 | - |
|
| 80 | - $scratchProvider = new ScratchTokenCredentialProvider($database, $this->getSiteConfiguration()); |
|
| 81 | - if($scratchProvider->getRemaining($currentUser->getId()) < 3) { |
|
| 82 | - $scratchProvider->setCredential($currentUser, 2, null); |
|
| 83 | - $tokens = $scratchProvider->getTokens(); |
|
| 84 | - $this->assign('tokens', $tokens); |
|
| 85 | - $this->setTemplate('mfa/regenScratchTokens.tpl'); |
|
| 86 | - return; |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - catch (ApplicationLogicException $ex) { |
|
| 90 | - SessionAlert::error('Error enabling YubiKey OTP: ' . $ex->getMessage()); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - $this->redirect('multiFactor'); |
|
| 94 | - } |
|
| 95 | - else { |
|
| 96 | - SessionAlert::error('Error enabling YubiKey OTP - invalid credentials.'); |
|
| 97 | - $this->redirect('multiFactor'); |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - else { |
|
| 101 | - if ($otpCredentialProvider->userIsEnrolled($currentUser->getId())) { |
|
| 102 | - // user is not enrolled, we shouldn't have got here. |
|
| 103 | - throw new ApplicationLogicException('User is already enrolled in the selected MFA mechanism'); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - $this->assignCSRFToken(); |
|
| 107 | - $this->setTemplate('mfa/enableYubikey.tpl'); |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - protected function disableYubikeyOtp() |
|
| 112 | - { |
|
| 113 | - $database = $this->getDatabase(); |
|
| 114 | - $currentUser = User::getCurrent($database); |
|
| 115 | - |
|
| 116 | - $otpCredentialProvider = new YubikeyOtpCredentialProvider($database, |
|
| 117 | - $this->getSiteConfiguration(), $this->getHttpHelper()); |
|
| 118 | - |
|
| 119 | - $factorType = 'YubiKey OTP'; |
|
| 120 | - |
|
| 121 | - $this->deleteCredential($database, $currentUser, $otpCredentialProvider, $factorType); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - protected function enableTotp() |
|
| 125 | - { |
|
| 126 | - $database = $this->getDatabase(); |
|
| 127 | - $currentUser = User::getCurrent($database); |
|
| 128 | - |
|
| 129 | - $otpCredentialProvider = new TotpCredentialProvider($database, $this->getSiteConfiguration()); |
|
| 130 | - |
|
| 131 | - if (WebRequest::wasPosted()) { |
|
| 132 | - $this->validateCSRFToken(); |
|
| 133 | - |
|
| 134 | - // used for routing only, not security |
|
| 135 | - $stage = WebRequest::postString('stage'); |
|
| 136 | - |
|
| 137 | - if ($stage === "auth") { |
|
| 138 | - $password = WebRequest::postString('password'); |
|
| 139 | - |
|
| 140 | - $passwordCredentialProvider = new PasswordCredentialProvider($database, |
|
| 141 | - $this->getSiteConfiguration()); |
|
| 142 | - $result = $passwordCredentialProvider->authenticate($currentUser, $password); |
|
| 143 | - |
|
| 144 | - if ($result) { |
|
| 145 | - $otpCredentialProvider->setCredential($currentUser, 2, null); |
|
| 146 | - |
|
| 147 | - $provisioningUrl = $otpCredentialProvider->getProvisioningUrl($currentUser); |
|
| 148 | - |
|
| 149 | - $renderer = new Svg(); |
|
| 150 | - $renderer->setHeight(256); |
|
| 151 | - $renderer->setWidth(256); |
|
| 152 | - $writer = new Writer($renderer); |
|
| 153 | - $svg = $writer->writeString($provisioningUrl); |
|
| 154 | - |
|
| 155 | - $this->assign('svg', $svg); |
|
| 156 | - $this->assign('secret', $otpCredentialProvider->getSecret($currentUser)); |
|
| 157 | - |
|
| 158 | - $this->assignCSRFToken(); |
|
| 159 | - $this->setTemplate('mfa/enableTotpEnroll.tpl'); |
|
| 160 | - |
|
| 161 | - return; |
|
| 162 | - } |
|
| 163 | - else { |
|
| 164 | - SessionAlert::error('Error enabling TOTP - invalid credentials.'); |
|
| 165 | - $this->redirect('multiFactor'); |
|
| 166 | - |
|
| 167 | - return; |
|
| 168 | - } |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - if ($stage === "enroll") { |
|
| 172 | - // we *must* have a defined credential already here, |
|
| 173 | - if ($otpCredentialProvider->isPartiallyEnrolled($currentUser)) { |
|
| 174 | - $otp = WebRequest::postString('otp'); |
|
| 175 | - $result = $otpCredentialProvider->verifyEnable($currentUser, $otp); |
|
| 176 | - |
|
| 177 | - if ($result) { |
|
| 178 | - SessionAlert::success('Enabled TOTP.'); |
|
| 179 | - |
|
| 180 | - $scratchProvider = new ScratchTokenCredentialProvider($database, $this->getSiteConfiguration()); |
|
| 181 | - if($scratchProvider->getRemaining($currentUser->getId()) < 3) { |
|
| 182 | - $scratchProvider->setCredential($currentUser, 2, null); |
|
| 183 | - $tokens = $scratchProvider->getTokens(); |
|
| 184 | - $this->assign('tokens', $tokens); |
|
| 185 | - $this->setTemplate('mfa/regenScratchTokens.tpl'); |
|
| 186 | - return; |
|
| 187 | - } |
|
| 188 | - } |
|
| 189 | - else { |
|
| 190 | - $otpCredentialProvider->deleteCredential($currentUser); |
|
| 191 | - SessionAlert::error('Error enabling TOTP: invalid token provided'); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - |
|
| 195 | - $this->redirect('multiFactor'); |
|
| 196 | - return; |
|
| 197 | - } |
|
| 198 | - else { |
|
| 199 | - SessionAlert::error('Error enabling TOTP - no enrollment found or enrollment expired.'); |
|
| 200 | - $this->redirect('multiFactor'); |
|
| 29 | + /** |
|
| 30 | + * Main function for this page, when no specific actions are called. |
|
| 31 | + * @return void |
|
| 32 | + */ |
|
| 33 | + protected function main() |
|
| 34 | + { |
|
| 35 | + $database = $this->getDatabase(); |
|
| 36 | + $currentUser = User::getCurrent($database); |
|
| 37 | + |
|
| 38 | + $yubikeyOtpCredentialProvider = new YubikeyOtpCredentialProvider($database, $this->getSiteConfiguration(), |
|
| 39 | + $this->getHttpHelper()); |
|
| 40 | + $this->assign('yubikeyOtpIdentity', $yubikeyOtpCredentialProvider->getYubikeyData($currentUser->getId())); |
|
| 41 | + $this->assign('yubikeyOtpEnrolled', $yubikeyOtpCredentialProvider->userIsEnrolled($currentUser->getId())); |
|
| 42 | + |
|
| 43 | + $totpCredentialProvider = new TotpCredentialProvider($database, $this->getSiteConfiguration()); |
|
| 44 | + $this->assign('totpEnrolled', $totpCredentialProvider->userIsEnrolled($currentUser->getId())); |
|
| 45 | + |
|
| 46 | + $u2fCredentialProvider = new U2FCredentialProvider($database, $this->getSiteConfiguration()); |
|
| 47 | + $this->assign('u2fEnrolled', $u2fCredentialProvider->userIsEnrolled($currentUser->getId())); |
|
| 48 | + |
|
| 49 | + $scratchCredentialProvider = new ScratchTokenCredentialProvider($database, $this->getSiteConfiguration()); |
|
| 50 | + $this->assign('scratchEnrolled', $scratchCredentialProvider->userIsEnrolled($currentUser->getId())); |
|
| 51 | + $this->assign('scratchRemaining', $scratchCredentialProvider->getRemaining($currentUser->getId())); |
|
| 52 | + |
|
| 53 | + $this->setTemplate('mfa/mfa.tpl'); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + protected function enableYubikeyOtp() |
|
| 57 | + { |
|
| 58 | + $database = $this->getDatabase(); |
|
| 59 | + $currentUser = User::getCurrent($database); |
|
| 60 | + |
|
| 61 | + $otpCredentialProvider = new YubikeyOtpCredentialProvider($database, |
|
| 62 | + $this->getSiteConfiguration(), $this->getHttpHelper()); |
|
| 63 | + |
|
| 64 | + if (WebRequest::wasPosted()) { |
|
| 65 | + $this->validateCSRFToken(); |
|
| 66 | + |
|
| 67 | + $passwordCredentialProvider = new PasswordCredentialProvider($database, |
|
| 68 | + $this->getSiteConfiguration()); |
|
| 69 | + |
|
| 70 | + $password = WebRequest::postString('password'); |
|
| 71 | + $otp = WebRequest::postString('otp'); |
|
| 72 | + |
|
| 73 | + $result = $passwordCredentialProvider->authenticate($currentUser, $password); |
|
| 74 | + |
|
| 75 | + if ($result) { |
|
| 76 | + try { |
|
| 77 | + $otpCredentialProvider->setCredential($currentUser, 2, $otp); |
|
| 78 | + SessionAlert::success('Enabled YubiKey OTP.'); |
|
| 79 | + |
|
| 80 | + $scratchProvider = new ScratchTokenCredentialProvider($database, $this->getSiteConfiguration()); |
|
| 81 | + if($scratchProvider->getRemaining($currentUser->getId()) < 3) { |
|
| 82 | + $scratchProvider->setCredential($currentUser, 2, null); |
|
| 83 | + $tokens = $scratchProvider->getTokens(); |
|
| 84 | + $this->assign('tokens', $tokens); |
|
| 85 | + $this->setTemplate('mfa/regenScratchTokens.tpl'); |
|
| 86 | + return; |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + catch (ApplicationLogicException $ex) { |
|
| 90 | + SessionAlert::error('Error enabling YubiKey OTP: ' . $ex->getMessage()); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + $this->redirect('multiFactor'); |
|
| 94 | + } |
|
| 95 | + else { |
|
| 96 | + SessionAlert::error('Error enabling YubiKey OTP - invalid credentials.'); |
|
| 97 | + $this->redirect('multiFactor'); |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + else { |
|
| 101 | + if ($otpCredentialProvider->userIsEnrolled($currentUser->getId())) { |
|
| 102 | + // user is not enrolled, we shouldn't have got here. |
|
| 103 | + throw new ApplicationLogicException('User is already enrolled in the selected MFA mechanism'); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + $this->assignCSRFToken(); |
|
| 107 | + $this->setTemplate('mfa/enableYubikey.tpl'); |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + protected function disableYubikeyOtp() |
|
| 112 | + { |
|
| 113 | + $database = $this->getDatabase(); |
|
| 114 | + $currentUser = User::getCurrent($database); |
|
| 201 | 115 | |
| 202 | - return; |
|
| 203 | - } |
|
| 204 | - } |
|
| 116 | + $otpCredentialProvider = new YubikeyOtpCredentialProvider($database, |
|
| 117 | + $this->getSiteConfiguration(), $this->getHttpHelper()); |
|
| 118 | + |
|
| 119 | + $factorType = 'YubiKey OTP'; |
|
| 120 | + |
|
| 121 | + $this->deleteCredential($database, $currentUser, $otpCredentialProvider, $factorType); |
|
| 122 | + } |
|
| 205 | 123 | |
| 206 | - // urgh, dunno what happened, but it's not something expected. |
|
| 207 | - throw new ApplicationLogicException(); |
|
| 208 | - } |
|
| 209 | - else { |
|
| 210 | - if ($otpCredentialProvider->userIsEnrolled($currentUser->getId())) { |
|
| 211 | - // user is not enrolled, we shouldn't have got here. |
|
| 212 | - throw new ApplicationLogicException('User is already enrolled in the selected MFA mechanism'); |
|
| 213 | - } |
|
| 124 | + protected function enableTotp() |
|
| 125 | + { |
|
| 126 | + $database = $this->getDatabase(); |
|
| 127 | + $currentUser = User::getCurrent($database); |
|
| 128 | + |
|
| 129 | + $otpCredentialProvider = new TotpCredentialProvider($database, $this->getSiteConfiguration()); |
|
| 130 | + |
|
| 131 | + if (WebRequest::wasPosted()) { |
|
| 132 | + $this->validateCSRFToken(); |
|
| 133 | + |
|
| 134 | + // used for routing only, not security |
|
| 135 | + $stage = WebRequest::postString('stage'); |
|
| 136 | + |
|
| 137 | + if ($stage === "auth") { |
|
| 138 | + $password = WebRequest::postString('password'); |
|
| 139 | + |
|
| 140 | + $passwordCredentialProvider = new PasswordCredentialProvider($database, |
|
| 141 | + $this->getSiteConfiguration()); |
|
| 142 | + $result = $passwordCredentialProvider->authenticate($currentUser, $password); |
|
| 143 | + |
|
| 144 | + if ($result) { |
|
| 145 | + $otpCredentialProvider->setCredential($currentUser, 2, null); |
|
| 146 | + |
|
| 147 | + $provisioningUrl = $otpCredentialProvider->getProvisioningUrl($currentUser); |
|
| 148 | + |
|
| 149 | + $renderer = new Svg(); |
|
| 150 | + $renderer->setHeight(256); |
|
| 151 | + $renderer->setWidth(256); |
|
| 152 | + $writer = new Writer($renderer); |
|
| 153 | + $svg = $writer->writeString($provisioningUrl); |
|
| 154 | + |
|
| 155 | + $this->assign('svg', $svg); |
|
| 156 | + $this->assign('secret', $otpCredentialProvider->getSecret($currentUser)); |
|
| 157 | + |
|
| 158 | + $this->assignCSRFToken(); |
|
| 159 | + $this->setTemplate('mfa/enableTotpEnroll.tpl'); |
|
| 160 | + |
|
| 161 | + return; |
|
| 162 | + } |
|
| 163 | + else { |
|
| 164 | + SessionAlert::error('Error enabling TOTP - invalid credentials.'); |
|
| 165 | + $this->redirect('multiFactor'); |
|
| 166 | + |
|
| 167 | + return; |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + if ($stage === "enroll") { |
|
| 172 | + // we *must* have a defined credential already here, |
|
| 173 | + if ($otpCredentialProvider->isPartiallyEnrolled($currentUser)) { |
|
| 174 | + $otp = WebRequest::postString('otp'); |
|
| 175 | + $result = $otpCredentialProvider->verifyEnable($currentUser, $otp); |
|
| 176 | + |
|
| 177 | + if ($result) { |
|
| 178 | + SessionAlert::success('Enabled TOTP.'); |
|
| 179 | + |
|
| 180 | + $scratchProvider = new ScratchTokenCredentialProvider($database, $this->getSiteConfiguration()); |
|
| 181 | + if($scratchProvider->getRemaining($currentUser->getId()) < 3) { |
|
| 182 | + $scratchProvider->setCredential($currentUser, 2, null); |
|
| 183 | + $tokens = $scratchProvider->getTokens(); |
|
| 184 | + $this->assign('tokens', $tokens); |
|
| 185 | + $this->setTemplate('mfa/regenScratchTokens.tpl'); |
|
| 186 | + return; |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | + else { |
|
| 190 | + $otpCredentialProvider->deleteCredential($currentUser); |
|
| 191 | + SessionAlert::error('Error enabling TOTP: invalid token provided'); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + |
|
| 195 | + $this->redirect('multiFactor'); |
|
| 196 | + return; |
|
| 197 | + } |
|
| 198 | + else { |
|
| 199 | + SessionAlert::error('Error enabling TOTP - no enrollment found or enrollment expired.'); |
|
| 200 | + $this->redirect('multiFactor'); |
|
| 201 | + |
|
| 202 | + return; |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + // urgh, dunno what happened, but it's not something expected. |
|
| 207 | + throw new ApplicationLogicException(); |
|
| 208 | + } |
|
| 209 | + else { |
|
| 210 | + if ($otpCredentialProvider->userIsEnrolled($currentUser->getId())) { |
|
| 211 | + // user is not enrolled, we shouldn't have got here. |
|
| 212 | + throw new ApplicationLogicException('User is already enrolled in the selected MFA mechanism'); |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + $this->assignCSRFToken(); |
|
| 216 | + $this->setTemplate('mfa/enableTotpAuth.tpl'); |
|
| 217 | + } |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + protected function disableTotp() |
|
| 221 | + { |
|
| 222 | + $database = $this->getDatabase(); |
|
| 223 | + $currentUser = User::getCurrent($database); |
|
| 224 | + |
|
| 225 | + $otpCredentialProvider = new TotpCredentialProvider($database, $this->getSiteConfiguration()); |
|
| 226 | + |
|
| 227 | + $factorType = 'TOTP'; |
|
| 228 | + |
|
| 229 | + $this->deleteCredential($database, $currentUser, $otpCredentialProvider, $factorType); |
|
| 230 | + } |
|
| 214 | 231 | |
| 215 | - $this->assignCSRFToken(); |
|
| 216 | - $this->setTemplate('mfa/enableTotpAuth.tpl'); |
|
| 217 | - } |
|
| 218 | - } |
|
| 232 | + protected function enableU2F() { |
|
| 233 | + $database = $this->getDatabase(); |
|
| 234 | + $currentUser = User::getCurrent($database); |
|
| 219 | 235 | |
| 220 | - protected function disableTotp() |
|
| 221 | - { |
|
| 222 | - $database = $this->getDatabase(); |
|
| 223 | - $currentUser = User::getCurrent($database); |
|
| 236 | + $otpCredentialProvider = new U2FCredentialProvider($database, $this->getSiteConfiguration()); |
|
| 224 | 237 | |
| 225 | - $otpCredentialProvider = new TotpCredentialProvider($database, $this->getSiteConfiguration()); |
|
| 238 | + if (WebRequest::wasPosted()) { |
|
| 239 | + $this->validateCSRFToken(); |
|
| 226 | 240 | |
| 227 | - $factorType = 'TOTP'; |
|
| 241 | + // used for routing only, not security |
|
| 242 | + $stage = WebRequest::postString('stage'); |
|
| 228 | 243 | |
| 229 | - $this->deleteCredential($database, $currentUser, $otpCredentialProvider, $factorType); |
|
| 230 | - } |
|
| 244 | + if ($stage === "auth") { |
|
| 245 | + $password = WebRequest::postString('password'); |
|
| 231 | 246 | |
| 232 | - protected function enableU2F() { |
|
| 233 | - $database = $this->getDatabase(); |
|
| 234 | - $currentUser = User::getCurrent($database); |
|
| 247 | + $passwordCredentialProvider = new PasswordCredentialProvider($database, |
|
| 248 | + $this->getSiteConfiguration()); |
|
| 249 | + $result = $passwordCredentialProvider->authenticate($currentUser, $password); |
|
| 235 | 250 | |
| 236 | - $otpCredentialProvider = new U2FCredentialProvider($database, $this->getSiteConfiguration()); |
|
| 237 | - |
|
| 238 | - if (WebRequest::wasPosted()) { |
|
| 239 | - $this->validateCSRFToken(); |
|
| 240 | - |
|
| 241 | - // used for routing only, not security |
|
| 242 | - $stage = WebRequest::postString('stage'); |
|
| 251 | + if ($result) { |
|
| 252 | + $otpCredentialProvider->setCredential($currentUser, 2, null); |
|
| 253 | + $this->assignCSRFToken(); |
|
| 243 | 254 | |
| 244 | - if ($stage === "auth") { |
|
| 245 | - $password = WebRequest::postString('password'); |
|
| 246 | - |
|
| 247 | - $passwordCredentialProvider = new PasswordCredentialProvider($database, |
|
| 248 | - $this->getSiteConfiguration()); |
|
| 249 | - $result = $passwordCredentialProvider->authenticate($currentUser, $password); |
|
| 250 | - |
|
| 251 | - if ($result) { |
|
| 252 | - $otpCredentialProvider->setCredential($currentUser, 2, null); |
|
| 253 | - $this->assignCSRFToken(); |
|
| 254 | - |
|
| 255 | - list($data, $reqs) = $otpCredentialProvider->getRegistrationData(); |
|
| 256 | - |
|
| 257 | - $u2fRequest =json_encode($data); |
|
| 258 | - $u2fSigns = json_encode($reqs); |
|
| 259 | - |
|
| 260 | - $this->addJs('/vendor/yubico/u2flib-server/examples/assets/u2f-api.js'); |
|
| 261 | - $this->setTailScript(<<<JS |
|
| 255 | + list($data, $reqs) = $otpCredentialProvider->getRegistrationData(); |
|
| 256 | + |
|
| 257 | + $u2fRequest =json_encode($data); |
|
| 258 | + $u2fSigns = json_encode($reqs); |
|
| 259 | + |
|
| 260 | + $this->addJs('/vendor/yubico/u2flib-server/examples/assets/u2f-api.js'); |
|
| 261 | + $this->setTailScript(<<<JS |
|
| 262 | 262 | var request = ${u2fRequest}; |
| 263 | 263 | var signs = ${u2fSigns}; |
| 264 | 264 | |
@@ -277,153 +277,153 @@ discard block |
||
| 277 | 277 | form.submit(); |
| 278 | 278 | }); |
| 279 | 279 | JS |
| 280 | - ); |
|
| 281 | - |
|
| 282 | - $this->setTemplate('mfa/enableU2FEnroll.tpl'); |
|
| 283 | - |
|
| 284 | - return; |
|
| 285 | - } |
|
| 286 | - else { |
|
| 287 | - SessionAlert::error('Error enabling TOTP - invalid credentials.'); |
|
| 288 | - $this->redirect('multiFactor'); |
|
| 289 | - |
|
| 290 | - return; |
|
| 291 | - } |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - if ($stage === "enroll") { |
|
| 295 | - // we *must* have a defined credential already here, |
|
| 296 | - if ($otpCredentialProvider->isPartiallyEnrolled($currentUser)) { |
|
| 297 | - |
|
| 298 | - $request = json_decode(WebRequest::postString('u2fRequest')); |
|
| 299 | - $u2fData = json_decode(WebRequest::postString('u2fData')); |
|
| 300 | - |
|
| 301 | - $otpCredentialProvider->enable($currentUser, $request, $u2fData); |
|
| 302 | - |
|
| 303 | - SessionAlert::success('Enabled TOTP.'); |
|
| 304 | - |
|
| 305 | - $scratchProvider = new ScratchTokenCredentialProvider($database, $this->getSiteConfiguration()); |
|
| 306 | - if($scratchProvider->getRemaining($currentUser->getId()) < 3) { |
|
| 307 | - $scratchProvider->setCredential($currentUser, 2, null); |
|
| 308 | - $tokens = $scratchProvider->getTokens(); |
|
| 309 | - $this->assign('tokens', $tokens); |
|
| 310 | - $this->setTemplate('mfa/regenScratchTokens.tpl'); |
|
| 311 | - return; |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - $this->redirect('multiFactor'); |
|
| 315 | - return; |
|
| 316 | - } |
|
| 317 | - else { |
|
| 318 | - SessionAlert::error('Error enabling TOTP - no enrollment found or enrollment expired.'); |
|
| 319 | - $this->redirect('multiFactor'); |
|
| 320 | - |
|
| 321 | - return; |
|
| 322 | - } |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - // urgh, dunno what happened, but it's not something expected. |
|
| 326 | - throw new ApplicationLogicException(); |
|
| 327 | - } |
|
| 328 | - else { |
|
| 329 | - if ($otpCredentialProvider->userIsEnrolled($currentUser->getId())) { |
|
| 330 | - // user is not enrolled, we shouldn't have got here. |
|
| 331 | - throw new ApplicationLogicException('User is already enrolled in the selected MFA mechanism'); |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - $this->assignCSRFToken(); |
|
| 335 | - $this->setTemplate('mfa/enableU2FAuth.tpl'); |
|
| 336 | - } |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - protected function disableU2F() { |
|
| 340 | - $database = $this->getDatabase(); |
|
| 341 | - $currentUser = User::getCurrent($database); |
|
| 342 | - |
|
| 343 | - $otpCredentialProvider = new U2FCredentialProvider($database, $this->getSiteConfiguration()); |
|
| 344 | - |
|
| 345 | - $factorType = 'U2F'; |
|
| 346 | - |
|
| 347 | - $this->deleteCredential($database, $currentUser, $otpCredentialProvider, $factorType); |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - protected function scratch() |
|
| 351 | - { |
|
| 352 | - $database = $this->getDatabase(); |
|
| 353 | - $currentUser = User::getCurrent($database); |
|
| 354 | - |
|
| 355 | - if (WebRequest::wasPosted()) { |
|
| 356 | - $this->validateCSRFToken(); |
|
| 357 | - |
|
| 358 | - $passwordCredentialProvider = new PasswordCredentialProvider($database, |
|
| 359 | - $this->getSiteConfiguration()); |
|
| 360 | - |
|
| 361 | - $otpCredentialProvider = new ScratchTokenCredentialProvider($database, |
|
| 362 | - $this->getSiteConfiguration()); |
|
| 363 | - |
|
| 364 | - $password = WebRequest::postString('password'); |
|
| 365 | - |
|
| 366 | - $result = $passwordCredentialProvider->authenticate($currentUser, $password); |
|
| 367 | - |
|
| 368 | - if ($result) { |
|
| 369 | - $otpCredentialProvider->setCredential($currentUser, 2, null); |
|
| 370 | - $tokens = $otpCredentialProvider->getTokens(); |
|
| 371 | - $this->assign('tokens', $tokens); |
|
| 372 | - $this->setTemplate('mfa/regenScratchTokens.tpl'); |
|
| 373 | - } |
|
| 374 | - else { |
|
| 375 | - SessionAlert::error('Error refreshing scratch tokens - invalid credentials.'); |
|
| 376 | - $this->redirect('multiFactor'); |
|
| 377 | - } |
|
| 378 | - } |
|
| 379 | - else { |
|
| 380 | - $this->assignCSRFToken(); |
|
| 381 | - $this->setTemplate('mfa/regenScratchAuth.tpl'); |
|
| 382 | - } |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * @param PdoDatabase $database |
|
| 387 | - * @param User $currentUser |
|
| 388 | - * @param ICredentialProvider $otpCredentialProvider |
|
| 389 | - * @param string $factorType |
|
| 390 | - * |
|
| 391 | - * @throws ApplicationLogicException |
|
| 392 | - */ |
|
| 393 | - private function deleteCredential( |
|
| 394 | - PdoDatabase $database, |
|
| 395 | - User $currentUser, |
|
| 396 | - ICredentialProvider $otpCredentialProvider, |
|
| 397 | - $factorType |
|
| 398 | - ) { |
|
| 399 | - if (WebRequest::wasPosted()) { |
|
| 400 | - $passwordCredentialProvider = new PasswordCredentialProvider($database, |
|
| 401 | - $this->getSiteConfiguration()); |
|
| 402 | - |
|
| 403 | - $this->validateCSRFToken(); |
|
| 404 | - |
|
| 405 | - $password = WebRequest::postString('password'); |
|
| 406 | - $result = $passwordCredentialProvider->authenticate($currentUser, $password); |
|
| 407 | - |
|
| 408 | - if ($result) { |
|
| 409 | - $otpCredentialProvider->deleteCredential($currentUser); |
|
| 410 | - SessionAlert::success('Disabled ' . $factorType . '.'); |
|
| 411 | - $this->redirect('multiFactor'); |
|
| 412 | - } |
|
| 413 | - else { |
|
| 414 | - SessionAlert::error('Error disabling ' . $factorType . ' - invalid credentials.'); |
|
| 415 | - $this->redirect('multiFactor'); |
|
| 416 | - } |
|
| 417 | - } |
|
| 418 | - else { |
|
| 419 | - if (!$otpCredentialProvider->userIsEnrolled($currentUser->getId())) { |
|
| 420 | - // user is not enrolled, we shouldn't have got here. |
|
| 421 | - throw new ApplicationLogicException('User is not enrolled in the selected MFA mechanism'); |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - $this->assignCSRFToken(); |
|
| 425 | - $this->assign('otpType', $factorType); |
|
| 426 | - $this->setTemplate('mfa/disableOtp.tpl'); |
|
| 427 | - } |
|
| 428 | - } |
|
| 280 | + ); |
|
| 281 | + |
|
| 282 | + $this->setTemplate('mfa/enableU2FEnroll.tpl'); |
|
| 283 | + |
|
| 284 | + return; |
|
| 285 | + } |
|
| 286 | + else { |
|
| 287 | + SessionAlert::error('Error enabling TOTP - invalid credentials.'); |
|
| 288 | + $this->redirect('multiFactor'); |
|
| 289 | + |
|
| 290 | + return; |
|
| 291 | + } |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + if ($stage === "enroll") { |
|
| 295 | + // we *must* have a defined credential already here, |
|
| 296 | + if ($otpCredentialProvider->isPartiallyEnrolled($currentUser)) { |
|
| 297 | + |
|
| 298 | + $request = json_decode(WebRequest::postString('u2fRequest')); |
|
| 299 | + $u2fData = json_decode(WebRequest::postString('u2fData')); |
|
| 300 | + |
|
| 301 | + $otpCredentialProvider->enable($currentUser, $request, $u2fData); |
|
| 302 | + |
|
| 303 | + SessionAlert::success('Enabled TOTP.'); |
|
| 304 | + |
|
| 305 | + $scratchProvider = new ScratchTokenCredentialProvider($database, $this->getSiteConfiguration()); |
|
| 306 | + if($scratchProvider->getRemaining($currentUser->getId()) < 3) { |
|
| 307 | + $scratchProvider->setCredential($currentUser, 2, null); |
|
| 308 | + $tokens = $scratchProvider->getTokens(); |
|
| 309 | + $this->assign('tokens', $tokens); |
|
| 310 | + $this->setTemplate('mfa/regenScratchTokens.tpl'); |
|
| 311 | + return; |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + $this->redirect('multiFactor'); |
|
| 315 | + return; |
|
| 316 | + } |
|
| 317 | + else { |
|
| 318 | + SessionAlert::error('Error enabling TOTP - no enrollment found or enrollment expired.'); |
|
| 319 | + $this->redirect('multiFactor'); |
|
| 320 | + |
|
| 321 | + return; |
|
| 322 | + } |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + // urgh, dunno what happened, but it's not something expected. |
|
| 326 | + throw new ApplicationLogicException(); |
|
| 327 | + } |
|
| 328 | + else { |
|
| 329 | + if ($otpCredentialProvider->userIsEnrolled($currentUser->getId())) { |
|
| 330 | + // user is not enrolled, we shouldn't have got here. |
|
| 331 | + throw new ApplicationLogicException('User is already enrolled in the selected MFA mechanism'); |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + $this->assignCSRFToken(); |
|
| 335 | + $this->setTemplate('mfa/enableU2FAuth.tpl'); |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + protected function disableU2F() { |
|
| 340 | + $database = $this->getDatabase(); |
|
| 341 | + $currentUser = User::getCurrent($database); |
|
| 342 | + |
|
| 343 | + $otpCredentialProvider = new U2FCredentialProvider($database, $this->getSiteConfiguration()); |
|
| 344 | + |
|
| 345 | + $factorType = 'U2F'; |
|
| 346 | + |
|
| 347 | + $this->deleteCredential($database, $currentUser, $otpCredentialProvider, $factorType); |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + protected function scratch() |
|
| 351 | + { |
|
| 352 | + $database = $this->getDatabase(); |
|
| 353 | + $currentUser = User::getCurrent($database); |
|
| 354 | + |
|
| 355 | + if (WebRequest::wasPosted()) { |
|
| 356 | + $this->validateCSRFToken(); |
|
| 357 | + |
|
| 358 | + $passwordCredentialProvider = new PasswordCredentialProvider($database, |
|
| 359 | + $this->getSiteConfiguration()); |
|
| 360 | + |
|
| 361 | + $otpCredentialProvider = new ScratchTokenCredentialProvider($database, |
|
| 362 | + $this->getSiteConfiguration()); |
|
| 363 | + |
|
| 364 | + $password = WebRequest::postString('password'); |
|
| 365 | + |
|
| 366 | + $result = $passwordCredentialProvider->authenticate($currentUser, $password); |
|
| 367 | + |
|
| 368 | + if ($result) { |
|
| 369 | + $otpCredentialProvider->setCredential($currentUser, 2, null); |
|
| 370 | + $tokens = $otpCredentialProvider->getTokens(); |
|
| 371 | + $this->assign('tokens', $tokens); |
|
| 372 | + $this->setTemplate('mfa/regenScratchTokens.tpl'); |
|
| 373 | + } |
|
| 374 | + else { |
|
| 375 | + SessionAlert::error('Error refreshing scratch tokens - invalid credentials.'); |
|
| 376 | + $this->redirect('multiFactor'); |
|
| 377 | + } |
|
| 378 | + } |
|
| 379 | + else { |
|
| 380 | + $this->assignCSRFToken(); |
|
| 381 | + $this->setTemplate('mfa/regenScratchAuth.tpl'); |
|
| 382 | + } |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * @param PdoDatabase $database |
|
| 387 | + * @param User $currentUser |
|
| 388 | + * @param ICredentialProvider $otpCredentialProvider |
|
| 389 | + * @param string $factorType |
|
| 390 | + * |
|
| 391 | + * @throws ApplicationLogicException |
|
| 392 | + */ |
|
| 393 | + private function deleteCredential( |
|
| 394 | + PdoDatabase $database, |
|
| 395 | + User $currentUser, |
|
| 396 | + ICredentialProvider $otpCredentialProvider, |
|
| 397 | + $factorType |
|
| 398 | + ) { |
|
| 399 | + if (WebRequest::wasPosted()) { |
|
| 400 | + $passwordCredentialProvider = new PasswordCredentialProvider($database, |
|
| 401 | + $this->getSiteConfiguration()); |
|
| 402 | + |
|
| 403 | + $this->validateCSRFToken(); |
|
| 404 | + |
|
| 405 | + $password = WebRequest::postString('password'); |
|
| 406 | + $result = $passwordCredentialProvider->authenticate($currentUser, $password); |
|
| 407 | + |
|
| 408 | + if ($result) { |
|
| 409 | + $otpCredentialProvider->deleteCredential($currentUser); |
|
| 410 | + SessionAlert::success('Disabled ' . $factorType . '.'); |
|
| 411 | + $this->redirect('multiFactor'); |
|
| 412 | + } |
|
| 413 | + else { |
|
| 414 | + SessionAlert::error('Error disabling ' . $factorType . ' - invalid credentials.'); |
|
| 415 | + $this->redirect('multiFactor'); |
|
| 416 | + } |
|
| 417 | + } |
|
| 418 | + else { |
|
| 419 | + if (!$otpCredentialProvider->userIsEnrolled($currentUser->getId())) { |
|
| 420 | + // user is not enrolled, we shouldn't have got here. |
|
| 421 | + throw new ApplicationLogicException('User is not enrolled in the selected MFA mechanism'); |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + $this->assignCSRFToken(); |
|
| 425 | + $this->assign('otpType', $factorType); |
|
| 426 | + $this->setTemplate('mfa/disableOtp.tpl'); |
|
| 427 | + } |
|
| 428 | + } |
|
| 429 | 429 | } |
@@ -17,90 +17,90 @@ |
||
| 17 | 17 | |
| 18 | 18 | class PageOAuthCallback extends InternalPageBase |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * @return bool |
|
| 22 | - */ |
|
| 23 | - protected function isProtectedPage() |
|
| 24 | - { |
|
| 25 | - // This page is critical to ensuring OAuth functionality is operational. |
|
| 26 | - return false; |
|
| 27 | - } |
|
| 20 | + /** |
|
| 21 | + * @return bool |
|
| 22 | + */ |
|
| 23 | + protected function isProtectedPage() |
|
| 24 | + { |
|
| 25 | + // This page is critical to ensuring OAuth functionality is operational. |
|
| 26 | + return false; |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Main function for this page, when no specific actions are called. |
|
| 31 | - * @return void |
|
| 32 | - */ |
|
| 33 | - protected function main() |
|
| 34 | - { |
|
| 35 | - // This should never get hit except by URL manipulation. |
|
| 36 | - $this->redirect(''); |
|
| 37 | - } |
|
| 29 | + /** |
|
| 30 | + * Main function for this page, when no specific actions are called. |
|
| 31 | + * @return void |
|
| 32 | + */ |
|
| 33 | + protected function main() |
|
| 34 | + { |
|
| 35 | + // This should never get hit except by URL manipulation. |
|
| 36 | + $this->redirect(''); |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * Registered endpoint for the account creation callback. |
|
| 41 | - * |
|
| 42 | - * If this ever gets hit, something is wrong somewhere. |
|
| 43 | - */ |
|
| 44 | - protected function create() |
|
| 45 | - { |
|
| 46 | - throw new Exception('OAuth account creation endpoint triggered.'); |
|
| 47 | - } |
|
| 39 | + /** |
|
| 40 | + * Registered endpoint for the account creation callback. |
|
| 41 | + * |
|
| 42 | + * If this ever gets hit, something is wrong somewhere. |
|
| 43 | + */ |
|
| 44 | + protected function create() |
|
| 45 | + { |
|
| 46 | + throw new Exception('OAuth account creation endpoint triggered.'); |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * Callback entry point |
|
| 51 | - */ |
|
| 52 | - protected function authorise() |
|
| 53 | - { |
|
| 54 | - $oauthToken = WebRequest::getString('oauth_token'); |
|
| 55 | - $oauthVerifier = WebRequest::getString('oauth_verifier'); |
|
| 49 | + /** |
|
| 50 | + * Callback entry point |
|
| 51 | + */ |
|
| 52 | + protected function authorise() |
|
| 53 | + { |
|
| 54 | + $oauthToken = WebRequest::getString('oauth_token'); |
|
| 55 | + $oauthVerifier = WebRequest::getString('oauth_verifier'); |
|
| 56 | 56 | |
| 57 | - $this->doCallbackValidation($oauthToken, $oauthVerifier); |
|
| 57 | + $this->doCallbackValidation($oauthToken, $oauthVerifier); |
|
| 58 | 58 | |
| 59 | - $database = $this->getDatabase(); |
|
| 59 | + $database = $this->getDatabase(); |
|
| 60 | 60 | |
| 61 | - $user = OAuthUserHelper::findUserByRequestToken($oauthToken, $database); |
|
| 62 | - $oauth = new OAuthUserHelper($user, $database, $this->getOAuthProtocolHelper(), $this->getSiteConfiguration()); |
|
| 61 | + $user = OAuthUserHelper::findUserByRequestToken($oauthToken, $database); |
|
| 62 | + $oauth = new OAuthUserHelper($user, $database, $this->getOAuthProtocolHelper(), $this->getSiteConfiguration()); |
|
| 63 | 63 | |
| 64 | - try { |
|
| 65 | - $oauth->completeHandshake($oauthVerifier); |
|
| 66 | - } |
|
| 67 | - catch (CurlException $ex) { |
|
| 68 | - throw new ApplicationLogicException($ex->getMessage(), 0, $ex); |
|
| 69 | - } |
|
| 64 | + try { |
|
| 65 | + $oauth->completeHandshake($oauthVerifier); |
|
| 66 | + } |
|
| 67 | + catch (CurlException $ex) { |
|
| 68 | + throw new ApplicationLogicException($ex->getMessage(), 0, $ex); |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - // OK, we're the same session that just did a partial login that was redirected to OAuth. Let's upgrade the |
|
| 72 | - // login to a full login |
|
| 73 | - if (WebRequest::getOAuthPartialLogin() === $user->getId()) { |
|
| 74 | - WebRequest::setLoggedInUser($user); |
|
| 75 | - } |
|
| 71 | + // OK, we're the same session that just did a partial login that was redirected to OAuth. Let's upgrade the |
|
| 72 | + // login to a full login |
|
| 73 | + if (WebRequest::getOAuthPartialLogin() === $user->getId()) { |
|
| 74 | + WebRequest::setLoggedInUser($user); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - // My thinking is there are three cases here: |
|
| 78 | - // a) new user => redirect to prefs - it's the only thing they can access other than stats |
|
| 79 | - // b) existing user hit the connect button in prefs => redirect to prefs since it's where they were |
|
| 80 | - // c) existing user logging in => redirect to wherever they came from |
|
| 81 | - $redirectDestination = WebRequest::clearPostLoginRedirect(); |
|
| 82 | - if ($redirectDestination !== null && !$user->isNewUser()) { |
|
| 83 | - $this->redirectUrl($redirectDestination); |
|
| 84 | - } |
|
| 85 | - else { |
|
| 86 | - $this->redirect('preferences', null, null, 'internal.php'); |
|
| 87 | - } |
|
| 88 | - } |
|
| 77 | + // My thinking is there are three cases here: |
|
| 78 | + // a) new user => redirect to prefs - it's the only thing they can access other than stats |
|
| 79 | + // b) existing user hit the connect button in prefs => redirect to prefs since it's where they were |
|
| 80 | + // c) existing user logging in => redirect to wherever they came from |
|
| 81 | + $redirectDestination = WebRequest::clearPostLoginRedirect(); |
|
| 82 | + if ($redirectDestination !== null && !$user->isNewUser()) { |
|
| 83 | + $this->redirectUrl($redirectDestination); |
|
| 84 | + } |
|
| 85 | + else { |
|
| 86 | + $this->redirect('preferences', null, null, 'internal.php'); |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | 89 | |
| 90 | - /** |
|
| 91 | - * @param string $oauthToken |
|
| 92 | - * @param string $oauthVerifier |
|
| 93 | - * |
|
| 94 | - * @throws ApplicationLogicException |
|
| 95 | - */ |
|
| 96 | - private function doCallbackValidation($oauthToken, $oauthVerifier) |
|
| 97 | - { |
|
| 98 | - if ($oauthToken === null) { |
|
| 99 | - throw new ApplicationLogicException('No token provided'); |
|
| 100 | - } |
|
| 90 | + /** |
|
| 91 | + * @param string $oauthToken |
|
| 92 | + * @param string $oauthVerifier |
|
| 93 | + * |
|
| 94 | + * @throws ApplicationLogicException |
|
| 95 | + */ |
|
| 96 | + private function doCallbackValidation($oauthToken, $oauthVerifier) |
|
| 97 | + { |
|
| 98 | + if ($oauthToken === null) { |
|
| 99 | + throw new ApplicationLogicException('No token provided'); |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - if ($oauthVerifier === null) { |
|
| 103 | - throw new ApplicationLogicException('No oauth verifier provided.'); |
|
| 104 | - } |
|
| 105 | - } |
|
| 102 | + if ($oauthVerifier === null) { |
|
| 103 | + throw new ApplicationLogicException('No oauth verifier provided.'); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | 106 | } |
| 107 | 107 | \ No newline at end of file |
@@ -21,315 +21,315 @@ |
||
| 21 | 21 | |
| 22 | 22 | abstract class LoginCredentialPageBase extends InternalPageBase |
| 23 | 23 | { |
| 24 | - /** @var User */ |
|
| 25 | - protected $partialUser = null; |
|
| 26 | - protected $nextPageMap = array( |
|
| 27 | - 'yubikeyotp' => 'otp', |
|
| 28 | - 'totp' => 'otp', |
|
| 29 | - 'scratch' => 'otp', |
|
| 30 | - 'u2f' => 'u2f', |
|
| 31 | - ); |
|
| 32 | - protected $names = array( |
|
| 33 | - 'yubikeyotp' => 'Yubikey OTP', |
|
| 34 | - 'totp' => 'TOTP (phone code generator)', |
|
| 35 | - 'scratch' => 'scratch token', |
|
| 36 | - 'u2f' => 'U2F security token', |
|
| 37 | - ); |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Main function for this page, when no specific actions are called. |
|
| 41 | - * @return void |
|
| 42 | - */ |
|
| 43 | - protected function main() |
|
| 44 | - { |
|
| 45 | - if (!$this->enforceHttps()) { |
|
| 46 | - return; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - if (WebRequest::wasPosted()) { |
|
| 50 | - $this->validateCSRFToken(); |
|
| 51 | - |
|
| 52 | - $database = $this->getDatabase(); |
|
| 53 | - try { |
|
| 54 | - list($partialId, $partialStage) = WebRequest::getAuthPartialLogin(); |
|
| 55 | - |
|
| 56 | - if ($partialStage === null) { |
|
| 57 | - $partialStage = 1; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - if ($partialId === null) { |
|
| 61 | - $username = WebRequest::postString('username'); |
|
| 62 | - |
|
| 63 | - if ($username === null || trim($username) === '') { |
|
| 64 | - throw new ApplicationLogicException('No username specified.'); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - $user = User::getByUsername($username, $database); |
|
| 68 | - } |
|
| 69 | - else { |
|
| 70 | - $user = User::getById($partialId, $database); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - if ($user === false) { |
|
| 74 | - throw new ApplicationLogicException("Authentication failed"); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - $authMan = new AuthenticationManager($database, $this->getSiteConfiguration(), |
|
| 78 | - $this->getHttpHelper()); |
|
| 79 | - |
|
| 80 | - $credential = $this->getProviderCredentials(); |
|
| 81 | - |
|
| 82 | - $authResult = $authMan->authenticate($user, $credential, $partialStage); |
|
| 83 | - |
|
| 84 | - if ($authResult === AuthenticationManager::AUTH_FAIL) { |
|
| 85 | - throw new ApplicationLogicException("Authentication failed"); |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - if ($authResult === AuthenticationManager::AUTH_REQUIRE_NEXT_STAGE) { |
|
| 89 | - $this->processJumpNextStage($user, $partialStage, $database); |
|
| 90 | - |
|
| 91 | - return; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - if ($authResult === AuthenticationManager::AUTH_OK) { |
|
| 95 | - $this->processLoginSuccess($user); |
|
| 96 | - |
|
| 97 | - return; |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - catch (ApplicationLogicException $ex) { |
|
| 101 | - WebRequest::clearAuthPartialLogin(); |
|
| 102 | - |
|
| 103 | - SessionAlert::error($ex->getMessage()); |
|
| 104 | - $this->redirect('login'); |
|
| 105 | - |
|
| 106 | - return; |
|
| 107 | - } |
|
| 108 | - } |
|
| 109 | - else { |
|
| 110 | - $this->assign('showSignIn', true); |
|
| 111 | - |
|
| 112 | - $this->setupPartial(); |
|
| 113 | - $this->assignCSRFToken(); |
|
| 114 | - $this->providerSpecificSetup(); |
|
| 115 | - } |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - protected function isProtectedPage() |
|
| 119 | - { |
|
| 120 | - return false; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Enforces HTTPS on the login form |
|
| 125 | - * |
|
| 126 | - * @return bool |
|
| 127 | - */ |
|
| 128 | - private function enforceHttps() |
|
| 129 | - { |
|
| 130 | - if ($this->getSiteConfiguration()->getUseStrictTransportSecurity() !== false) { |
|
| 131 | - if (WebRequest::isHttps()) { |
|
| 132 | - // Client can clearly use HTTPS, so let's enforce it for all connections. |
|
| 133 | - $this->headerQueue[] = "Strict-Transport-Security: max-age=15768000"; |
|
| 134 | - } |
|
| 135 | - else { |
|
| 136 | - // This is the login form, not the request form. We need protection here. |
|
| 137 | - $this->redirectUrl('https://' . WebRequest::serverName() . WebRequest::requestUri()); |
|
| 138 | - |
|
| 139 | - return false; |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - return true; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - protected abstract function providerSpecificSetup(); |
|
| 147 | - |
|
| 148 | - protected function setupPartial() |
|
| 149 | - { |
|
| 150 | - $database = $this->getDatabase(); |
|
| 151 | - |
|
| 152 | - // default stuff |
|
| 153 | - $this->assign('alternatives', array()); // 'u2f' => array('U2F token'), 'otp' => array('TOTP', 'scratch', 'yubiotp'))); |
|
| 154 | - |
|
| 155 | - // is this stage one? |
|
| 156 | - list($partialId, $partialStage) = WebRequest::getAuthPartialLogin(); |
|
| 157 | - if ($partialStage === null || $partialId === null) { |
|
| 158 | - WebRequest::clearAuthPartialLogin(); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - // Check to see if we have a partial login in progress |
|
| 162 | - $username = null; |
|
| 163 | - if ($partialId !== null) { |
|
| 164 | - // Yes, enforce this username |
|
| 165 | - $this->partialUser = User::getById($partialId, $database); |
|
| 166 | - $username = $this->partialUser->getUsername(); |
|
| 167 | - |
|
| 168 | - $this->setupAlternates($this->partialUser, $partialStage, $database); |
|
| 169 | - } |
|
| 170 | - else { |
|
| 171 | - // No, see if we've preloaded a username |
|
| 172 | - $preloadUsername = WebRequest::getString('tplUsername'); |
|
| 173 | - if ($preloadUsername !== null) { |
|
| 174 | - $username = $preloadUsername; |
|
| 175 | - } |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - if ($partialStage === null) { |
|
| 179 | - $partialStage = 1; |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - $this->assign('partialStage', $partialStage); |
|
| 183 | - $this->assign('username', $username); |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * Redirect the user back to wherever they came from after a successful login |
|
| 188 | - * |
|
| 189 | - * @param User $user |
|
| 190 | - */ |
|
| 191 | - protected function goBackWhenceYouCame(User $user) |
|
| 192 | - { |
|
| 193 | - // Redirect to wherever the user came from |
|
| 194 | - $redirectDestination = WebRequest::clearPostLoginRedirect(); |
|
| 195 | - if ($redirectDestination !== null) { |
|
| 196 | - $this->redirectUrl($redirectDestination); |
|
| 197 | - } |
|
| 198 | - else { |
|
| 199 | - if ($user->isNewUser()) { |
|
| 200 | - // home page isn't allowed, go to preferences instead |
|
| 201 | - $this->redirect('preferences'); |
|
| 202 | - } |
|
| 203 | - else { |
|
| 204 | - // go to the home page |
|
| 205 | - $this->redirect(''); |
|
| 206 | - } |
|
| 207 | - } |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - private function processLoginSuccess(User $user) |
|
| 211 | - { |
|
| 212 | - // Touch force logout |
|
| 213 | - $user->setForceLogout(false); |
|
| 214 | - $user->save(); |
|
| 215 | - |
|
| 216 | - $oauth = new OAuthUserHelper($user, $this->getDatabase(), $this->getOAuthProtocolHelper(), |
|
| 217 | - $this->getSiteConfiguration()); |
|
| 218 | - |
|
| 219 | - if ($oauth->isFullyLinked()) { |
|
| 220 | - try { |
|
| 221 | - // Reload the user's identity ticket. |
|
| 222 | - $oauth->refreshIdentity(); |
|
| 223 | - |
|
| 224 | - // Check for blocks |
|
| 225 | - if ($oauth->getIdentity()->getBlocked()) { |
|
| 226 | - // blocked! |
|
| 227 | - SessionAlert::error("You are currently blocked on-wiki. You will not be able to log in until you are unblocked."); |
|
| 228 | - $this->redirect('login'); |
|
| 229 | - |
|
| 230 | - return; |
|
| 231 | - } |
|
| 232 | - } |
|
| 233 | - catch (OAuthException $ex) { |
|
| 234 | - // Oops. Refreshing ticket failed. Force a re-auth. |
|
| 235 | - $authoriseUrl = $oauth->getRequestToken(); |
|
| 236 | - WebRequest::setOAuthPartialLogin($user); |
|
| 237 | - $this->redirectUrl($authoriseUrl); |
|
| 238 | - |
|
| 239 | - return; |
|
| 240 | - } |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - if (($this->getSiteConfiguration()->getEnforceOAuth() && !$oauth->isFullyLinked()) |
|
| 244 | - || $oauth->isPartiallyLinked() |
|
| 245 | - ) { |
|
| 246 | - $authoriseUrl = $oauth->getRequestToken(); |
|
| 247 | - WebRequest::setOAuthPartialLogin($user); |
|
| 248 | - $this->redirectUrl($authoriseUrl); |
|
| 249 | - |
|
| 250 | - return; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - WebRequest::setLoggedInUser($user); |
|
| 254 | - |
|
| 255 | - $this->goBackWhenceYouCame($user); |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - protected abstract function getProviderCredentials(); |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * @param User $user |
|
| 262 | - * @param int $partialStage |
|
| 263 | - * @param PdoDatabase $database |
|
| 264 | - * |
|
| 265 | - * @throws ApplicationLogicException |
|
| 266 | - */ |
|
| 267 | - private function processJumpNextStage(User $user, $partialStage, PdoDatabase $database) |
|
| 268 | - { |
|
| 269 | - WebRequest::setAuthPartialLogin($user->getId(), $partialStage + 1); |
|
| 270 | - |
|
| 271 | - $sql = 'SELECT type FROM credential WHERE user = :user AND factor = :stage AND disabled = 0 ORDER BY priority'; |
|
| 272 | - $statement = $database->prepare($sql); |
|
| 273 | - $statement->execute(array(':user' => $user->getId(), ':stage' => $partialStage + 1)); |
|
| 274 | - $nextStage = $statement->fetchColumn(); |
|
| 275 | - $statement->closeCursor(); |
|
| 276 | - |
|
| 277 | - if (!isset($this->nextPageMap[$nextStage])) { |
|
| 278 | - throw new ApplicationLogicException('Unknown page handler for next authentication stage.'); |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - $this->redirect("login/" . $this->nextPageMap[$nextStage]); |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - private function setupAlternates(User $user, $partialStage, PdoDatabase $database) |
|
| 285 | - { |
|
| 286 | - // get the providers available |
|
| 287 | - $sql = 'SELECT type FROM credential WHERE user = :user AND factor = :stage AND disabled = 0'; |
|
| 288 | - $statement = $database->prepare($sql); |
|
| 289 | - $statement->execute(array(':user' => $user->getId(), ':stage' => $partialStage)); |
|
| 290 | - $alternates = $statement->fetchAll(PDO::FETCH_COLUMN); |
|
| 291 | - |
|
| 292 | - $types = array(); |
|
| 293 | - foreach ($alternates as $item) { |
|
| 294 | - $type = $this->nextPageMap[$item]; |
|
| 295 | - if (!isset($types[$type])) { |
|
| 296 | - $types[$type] = array(); |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - $types[$type][] = $item; |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - $userOptions = array(); |
|
| 303 | - if (get_called_class() === PageOtpLogin::class) { |
|
| 304 | - $userOptions = $this->setupUserOptionsForType($types, 'u2f', $userOptions); |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - if (get_called_class() === PageU2FLogin::class) { |
|
| 308 | - $userOptions = $this->setupUserOptionsForType($types, 'otp', $userOptions); |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - $this->assign('alternatives', $userOptions); |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * @param $types |
|
| 316 | - * @param $type |
|
| 317 | - * @param $userOptions |
|
| 318 | - * |
|
| 319 | - * @return mixed |
|
| 320 | - */ |
|
| 321 | - private function setupUserOptionsForType($types, $type, $userOptions) |
|
| 322 | - { |
|
| 323 | - if (isset($types[$type])) { |
|
| 324 | - $options = $types[$type]; |
|
| 325 | - |
|
| 326 | - array_walk($options, function(&$val) { |
|
| 327 | - $val = $this->names[$val]; |
|
| 328 | - }); |
|
| 329 | - |
|
| 330 | - $userOptions[$type] = $options; |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - return $userOptions; |
|
| 334 | - } |
|
| 24 | + /** @var User */ |
|
| 25 | + protected $partialUser = null; |
|
| 26 | + protected $nextPageMap = array( |
|
| 27 | + 'yubikeyotp' => 'otp', |
|
| 28 | + 'totp' => 'otp', |
|
| 29 | + 'scratch' => 'otp', |
|
| 30 | + 'u2f' => 'u2f', |
|
| 31 | + ); |
|
| 32 | + protected $names = array( |
|
| 33 | + 'yubikeyotp' => 'Yubikey OTP', |
|
| 34 | + 'totp' => 'TOTP (phone code generator)', |
|
| 35 | + 'scratch' => 'scratch token', |
|
| 36 | + 'u2f' => 'U2F security token', |
|
| 37 | + ); |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Main function for this page, when no specific actions are called. |
|
| 41 | + * @return void |
|
| 42 | + */ |
|
| 43 | + protected function main() |
|
| 44 | + { |
|
| 45 | + if (!$this->enforceHttps()) { |
|
| 46 | + return; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + if (WebRequest::wasPosted()) { |
|
| 50 | + $this->validateCSRFToken(); |
|
| 51 | + |
|
| 52 | + $database = $this->getDatabase(); |
|
| 53 | + try { |
|
| 54 | + list($partialId, $partialStage) = WebRequest::getAuthPartialLogin(); |
|
| 55 | + |
|
| 56 | + if ($partialStage === null) { |
|
| 57 | + $partialStage = 1; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + if ($partialId === null) { |
|
| 61 | + $username = WebRequest::postString('username'); |
|
| 62 | + |
|
| 63 | + if ($username === null || trim($username) === '') { |
|
| 64 | + throw new ApplicationLogicException('No username specified.'); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + $user = User::getByUsername($username, $database); |
|
| 68 | + } |
|
| 69 | + else { |
|
| 70 | + $user = User::getById($partialId, $database); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + if ($user === false) { |
|
| 74 | + throw new ApplicationLogicException("Authentication failed"); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + $authMan = new AuthenticationManager($database, $this->getSiteConfiguration(), |
|
| 78 | + $this->getHttpHelper()); |
|
| 79 | + |
|
| 80 | + $credential = $this->getProviderCredentials(); |
|
| 81 | + |
|
| 82 | + $authResult = $authMan->authenticate($user, $credential, $partialStage); |
|
| 83 | + |
|
| 84 | + if ($authResult === AuthenticationManager::AUTH_FAIL) { |
|
| 85 | + throw new ApplicationLogicException("Authentication failed"); |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + if ($authResult === AuthenticationManager::AUTH_REQUIRE_NEXT_STAGE) { |
|
| 89 | + $this->processJumpNextStage($user, $partialStage, $database); |
|
| 90 | + |
|
| 91 | + return; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + if ($authResult === AuthenticationManager::AUTH_OK) { |
|
| 95 | + $this->processLoginSuccess($user); |
|
| 96 | + |
|
| 97 | + return; |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + catch (ApplicationLogicException $ex) { |
|
| 101 | + WebRequest::clearAuthPartialLogin(); |
|
| 102 | + |
|
| 103 | + SessionAlert::error($ex->getMessage()); |
|
| 104 | + $this->redirect('login'); |
|
| 105 | + |
|
| 106 | + return; |
|
| 107 | + } |
|
| 108 | + } |
|
| 109 | + else { |
|
| 110 | + $this->assign('showSignIn', true); |
|
| 111 | + |
|
| 112 | + $this->setupPartial(); |
|
| 113 | + $this->assignCSRFToken(); |
|
| 114 | + $this->providerSpecificSetup(); |
|
| 115 | + } |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + protected function isProtectedPage() |
|
| 119 | + { |
|
| 120 | + return false; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Enforces HTTPS on the login form |
|
| 125 | + * |
|
| 126 | + * @return bool |
|
| 127 | + */ |
|
| 128 | + private function enforceHttps() |
|
| 129 | + { |
|
| 130 | + if ($this->getSiteConfiguration()->getUseStrictTransportSecurity() !== false) { |
|
| 131 | + if (WebRequest::isHttps()) { |
|
| 132 | + // Client can clearly use HTTPS, so let's enforce it for all connections. |
|
| 133 | + $this->headerQueue[] = "Strict-Transport-Security: max-age=15768000"; |
|
| 134 | + } |
|
| 135 | + else { |
|
| 136 | + // This is the login form, not the request form. We need protection here. |
|
| 137 | + $this->redirectUrl('https://' . WebRequest::serverName() . WebRequest::requestUri()); |
|
| 138 | + |
|
| 139 | + return false; |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + return true; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + protected abstract function providerSpecificSetup(); |
|
| 147 | + |
|
| 148 | + protected function setupPartial() |
|
| 149 | + { |
|
| 150 | + $database = $this->getDatabase(); |
|
| 151 | + |
|
| 152 | + // default stuff |
|
| 153 | + $this->assign('alternatives', array()); // 'u2f' => array('U2F token'), 'otp' => array('TOTP', 'scratch', 'yubiotp'))); |
|
| 154 | + |
|
| 155 | + // is this stage one? |
|
| 156 | + list($partialId, $partialStage) = WebRequest::getAuthPartialLogin(); |
|
| 157 | + if ($partialStage === null || $partialId === null) { |
|
| 158 | + WebRequest::clearAuthPartialLogin(); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + // Check to see if we have a partial login in progress |
|
| 162 | + $username = null; |
|
| 163 | + if ($partialId !== null) { |
|
| 164 | + // Yes, enforce this username |
|
| 165 | + $this->partialUser = User::getById($partialId, $database); |
|
| 166 | + $username = $this->partialUser->getUsername(); |
|
| 167 | + |
|
| 168 | + $this->setupAlternates($this->partialUser, $partialStage, $database); |
|
| 169 | + } |
|
| 170 | + else { |
|
| 171 | + // No, see if we've preloaded a username |
|
| 172 | + $preloadUsername = WebRequest::getString('tplUsername'); |
|
| 173 | + if ($preloadUsername !== null) { |
|
| 174 | + $username = $preloadUsername; |
|
| 175 | + } |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + if ($partialStage === null) { |
|
| 179 | + $partialStage = 1; |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + $this->assign('partialStage', $partialStage); |
|
| 183 | + $this->assign('username', $username); |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * Redirect the user back to wherever they came from after a successful login |
|
| 188 | + * |
|
| 189 | + * @param User $user |
|
| 190 | + */ |
|
| 191 | + protected function goBackWhenceYouCame(User $user) |
|
| 192 | + { |
|
| 193 | + // Redirect to wherever the user came from |
|
| 194 | + $redirectDestination = WebRequest::clearPostLoginRedirect(); |
|
| 195 | + if ($redirectDestination !== null) { |
|
| 196 | + $this->redirectUrl($redirectDestination); |
|
| 197 | + } |
|
| 198 | + else { |
|
| 199 | + if ($user->isNewUser()) { |
|
| 200 | + // home page isn't allowed, go to preferences instead |
|
| 201 | + $this->redirect('preferences'); |
|
| 202 | + } |
|
| 203 | + else { |
|
| 204 | + // go to the home page |
|
| 205 | + $this->redirect(''); |
|
| 206 | + } |
|
| 207 | + } |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + private function processLoginSuccess(User $user) |
|
| 211 | + { |
|
| 212 | + // Touch force logout |
|
| 213 | + $user->setForceLogout(false); |
|
| 214 | + $user->save(); |
|
| 215 | + |
|
| 216 | + $oauth = new OAuthUserHelper($user, $this->getDatabase(), $this->getOAuthProtocolHelper(), |
|
| 217 | + $this->getSiteConfiguration()); |
|
| 218 | + |
|
| 219 | + if ($oauth->isFullyLinked()) { |
|
| 220 | + try { |
|
| 221 | + // Reload the user's identity ticket. |
|
| 222 | + $oauth->refreshIdentity(); |
|
| 223 | + |
|
| 224 | + // Check for blocks |
|
| 225 | + if ($oauth->getIdentity()->getBlocked()) { |
|
| 226 | + // blocked! |
|
| 227 | + SessionAlert::error("You are currently blocked on-wiki. You will not be able to log in until you are unblocked."); |
|
| 228 | + $this->redirect('login'); |
|
| 229 | + |
|
| 230 | + return; |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | + catch (OAuthException $ex) { |
|
| 234 | + // Oops. Refreshing ticket failed. Force a re-auth. |
|
| 235 | + $authoriseUrl = $oauth->getRequestToken(); |
|
| 236 | + WebRequest::setOAuthPartialLogin($user); |
|
| 237 | + $this->redirectUrl($authoriseUrl); |
|
| 238 | + |
|
| 239 | + return; |
|
| 240 | + } |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + if (($this->getSiteConfiguration()->getEnforceOAuth() && !$oauth->isFullyLinked()) |
|
| 244 | + || $oauth->isPartiallyLinked() |
|
| 245 | + ) { |
|
| 246 | + $authoriseUrl = $oauth->getRequestToken(); |
|
| 247 | + WebRequest::setOAuthPartialLogin($user); |
|
| 248 | + $this->redirectUrl($authoriseUrl); |
|
| 249 | + |
|
| 250 | + return; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + WebRequest::setLoggedInUser($user); |
|
| 254 | + |
|
| 255 | + $this->goBackWhenceYouCame($user); |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + protected abstract function getProviderCredentials(); |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * @param User $user |
|
| 262 | + * @param int $partialStage |
|
| 263 | + * @param PdoDatabase $database |
|
| 264 | + * |
|
| 265 | + * @throws ApplicationLogicException |
|
| 266 | + */ |
|
| 267 | + private function processJumpNextStage(User $user, $partialStage, PdoDatabase $database) |
|
| 268 | + { |
|
| 269 | + WebRequest::setAuthPartialLogin($user->getId(), $partialStage + 1); |
|
| 270 | + |
|
| 271 | + $sql = 'SELECT type FROM credential WHERE user = :user AND factor = :stage AND disabled = 0 ORDER BY priority'; |
|
| 272 | + $statement = $database->prepare($sql); |
|
| 273 | + $statement->execute(array(':user' => $user->getId(), ':stage' => $partialStage + 1)); |
|
| 274 | + $nextStage = $statement->fetchColumn(); |
|
| 275 | + $statement->closeCursor(); |
|
| 276 | + |
|
| 277 | + if (!isset($this->nextPageMap[$nextStage])) { |
|
| 278 | + throw new ApplicationLogicException('Unknown page handler for next authentication stage.'); |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + $this->redirect("login/" . $this->nextPageMap[$nextStage]); |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + private function setupAlternates(User $user, $partialStage, PdoDatabase $database) |
|
| 285 | + { |
|
| 286 | + // get the providers available |
|
| 287 | + $sql = 'SELECT type FROM credential WHERE user = :user AND factor = :stage AND disabled = 0'; |
|
| 288 | + $statement = $database->prepare($sql); |
|
| 289 | + $statement->execute(array(':user' => $user->getId(), ':stage' => $partialStage)); |
|
| 290 | + $alternates = $statement->fetchAll(PDO::FETCH_COLUMN); |
|
| 291 | + |
|
| 292 | + $types = array(); |
|
| 293 | + foreach ($alternates as $item) { |
|
| 294 | + $type = $this->nextPageMap[$item]; |
|
| 295 | + if (!isset($types[$type])) { |
|
| 296 | + $types[$type] = array(); |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + $types[$type][] = $item; |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + $userOptions = array(); |
|
| 303 | + if (get_called_class() === PageOtpLogin::class) { |
|
| 304 | + $userOptions = $this->setupUserOptionsForType($types, 'u2f', $userOptions); |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + if (get_called_class() === PageU2FLogin::class) { |
|
| 308 | + $userOptions = $this->setupUserOptionsForType($types, 'otp', $userOptions); |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + $this->assign('alternatives', $userOptions); |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * @param $types |
|
| 316 | + * @param $type |
|
| 317 | + * @param $userOptions |
|
| 318 | + * |
|
| 319 | + * @return mixed |
|
| 320 | + */ |
|
| 321 | + private function setupUserOptionsForType($types, $type, $userOptions) |
|
| 322 | + { |
|
| 323 | + if (isset($types[$type])) { |
|
| 324 | + $options = $types[$type]; |
|
| 325 | + |
|
| 326 | + array_walk($options, function(&$val) { |
|
| 327 | + $val = $this->names[$val]; |
|
| 328 | + }); |
|
| 329 | + |
|
| 330 | + $userOptions[$type] = $options; |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + return $userOptions; |
|
| 334 | + } |
|
| 335 | 335 | } |
@@ -13,31 +13,31 @@ |
||
| 13 | 13 | |
| 14 | 14 | class PagePasswordLogin extends LoginCredentialPageBase |
| 15 | 15 | { |
| 16 | - protected function providerSpecificSetup() |
|
| 17 | - { |
|
| 18 | - list($partialId, $partialStage) = WebRequest::getAuthPartialLogin(); |
|
| 19 | - |
|
| 20 | - if($partialId !== null && $partialStage > 1) { |
|
| 21 | - $sql = 'SELECT type FROM credential WHERE user = :user AND factor = :stage AND disabled = 0 ORDER BY priority'; |
|
| 22 | - $statement = $this->getDatabase()->prepare($sql); |
|
| 23 | - $statement->execute(array(':user' => $partialId, ':stage' => $partialStage)); |
|
| 24 | - $nextStage = $statement->fetchColumn(); |
|
| 25 | - $statement->closeCursor(); |
|
| 26 | - |
|
| 27 | - $this->redirect("login/" . $this->nextPageMap[$nextStage]); |
|
| 28 | - return; |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - $this->setTemplate('login/password.tpl'); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - protected function getProviderCredentials() |
|
| 35 | - { |
|
| 36 | - $password = WebRequest::postString("password"); |
|
| 37 | - if ($password === null || $password === "") { |
|
| 38 | - throw new ApplicationLogicException("No password specified"); |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - return $password; |
|
| 42 | - } |
|
| 16 | + protected function providerSpecificSetup() |
|
| 17 | + { |
|
| 18 | + list($partialId, $partialStage) = WebRequest::getAuthPartialLogin(); |
|
| 19 | + |
|
| 20 | + if($partialId !== null && $partialStage > 1) { |
|
| 21 | + $sql = 'SELECT type FROM credential WHERE user = :user AND factor = :stage AND disabled = 0 ORDER BY priority'; |
|
| 22 | + $statement = $this->getDatabase()->prepare($sql); |
|
| 23 | + $statement->execute(array(':user' => $partialId, ':stage' => $partialStage)); |
|
| 24 | + $nextStage = $statement->fetchColumn(); |
|
| 25 | + $statement->closeCursor(); |
|
| 26 | + |
|
| 27 | + $this->redirect("login/" . $this->nextPageMap[$nextStage]); |
|
| 28 | + return; |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + $this->setTemplate('login/password.tpl'); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + protected function getProviderCredentials() |
|
| 35 | + { |
|
| 36 | + $password = WebRequest::postString("password"); |
|
| 37 | + if ($password === null || $password === "") { |
|
| 38 | + throw new ApplicationLogicException("No password specified"); |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + return $password; |
|
| 42 | + } |
|
| 43 | 43 | } |
| 44 | 44 | \ No newline at end of file |
@@ -14,20 +14,20 @@ discard block |
||
| 14 | 14 | |
| 15 | 15 | class PageU2FLogin extends LoginCredentialPageBase |
| 16 | 16 | { |
| 17 | - protected function providerSpecificSetup() |
|
| 18 | - { |
|
| 19 | - $this->assign('showSignIn', false); |
|
| 20 | - $this->setTemplate('login/u2f.tpl'); |
|
| 17 | + protected function providerSpecificSetup() |
|
| 18 | + { |
|
| 19 | + $this->assign('showSignIn', false); |
|
| 20 | + $this->setTemplate('login/u2f.tpl'); |
|
| 21 | 21 | |
| 22 | - if ($this->partialUser === null) { |
|
| 23 | - throw new ApplicationLogicException("U2F cannot be first-stage authentication"); |
|
| 24 | - } |
|
| 22 | + if ($this->partialUser === null) { |
|
| 23 | + throw new ApplicationLogicException("U2F cannot be first-stage authentication"); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - $u2f = new U2FCredentialProvider($this->getDatabase(), $this->getSiteConfiguration()); |
|
| 27 | - $authData = json_encode($u2f->getAuthenticationData($this->partialUser)); |
|
| 26 | + $u2f = new U2FCredentialProvider($this->getDatabase(), $this->getSiteConfiguration()); |
|
| 27 | + $authData = json_encode($u2f->getAuthenticationData($this->partialUser)); |
|
| 28 | 28 | |
| 29 | - $this->addJs('/vendor/yubico/u2flib-server/examples/assets/u2f-api.js'); |
|
| 30 | - $this->setTailScript(<<<JS |
|
| 29 | + $this->addJs('/vendor/yubico/u2flib-server/examples/assets/u2f-api.js'); |
|
| 30 | + $this->setTailScript(<<<JS |
|
| 31 | 31 | var request = ${authData}; |
| 32 | 32 | console.log("starting sign"); |
| 33 | 33 | u2f.sign(request, function(data) { |
@@ -44,19 +44,19 @@ discard block |
||
| 44 | 44 | form.submit(); |
| 45 | 45 | }); |
| 46 | 46 | JS |
| 47 | - ); |
|
| 47 | + ); |
|
| 48 | 48 | |
| 49 | - } |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - protected function getProviderCredentials() |
|
| 52 | - { |
|
| 53 | - $authenticate = WebRequest::postString("authenticate"); |
|
| 54 | - $request = WebRequest::postString("request"); |
|
| 51 | + protected function getProviderCredentials() |
|
| 52 | + { |
|
| 53 | + $authenticate = WebRequest::postString("authenticate"); |
|
| 54 | + $request = WebRequest::postString("request"); |
|
| 55 | 55 | |
| 56 | - if ($authenticate === null || $authenticate === "" || $request === null || $request === "") { |
|
| 57 | - throw new ApplicationLogicException("No authentication specified"); |
|
| 58 | - } |
|
| 56 | + if ($authenticate === null || $authenticate === "" || $request === null || $request === "") { |
|
| 57 | + throw new ApplicationLogicException("No authentication specified"); |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - return array(json_decode($authenticate), json_decode($request), 'u2f'); |
|
| 61 | - } |
|
| 60 | + return array(json_decode($authenticate), json_decode($request), 'u2f'); |
|
| 61 | + } |
|
| 62 | 62 | } |
| 63 | 63 | \ No newline at end of file |
@@ -13,18 +13,18 @@ |
||
| 13 | 13 | |
| 14 | 14 | class PageOtpLogin extends LoginCredentialPageBase |
| 15 | 15 | { |
| 16 | - protected function providerSpecificSetup() |
|
| 17 | - { |
|
| 18 | - $this->setTemplate('login/otp.tpl'); |
|
| 19 | - } |
|
| 16 | + protected function providerSpecificSetup() |
|
| 17 | + { |
|
| 18 | + $this->setTemplate('login/otp.tpl'); |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | - protected function getProviderCredentials() |
|
| 22 | - { |
|
| 23 | - $otp = WebRequest::postString("otp"); |
|
| 24 | - if ($otp === null || $otp === "") { |
|
| 25 | - throw new ApplicationLogicException("No one-time code specified"); |
|
| 26 | - } |
|
| 21 | + protected function getProviderCredentials() |
|
| 22 | + { |
|
| 23 | + $otp = WebRequest::postString("otp"); |
|
| 24 | + if ($otp === null || $otp === "") { |
|
| 25 | + throw new ApplicationLogicException("No one-time code specified"); |
|
| 26 | + } |
|
| 27 | 27 | |
| 28 | - return $otp; |
|
| 29 | - } |
|
| 28 | + return $otp; |
|
| 29 | + } |
|
| 30 | 30 | } |
| 31 | 31 | \ No newline at end of file |