5

Password Recovery in Clojure

 3 years ago
source link: https://blog.knoldus.com/password-recovery-in-clojure/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Password Recovery in Clojure

Reading Time: 2 minutes

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

xxxxxxxxxx
: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"]]

Now follow the given steps to add password recovery functionality:

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

xxxxxxxxxx
(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]))

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

xxxxxxxxxx
(def alphanumeric "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz")
(def length 10)
(defn get-random-id []
(apply str (repeatedly length #(rand-nth alphanumeric))))

We also have to define the routes :-

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

xxxxxxxxxx
(GET "/forgot-password" [] (forgot-password))

Function to render the page using default luminus template:

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

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

xxxxxxxxxx
(POST "/forgot-password" [email]
(forgot-password-post email))

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:

xxxxxxxxxx
(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 "[email protected]" "TestApp")
(.setSubject "Your New Password on testapp account is")
(.setMsg newpassword)
(.setAuthentication "[email protected]" "your password")
(.send))
(resp/redirect "/login"))
(catch Exception e
(vali/rule false [:email (.getMessage e)])
(forgot-password)))
(forgot-password email))))

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK