From 18ae691429af89d9949d6e30650d70e3ab4239cb Mon Sep 17 00:00:00 2001 From: dan Date: Sun, 28 Apr 2024 12:12:03 -0400 Subject: feat: users are stored in db --- add-user.scm | 28 ++++++++++++++++++++++++++++ main.scm | 20 ++++++++++++++++---- makefile | 2 ++ 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 add-user.scm diff --git a/add-user.scm b/add-user.scm new file mode 100644 index 0000000..9799bf5 --- /dev/null +++ b/add-user.scm @@ -0,0 +1,28 @@ +(import + scheme (chicken base) + (chicken process-context) + sqlite3 + crypt + srfi-1 +) + +(define params (argv)) +(define username (second params)) +(define password (third params)) +(define password-hash (crypt password)) + +(define db (open-database (or (get-environment-variable "DB_FILE") "/tmp/54itter.db"))) + +(execute db "CREATE TABLE IF NOT EXISTS users ( + user_id TEXT PRIMARY KEY, + pw_hash TEXT, + created_at DATETIME default current_timestamp + )") + +(execute db "INSERT INTO users + (user_id, pw_hash) + VALUES (?, ?)" + username password-hash) +(finalize! db) + +(print "Created user " username) diff --git a/main.scm b/main.scm index a57d8c2..f163267 100644 --- a/main.scm +++ b/main.scm @@ -112,13 +112,25 @@ (define (parent-id p) (fifth p)) (define (children-count p) (sixth p)) -(define users (alist->hash-table `(("example" . ,(crypt "pw"))))) - (define apikeys (make-hash-table)) +(define (get-pw-hash-from-db user-id) + (let ([pw-hash-row (condition-case (first-row db + "SELECT pw_hash + FROM users + WHERE user_id = ? + LIMIT 1" + user-id) + [(exn sqlite3) #f])]) + (and pw-hash-row + (list? pw-hash-row) + (not (sql-null? (car pw-hash-row))) + (car pw-hash-row)) + )) + (define (login username password) - (let ([pw-hash-in-db (hash-table-ref/default users username #f)]) - (and password (string=? (crypt password pw-hash-in-db) pw-hash-in-db) + (let ([pw-hash-in-db (get-pw-hash-from-db username)]) + (and password pw-hash-in-db (string=? (crypt password pw-hash-in-db) pw-hash-in-db) (let ([apikey (number->string (pseudo-random-integer 340282366920938463463374607431768211455))]) (hash-table-set! apikeys apikey username) apikey)))) diff --git a/makefile b/makefile index b840280..52c16cc 100644 --- a/makefile +++ b/makefile @@ -11,7 +11,9 @@ build: ./main.scm build-cpp -L /lib/libexiv2.so \ -L /usr/lib/libstdc++.so.6 \ -C -Ilib/ + chicken-csc -static add-user.scm -L -lcrypt -L -lsqlite3 mv ./main ./build/main + mv ./add-user ./build/add-user build-cpp: g++ -g -c lib/exif_wrapper.cpp -o lib/exif_wrapper.o # g++ -static -g -c lib/exif_wrapper.cpp -o lib/exif_wrapper.o -- cgit v1.2.3