<commit_before>;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2019 <NAME> <<EMAIL>>

(ns uxbox.http.session
  (:require
   [promesa.core :as p]
   [sieppari.context :as spx]
   [vertx.core :as vc]
   [uxbox.db :as db]
   [uxbox.util.exceptions :as ex]
   [uxbox.util.uuid :as uuid]))

;; --- Main API

(defn retrieve
  "Retrieves a user id associated with the provided auth token."
  [token]
  (let [sql "select user_id from sessions where id = $1"]
    (-> (db/query-one db/pool [sql token])
        (p/then' (fn [row] (when row (:user-id row)))))))

(defn create
  [user-id user-agent]
  (let [id  (uuid/random)
        sql "insert into sessions (id, user_id, user_agent) values ($1, $2, $3)"]
    (-> (db/query-one db/pool [sql id user-id user-agent])
        (p/then (constantly (str id))))))

(defn delete
  [token]
  (let [sql "delete from sessions where id = $1"]
    (-> (db/query-one db/pool [sql token])
        (p/then' (constantly nil)))))

;; --- Interceptor

(defn parse-token
  [request]
  (try
    (when-let [token (get-in request [:cookies "auth-token"])]
      (uuid/from-string token))
    (catch java.lang.IllegalArgumentException e
      nil)))

(defn auth
  []
  {:enter (fn [data]
            (let [token (parse-token (:request data))]
              (-> (retrieve token)
                  (p/then' (fn [user-id]
                             (if user-id
                               (update data :request assoc :user user-id)
                               (spx/terminate (assoc data ::unauthorized true)))))
                  (vc/handle-on-context))))
   :leave (fn [data]
            (if (and (::unauthorized data) (:response data))
              (update data :response
                      assoc :status 403 :body {:type :authentication
                                               :code :unauthorized})
              data))})<commit_msg>:bug: Properly send 403 on not authorized request.<commit_after>;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) 2019 <NAME> <<EMAIL>>

(ns uxbox.http.session
  (:require
   [promesa.core :as p]
   [sieppari.context :as spx]
   [vertx.core :as vc]
   [uxbox.db :as db]
   [uxbox.util.exceptions :as ex]
   [uxbox.util.uuid :as uuid]))

;; --- Main API

(defn retrieve
  "Retrieves a user id associated with the provided auth token."
  [token]
  (let [sql "select user_id from sessions where id = $1"]
    (-> (db/query-one db/pool [sql token])
        (p/then' (fn [row] (when row (:user-id row)))))))

(defn create
  [user-id user-agent]
  (let [id  (uuid/random)
        sql "insert into sessions (id, user_id, user_agent) values ($1, $2, $3)"]
    (-> (db/query-one db/pool [sql id user-id user-agent])
        (p/then (constantly (str id))))))

(defn delete
  [token]
  (let [sql "delete from sessions where id = $1"]
    (-> (db/query-one db/pool [sql token])
        (p/then' (constantly nil)))))

;; --- Interceptor

(defn parse-token
  [request]
  (try
    (when-let [token (get-in request [:cookies "auth-token"])]
      (uuid/from-string token))
    (catch java.lang.IllegalArgumentException e
      nil)))

(defn auth
  []
  {:enter (fn [data]
            (let [token (parse-token (:request data))]
              (-> (retrieve token)
                  (p/then' (fn [user-id]
                             (if user-id
                               (update data :request assoc :user user-id)
                               (spx/terminate (assoc data ::unauthorized true)))))
                  (vc/handle-on-context))))
   :leave (fn [data]
            (if (::unauthorized data)
              (update data :response
                      assoc :status 403 :body {:type :authentication
                                               :code :unauthorized})
              data))})
{'fraction_non_alphanumeric': 0.14829268292682926, 'fraction_numerical': 0.00853658536585366, 'mean_word_length': 2.0468053491827636, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7467055', 'n_tokens_mistral': 1355, 'n_tokens_neox': 1312, 'n_words': 421}