<commit_before>(ns com.sixsq.slipstream.auth.simple-authentication
  (:refer-clojure :exclude [update])
  (:require

    [clojure.tools.logging                            :as log]
    [clojure.set                                      :refer [rename-keys]]
    [korma.core                                       :as kc]
    [com.sixsq.slipstream.auth.core                   :as core]
    [com.sixsq.slipstream.auth.database.korma-helper  :as kh]
    [com.sixsq.slipstream.auth.conf.config            :as cf]

    [environ.core                                     :as environ]

    [buddy.sign.jws                                   :as jws]
    [buddy.core.hash                                  :as ha]
    [buddy.core.codecs                                :as co]
    [buddy.core.keys                                  :as ks]

    [clj-time.core                                    :as t]
    [clojure.java.io                                  :as io]))

(def auth-conf {:pubkey     "auth_pubkey.pem"
                :privkey    "auth_privkey.pem"})
(def default-nb-minutes-expiry (* 24 60))
(def signing-algorithm {:alg :rs256})

;;
;; DB
;;

(def init-db
  (delay
    (kh/korma-init)
    (log/info "Korma init done")

    (kc/defentity users (kc/table "USER"))
    (log/info "Korma Entities defined")))
;;
;; DB
;;

(defn private-key
  [auth-conf]
  (if-let [passphrase (environ/env :passphrase)]
    (ks/private-key
      (io/resource (:privkey auth-conf))
      passphrase)
    (throw (IllegalStateException. "passphrase not defined"))))

(defn public-key
  [auth-conf]
  (ks/public-key (io/resource (:pubkey auth-conf))))

(defn init
  []
  @init-db)

(defn sha512
  "Encrypt secret exactly as done in SlipStream Java server."
  [secret]
  (-> (ha/sha512 secret)
      co/bytes->hex
      clojure.string/upper-case))

;; TODO : check user not already present, password rules (length, complexity...)
;; TODO : Currently unused as DB insertion is done by Java server
(defn add-user-impl<commit_msg>Improve error message when passphrase or key absent<commit_after>(ns com.sixsq.slipstream.auth.simple-authentication
  (:refer-clojure :exclude [update])
  (:require

    [clojure.tools.logging                            :as log]
    [clojure.set                                      :refer [rename-keys]]
    [korma.core                                       :as kc]
    [com.sixsq.slipstream.auth.core                   :as core]
    [com.sixsq.slipstream.auth.database.korma-helper  :as kh]
    [com.sixsq.slipstream.auth.conf.config            :as cf]

    [environ.core                                     :as environ]

    [buddy.sign.jws                                   :as jws]
    [buddy.core.hash                                  :as ha]
    [buddy.core.codecs                                :as co]
    [buddy.core.keys                                  :as ks]

    [clj-time.core                                    :as t]
    [clojure.java.io                                  :as io]))

(def auth-conf {:pubkey     "auth_pubkey.pem"
                :privkey    "auth_privkey.pem"})
(def default-nb-minutes-expiry (* 24 60))
(def signing-algorithm {:alg :rs256})

;;
;; DB
;;

(def init-db
  (delay
    (kh/korma-init)
    (log/info "Korma init done")

    (kc/defentity users (kc/table "USER"))
    (log/info "Korma Entities defined")))
;;
;; DB
;;

(defn private-key
  [auth-conf]
  (let [passphrase (environ/env :passphrase)
        privkey    (io/resource (:privkey auth-conf))]
    (if (and passphrase privkey)
      (ks/private-key privkey passphrase)
      (throw (IllegalStateException. "Passphrase not defined or private key not accessible (must be in the classpath).")))))

(defn public-key
  [auth-conf]
  (let [pubkey (io/resource (:pubkey auth-conf))]
    (if pubkey
      (ks/public-key pubkey)
      (throw (IllegalStateException. "Public key not accessible (must be in the classpath).")))))

(defn init
  []
  @init-db)

(defn sha512
  "Encrypt secret exactly as done in SlipStream Java server."
  [secret]
  (-> (ha/sha512 secret)
      co/bytes->hex
      clojure.string/upper-case))

;; TODO : check user not already present, password rules (length, complexity...)
;; TODO : Currently unused as DB insertion is done by Java server
(defn add-user-impl
{'fraction_non_alphanumeric': 0.12339331619537275, 'fraction_numerical': 0.0060761860247721435, 'mean_word_length': 1.6867545511613309, 'pattern_counts': {'":': 0, '<': 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': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2140774', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1349, 'n_words': 324}