Password Recovery in Clojure

Knoldus Inc.
Knoldus - Technical Insights
2 min readMar 19, 2014

This Blog post will help you to add the password recovery functionalities in your clojure web application.

Firstly, create html file containing textbox for getting email address, on which a new passowrd will be sent.

Next, add this in your project.clj File

[code language=”html”]

:repositories [[“central-proxy” “http://repository.sonatype.org/content/repositories/central/">http://repository.sonatype.org/content/repositories/central/]]

:dependencies [[org.apache.commons/commons-email “1.2”]]

[/code]

Now follow the given steps to add password recovery functionality:

Let’s define a forgot-password.clj namespace and import the following :

[code language=”html”]

(ns testapp.routes.forgot-password
(: import org.apache.commons.mail.SimpleEmail)
(:require [noir.validation :as vali]
[testapp.models.db :as db]
[noir.util.crypt :as crypt]))

[/code]

To get the random string we can use the following code:-

[code language=”html”]
(def alphanumeric “ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz”)
(def length 10)
(defn get-random-id []
(apply str (repeatedly length #(rand-nth alphanumeric))))

[/code]

We also have to define the routes :-

The GET forgot-password route simply call the forgot-password function to render the page

[code language=”html”]

(GET “/forgot-password” [] (forgot-password))

[/code]

Function to render the page using default luminus template:

[code language=”html”]

(defn forgot-password[&[email]]
(layout/render “forgot-password.html”
{
* CODE
}))

[/code]

The POST forgot-password route simply call the forgot-password-post function and pass the email as an argument

[code language=”html”]

(POST “/forgot-password” [email]
(forgot-password-post email))

[/code]

Define the forgot-password-post function in (testapp.routes.forgot-password.clj) . This function sends the password (a random string) to the given e-mail:

[code language=”html”]
(defn forgot-password-post [email]
(def newpassword (get-random-id))
(if (and (vali/valid-email? email) (= email (:email (db/get-email email))))
(try
(do
(db/update-user-password email (crypt/encrypt newpassword))
(doto (SimpleEmail.)
(.setHostName “smtp.gmail.com”)
(.setSslSmtpPort “465”)
(.setSSL true)
(.addTo email)
(.setFrom “youremailid@gmail.com” “TestApp”)
(.setSubject “Your New Password on testapp account is”)
(.setMsg newpassword)
(.setAuthentication “youremailid@gmail.com” “your password”)
(.send))
(resp/redirect “/login”))
(catch Exception e
(vali/rule false [:email (.getMessage e)])
(forgot-password)))
(forgot-password email))))

[/code]

Note :- * This code may contain some validation which is applicable on email. Like:email-error (vali/on-error :email first)

Define “get-email”, “update-user-password” in namesapce “db” to get the email addressand to update the user password respectively.

“encrypt” from “noir.util.crypt” is used to encrypt the password.

--

--

Knoldus Inc.
Knoldus - Technical Insights

Group of smart Engineers with a Product mindset who partner with your business to drive competitive advantage | www.knoldus.com