Skip to content

Commit

Permalink
Merge branch 'hotfix/1.1.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
amalloy committed Jul 24, 2011
2 parents dcfb368 + b3d171b commit d44f9bd
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
2 changes: 1 addition & 1 deletion project.clj
@@ -1,4 +1,4 @@
(defproject foreclojure "1.1.1" (defproject foreclojure "1.1.2"
:description "4clojure - a website for lisp beginners" :description "4clojure - a website for lisp beginners"
:dependencies [[clojure "1.2.1"] :dependencies [[clojure "1.2.1"]
[clojure-contrib "1.2.0"] [clojure-contrib "1.2.0"]
Expand Down
64 changes: 44 additions & 20 deletions src/foreclojure/login.clj
Expand Up @@ -4,7 +4,9 @@
hiccup.page-helpers hiccup.page-helpers
[foreclojure utils config users] [foreclojure utils config users]
compojure.core compojure.core
[amalloy.utils :only [rand-in-range]] [amalloy.utils :only [rand-in-range keywordize]]
[clojail.core :only [thunk-timeout]]
clojure.stacktrace
somnium.congomongo) somnium.congomongo)
(:require [sandbar.stateful-session :as session] (:require [sandbar.stateful-session :as session]
[ring.util.response :as response])) [ring.util.response :as response]))
Expand Down Expand Up @@ -87,30 +89,52 @@
(text-field :email) (text-field :email)
[:button {:type "submit"} "Reset!"])]]]) [:button {:type "submit"} "Reset!"])]]])


(def pw-chars "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVWXY1234567890") (let [pw-chars "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVWXY1234567890"]
(defn random-pwd []
(let [pw (apply str
(repeatedly 10 #(rand-nth pw-chars)))
hash (.encryptPassword (StrongPasswordEncryptor.) pw)]
(keywordize [pw hash]))))

(defn try-to-email [email name id]
(let [{:keys [pw hash]} (random-pwd)]
(try
(thunk-timeout
(fn []
(update! :users
{:_id id}
{:$set {:pwd hash}})
(send-email
{:from "team@4clojure.com"
:to [email]
:subject "Password reset"
:body
(str "The password for your 4clojure.com account "
name " has been reset to " pw ". Make sure to change it"
" soon at https://4clojure.com/login/update - pick"
" something you'll remember!")})
{:success true})
10 :sec)
(catch Throwable t
{:success false, :exception t,
:message (.getMessage t),
:trace (with-out-str
(binding [*err* *out*]
(print-cause-trace t)))
:pw pw, :hash hash}))))


(defn do-reset-password! [email] (defn do-reset-password! [email]
(if-let [{id :_id, name :user} (fetch-one :users (if-let [{id :_id, name :user} (fetch-one :users
:where {:email email} :where {:email email}
:only [:_id :user])] :only [:_id :user])]
(let [pw (apply str (let [{:keys [success] :as diagnostics} (? (try-to-email email name id))]
(repeatedly 10 #(rand-nth pw-chars))) (if success
pw-hash (.encryptPassword (StrongPasswordEncryptor.) pw)] (do (session/session-put! :login-to "/login/update")
(update! :users (flash-msg "Your password has been reset! You should receive an email soon."
{:_id id} (login-url "/login/update")))
{:$set {:pwd pw-hash}}) (do (spit (? (str name ".pwd")) (? diagnostics))
(send-email (flash-error (str "Something went wrong emailing your new password! Please contact <a href='mailto:team@4clojure.com?subject=Password Reset: " name "'>team@4clojure.com</a> - we'll reset it manually and look into the problem. When you do, please mention your username.")
{:from "team@4clojure.com" "/login/reset"))))
:to [email]
:subject "Password reset"
:body
(str "The password for your 4clojure.com account "
name " has been reset to " pw ". Make sure to change it"
" soon at https://4clojure.com/login/update - pick"
" something you'll remember!")})
(session/session-put! :login-to "/login/update")
(flash-msg "Your password has been reset! You should receive an email soon"
(login-url "/login/update")))
(flash-error "We don't know anyone with that email address!" (flash-error "We don't know anyone with that email address!"
"/login/reset"))) "/login/reset")))


Expand Down

0 comments on commit d44f9bd

Please sign in to comment.