~pepe/neil

cecee4378a6527595a64ec07651caf0a8380af88 — Josef Pospíšil 11 days ago 6342e4a
Add authentification
4 files changed, 43 insertions(+), 10 deletions(-)

M neil/acts.janet
M neil/watch/init.janet
M neiloffice
A templates/auth/login.temple
M neil/acts.janet => neil/acts.janet +3 -2
@@ 32,9 32,10 @@
(defact OpenWatch
  {:watch
   (fn [_ e _]
     (def {:chidi c :trevor t :hemple h :env env} e)
     (def {:chidi c :trevor t :hemple h :env env
           :password p :key k} e)
     (cocoon/give
       (watch/start c h t env)
       (watch/start c h t env p k)
       (cocoon/die)))
   :effect
   (fn [_ {:chidi h :env env} _]

M neil/watch/init.janet => neil/watch/init.janet +33 -8
@@ 3,6 3,7 @@
(use chidi/middleware)
(use marble)
(use bearimy)
(use jhydro)
(import shawn/cocoon)

(use /neil/utils)


@@ 68,31 69,55 @@
    (put conn :neil neil)
    (next-middleware neil req)))

(defn check-session [next-middleware]
  (fn [req]
    (def {:headers h} req)
    (if (get-in (tracev h) ["Cookie" "session"])
      (next-middleware req)
      (if (h "HX-Request")
        "<script>document.location = '/auth'</script>"
        (see-other "/auth")))))

(defn auth-form [req]
  (layout @{:content (page auth/login @{})}))

(defn auth-set [pwd key]
  (fn [{:body body}]
    (if (pwhash/verify pwd (body "password") key)
      (response 303 "" (merge {"Location" "/" "Content-Length" 0}
                              (cookie "session" (string "yes; Expires=" (:format (:later (make-calendar (today)) "24h")) "; HttpOnly"))))
      (see-other "/auth"))))

(defn dashboard [trevor]
  (fn [neil &]
  (fn [neil req]
    (def running (:task/running neil))
    (layout @{:title "Dashboard"
              :content
    (layout @{:content
              (page dash
                    @{:name "Ten last active tasks"
                      :trevor trevor
                      :running running})})))

(defn open [hemple trevor]
(defn open [hemple trevor pwd key]
  (defn neil-html-get [nm]
    (-> nm
        (set-neil hemple)
        (guard-methods "GET")
        html-success))
        html-success
        check-session
        cookies))

  (defn neil-urlenc-post [nm]
    (-> nm
        (set-neil hemple)
        urlencoded
        (guard-methods "POST")))
        (guard-methods "POST")
        check-session
        cookies))

  (drive
    {"/" (-> (dashboard trevor) neil-html-get)
     "/auth" (dispatch @{"GET" (-> auth-form html-success)
                         "POST" (-> (auth-set pwd key) urlencoded)})
     "/tasks" @{"/stop" (-> tasks/stop neil-urlenc-post)
                "/done" (-> tasks/done neil-urlenc-post)
                "/add" (-> (tasks/add) neil-urlenc-post)


@@ 130,8 155,8 @@
     :not-found (stoic "public")}
    journal))

(defn start [c h t env]
(defn start [c h t env p k]
  (chidi/start
    (open (h env) (t env))
    (open (h env) (t env) p k)
    ;(string/split ":" (c env))
    supervisor))

M neiloffice => neiloffice +3 -0
@@ 1,10 1,13 @@
#!/usr/bin/env janet
(use /neil/acts)
(use shawn)
(use jhydro)

(def shawn
  (initialize
    @{:image :neilstore
      :key "\xBEx\xCD\x1F\x97\xC1Ts/+\xFF\xF5\x9E\n\xA8.\x8B\x94CYW\x9CzW\xA5\x92\x1A}\xAA\xE4\x17q"
      :password "\x01^\x04 H\xFD@\xD9,\n\xEE\xA8\x88\x85\x8Fx\x07x\x9A\x19p:\xC8\x1A\xAB:\xE0\xA0\xBDr\x0FD\x90|\x8C\xC4kZ\x16u$XVu\x81\xC4\n\xFFk\xE9\xF8\x83u\x08\xBD\xB6K\xF2\x0F\xB2\x18 \x144.[r5\x1A\xBE\xF6\x81\xDA|\xB5\xFC'F(\x07\x90\x95\xB0e<@qs\xA3R\xF0\xFC2\x17r\xBF&\xA7O\xFB\xCA#\x99\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
      :hemple {:dev "neil:6660"
               :prod "neil.laststar.work:6660"}
      :chidi {:dev "neil:6661"

A templates/auth/login.temple => templates/auth/login.temple +4 -0
@@ 0,0 1,4 @@
<form action="/auth" method="POST">
  <input type="password" name="password"/>
  <button>Login</button>
</form>