Switch to HTTP Basic Auth

Resolves T115
This commit is contained in:
Marcel Kapfer (mmk2410) 2016-05-17 22:36:26 +02:00
parent 42f1992c97
commit 5e45579ef1
5 changed files with 19 additions and 88 deletions

View file

@ -1,52 +0,0 @@
<?php
/**
* PHP Version 7
*
* Authentication Helper Class
*
* @category Authentication
* @package Rbe
* @author Marcel Kapfer (mmk2410) <marcelmichaelkapfer@yahoo.co.nz>
* @license MIT License
* @link http://marcel-kapfer.de/rangitaki
*/
namespace mmk2410\rbe\digestAuth;
/**
* PHP Version 7
*
* Authentication Helper Class
*
* @category Authentication
* @package Rbe
* @author Marcel Kapfer (mmk2410) <marcelmichaelkapfer@yahoo.co.nz>
* @license MIT License
* @link http://marcel-kapfer.de/rangitaki
*/
class DigestAuth
{
/**
* parser for http digest
*
* @param $txt data to parse
*
* @return parsed data or FALSE
*/
public function httpDigestParse($txt)
{
// protect against missing data
$needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);
$data = array();
$keys = implode('|', array_keys($needed_parts));
preg_match_all('@(' . $keys . ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@', $txt, $matches, PREG_SET_ORDER);
foreach ($matches as $m) {
$data[$m[1]] = $m[3] ? $m[3] : $m[4];
unset($needed_parts[$m[1]]);
}
return $needed_parts ? false : $data;
}
}

View file

@ -1,39 +1,22 @@
<?php <?php
// Marcel Kapfer (mmk2410) // Marcel Kapfer (mmk2410)
// License: MIT License // License: MIT License
// api digest auth // HTTP Basic Auth for the API
require 'DigestAuth.php'; $basedir = "../../../";
require '../../ssl.php';
require '../../password.php'; require '../../password.php';
use \mmk2410\rbe\digestAuth\DigestAuth as DigestAuth; if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="RCC API"');
$realm = 'Restricted area';
$users = array($username => $password);
if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
header('HTTP/1.1 401 Unauthorized'); header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Digest realm="'.$realm. echo "Access denied to the RCC API!";
'",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"'); exit;
} elseif ($_SERVER['PHP_AUTH_USER'] != $username ||
die('Access to RCC API not granted'); !password_verify($_SERVER['PHP_AUTH_PW'], $password)) {
} header('HTTP/1.1 401 Unauthorized');
echo "Wrong credentials: Access denied!";
exit;
// analyze the PHP_AUTH_DIGEST variable
if (!($data = DigestAuth::httpDigestParse($_SERVER['PHP_AUTH_DIGEST'])) ||
!isset($users[$data['username']])) {
die('Wrong Credentials!');
}
// generate the valid response
$A1 = md5($data['username'] . ':' . $realm . ':' . $users[$data['username']]);
$A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']);
$valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2);
if ($data['response'] != $valid_response) {
die('Wrong Credentials!');
} }

View file

@ -9,13 +9,13 @@ use \Psr\Http\Message\ResponseInterface as Response;
require '../../../vendor/autoload.php'; require '../../../vendor/autoload.php';
require '../../../res/php/Config.php'; require '../../../res/php/Config.php';
include '../auth/auth.php';
use \mmk2410\rbe\config\Config as Config; use \mmk2410\rbe\config\Config as Config;
$config = new Config("../../../config.yaml", '../../../vendor/autoload.php'); $config = new Config("../../../config.yaml", '../../../vendor/autoload.php');
$settings = $config->getConfig(); $settings = $config->getConfig();
include '../auth/auth.php';
if ($settings["rcc"]["api"] == "on" && $settings["rcc"]["rcc"] == "on") { if ($settings["rcc"]["api"] == "on" && $settings["rcc"]["rcc"] == "on") {
$app = new \Slim\App(); $app = new \Slim\App();

View file

@ -10,13 +10,13 @@ require '../../../vendor/autoload.php';
require '../../../res/php/Config.php'; require '../../../res/php/Config.php';
require '../../../res/php/ArticleGenerator.php'; require '../../../res/php/ArticleGenerator.php';
include '../auth/auth.php';
use \mmk2410\rbe\config\Config as Config; use \mmk2410\rbe\config\Config as Config;
$config = new Config("../../../config.yaml", '../../../vendor/autoload.php'); $config = new Config("../../../config.yaml", '../../../vendor/autoload.php');
$settings = $config->getConfig(); $settings = $config->getConfig();
include '../auth/auth.php';
if ($settings["rcc"]["api"] == "on" && $settings["rcc"]["rcc"] == "on") { if ($settings["rcc"]["api"] == "on" && $settings["rcc"]["rcc"] == "on") {
$app = new \Slim\App(); $app = new \Slim\App();

View file

@ -10,13 +10,13 @@ require '../../../vendor/autoload.php';
require '../../../res/php/Config.php'; require '../../../res/php/Config.php';
require '../../../res/php/ArticleGenerator.php'; require '../../../res/php/ArticleGenerator.php';
include '../auth/auth.php';
use \mmk2410\rbe\config\Config as Config; use \mmk2410\rbe\config\Config as Config;
$config = new Config("../../../config.yaml", '../../../vendor/autoload.php'); $config = new Config("../../../config.yaml", '../../../vendor/autoload.php');
$settings = $config->getConfig(); $settings = $config->getConfig();
include '../auth/auth.php';
if ($settings["rcc"]["api"] == "on" && $settings["rcc"]["rcc"] == "on") { if ($settings["rcc"]["api"] == "on" && $settings["rcc"]["rcc"] == "on") {
$app = new \Slim\App(); $app = new \Slim\App();