summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authordan <[email protected]>2024-04-28 12:12:03 -0400
committerdan <[email protected]>2024-04-28 12:12:03 -0400
commit18ae691429af89d9949d6e30650d70e3ab4239cb (patch)
treedd7baee36f6924dd6210de552fcce2c3fe9ad69c
parent4a0316c7970c59937d73744e482acaa524a6741b (diff)
download54-18ae691429af89d9949d6e30650d70e3ab4239cb.tar.gz
54-18ae691429af89d9949d6e30650d70e3ab4239cb.tar.bz2
54-18ae691429af89d9949d6e30650d70e3ab4239cb.zip
feat: users are stored in db
-rw-r--r--add-user.scm28
-rw-r--r--main.scm20
-rw-r--r--makefile2
3 files changed, 46 insertions, 4 deletions
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