:credential-fn没有被朋友/身份验证中间件调用?

时间:2014-01-25 16:24:57

标签: clojure compojure ring

我正在尝试将FriendLuminus(基本上为lib-noir)一起使用,而且我发现我的:credential-fn根本没有被调用。起初我想也许我添加了中间件错误,但我尝试设置一个自定义:login-failure-handler,看看是否至少有效,但确实如此。

以下是我的应用的样子:

(defroutes my-routes
  (GET "/login" [login_failed username]
       (login-page login_failed username))  ;; login-page just renders HTML form
  (GET "/logout" req
       (friend/logout* (resp/redirect (str (:context req) "/")))))

(defn login-failure-handler [req]
  (println "Failed")  ;; <-- get's printed and redirects no problem
  (redirect "/login?failed=Y"))

(defn credential-fn [creds-map]
  (println creds-map)  ;; <-- doesn't get printed at all
  {:identity "test" :roles [::user]})

;; define custom wrapping middleware as noir's middleware/app-handler does
;; its own thing with routes + middleware
(defn authenticate [handler]
  (friend/authenticate
    handler
    {:workflows [(workflows/interactive-form
                   :credential-fn credential-fn
                   :login-failure-handler login-failure-handler)]}))

(defn debugger [handler]
  (fn [req]
    (println req)
    (handler req)))

(def app (middleware/app-handler
          ;; add your application routes here
          [my-routes]

          ;; add custom middleware here
          :middleware [debugger authenticate]

          ;; add access rules here
          :access-rules []

          ;; serialize/deserialize the following data formats
          ;; available formats:
          ;; :json :json-kw :yaml :yaml-kw :edn :yaml-in-html
          :formats [:json-kw :edn]))

我还添加了一个debugger中间件,只打印出请求图,我注意到当我点击我的登录表单时它根本不打印任何提交按钮(即没有发生POST请求)。它只是立即将GET打印到login-failure-handler

我错过了什么?

2 个答案:

答案 0 :(得分:2)

原来我的登录表单不正确。朋友具体需要来自POST请求的{2}格式参数,usernamepassword。因此登录表单需要才能拥有这两个字段。似乎没有办法在Friend方面自定义这些字段,我认为这是一个缺陷。

答案 1 :(得分:0)

您需要将credential-fn传递给friend/authenticate,如下所示:

(friend/authenticate
  handler
  {:credential-fn credential-fn
   :workflows [(workflows/interactive-form)]})