LoginSignup
0
0

More than 5 years have passed since last update.

ductでbasic認証をかける

Last updated at Posted at 2018-01-21

正直よくわかってないけど動いたからメモしておきます。

依存関係にbuddyを追加する

project.clj
[buddy/buddy-auth "2.1.0"]

middlewareを設定する

config.edn
:duct.core/handler
{:middleware [#ig/ref :foo.auth/basic-auth]}

:foo.auth/basic-auth {}

basic認証の処理を定義する

auth.clj
(ns foo.auth
  (:require [integrant.core :as ig]
            [buddy.auth :refer [authenticated?]]
            [buddy.auth.accessrules :refer [restrict]]
            [buddy.auth.backends.httpbasic :refer [http-basic-backend]]
            [buddy.auth.middleware :refer [wrap-authentication wrap-authorization]]))

;; 入力が正しければtrueを返す関数を用意する
(defn authfn [_ {:keys [username password]}]
  (and (= username "a") (= password "a")))

(def auth-backend (http-basic-backend {:authfn authfn}))

(defmethod ig/init-key ::basic-auth [_ options]
  (fn [handler]
    (-> handler
        ;; サイト全体にbasic認証をかけたいのでrestrictを使う
        (restrict {:handler authenticated?})
        (wrap-authorization auth-backend)
        (wrap-authentication auth-backend))))

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0