Public archive for the Return to Ritherdon project.
https://www.nicolaellisandritherdon.com
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
91 lines
2.8 KiB
91 lines
2.8 KiB
2 years ago
|
(defpackage #:auth
|
||
2 years ago
|
(:use #:cl
|
||
|
#:hermetic
|
||
|
#:sxql
|
||
|
;; #:datafly
|
||
|
#:ningle
|
||
|
#:mito
|
||
|
#:app-constants
|
||
|
#:user)
|
||
|
(:import-from #:ritherdon-archive.db
|
||
|
#:connection-settings
|
||
|
#:db
|
||
|
#:with-connection)
|
||
|
(:export #:csrf-token
|
||
|
#:get-user-roles
|
||
|
#:get-current-user
|
||
2 years ago
|
#:get-user-id
|
||
|
#:flash-gethash
|
||
|
#:auth-user-data))
|
||
2 years ago
|
|
||
2 years ago
|
(in-package #:auth)
|
||
2 years ago
|
|
||
|
(defun csrf-token ()
|
||
|
"Cross-Site Request Forgery (CSRF) token."
|
||
|
(cdr (assoc "lack.session"
|
||
|
(lack.request:request-cookies ningle:*request*)
|
||
|
:test #'string=)))
|
||
|
|
||
|
(hermetic:setup
|
||
|
;; #' is needed. (hermetic:roles) generates infinite-loop when called
|
||
|
;; otherwise -- 'roles' called in other parts of code-base.
|
||
|
;; #' is shorthand for the 'function' operator (returns the function
|
||
|
;; object associated with the name of the function which is supplied
|
||
|
;; as an argument. Keep forgetting that.
|
||
|
:user-p #'(lambda (username)
|
||
|
(with-connection (db)
|
||
2 years ago
|
(mito:find-dao 'user::user :username username)))
|
||
2 years ago
|
:user-pass #'(lambda (username)
|
||
|
(user::password-of
|
||
|
(with-connection (db)
|
||
2 years ago
|
(mito:find-dao 'user::user :username username))))
|
||
2 years ago
|
:user-roles #'(lambda (username)
|
||
|
(cons :logged-in
|
||
|
(let ((user (with-connection (db)
|
||
|
(mito:find-dao
|
||
2 years ago
|
'user::user :username username))))
|
||
2 years ago
|
(and user
|
||
|
(= (user::is-administrator-p user) app-constants:+true+)
|
||
|
'(:administrator)))))
|
||
2 years ago
|
:session ningle:*session*
|
||
2 years ago
|
:denied (constantly '(400 (:content-type "text/plain") ("Authentication denied"))))
|
||
|
|
||
2 years ago
|
;; TODO: MOVE GET-USER OUT OF AUTH PACKAGE (NOT APPLICABLE HERE))
|
||
|
(defun get-user (username)
|
||
|
"Returns a `USER' profile from the database."
|
||
|
(with-connection (db)
|
||
|
(mito:find-dao 'user :username username)))
|
||
|
|
||
2 years ago
|
(defun get-current-user()
|
||
|
"Returns the currently logged in user from the browser session."
|
||
|
(with-connection (db)
|
||
2 years ago
|
(mito:find-dao 'user :id (gethash :id ningle:*session*))))
|
||
2 years ago
|
|
||
2 years ago
|
(defun auth-user-data ()
|
||
|
"Get usual session data for logged in `USER'."
|
||
|
`(:token ,(auth:csrf-token)
|
||
|
:roles ,(auth:get-user-roles)
|
||
|
:user ,(auth:get-current-user)))
|
||
2 years ago
|
|
||
2 years ago
|
;; TODO: MOVE GET-USER-ID OUT OF AUTH PACKAGE (NOT APPLICABLE HERE))
|
||
2 years ago
|
(defun get-user-id (username)
|
||
|
"Returns the Id. number of the specified `USERNAME' in the database."
|
||
|
(with-connection (db)
|
||
|
(mito:object-id
|
||
|
(mito:find-dao 'user :username username))))
|
||
|
|
||
|
(defun get-user-roles()
|
||
|
"Returns a list of roles the current user has assigned to them.
|
||
|
This is mostly to check if the user is logged-in or has administration
|
||
|
privileges. You can then create if-blocks in the HTML templates and
|
||
|
control what the user can and cannot see or do."
|
||
|
(loop :for role :in (hermetic:roles)
|
||
|
:collect role
|
||
|
:collect t))
|
||
2 years ago
|
|
||
|
(defun flash-gethash (key table)
|
||
|
"Clears out the session hash."
|
||
|
(let ((value (gethash key table)))
|
||
|
(remhash key table)
|
||
|
value))
|