pub / rememori

Simple file-based bookmarking and notes application
git clone https://src.jayvii.de/pub/rememori.git
Home | Log | Files | Exports | Refs | README | RSS

commit 6683d9df4a429ce479b4d7f7b8a3df9ef13e3dcb
parent a2cd9bde6f974bac3d36df6b19a97e0da6d44040
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date:   Tue, 29 Oct 2024 21:22:13 +0100

feat: store tokens in json file and allow multiple tokens

Diffstat:
Mlib/users.php | 80++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
Amisc/add_token_for_user.php | 19+++++++++++++++++++
2 files changed, 86 insertions(+), 13 deletions(-)

diff --git a/lib/users.php b/lib/users.php @@ -40,18 +40,19 @@ function auth_user( } /* read token file of user */ - $tokens_storage = file( - $GLOBALS["data_dir"] . "/" . $user . "/.tokens", - FILE_IGNORE_NEW_LINES - ); - if ($tokens_storage === false) { - $tokens_storage = array(); + $tokens_storage = $GLOBALS["data_dir"] . "/" . $user . "/tokens.json"; + if (is_file($tokens_storage)) { + $tokens_true = json_decode( + file_get_contents($tokens_storage) + ); + } else { + return false; } /* find any matches between given hash and tokens */ $matches = preg_grep( "/^" . $token . "$/", - $tokens_storage + $tokens_true ); /* If matches between given hash and tokens were found, return "true" */ @@ -83,11 +84,12 @@ function create_auth( $token = create_password_hash($pass); /* generate token path */ - $user_path = $GLOBALS["data_dir"] . "/" . $user; - $token_path = $user_path . "/.tokens"; - + $user_path = "./" . $GLOBALS["data_dir"] . "/" . $user; + $tokens_storage = $user_path . "/tokens.json"; + /* Only create new auth, if no previous token file exists already */ - if (file_exists($token_path)) { + if (file_exists($token_storage)) { + // FIXME: create change_auth() exit("Token already exists! Use \"change_auth()\" instead!"); } @@ -102,14 +104,14 @@ function create_auth( /* create file-handle */ $file = fopen( - $token_path, + $tokens_storage, "w" ); /* Write to file */ $bytes = fwrite( $file, - $token + json_encode(array($token)) ); /* close file-hanle */ @@ -120,4 +122,56 @@ function create_auth( } +function add_auth( + string $user, + string $pass +) { + + /* create hash from given password */ + $token = create_password_hash($pass); + + echo "New token: " . $token . PHP_EOL; + + /* generate token path */ + $tokens_storage = "./" . $GLOBALS["data_dir"] . "/" . $user . "/tokens.json"; + + /* get filesize */ + $filesize = filesize($tokens_storage); + + if ($filesize > 0){ + + /* create file-handle */ + $file = fopen( + $tokens_storage, + "r+" + ); + + /* Read from file */ + $tokens = json_decode(fread($file, $filesize)); + + /* add new token to existing tokens */ + array_push($tokens, $token); + + /* go back to start of the file for writing */ + fseek($file, 0); + + /* Write to file */ + $bytes = fwrite( + $file, + json_encode($tokens) + ); + + /* close file-hanle */ + fclose($file); + + /* return "true" on success and "false" otherwise */ + return $bytes !== false; + + } else { + return false; + } + +} + + ?> diff --git a/misc/add_token_for_user.php b/misc/add_token_for_user.php @@ -0,0 +1,19 @@ +<?php + +include("./lib/users.php"); +include("./config/config.php"); + +$user = readline('Enter the username: '); +$pass = readline('Enter new password: '); + +$success = add_auth($user, $pass); + +if ($success) { + echo "New token added for " . $user . PHP_EOL; +} else { + exit("Token could not be added for some reason..."); +} + +?> + +