1
0
mirror of https://github.com/til-schneider/slim-wiki.git synced 2025-08-07 17:17:05 +02:00

Added login via base authentication

This commit is contained in:
til-schneider
2015-12-23 19:20:29 +01:00
parent d3ae27ca9c
commit 664979eb56
2 changed files with 36 additions and 0 deletions

View File

@@ -13,7 +13,32 @@ class EditorService {
return ($methodName == 'saveArticle' || $methodName == 'createUserConfig');
}
// Returns one of: 'logged-in', 'no-credentials', 'wrong-credentials'
public function getLoginState() {
if (!isset($_SERVER['PHP_AUTH_USER'])) {
return 'no-credentials';
} else {
$userInfo = $this->context->getConfig()['user.' . $_SERVER['PHP_AUTH_USER']];
if (isset($userInfo)) {
$loginHash = hash($userInfo['type'], $_SERVER['PHP_AUTH_PW'] . $userInfo['salt']);
if ($loginHash == $userInfo['hash']) {
return 'logged-in';
}
}
return 'wrong-credentials';
}
}
public function assertLoggedIn() {
if ($this->getLoginState() != 'logged-in') {
throw new Exception('Not logged in');
}
}
public function saveArticle($articleFilename, $markdownText) {
$this->assertLoggedIn();
if (! $this->context->isValidArticleFilename($articleFilename)) {
throw new Exception("Invalid article filename: '$articleFilename'");
}

View File

@@ -79,6 +79,17 @@ class Main {
$mode = 'view';
}
if ($mode == 'edit') {
$loginState = $this->context->getEditorService()->getLoginState();
if ($loginState != 'logged-in') {
$wikiName = $this->context->getConfig()['wikiName'];
header('WWW-Authenticate: Basic realm="'.$wikiName.'"');
header('HTTP/1.0 401 Unauthorized');
$mode = 'view';
}
}
$articleFilename = $this->getArticleFilename($requestPathArray);
if ($articleFilename == null) {
header('HTTP/1.0 404 Not Found');