1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
<?php defined("SYSPATH") or die("No direct script access.");
/**
* Gallery - a web based photo album viewer and editor
* Copyright (C) 2000-2008 Bharat Mediratta
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
class Auth_Basic_Driver implements Auth_Driver {
// Configuration
protected $_config;
/**
* Instantiate the Driver and initialize it's configuration.
*/
public function __construct($config = array()) {
// Load config
$config += Kohana::config('basic_auth');
// Clean up the salt pattern and split it into an array
$config['salt_pattern'] = preg_split('/,\s*/', $config['salt_pattern']);
$this->_config = $config;
Kohana::log('debug', 'Auth_Basic_Driver Library initialized');
}
/**
* @see Auth_Driver::set_user_password
*
* @param int $user_id
* @param string $password
* @return void
*/
public function set_user_password($user_id, $password_text) {
$password = ORM::factory("password")->where('user_id', $user_id)->find();
$password->password = $this->_hash_password($password_text);
if (empty($password->user_id)) {
$password->user_id = $user_id;
}
$password->save();
}
/**
* Validates a user id password combination.
*
* @param int user_id
* @param string password
* @return boolean
*/
public function is_valid_password($user_id, $password_text) {
$password = ORM::factory("password")
->where('user_id', $user_id)
->find();
if ($password->loaded != true) {
return false;
}
// Get the salt from the stored password
$salt = $this->_find_salt($password->password);
$hashed = $this->_hash_password($password_text, $salt);
return $hashed === $password->password;
}
/**
* Creates a hashed password from a plaintext password, inserting salt
* based on the configured salt pattern.
*
* @param string plaintext password
* @return string hashed password string
*/
private function _hash_password($password, $salt = FALSE) {
if ($salt === FALSE) {
// Create a salt seed, same length as the number of offsets in the pattern
$salt = substr($this->_hash(uniqid(NULL, TRUE)), 0, count($this->_config['salt_pattern']));
}
// Password hash that the salt will be inserted into
$hash = $this->_hash($salt . $password);
// Change salt to an array
$salt = str_split($salt, 1);
// Returned password
$password = '';
// Used to calculate the length of splits
$last_offset = 0;
foreach ($this->_config['salt_pattern'] as $offset) {
// Split a new part of the hash off
$part = substr($hash, 0, $offset - $last_offset);
// Cut the current part out of the hash
$hash = substr($hash, $offset - $last_offset);
// Add the part to the password, appending the salt character
$password .= $part . array_shift($salt);
// Set the last offset to the current offset
$last_offset = $offset;
}
// Return the password, with the remaining hash appended
return $password . $hash;
}
/**
* Perform a hash, using the configured method.
*
* @param string string to hash
* @return string
*/
private function _hash($str) {
return hash($this->_config['hash_method'], $str);
}
/**
* Finds the salt from a password, based on the configured salt pattern.
*
* @param string hashed password
* @return string
*/
private function _find_salt($password) {
$salt = '';
foreach ($this->_config['salt_pattern'] as $i => $offset) {
// Find salt characters... take a good long look..
$salt .= substr($password, $offset + $i, 1);
}
return $salt;
}
}
|