<commit_before>(ns obb-api.interceptors.auth-interceptor
  (:require [io.pedestal.interceptor :as interceptor]
            [io.pedestal.impl.interceptor :as interceptor-impl]
            [ring.util.response :as ring-response]
            [obb-api.core.auth :as auth]))

(defn- get-raw-token
  "Gets a token from the request"
  [context]
  (get-in context [:request :params :token]))

(defn- parse-token
  "Parses a token from the context"
  [context]
  (try
    (if-let [token (get-raw-token context)]
      (auth/parse token))
    (catch Exception e)))

(defn token
  "Gets the request's token"
  [request]
  (request :auth))

(defn username
  "Gets the request's username"
  [request]
  (get-in request [:auth :claims :iss]))

(defn- handle-token
  "Loads and stores the token"
  [context]
  (let [token (parse-token context)]
    (-> context
        (assoc-in [:request :auth] token)
        (assoc-in [:request :auth :valid] (auth/valid? token)))))

(interceptor/defbefore parse
  "Loads and stores the token"
  [context]
  (handle-token context))

(defn- valid-token?
  "Checks if the token is present and valid"
  [context]
  (= true (get-in context [:request :auth :valid])))

(interceptor/defbefore enforce
  "Enforces a request token"
  [context]
  (let [parsed-context (handle-token context)]
    (if (valid-token? parsed-context)
      parsed-context
      (-> context
          (assoc :response {:status  403
                            :body    "{\"error\":\"Forbidden\"}"})
          (interceptor-impl/terminate)))))<commit_msg>Yield username only if token is valid<commit_after>(ns obb-api.interceptors.auth-interceptor
  (:require [io.pedestal.interceptor :as interceptor]
            [io.pedestal.impl.interceptor :as interceptor-impl]
            [ring.util.response :as ring-response]
            [obb-api.core.auth :as auth]))

(defn- get-raw-token
  "Gets a token from the request"
  [context]
  (get-in context [:request :params :token]))

(defn- parse-token
  "Parses a token from the context"
  [context]
  (try
    (if-let [token (get-raw-token context)]
      (auth/parse token))
    (catch Exception e)))

(defn token
  "Gets the request's token"
  [request]
  (request :auth))

(defn- handle-token
  "Loads and stores the token"
  [context]
  (let [token (parse-token context)]
    (-> context
        (assoc-in [:request :auth] token)
        (assoc-in [:request :auth :valid] (auth/valid? token)))))

(interceptor/defbefore parse
  "Loads and stores the token"
  [context]
  (handle-token context))

(defn- valid-token?
  "Checks if the token is present and valid"
  [context_or_request]
  (or
    (= true (get-in context_or_request [:request :auth :valid]))
    (= true (get-in context_or_request [:auth :valid]))))

(defn username
  "Gets the request's username"
  [request]
  (when (valid-token? request)
    (get-in request [:auth :claims :iss])))

(interceptor/defbefore enforce
  "Enforces a request token"
  [context]
  (let [parsed-context (handle-token context)]
    (if (valid-token? parsed-context)
      parsed-context
      (-> context
          (assoc :response {:status  403
                            :body    "{\"error\":\"Forbidden\"}"})
          (interceptor-impl/terminate)))))
{'fraction_non_alphanumeric': 0.1424589656240322, 'fraction_numerical': 0.0018581604211830288, 'mean_word_length': 2.96319018404908, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1682091', 'n_tokens_mistral': 1092, 'n_tokens_neox': 1079, 'n_words': 298}