HTTP digest authentication

The digest http authentication does not allow to use a password storing
systems, that do not successively allow to retrieve it, even if hashed
This commit is contained in:
paolo
2016-04-14 15:24:53 +02:00
parent 64de1ed4f4
commit aec2e7b5ac
17 changed files with 311 additions and 4195 deletions
+1 -5
View File
@@ -60,11 +60,7 @@ $autoload['packages'] = array();
*/
//$autoload['libraries'] = array();
$autoload['libraries'] = array('session', 'Fhcauth');
//$autoload['libraries'] = array();
$autoload['libraries'] = array('session');
$autoload['libraries'] = array('Session', 'FHC_Auth');
/*
| -------------------------------------------------------------------
+7 -5
View File
@@ -139,14 +139,16 @@ $config['auth_source'] = 'library';
| In other cases override the function _perform_library_auth in your controller
|
| For digest authentication the library function should return already a stored
| md5(username:restrealm:password) for that username
|
| e.g: md5('admin:REST API:1234') = '1e957ebc35631ab22d5bd6526bd14ea2'
| password for that username, even if it is hashed
|
*/
$config['auth_library_class'] = 'fhcauth';
$config['auth_library_class'] = 'FHC_Auth';
// rest_auth is basic
//$config['auth_library_function'] = 'auth';
$config['auth_library_function'] = 'auth_digest';
// rest_auth is digest
$config['auth_library_function'] = 'digestAuthentication';
/*
|--------------------------------------------------------------------------
+2 -2
View File
@@ -43,7 +43,7 @@ class APIAuth extends APIv1_Controller
// Load helper
//$this->load->helper('fhcauth');
$this->load->library('session');
$this->load->library('Fhcauth');
$this->load->library('FHC_Auth');
}
/**
@@ -62,7 +62,7 @@ class APIAuth extends APIv1_Controller
$username = urldecode($this->get('username'));
$password = urldecode($this->get('password'));
$account = $this->fhcauth->auth($username, $password);
$account = $this->fhc_auth->auth($username, $password);
// perform login checks
if (!$account)
+145 -142
View File
@@ -15,7 +15,7 @@
defined('BASEPATH') || exit('No direct script access allowed');
class Person extends REST_Controller
class Person extends APIv1_Controller
{
//public $session;
@@ -24,181 +24,184 @@ class Person extends REST_Controller
*/
public function __construct()
{
parent::__construct();
parent::__construct();
$this->load->model('person/person_model');
$this->load->model('person/person_model');
}
public function person_get()
{
//if (!$this->session_model->validate($this->get('session_id'), $this->get('device_id')))
// $this->response(array(['success' => false, 'message' => 'access denied']), REST_Controller::HTTP_UNAUTHORIZED);
//if (!$this->session_model->validate($this->get('session_id'), $this->get('device_id')))
// $this->response(array(['success' => false, 'message' => 'access denied']), REST_Controller::HTTP_UNAUTHORIZED);
$code = $this->get('code');
$email = $this->get('email');
$person_id = $this->get('person_id');
$code = $this->get('code');
$email = $this->get('email');
$person_id = $this->get('person_id');
if ((!is_null($code)) && (!is_null($email)))
{
$result = $this->person_model->getPersonByCodeAndEmail($code, $email);
}
elseif (!is_null($person_id))
{
$result = $this->person_model->getPerson($person_id);
}
else
{
$result = $this->person_model->getPerson();
}
if ((!is_null($code)) && (!is_null($email)))
{
$result = $this->person_model->getPersonByCodeAndEmail($code, $email);
}
elseif (! is_null($code))
{
$result = $this->person_model->getPersonByCode($code, $email);
}
elseif (!is_null($person_id))
{
$result = $this->person_model->getPerson($person_id);
}
else
{
$result = $this->person_model->getPerson();
}
if (empty($result))
{
$payload = [
'success' => false,
'message' => 'Person not found'
];
$httpstatus = REST_Controller::HTTP_OK;
}
else
{
// return all available persons
$payload = [
'success' => true,
'message' => 'Persons found'
];
$payload['data'] = $result;
$httpstatus = REST_Controller::HTTP_OK;
}
if (empty($result))
{
$payload = [
'success' => false,
'message' => 'Person not found'
];
$httpstatus = REST_Controller::HTTP_OK;
}
else
{
// return all available persons
$payload = [
'success' => true,
'message' => 'Persons found'
];
$payload['data'] = $result;
$httpstatus = REST_Controller::HTTP_OK;
}
// Set the response and exit
$this->response($payload, $httpstatus);
// Set the response and exit
$this->response($payload, $httpstatus);
}
public function personFromCode_post()
{
$code = $this->post('code');
$email = $this->post('email');
$person_id = $this->post('person_id');
$code = $this->post('code');
$email = $this->post('email');
$person_id = $this->post('person_id');
if ((!is_null($code)) && (!is_null($email)))
{
$result = $this->person_model->getPersonByCodeAndEmail($code, $email);
}
elseif (!is_null($person_id))
{
$result = $this->person_model->getPerson($person_id);
}
else
{
$result = $this->person_model->getPerson();
}
if ((!is_null($code)) && (!is_null($email)))
{
$result = $this->person_model->getPersonByCodeAndEmail($code, $email);
}
elseif (!is_null($person_id))
{
$result = $this->person_model->getPerson($person_id);
}
else
{
$result = $this->person_model->getPerson();
}
if (empty($result))
{
$payload = [
'success' => false,
'message' => 'Person not found'
];
$httpstatus = REST_Controller::HTTP_OK;
}
else
{
// return all available persons
$payload = [
'success' => true,
'message' => 'Persons found'
];
$payload['data'] = $result;
$httpstatus = REST_Controller::HTTP_OK;
}
if (empty($result))
{
$payload = [
'success' => false,
'message' => 'Person not found'
];
$httpstatus = REST_Controller::HTTP_OK;
}
else
{
// return all available persons
$payload = [
'success' => true,
'message' => 'Persons found'
];
$payload['data'] = $result;
$httpstatus = REST_Controller::HTTP_OK;
}
// Set the response and exit
$this->response($payload, $httpstatus);
// Set the response and exit
$this->response($payload, $httpstatus);
}
public function person_post()
{
$result = $this->person_model->savePerson($this->post());
if($result != FALSE)
{
$httpstatus = REST_Controller::HTTP_OK;
$payload = [
'success' => true,
'message' => 'Person saved.'
];
$payload['data'] = $result;
}
else
{
$payload = [
'success' => false,
'message' => 'Could not save person.'
];
$httpstatus = REST_Controller::HTTP_OK;
}
$result = $this->person_model->savePerson($this->post());
if($result != FALSE)
{
$httpstatus = REST_Controller::HTTP_OK;
$payload = [
'success' => true,
'message' => 'Person saved.'
];
$payload['data'] = $result;
}
else
{
$payload = [
'success' => false,
'message' => 'Could not save person.'
];
$httpstatus = REST_Controller::HTTP_OK;
}
$this->response($payload, $httpstatus);
$this->response($payload, $httpstatus);
}
public function personUpdate_post()
{
$result = $this->person_model->updatePerson($this->post());
if($result != FALSE)
{
$httpstatus = REST_Controller::HTTP_OK;
$payload = [
'success' => true,
'message' => 'Person updated.'
];
$payload['data'] = $result;
}
else
{
$payload = [
'success' => false,
'message' => 'Could not update person.'
];
$httpstatus = REST_Controller::HTTP_OK;
}
//
$this->response($payload, $httpstatus);
$result = $this->person_model->updatePerson($this->post());
if($result != FALSE)
{
$httpstatus = REST_Controller::HTTP_OK;
$payload = [
'success' => true,
'message' => 'Person updated.'
];
$payload['data'] = $result;
}
else
{
$payload = [
'success' => false,
'message' => 'Could not update person.'
];
$httpstatus = REST_Controller::HTTP_OK;
}
//
$this->response($payload, $httpstatus);
}
public function checkBewerbung_get()
{
$result = $this->person_model->checkBewerbung($this->get("email"),$this->get("studiensemester_kurzbz"));
$httpstatus = REST_Controller::HTTP_OK;
$payload = [
'success' => true,
'message' => 'Bewerbung exists.'
];
$payload['data'] = $result;
$result = $this->person_model->checkBewerbung($this->get("email"),$this->get("studiensemester_kurzbz"));
$httpstatus = REST_Controller::HTTP_OK;
$payload = [
'success' => true,
'message' => 'Bewerbung exists.'
];
$payload['data'] = $result;
$this->response($payload, $httpstatus);
$this->response($payload, $httpstatus);
}
public function checkZugangscodePerson_get()
{
$result = $this->person_model->checkZugangscodePerson($this->get("code"));
$httpstatus = REST_Controller::HTTP_OK;
if(!empty($result))
{
$payload = [
'success' => true,
'message' => 'Zugangscode exists.'
];
$payload['data'] = $result;
}
else
{
$payload = [
'success' => false,
'message' => 'Zugangscode does not exist.'
];
$httpstatus = REST_Controller::HTTP_OK;
}
$result = $this->person_model->checkZugangscodePerson($this->get("code"));
$httpstatus = REST_Controller::HTTP_OK;
if(!empty($result))
{
$payload = [
'success' => true,
'message' => 'Zugangscode exists.'
];
$payload['data'] = $result;
}
else
{
$payload = [
'success' => false,
'message' => 'Zugangscode does not exist.'
];
$httpstatus = REST_Controller::HTTP_OK;
}
$this->response($payload, $httpstatus);
$this->response($payload, $httpstatus);
}
}
@@ -36,9 +36,14 @@ class Person extends APIv1_Controller
// $this->response(array(['success' => false, 'message' => 'access denied']), REST_Controller::HTTP_UNAUTHORIZED);
$code = $this->get('code');
$email = $this->get('email');
$person_id = $this->get('person_id');
if (! is_null($code))
if ((!is_null($code)) && (!is_null($email)))
{
$result = $this->person_model->getPersonByCodeAndEmail($code, $email);
}
elseif (! is_null($code))
{
$result = $this->person_model->getPersonByCode($code);
}
+65
View File
@@ -0,0 +1,65 @@
<?php
/**
* FH-Complete
*
* @package FHC-Helper
* @author FHC-Team
* @copyright Copyright (c) 2016 fhcomplete.org
* @license GPLv3
* @link https://fhcomplete.org
* @since Version 1.0.0
* @filesource
*/
defined('BASEPATH') OR exit('No direct script access allowed');
require_once FCPATH.'include/authentication.class.php';
require_once FCPATH.'include/AddonAuthentication.php';
/**
* FHC-Auth Helpers
*
* @package FH-Complete
* @subpackage Helpers
* @category Helpers
* @author FHC-Team
* @link http://fhcomplete.org/user_guide/helpers/fhcauth_helper.html
*/
// ------------------------------------------------------------------------
class FHC_Auth
{
/**
* Auth Username, Password over FH-Complete
*
* @param string $username
* @param string $password
* @return bool
*/
function auth($username, $password)
{
$auth = new authentication();
if ($auth->checkpassword($username, $password))
{
//echo 'Auth-Method-True';
return true;
}
else
{
//echo 'Auth-Method-False';
return false;
}
}
/**
* Get the md5 hashed password by the addon username
*
* @param string $username addon username
* @return string md5 hashed string
*/
public function digestAuthentication($username)
{
$aam = new AddonAuthentication();
return md5($aam->getPasswordByUsername($username));
}
}
+9 -2
View File
@@ -1978,12 +1978,19 @@ abstract class REST_Controller extends CI_Controller {
preg_match_all('@(username|nonce|uri|nc|cnonce|qop|response)=[\'"]?([^\'",]+)@', $digest_string, $matches);
$digest = (empty($matches[1]) || empty($matches[2])) ? [] : array_combine($matches[1], $matches[2]);
// For digest authentication the library function should return already stored md5(username:restrealm:password) for that username @see rest.php::auth_library_function config
// For digest authentication the library function should return
// already stored password for that username, even if it is hashed
$username = $this->_check_login($digest['username'], TRUE);
if (array_key_exists('username', $digest) === FALSE || $username === FALSE)
// If there no password
if (array_key_exists('username', $digest) === FALSE || $username === FALSE || $username === NULL)
{
$this->_force_login($unique_id);
}
// If the password was found for this username, generete the string md5('USERNAME:REALM:PASSWORD')
else
{
$username = md5($digest['username'].":".$this->config->item('rest_realm').":".$username);
}
$md5 = md5(strtoupper($this->request->method) . ':' . $digest['uri']);
$valid_response = md5($username . ':' . $digest['nonce'] . ':' . $digest['nc'] . ':' . $digest['cnonce'] . ':' . $digest['qop'] . ':' . $md5);
+9 -2
View File
@@ -30,8 +30,15 @@ class Person_model extends DB_Model
->where("k.kontakt", $email);
return $this->db->get()->result_object();
// $query = $this->db->get_where('public.tbl_person p ', array('zugangscode' => $code));
// return $query->result_object();
// }
}
public function getPersonByCode($code)
{
// if ($this->fhc_db_acl->bb->isBerechtigt('person', 'suid'))
// {
$query = $this->db->get_where('public.tbl_person', array('zugangscode' => $code));
return $query->result_object();
// }
}