diff options
| -rw-r--r-- | add-user.scm | 28 | ||||
| -rw-r--r-- | main.scm | 20 | ||||
| -rw-r--r-- | makefile | 2 | 
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) @@ -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)))) @@ -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 | 
