-
-
Save anonymous/bf566201083c937e868d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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