Skip to content
This repository has been archived by the owner on Jan 15, 2022. It is now read-only.

Commit

Permalink
refactored auth
Browse files Browse the repository at this point in the history
  • Loading branch information
yogthos committed Sep 2, 2014
1 parent 9dafc7b commit dc7881e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
7 changes: 5 additions & 2 deletions src/yuggoth/routes/services/auth.clj
Expand Up @@ -6,15 +6,18 @@
(:import org.apache.commons.codec.binary.Base64
java.nio.charset.Charset))

(defn auth [req]
(get-in req [:headers "authorization"]))

(defn decode-auth [encoded]
(let [auth (second (.split encoded " "))]
(-> (Base64/decodeBase64 auth)
(String. (Charset/forName "UTF-8"))
(.split ":"))))

(defn login! [auth]
(defn login! [req]
(let [admin (db/get-admin)
[user pass] (decode-auth auth)]
[user pass] (decode-auth (auth req))]
(if (and (= user (:handle admin))
(crypt/compare pass (:pass admin)))
(do (session/put! :admin admin)
Expand Down
26 changes: 9 additions & 17 deletions src/yuggoth/routes/services/core.clj
Expand Up @@ -17,17 +17,6 @@
(defn edn-response [result]
(->> result edn (set-headers no-cache)))

(defmacro GET [uri params & body]
`(compojure/GET ~uri ~params
(do ~@(butlast body)
(edn-response ~(last body)))))

(defmacro GET-restricted [uri params & body]
`(compojure/GET ~uri ~params
(restricted
(do ~@(butlast body)
(edn-response ~(last body))))))

(defmacro try-body [body]
`(try
~@(butlast body)
Expand All @@ -36,14 +25,20 @@
(.printStackTrace t#)
(status 400 (edn {:error (.getMessage t#)})))))

(defmacro GET [uri params & body]
`(compojure/GET ~uri ~params (~'try-body ~body)))

(defmacro GET-restricted [uri params & body]
`(compojure/GET ~uri ~params
(restricted (~'try-body ~body))))

(defmacro POST [uri params & body]
`(compojure/POST ~uri ~params
(~'try-body ~body)))

(defmacro POST-restricted [uri params & body]
`(compojure/POST ~uri ~params
(restricted
(~'try-body ~body))))
(restricted (~'try-body ~body))))

(defmacro POST-restricted-raw [uri params & body]
`(compojure/POST ~uri ~params
Expand All @@ -54,11 +49,8 @@
(catch Throwable t#
(.getMessage t#))))))

(defn auth [req]
(get-in req [:headers "authorization"]))

(compojure/defroutes service-routes
(POST "/login" req (login! (auth req)))
(POST "/login" req (login! req))
(GET "/logout" [] (logout!))

(GET "/tag/:tagname" [tagname] (archives-by-date (db/posts-by-tag tagname)))
Expand Down

0 comments on commit dc7881e

Please sign in to comment.