<commit_before>;; Copyright 2014 <NAME> <<EMAIL>>
;;
;; Licensed under the Apache License, Version 2.0 (the "License")
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;;     http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.

(ns buddy.auth.backends.session
  (:require [buddy.auth.protocols :as proto]
            [buddy.auth :refer [authenticated?]]
            [clojure.string :refer [split]]
            [ring.util.response :refer [response response? header status]]))

(defrecord SessionBackend [unauthorized-handler]
  proto/Authentication
  (parse [_ request]
    (:identity (:session request)))
  (authenticate [_ request data]
    (assoc request :identity data))

  proto/Authorization
  (handle-unauthorized [_ request metadata]
    (if unauthorized-handler
      (unauthorized-handler request metadata)
      (if (authenticated? request)
        (-> (response "Permission denied")
            (status 403))
        (-> (response "Unauthorized")
            (status 401))))))

(defn session-backend
  "Given some options, create a new instance
  of HttpBasicBackend and return it."
  [& [{:keys [unauthorized-handler]}]]
  (->SessionBackend unauthorized-handler))<commit_msg>Use reify instead defrecord for session backend.<commit_after>;; Copyright 2014 <NAME> <<EMAIL>>
;;
;; Licensed under the Apache License, Version 2.0 (the "License")
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;;     http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.

(ns buddy.auth.backends.session
  (:require [buddy.auth.protocols :as proto]
            [buddy.auth :refer [authenticated?]]
            [clojure.string :refer [split]]
            [ring.util.response :refer [response response? header status]]))

(defn session-backend
  "Given some options, create a new instance
  of HttpBasicBackend and return it."
  [& [{:keys [unauthorized-handler]}]]
  (reify
    proto/Authentication
    (parse [_ request]
      (:identity (:session request)))
    (authenticate [_ request data]
      (assoc request :identity data))

    proto/Authorization
    (handle-unauthorized [_ request metadata]
      (if unauthorized-handler
        (unauthorized-handler request metadata)
        (if (authenticated? request)
          (-> (response "Permission denied")
              (status 403))
          (-> (response "Unauthorized")
              (status 401)))))))

{'fraction_non_alphanumeric': 0.1101123595505618, 'fraction_numerical': 0.008988764044943821, 'mean_word_length': 3.4577968526466383, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, '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': '4544547', 'n_tokens_mistral': 845, 'n_tokens_neox': 809, 'n_words': 313}