<commit_before>(ns com.sixsq.slipstream.auth.external
  (:require
    [clojure.string :as str]
    [clojure.tools.logging :as log]
    [com.sixsq.slipstream.auth.utils.db :as db]
    [com.sixsq.slipstream.ssclj.resources.common.utils :as u]))


(defn- mapped-user
  [authn-method username]
  (log/infof "External (%s) user '%s' already mapped => login ok." authn-method username)
  username)


(defn- map-slipstream-user!
  [authn-method slipstream-username external-login]
  (log/infof "Mapping external (%s) user '%s' to existing SlipStream user '%s'"
             authn-method external-login slipstream-username)
  (db/update-user-authn-info authn-method slipstream-username external-login))


(defn- create-slipstream-user!
  ([authn-method external-login external-email]
   (log/infof "Creating new SlipStream user with external (%s) user '%s'"
              authn-method external-login)
   (let [user-name (db/create-user! authn-method external-login external-email)]
     (when user-name
       (db/create-user-params! user-name))
     user-name))

  ([{:keys [authn-login] :as user-record}]
   (log/infof "Creating new SlipStream user '%'" authn-login)
   (let [user-name (db/create-user! user-record)]
     (when user-name
       (db/create-user-params! user-name))
     user-name)))


(defn match-external-user!
  [authn-method external-login external-email]
  (if-let [username-mapped (db/find-username-by-authn authn-method external-login)]
    (mapped-user authn-method username-mapped)
    (let [usernames-same-email (db/find-usernames-by-email external-email)]
      (if (empty? usernames-same-email)
        (let [name-new-user (create-slipstream-user! authn-method external-login external-email)]
          [name-new-user (format "/user/%s?edit=true" name-new-user)])
        (map-slipstream-user! authn-method (first usernames-same-email) external-login)))))


(defn match-existing-external-user
  [authn-method external-login external-email]
  (log/debugf "Matching external user with method '%s', external-login '%s', and external-email '%s'"
              authn-method external-login external-email)
  (when-let [username-mapped (db/find-username-by-authn authn-method external-login)]
    (mapped-user authn-method username-mapped)))


(defn sanitize-login-name
  "Replace characters not satisfying [a-zA-Z0-9_] with underscore"
  [s]<commit_msg>fix incorrect format string<commit_after>(ns com.sixsq.slipstream.auth.external
  (:require
    [clojure.string :as str]
    [clojure.tools.logging :as log]
    [com.sixsq.slipstream.auth.utils.db :as db]
    [com.sixsq.slipstream.ssclj.resources.common.utils :as u]))


(defn- mapped-user
  [authn-method username]
  (log/infof "External (%s) user '%s' already mapped => login ok." authn-method username)
  username)


(defn- map-slipstream-user!
  [authn-method slipstream-username external-login]
  (log/infof "Mapping external (%s) user '%s' to existing SlipStream user '%s'"
             authn-method external-login slipstream-username)
  (db/update-user-authn-info authn-method slipstream-username external-login))


(defn- create-slipstream-user!
  ([authn-method external-login external-email]
   (log/infof "Creating new SlipStream user with external (%s) user '%s'"
              authn-method external-login)
   (let [user-name (db/create-user! authn-method external-login external-email)]
     (when user-name
       (db/create-user-params! user-name))
     user-name))

  ([{:keys [authn-login] :as user-record}]
   (log/infof "Creating new SlipStream user '%s'" authn-login)
   (let [user-name (db/create-user! user-record)]
     (when user-name
       (db/create-user-params! user-name))
     user-name)))


(defn match-external-user!
  [authn-method external-login external-email]
  (if-let [username-mapped (db/find-username-by-authn authn-method external-login)]
    (mapped-user authn-method username-mapped)
    (let [usernames-same-email (db/find-usernames-by-email external-email)]
      (if (empty? usernames-same-email)
        (let [name-new-user (create-slipstream-user! authn-method external-login external-email)]
          [name-new-user (format "/user/%s?edit=true" name-new-user)])
        (map-slipstream-user! authn-method (first usernames-same-email) external-login)))))


(defn match-existing-external-user
  [authn-method external-login external-email]
  (log/debugf "Matching external user with method '%s', external-login '%s', and external-email '%s'"
              authn-method external-login external-email)
  (when-let [username-mapped (db/find-username-by-authn authn-method external-login)]
    (mapped-user authn-method username-mapped)))


(defn sanitize-login-name
  "Replace characters not satisfying [a-zA-Z0-9_] with underscore"
  [s]
{'fraction_non_alphanumeric': 0.14354872658387707, 'fraction_numerical': 0.0008419280151547042, 'mean_word_length': 3.595744680851064, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1452572', 'n_tokens_mistral': 1616, 'n_tokens_neox': 1593, 'n_words': 351}