Skip to content

Instantly share code, notes, and snippets.

/web.clj Secret

Created November 28, 2014 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/bf566201083c937e868d to your computer and use it in GitHub Desktop.
Save anonymous/bf566201083c937e868d to your computer and use it in GitHub Desktop.
(ns grandy.web
(:require [compojure.core :refer :all]
[ring.middleware.session :refer :all]
[ring.middleware.session.cookie :refer :all]
[compojure.handler :as handler]
[compojure.route :as route]
[hiccup.core :as h]
[hiccup.page :as page]
[grandy.views.pages :as pages]
[cemerick.friend :as friend]
(cemerick.friend [workflows :as workflows]
[credentials :as creds])))
(def users (atom {"friend" {:username "friend"
:password (creds/hash-bcrypt "clojure")
:pin "1234" ;; only used by multi-factor
:roles #{::user}}
"friend-admin" {:username "friend-admin"
:password (creds/hash-bcrypt "clojure")
:pin "1234" ;; only used by multi-factor
:roles #{::admin}}}))
(derive ::admin ::user)
(defroutes app-routes
(GET "/" [] pages/home)
(GET "/login2" [] pages/login)
(GET "/requires-authentication" req
(friend/authenticated "Thanks for authenticating!"))
(GET "/role-user" req
(friend/authorize #{::user} "You're a user!"))
(GET "/role-admin" req
(friend/authorize #{::admin} "You're an admin!"))
(GET "/form" [] pages/form-test)
(POST "/form" {form :form-params}
(str "hello " (get form "username")))
(GET "/session" {session :session :as params} (pages/test-sess params))
(GET "/admin" req
(friend/authorize #{::admin}
#_any-code-requiring-admin-authorization
"Admin page."))
(route/resources "/")
(route/not-found "Not Found"))
(defn cookie-session
[handler]
(wrap-session handler
{:store (cookie-store {:key "XXXXXXXXXXXXXXXX"})
:cookie-name "my-app-session"
:cookie-attrs {:max-age (* 60 60 24 30)}}))
(def app
(-> (handler/site app-routes)
cookie-session))
(def secured-app
(-> app
(friend/authenticate {:allow-anon? true
:credential-fn (partial creds/bcrypt-credential-fn @users)
:workflows [(workflows/interactive-form)]
:login-uri "/login2"
})
; ...required Ring middlewares ...
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment