<commit_before>(ns cia.auth.middleware
  (:require [cia.auth :as auth :refer [auth-service]]
            [compojure.api.meta :as meta]
            [ring.util.http-response :as http-response]))

(defn wrap-authentication [handler]
  (fn [request]
    (handler
     (assoc request
            :identity (some->> (get-in request [:headers "api_key"])
                               (auth/identity-for-token @auth-service))))))

(defn require-capability! [granting-capabilities id]
  (if granting-capabilities
    (cond
      (nil? id)
      (http-response/forbidden! {:message "Only authenticated users allowed"})

      (not (auth/allowed-capability? id granting-capabilities))
      (http-response/unauthorized! {:message "Missing capability"
                                    :capabilities granting-capabilities
                                    :owner (auth/login id)}))))


;; Create a compojure-api meta-data handler for capability-based
;; security. The :owner field must by on the request object
;; already, put there by buddy-auth.  This lets us add :capabilities
;; to the handler spec.
;; Reference:
;; https://github.com/metosin/compojure-api/wiki/Creating-your-own-metadata-handlers
(defmethod meta/restructure-param :capabilities
  [_ capabilities acc]
  (update-in acc
             [:lets]
             into
             ['_ `(require-capability! ~capabilities
                                       (:identity ~'+compojure-api-request+))]))

(defmethod meta/restructure-param :login [_ bind-to acc]
  (update-in acc [:lets] into [bind-to `(get-in  ~'+compojure-api-request+ [:identity :login])]))<commit_msg>But fix and code cleanup (from <NAME>.)<commit_after>(ns cia.auth.middleware
  (:require [cia.auth :as auth :refer [auth-service]]
            [compojure.api.meta :as meta]
            [ring.util.http-response :as http-response]))

(defn wrap-authentication [handler]
  (fn [request]
    (handler
     (assoc request
            :identity (some->> (get-in request [:headers "api_key"])
                               (auth/identity-for-token @auth-service))))))

(defn require-capability! [granting-capabilities id]
  (if granting-capabilities
    (cond
      (nil? id)
      (http-response/forbidden! {:message "Only authenticated users allowed"})

      (not (auth/allowed-capability? id granting-capabilities))
      (http-response/unauthorized! {:message "Missing capability"
                                    :capabilities granting-capabilities
                                    :owner (auth/login id)}))))


;; Create a compojure-api meta-data handler for capability-based
;; security. The :owner field must by on the request object
;; already, put there by buddy-auth.  This lets us add :capabilities
;; to the handler spec.
;; Reference:
;; https://github.com/metosin/compojure-api/wiki/Creating-your-own-metadata-handlers
(defmethod meta/restructure-param :capabilities
  [_ capabilities acc]
  (update acc :lets into
          ['_ `(require-capability! ~capabilities
                                    (:identity ~'+compojure-api-request+))]))

(defmethod meta/restructure-param :login [_ bind-to acc]
  (update acc :lets into
          [bind-to `(-> (:identity ~'+compojure-api-request+)
                        auth/login)]))
{'fraction_non_alphanumeric': 0.12917049280685644, 'fraction_numerical': 0.0, 'mean_word_length': 2.5064377682403434, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '7029841', 'n_tokens_mistral': 965, 'n_tokens_neox': 918, 'n_words': 240}