liberator

kwladyka 2019-01-17T10:36:10.023100Z

Track UI is so cool. Thanks you showed it to me.

kwladyka 2019-01-17T10:36:49.023600Z

How can I know decision: authorized? true use :authorized key under the hood?

ordnungswidrig 2019-01-17T13:34:34.024600Z

Oh, sorry, I had a typo earlier I always ment to write :authorized?

kwladyka 2019-01-17T13:35:43.024900Z

oh ok

kwladyka 2019-01-17T13:36:33.025200Z

What about fn like :as-response? Are there more of them?

kwladyka 2019-01-17T13:37:03.025500Z

I mean this fn is not listed anywhere in Actions, Decisions, Hanlders etc.

kwladyka 2019-01-17T14:00:46.026Z

But fn is useful

kwladyka 2019-01-17T14:55:43.026800Z

(resource {:allowed-methods [:post :options]
                       :available-media-types ["application/edn" "application/json"]
                       :handle-options (fn [_]
                                         (ring-response {}))
                       :handle-created (fn [{:keys [request] :as ctx}]
                                         (let [{:keys [params]} request
                                               {:keys [query]} params]
                                           (ring-response
                                             {:status 200
                                              :body (when query (lacinia/execute foo/schema query nil nil))})))
                       :as-response (fn [d ctx]
                                      (-> (as-response d ctx)
                                          (assoc-in [:headers "Access-Control-Allow-Origin"] "*")
                                          (assoc-in [:headers "Access-Control-Allow-Headers"] "Content-Type")))})
Is a way to not do :handle-options which return nothing to add headers in :as-response?

kwladyka 2019-01-17T14:56:06.027300Z

without this key, as-reponse is not run 😕

kwladyka 2019-01-17T14:56:21.027600Z

because :handle-options is nil

kwladyka 2019-01-17T15:00:55.028Z

I guess it should because :options is in allowed-methods

kwladyka 2019-01-17T19:10:42.029Z

From tests it sounds like :as-response should work without :handle-options

ordnungswidrig 2019-01-17T20:07:21.029700Z

@kwladyka I will look into that tomorrow

kwladyka 2019-01-17T20:08:03.030200Z

thank, at that moment I have this:

(resource {:allowed-methods [:post :options]
                              :new? false
                              :handle-options (constantly (ring-response {:status 401}))
                              :handle-no-content (fn [{:keys [request] :as ctx}]
                                                   (let [{:keys [params]} request
                                                         {:keys [email password]} params]
                                                     (if-let [uuid (auth/?login->uuid email password)]
                                                       (ring-response {:session {:uuid uuid}})
                                                       (ring-response {:status 401
                                                                       :session nil}))))})
This is the best what I achieved so far

kwladyka 2019-01-17T20:08:09.030500Z

I am working on graphql response now

kwladyka 2019-01-17T21:56:45.031200Z

(def handler-map
  {:not-found (constantly (not-found "404 not found"))
   ;; authentication - only for web browser users from our domains
   :authentication (resource {:allowed-methods [:post :options]
                              :new? false
                              :handle-options (constantly (ring-response {:status 204}))
                              :handle-no-content (fn [{:keys [request] :as ctx}]
                                                   (let [{:keys [params]} request
                                                         {:keys [email password]} params]
                                                     (if-let [uuid (auth/?login->uuid email password)]
                                                       (ring-response {:session {:uuid uuid}})
                                                       (ring-response {:status 401
                                                                       :session nil}))))})
   ;; public API, authentication and authorization by token or session
   :graphql (resource {:allowed-methods [:post :options]
                       :available-media-types ["application/edn" "application/json"]
                       :handle-options (constantly (ring-response {:status 204}))
                       :authorized? (fn [{:keys [request] :as ctx}]
                                      (let [{:keys [session headers]} request
                                            {:keys [uuid]} session
                                            {:strs [authorization]} headers
                                            token (some->> authorization
                                                           (re-matches #"(?i)Bearer\s([\d|a-f]{8}-(?:[\d|a-f]{4}-){3}[\d|a-f]{12})")
                                                           (second))]
                                        (or (auth/?token->uuid token)
                                            (auth/uuid-exist? uuid))))
                       :new? false
                       :respond-with-entity? true
                       :handle-ok (fn [{:keys [request] :as ctx}]
                                    (let [{:keys [params]} request
                                          {:keys [query]} params]
                                      (ring-response
                                        {:body (when query (lacinia/execute graphql/schema query nil nil))})))
                       :as-response (fn [d ctx]
                                      (-> (as-response d ctx)
                                          (assoc-in [:headers "Access-Control-Allow-Origin"] "*")
                                          (assoc-in [:headers "Access-Control-Allow-Headers"] "Content-Type")))})})
I did progress

kwladyka 2019-01-17T21:57:28.031900Z

But probably I will have to move :authorized? to :handle-ok, because I have to get this token once again there or get uuid from session

kwladyka 2019-01-17T21:58:04.032500Z

Unless there is a way to inject uuid to :handle-ok