Skip to content

Commit 7d8e6ba

Browse files
author
Richard McDaniel
committed
make class
1 parent 0f33f6a commit 7d8e6ba

File tree

7 files changed

+124
-113
lines changed

7 files changed

+124
-113
lines changed

api/application/controllers/cli.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ public function install()
7171
// ... add your custom resources to protect here
7272
// ...
7373

74-
echo "installed\r\n";
74+
if (!defined('PHPUNIT_TEST'))
75+
echo "installed\r\n";
7576
}
7677

7778
public function add($type, $email, $password)
@@ -82,15 +83,17 @@ public function add($type, $email, $password)
8283
{
8384
$this->Users->register($email, $password);
8485

85-
echo "user added\r\n";
86+
if (!defined('PHPUNIT_TEST'))
87+
echo "user added\r\n";
8688
}
8789
else if ($type == 'administrator')
8890
{
8991
$id = $this->Users->register($email, $password);
9092
$acl = new ACL();
9193
$acl->addUserRoles($id, 'administrator');
9294

93-
echo "administrator added\r\n";
95+
if (!defined('PHPUNIT_TEST'))
96+
echo "administrator added\r\n";
9497
}
9598
}
9699

api/application/controllers/resource.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public function table()
66
{
77
$this->form_validation->set_rules('params', 'params', 'required');
88
$this->form_validation->set_rules('role', 'role', 'required');
9-
validate($this, 'resource', 'read', function($token, $output)
9+
return Validation::validate($this, 'resource', 'read', function($token, $output)
1010
{
1111
$params = json_decode(stripslashes($this->input->post('params')));
1212
$role = $this->input->post('role');

api/application/controllers/user.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public function login()
66
{
77
$this->form_validation->set_rules('email', 'email', 'required|valid_email|max_length[256]');
88
$this->form_validation->set_rules('password', 'password', 'required|min_length[8]|max_length[256]');
9-
return validate($this, '', '', function($token, $output)
9+
return Validation::validate($this, '', '', function($token, $output)
1010
{
1111
$email = $this->input->post('email');
1212
$password = $this->input->post('password');
@@ -30,7 +30,7 @@ public function register()
3030
{
3131
$this->form_validation->set_rules('email', 'email', 'required|valid_email|is_unique[users.email]|max_length[256]');
3232
$this->form_validation->set_rules('password', 'password', 'required|min_length[8]|max_length[256]');
33-
return validate($this, '', '', function($token, $output)
33+
return Validation::validate($this, '', '', function($token, $output)
3434
{
3535
$email = $this->input->post('email');
3636
$password = $this->input->post('password');
@@ -43,7 +43,7 @@ public function register()
4343
public function permissions()
4444
{
4545
$this->form_validation->set_rules('resource', 'resource', 'required');
46-
return validate($this, 'user', 'read', function($token, $output)
46+
return Validation::validate($this, 'user', 'read', function($token, $output)
4747
{
4848
$resource = $this->input->post('resource');
4949
$acl = new ACL();

api/application/core/REST_Controller.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function table()
2020
{
2121
$this->__load();
2222
$this->form_validation->set_rules('params', 'params', 'required');
23-
return validate($this, $this->resource, 'read', function($token, $output)
23+
return Validation::validate($this, $this->resource, 'read', function($token, $output)
2424
{
2525
$params = json_decode(stripslashes($this->input->post('params')));
2626
$table = $this->model->table($params);
@@ -35,7 +35,7 @@ public function create()
3535
{
3636
$this->__load();
3737
$this->form_validation->set_rules($this->resource, $this->resource, 'required');
38-
validate($this, $this->resource, 'create', function($token, $output)
38+
return Validation::validate($this, $this->resource, 'create', function($token, $output)
3939
{
4040
$resource = $this->input->post($this->resource);
4141
$this->model->create($resource);
@@ -48,7 +48,7 @@ public function read()
4848
{
4949
$this->__load();
5050
$this->form_validation->set_rules('id', 'id', 'required');
51-
return validate($this, $this->resource, 'read', function($token, $output)
51+
return Validation::validate($this, $this->resource, 'read', function($token, $output)
5252
{
5353
$id = $this->input->post('id');
5454
$resource = $this->model->read($id);
@@ -62,7 +62,7 @@ public function update()
6262
{
6363
$this->__load();
6464
$this->form_validation->set_rules($this->resource, $this->resource, 'required');
65-
return validate($this, $this->resource, 'update', function($token, $output)
65+
return Validation::validate($this, $this->resource, 'update', function($token, $output)
6666
{
6767
$resource = json_decode(stripslashes($this->input->post($this->resource)));
6868
$resource = $this->model->update($resource);
@@ -76,7 +76,7 @@ public function delete()
7676
{
7777
$this->__load();
7878
$this->form_validation->set_rules('id', 'id', 'required');
79-
return validate($this, $this->resource, 'delete', function($token, $output)
79+
return Validation::validate($this, $this->resource, 'delete', function($token, $output)
8080
{
8181
$id = $this->input->post('id');
8282
$this->model->delete($id);

api/application/helpers/password_helper.php

+70-66
Original file line numberDiff line numberDiff line change
@@ -11,75 +11,79 @@
1111
define("HASH_SALT_INDEX", 2);
1212
define("HASH_PBKDF2_INDEX", 3);
1313

14-
function create_hash($password)
15-
{
16-
$salt = base64_encode(mcrypt_create_iv(PBKDF2_SALT_BYTES, MCRYPT_DEV_URANDOM));
17-
return PBKDF2_HASH_ALGORITHM . ":" . PBKDF2_ITERATIONS . ":" . $salt . ":" .
18-
base64_encode(pbkdf2(
19-
PBKDF2_HASH_ALGORITHM,
20-
$password,
21-
$salt,
22-
PBKDF2_ITERATIONS,
23-
PBKDF2_HASH_BYTES,
24-
true
25-
));
26-
}
14+
class Password {
2715

28-
function validate_password($password, $good_hash)
29-
{
30-
$params = explode(":", $good_hash);
31-
if(count($params) < HASH_SECTIONS)
32-
return false;
33-
$pbkdf2 = base64_decode($params[HASH_PBKDF2_INDEX]);
34-
return slow_equals(
35-
$pbkdf2,
36-
pbkdf2(
37-
$params[HASH_ALGORITHM_INDEX],
38-
$password,
39-
$params[HASH_SALT_INDEX],
40-
(int)$params[HASH_ITERATION_INDEX],
41-
strlen($pbkdf2),
42-
true
43-
)
44-
);
45-
}
46-
47-
function slow_equals($a, $b)
48-
{
49-
$diff = strlen($a) ^ strlen($b);
50-
for($i = 0; $i < strlen($a) && $i < strlen($b); $i++)
16+
public static function create_hash($password)
5117
{
52-
$diff |= ord($a[$i]) ^ ord($b[$i]);
18+
$salt = base64_encode(mcrypt_create_iv(PBKDF2_SALT_BYTES, MCRYPT_DEV_URANDOM));
19+
return PBKDF2_HASH_ALGORITHM . ":" . PBKDF2_ITERATIONS . ":" . $salt . ":" .
20+
base64_encode(self::pbkdf2(
21+
PBKDF2_HASH_ALGORITHM,
22+
$password,
23+
$salt,
24+
PBKDF2_ITERATIONS,
25+
PBKDF2_HASH_BYTES,
26+
true
27+
));
5328
}
54-
return $diff === 0;
55-
}
56-
57-
function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false)
58-
{
59-
$algorithm = strtolower($algorithm);
60-
if(!in_array($algorithm, hash_algos(), true))
61-
die('PBKDF2 ERROR: Invalid hash algorithm.');
62-
if($count <= 0 || $key_length <= 0)
63-
die('PBKDF2 ERROR: Invalid parameters.');
64-
65-
$hash_length = strlen(hash($algorithm, "", true));
66-
$block_count = ceil($key_length / $hash_length);
67-
68-
$output = "";
69-
for($i = 1; $i <= $block_count; $i++) {
70-
// $i encoded as 4 bytes, big endian.
71-
$last = $salt . pack("N", $i);
72-
// first iteration
73-
$last = $xorsum = hash_hmac($algorithm, $last, $password, true);
74-
// perform the other $count - 1 iterations
75-
for ($j = 1; $j < $count; $j++) {
76-
$xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
29+
30+
public static function validate_password($password, $good_hash)
31+
{
32+
$params = explode(":", $good_hash);
33+
if(count($params) < HASH_SECTIONS)
34+
return false;
35+
$pbkdf2 = base64_decode($params[HASH_PBKDF2_INDEX]);
36+
return self::slow_equals(
37+
$pbkdf2,
38+
self::pbkdf2(
39+
$params[HASH_ALGORITHM_INDEX],
40+
$password,
41+
$params[HASH_SALT_INDEX],
42+
(int)$params[HASH_ITERATION_INDEX],
43+
strlen($pbkdf2),
44+
true
45+
)
46+
);
47+
}
48+
49+
public static function slow_equals($a, $b)
50+
{
51+
$diff = strlen($a) ^ strlen($b);
52+
for($i = 0; $i < strlen($a) && $i < strlen($b); $i++)
53+
{
54+
$diff |= ord($a[$i]) ^ ord($b[$i]);
55+
}
56+
return $diff === 0;
57+
}
58+
59+
public static function pbkdf2($algorithm, $password, $salt, $count, $key_length, $raw_output = false)
60+
{
61+
$algorithm = strtolower($algorithm);
62+
if(!in_array($algorithm, hash_algos(), true))
63+
die('PBKDF2 ERROR: Invalid hash algorithm.');
64+
if($count <= 0 || $key_length <= 0)
65+
die('PBKDF2 ERROR: Invalid parameters.');
66+
67+
$hash_length = strlen(hash($algorithm, "", true));
68+
$block_count = ceil($key_length / $hash_length);
69+
70+
$output = "";
71+
for($i = 1; $i <= $block_count; $i++) {
72+
// $i encoded as 4 bytes, big endian.
73+
$last = $salt . pack("N", $i);
74+
// first iteration
75+
$last = $xorsum = hash_hmac($algorithm, $last, $password, true);
76+
// perform the other $count - 1 iterations
77+
for ($j = 1; $j < $count; $j++) {
78+
$xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
79+
}
80+
$output .= $xorsum;
7781
}
78-
$output .= $xorsum;
82+
83+
if($raw_output)
84+
return substr($output, 0, $key_length);
85+
else
86+
return bin2hex(substr($output, 0, $key_length));
7987
}
8088

81-
if($raw_output)
82-
return substr($output, 0, $key_length);
83-
else
84-
return bin2hex(substr($output, 0, $key_length));
85-
}
89+
}
+37-33
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,43 @@
11
<?php
22

3-
function validate($context, $class, $function, $callback) {
4-
$output = array();
5-
$output['status'] = false;
6-
$token = false;
7-
if (!empty($class)) {
8-
$token = ACL::authenticate($class, $function);
9-
if ($token == false) {
10-
if (defined('PHPUNIT_TEST')) {
11-
return json_encode(array('output' => $output));
12-
} else {
13-
$context->load->view('json', array('output' => $output));
3+
class Validation {
4+
5+
public static function validate($context, $class, $function, $callback) {
6+
$output = array();
7+
$output['status'] = false;
8+
$token = false;
9+
if (!empty($class)) {
10+
$token = ACL::authenticate($class, $function);
11+
if ($token == false) {
12+
if (defined('PHPUNIT_TEST')) {
13+
return json_encode(array('output' => $output));
14+
} else {
15+
$context->load->view('json', array('output' => $output));
16+
}
17+
}
18+
}
19+
$context->form_validation->set_error_delimiters('', '');
20+
$validated = $context->form_validation->run();
21+
if ($validated)
22+
{
23+
$output = $callback($token, $output);
24+
}
25+
else
26+
{
27+
$output['errors'] = validation_errors();
28+
}
29+
if (array_key_exists('errors', $output)) {
30+
$errors = explode("\n", $output['errors']);
31+
foreach ($errors as $key => $error) {
32+
$errors[$key] = json_decode($error);
1433
}
15-
}
16-
}
17-
$context->form_validation->set_error_delimiters('', '');
18-
$validated = $context->form_validation->run();
19-
if ($validated)
20-
{
21-
$output = $callback($token, $output);
22-
}
23-
else
24-
{
25-
$output['errors'] = validation_errors();
26-
}
27-
if (array_key_exists('errors', $output)) {
28-
$errors = explode("\n", $output['errors']);
29-
foreach ($errors as $key => $error) {
30-
$errors[$key] = json_decode($error);
34+
$output['errors'] = $errors;
35+
}
36+
if (defined('PHPUNIT_TEST')) {
37+
return json_encode(array('output' => $output));
38+
} else {
39+
$context->load->view('json', array('output' => $output));
3140
}
32-
$output['errors'] = $errors;
33-
}
34-
if (defined('PHPUNIT_TEST')) {
35-
return json_encode(array('output' => $output));
36-
} else {
37-
$context->load->view('json', array('output' => $output));
3841
}
42+
3943
}

api/application/models/users.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public function login($email, $password)
1212
if ($query->num_rows() == 1)
1313
{
1414
$result = $query->result();
15-
if (validate_password($password, $result[0]->password))
15+
if (Password::validate_password($password, $result[0]->password))
1616
{
1717
return $result[0]->id;
1818
}
@@ -24,7 +24,7 @@ public function login($email, $password)
2424
public function register($email, $password)
2525
{
2626
$this->db->set('email', $email);
27-
$this->db->set('password', create_hash($password));
27+
$this->db->set('password', Password::create_hash($password));
2828
$this->db->insert('users');
2929
return $this->db->insert_id();
3030
}

0 commit comments

Comments
 (0)