user.php
4.47 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
* ownCloud - Django Authentification Backend
*
* @author Florian Reinhard
* @copyright 2012 Florian Reinhard <florian.reinhard@googlemail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library 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 AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
require_once 'django_auth/3rdparty/phpsec.crypt.php';
/**
* @brief Class providing django users to ownCloud
* @see http://www.djangoproject.com
*
* Authentification backend to authenticate agains a django webapplication using
* django.contrib.auth.
*/
class OC_USER_DJANGO extends OC_User_Backend {
/**
* @brief Create a new user
* @param $uid The username of the user to create
* @param $password The password of the new user
* @returns true/false
*
* Creates a new user. Basic checking of username is done in OC_User
* itself, not in its subclasses.
*/
public function createUser($uid, $password) {
OCP\Util::writeLog('OC_User_Django', 'Use the django webinterface to create users',3);
return OC_USER_BACKEND_NOT_IMPLEMENTED;
}
/**
* @brief delete a user
* @param $uid The username of the user to delete
* @returns true/false
*
* Deletes a user
*/
public function deleteUser( $uid ) {
OCP\Util::writeLog('OC_User_Django', 'Use the django webinterface to delete users',3);
return OC_USER_BACKEND_NOT_IMPLEMENTED;
}
/**
* @brief Set password
* @param $uid The username
* @param $password The new password
* @returns true/false
*
* Change the password of a user
*/
public function setPassword($uid, $password) {
OCP\Util::writeLog('OC_User_Django', 'Use the django webinterface to change passwords',3);
return OC_USER_BACKEND_NOT_IMPLEMENTED;
}
/**
* @brief Helper function for checkPassword
* @param $str The String to be searched
* @param $sub The String to be found
* @returns true/false
*/
private function beginsWith($str,$sub) {
return ( substr( $str, 0, strlen( $sub ) ) === $sub );
}
/**
* @brief Check if the password is correct
* @param $uid The username
* @param $password The password
* @returns true/false
*
* Check if the password is correct without logging in the user
*/
public function checkPassword($uid, $password) {
$query = OCP\DB::prepare( 'SELECT username, password FROM auth_user WHERE username = ?' );
$result = $query->execute( array( $uid));
$row = $result->fetchRow();
if ($row) {
$storedHash=$row['password'];
if (self::beginsWith($storedHash, 'sha1')) {
$chunks = preg_split('/\$/', $storedHash,3);
$salt = $chunks[1];
$hash = $chunks[2];
if (sha1($salt.$password) === $hash)
return $uid;
else
return false;
}
elseif (self::beginsWith($storedHash, 'pbkdf2')) {
$chunks = preg_split('/\$/', $storedHash,4);
list($pbkdf, $algorithm) = preg_split('/_/', $chunks[0]);
$iter = $chunks[1];
$salt = $chunks[2];
$hash = $chunks[3];
if ($algorithm === 'sha1') {
$digest_size = 20;
}
elseif ($algorithm === 'sha256') {
$digest_size = 32;
}
else {
return false;
}
if (base64_encode (phpsecCrypt::pbkdf2($password, $salt, $iter, $digest_size, $algorithm)) === $hash) {
return $uid;
}
else {
return false;
}
}
}
else {
return false;
}
}
/**
* @brief Get a list of all users
* @returns array with all active usernames
*
* Get a list of all users.
*/
public function getUsers($search = '', $limit = 10, $offset = 0) {
$query = OCP\DB::prepare( 'SELECT id, username, is_active FROM `auth_user` WHERE is_active=1 ORDER BY username' );
$result = $query->execute();
$users = array();
while ( $row = $result->fetchRow()) {
$users[] = $row['username'];
}
return $users;
}
/**
* @brief check if a user exists
* @param string $uid the username
* @return boolean
*/
public function userExists($uid) {
$query = OCP\DB::prepare( 'SELECT username FROM `auth_user` WHERE username = ? AND is_active=1' );
$result = $query->execute( array( $uid ));
return $result->numRows() > 0;
}
}