<commit_before>(ns foreclojure.login
  (:import [org.jasypt.util.password StrongPasswordEncryptor])
  (:use hiccup.form-helpers
        hiccup.page-helpers
        foreclojure.utils
        compojure.core
        somnium.congomongo)
  (:require [sandbar.stateful-session :as session]
            [ring.util.response :as response]))
                        
(def-page my-login-page []
  [:div.error (session/flash-get :error)]
  (form-to [:post "/login"]
    [:table
     [:tr
      [:td (label :user "Username")]
      [:td (text-field :user)]]
     [:tr
      [:td (label :pwd "Password")]
      [:td (password-field :pwd)]]
     [:tr
      [:td (submit-button {:type "image" :src "/login.png"}
                          "Log In")]]]))

(defn do-login [user pwd]
  (let [{db-pwd :pwd} (from-mongo (fetch-one :users :where {:user user}))]
    (if (and db-pwd (.checkPassword (StrongPasswordEncryptor.) pwd db-pwd))
      (do (session/session-put! :user user)
          (response/redirect "/problems"))
      (flash-error "Error logging in." "/login"))))

(def-page reset-password-page []
  (with-user [{:keys [user]}]
    [:div#reset-pwd
     [:h2 "Reset password for " user]
     [:span.error (session/flash-get :error)]
     [:table
      (form-to [:post "/login/reset"]
        (map form-row
             [[password-field :old-pwd "<PASSWORD>"]
              [password-field :pwd "<PASSWORD>"]
              [password-field :repeat-pwd "<PASSWORD>"]])
        [:tr
         [:td (submit-button "Reset now")]])]]))

(def-page do-reset-password! [old-pwd new-pwd repeat-pwd]
  (with-user [{:keys [user pwd]}]
    (let [encryptor (StrongPasswordEncryptor.)]
      (assuming [(= new-pwd repeat-pwd)
                 "New password was not entered identically twice"
                 (.checkPassword encryptor old-pwd pwd)
                 "Old password incorrect"]
        (let [new-pwd-hash (.encryptPassword encryptor new-pwd)]
          (update! :users {:user user}
                   {:$set {:pwd <PASSWORD>}}
                   :upsert false)
          [:div#reset-succeeded "Password for " user " reset successfully"])
        (flash-error why "/login/reset")))))

(defroutes login-routes
  (GET  "/login" [] (my-login-page))
  (POST "/login" {{:strs [user pwd]} :form-params}
    (do-login user pwd))
  (GET  "/login/reset" [] (reset-password-page))
  (POST "/login/reset" {{:strs [old-pwd pwd repeat-pwd]} :form-params}
    (do-reset-password! old-pwd pwd repeat-pwd))
  (GET "/logout" []
    (do (session/session-delete-key! :user)
        (response/redirect "/"))))
<commit_msg>Make redirects work right when user specifies new passwords wrong<commit_after>(ns foreclojure.login
  (:import [org.jasypt.util.password StrongPasswordEncryptor])
  (:use hiccup.form-helpers
        hiccup.page-helpers
        foreclojure.utils
        compojure.core
        somnium.congomongo)
  (:require [sandbar.stateful-session :as session]
            [ring.util.response :as response]))
                        
(def-page my-login-page []
  [:div.error (session/flash-get :error)]
  (form-to [:post "/login"]
    [:table
     [:tr
      [:td (label :user "Username")]
      [:td (text-field :user)]]
     [:tr
      [:td (label :pwd "Password")]
      [:td (password-field :pwd)]]
     [:tr
      [:td (submit-button {:type "image" :src "/login.png"}
                          "Log In")]]]))

(defn do-login [user pwd]
  (let [{db-pwd :pwd} (from-mongo (fetch-one :users :where {:user user}))]
    (if (and db-pwd (.checkPassword (StrongPasswordEncryptor.) pwd db-pwd))
      (do (session/session-put! :user user)
          (response/redirect "/problems"))
      (flash-error "Error logging in." "/login"))))

(def-page reset-password-page []
  (with-user [{:keys [user]}]
    [:div#reset-pwd
     [:h2 "Reset password for " user]
     [:span.error (session/flash-get :error)]
     [:table
      (form-to [:post "/login/reset"]
        (map form-row
             [[password-field :old-pwd "<PASSWORD>"]
              [password-field :pwd "<PASSWORD>"]
              [password-field :repeat-pwd "<PASSWORD>"]])
        [:tr
         [:td (submit-button "Reset now")]])]]))

(defn do-reset-password! [old-pwd new-pwd repeat-pwd]
  (with-user [{:keys [user pwd]}]
    (let [encryptor (StrongPasswordEncryptor.)]
      (assuming [(= new-pwd repeat-pwd)
                 "New password was not entered identically twice"
                 (.checkPassword encryptor old-pwd pwd)
                 "Old password incorrect"]
        (let [new-pwd-hash (.encryptPassword encryptor new-pwd)]
          (update! :users {:user user}
                   {:$set {:pwd <PASSWORD>-<PASSWORD>}}
                   :upsert false)
          (html-doc
           [:div#reset-succeeded "Password for " user " reset successfully"]))
        (flash-error why "/login/reset")))))

(defroutes login-routes
  (GET  "/login" [] (my-login-page))
  (POST "/login" {{:strs [user pwd]} :form-params}
    (do-login user pwd))
  (GET  "/login/reset" [] (reset-password-page))
  (POST "/login/reset" {{:strs [old-pwd pwd repeat-pwd]} :form-params}
    (do-reset-password! old-pwd pwd repeat-pwd))
  (GET "/logout" []
    (do (session/session-delete-key! :user)
        (response/redirect "/"))))

{'fraction_non_alphanumeric': 0.17190177041690463, 'fraction_numerical': 0.0003807348181991243, 'mean_word_length': 2.3701090442591406, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '2980024', 'n_tokens_mistral': 1810, 'n_tokens_neox': 1730, 'n_words': 416}