<commit_before>(in-package :bknr.web)

(defclass bknr-authorizer ()
  ())

(defmethod http-request-remote-host ()
  (format *debug-io* "can't determin originating host yet~%")
  #+(or)
  (let ((remote-host (socket:remote-host (request-socket)))
	(forwarded-for (regex-replace
			"^.*?([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*$"
			(header-slot-value :x-forwarded-for)
			"\\1")))
    (when (and forwarded-for
	       (equal "127.0.0.1" (socket:ipaddr-to-dotted remote-host)))
      ;; request is via proxy, use client's ip address
      (setf remote-host (socket:dotted-to-ipaddr forwarded-for)))
    (find-host :create t :ipaddr remote-host)))

(defun session-from-request ()
  "check whether the request has a valid session id in either the bknr-sessionid cookie or query parameter"
  (session-value 'bknr-session))

(define-condition login-failure (serious-condition)
  ()
  (:report (lambda (c s)
             (declare (ignore c))
             (format s "Login failed"))))

(defgeneric find-user-from-request-parameters ((authorizer authorizer))
  (:documentation "Return the user that is associated with the current
request or NIL.")
  (:method ((authorizer bknr-authorizer))
    (with-query-params (__username __password)
      (unless (and __username __password
                   (not (equal __username ""))
                   (not (equal __password "")))
        (return-from find-user-from-request-parameters nil))
      (let ((user (find-user __username)))
	(when (and user
                   (not (user-disabled user))
		   (verify-password user __password))
          (return-from find-user-from-request-parameters user)))
      (error 'login-failure))))

(defmethod authorize ((authorizer bknr-authorizer))
  ;; Catch any errors that occur during request body processing
  (handler-case
      (when (session-value 'bknr-session)
	(return-from authorize t))
    (error (e)
      (format t "; Caught error ~A during request processing~%" e)
      (setf (return-code) +http-bad-request+)
      (princ-to-string e)))

  ;; unauthorized, come up with 401 response to the web browser
  (redirect "/login")<commit_msg>(defgeneric find-user-from-request-parameters (authorizer) ...) small syntax fix<commit_after>(in-package :bknr.web)

(defclass bknr-authorizer ()
  ())

(defmethod http-request-remote-host ()
  (format *debug-io* "can't determin originating host yet~%")
  #+(or)
  (let ((remote-host (socket:remote-host (request-socket)))
	(forwarded-for (regex-replace
			"^.*?([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*$"
			(header-slot-value :x-forwarded-for)
			"\\1")))
    (when (and forwarded-for
	       (equal "127.0.0.1" (socket:ipaddr-to-dotted remote-host)))
      ;; request is via proxy, use client's ip address
      (setf remote-host (socket:dotted-to-ipaddr forwarded-for)))
    (find-host :create t :ipaddr remote-host)))

(defun session-from-request ()
  "check whether the request has a valid session id in either the bknr-sessionid cookie or query parameter"
  (session-value 'bknr-session))

(define-condition login-failure (serious-condition)
  ()
  (:report (lambda (c s)
             (declare (ignore c))
             (format s "Login failed"))))

(defgeneric find-user-from-request-parameters (authorizer)
  (:documentation "Return the user that is associated with the current
request or NIL.")
  (:method ((authorizer bknr-authorizer))
    (with-query-params (__username __password)
      (unless (and __username __password
                   (not (equal __username ""))
                   (not (equal __password "")))
        (return-from find-user-from-request-parameters nil))
      (let ((user (find-user __username)))
	(when (and user
                   (not (user-disabled user))
		   (verify-password user __password))
          (return-from find-user-from-request-parameters user)))
      (error 'login-failure))))

(defmethod authorize ((authorizer bknr-authorizer))
  ;; Catch any errors that occur during request body processing
  (handler-case
      (when (session-value 'bknr-session)
	(return-from authorize t))
    (error (e)
      (format t "; Caught error ~A during request processing~%" e)
      (setf (return-code) +http-bad-request+)
      (princ-to-string e)))

  ;; unauthorized, come up with 401 response to the web browser
  (redirect "/login")
{'fraction_non_alphanumeric': 0.1484848484848485, 'fraction_numerical': 0.008391608391608392, 'mean_word_length': 3.153920619554695, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1170257', 'n_tokens_mistral': 1402, 'n_tokens_neox': 1314, 'n_words': 373}