Jonathon McKitrick
unread,Aug 6, 2014, 7:21:22 AM8/6/14Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to clo...@googlegroups.com
First, the code:
(ns pts.server
(:use [compojure.core])
(:require [ring.adapter.jetty :as jetty]
[ring.util.response :as response]
[compojure.handler :as handler]
[compojure.route :as route]
[cemerick.friend :as friend]
(cemerick.friend [workflows :as workflows]
[credentials :as creds])))
(defroutes www-routes
(GET "/locked" [] (friend/authorize #{::admin} "Admin only"))
(GET "/home" [] (response/file-response "home.html" {:root "resources/public"}))
(GET "/login" [] (response/file-response "login.html" {:root "resources/public"}))
(GET "/" [] (response/redirect "index.html"))
(route/resources "/")
(route/not-found "Not Found"))
(def app (handler/site www-routes))
(def users {"root" {:username "root"
:password (creds/hash-bcrypt "toor")
:roles #{::admin}}})
(def secure-app
(-> app
(friend/authenticate {:unauthorized-handler #(response/status (response/response "NO") 401)
:credential-fn (partial creds/bcrypt-credential-fn users)
:workflows [(workflows/interactive-form)]})))
(defn -main [& args]
(let [port (Integer/parseInt (get (System/getenv) "PORT" "3000"))]
(jetty/run-jetty secure-app {:port port :join? false})))
It's dead simple, but 2 major things are not working.
1. The POST to /login to submit the login form gives a 404 Not Found. Isn't the POST handler part of the friend/authenticate middleware?
2. Attempts to access the /locked URL throw an exception and a stacktrace, rather than calling the unauthorized handler:
throw+: {:cemerick.friend/required-roles #{:pts.server/admin},
:cemerick.friend/exprs ["Admin only"], :cemerick.friend/type
:unauthorized, :cemerick.friend/identity nil}
What am I doing wrong here?