|
1 | 1 | <?php
|
2 | 2 |
|
3 |
| -function login($username, $password) { |
4 |
| -$dbconn = pg_connect("host=localhost port=5432 dbname=tournesol_staging user=tournesol password=Tetu8raiwieGh3I"); |
5 |
| -$auth_table = 'auth_user'; |
6 |
| - |
7 |
| -$result = pg_query($dbconn, "select * from $auth_table where username='$username'"); |
8 |
| - |
9 |
| -if(!$result) { |
10 |
| -return false; |
11 |
| -} |
12 |
| - |
13 |
| -$n_rows = pg_num_rows($result); |
14 |
| - |
15 |
| -if($n_rows != 1) { |
16 |
| -return false; |
17 |
| -} |
18 |
| - |
19 |
| -echo "$n_rows rows\n"; |
20 |
| - |
21 |
| -$rs = pg_fetch_assoc($result); |
22 |
| - |
23 |
| -var_dump($rs); |
24 |
| - |
25 |
| -$password_hashed_db = $rs['password']; |
26 |
| - |
27 |
| -var_dump($password_hashed_db); |
28 |
| - |
29 |
| -list($algo_db, $iterations_db, $salt_db, $hash_db) = explode('$', $password_hashed_db); |
30 |
| -list($algo_db_1, $algo_db_2) = explode('_', $algo_db); |
31 |
| - |
32 |
| -if($algo_db_1 != 'pbkdf2') { |
33 |
| - echo "Unknown algorithm $algo_db_1"; |
34 |
| - return false; |
| 3 | +require 'postgres_django_auth.php'; |
| 4 | + |
| 5 | +// command-line interface |
| 6 | +if ('cli' === PHP_SAPI) { |
| 7 | + $options_default = [ |
| 8 | + "host" => "localhost", |
| 9 | + "port" => 5432, |
| 10 | + "dbname" => "tournesol", |
| 11 | + "db_username" => "tournesol", |
| 12 | + "auth_table" => "auth_user", |
| 13 | + "password" => "", |
| 14 | + "db_password" => "", |
| 15 | + "username" => "", |
| 16 | + ]; |
| 17 | + $options = getopt('', ["username:", "password:", "db_password:", |
| 18 | + "host::", "port::", "dbname::", |
| 19 | + "db_username::", "auth_table::"]); |
| 20 | + $options = $options + $options_default; |
| 21 | + |
| 22 | + if(!$options['username'] || !$options['password'] || !$options['db_password']) { |
| 23 | + $fn = $_SERVER['SCRIPT_FILENAME']; |
| 24 | + echo "Usage: php $fn --username=LOGIN_USERNAME --password=LOGIN_PASSWORD "; |
| 25 | + echo "--db_password=DB_PASSWORD\n"; |
| 26 | + echo " [--host=DB_HOST] [--port=DB_PORT] [--dbname=DB_NAME] "; |
| 27 | + echo " [--db_username=DB_USERNAME] [--auth_table=AUTH_TABLE]"; |
| 28 | + echo "\n"; |
| 29 | + exit(1); |
| 30 | + } |
| 31 | + |
| 32 | + // logging in |
| 33 | + try { |
| 34 | + $result = call_user_func_array("login_django_postgres", $options); |
| 35 | + } catch (Exception $e) { |
| 36 | + $result = false; |
| 37 | + $error = $e->getMessage(); |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + if($result) { |
| 42 | + echo "Login successful\n"; |
| 43 | + exit(0); |
| 44 | + } |
| 45 | + else { |
| 46 | + echo "Login failed: $error\n"; |
| 47 | + exit(1); |
| 48 | + } |
35 | 49 | }
|
36 | 50 |
|
37 |
| -var_dump($algo_db, $iterations_db, $salt_db); |
38 |
| - |
39 |
| -$supplied_hash_password = base64_encode(hash_pbkdf2($algo_db_2, $password, $salt_db, $iterations_db, 32, true)); |
40 |
| - |
41 |
| -echo "$supplied_hash_password\n"; |
42 |
| - |
43 |
| - |
44 |
| -return $supplied_hash_password == $hash_db; |
45 |
| - |
46 |
| - |
47 |
| -} |
48 |
| - |
49 |
| -$result = login('harry_potter', 'Iefaegh0Yohdah6k'); |
50 |
| -var_dump($result); |
51 |
| -echo "res=$result\n"; |
52 |
| - |
53 | 51 | ?>
|
0 commit comments