Fork me on GitHub
#yada
<
2019-01-25
>
jaihindhreddy16:01:09

^ Thank you for that.

kwladyka16:01:34

ok, so how can I help?

kwladyka16:01:14

this is my research code:

(defn build-resource [model]
  (-> (merge {} #_{:show-stack-traces? false} model)
      (yada/resource)))

(def not-found
  (-> (yada/as-resource nil)
      (build-resource)))

(def authentication
  (build-resource
    {:id :authentication
     :consumes #{"application/x-www-form-urlencoded" "application/edn" "application/json"}
     ; :access-control
     :methods {:post {:produces "application/json"
                      :parameters {:form {:email String
                                          :password String}}
                      :response (fn [{:keys [parameters] :as ctx}]
                                  (get-in parameters [:parameters :form :query])
                                  (println "ctx" (pr-str ctx))
                                  ;(println (pr-str body))
                                  (println "parameters" (pr-str parameters))
                                  ;(println (pr-str form))
                                  (let [{:keys [email password]} (:body parameters)]
                                    {:token "foo"
                                     :email email
                                     :password password}))}}}))

(def graphql
  (build-resource
    {:id :graphql
     :access-control {:realms {"session" {:authentication-schemes [{:scheme :cookie
                                                                    :verify (fn [cookie]
                                                                              (println "auth cookie" cookie)
                                                                              {:a 1})}]
                                          :authorization {:validate (fn [ctx creds]
                                                                      (println "creds" creds)
                                                                      ctx)}}}}
     :produces #{"application/json" "application/edn"}
     :methods {:post {:consumes #{"application/json" "application/graphql"}
                      :parameters {:body s/Any}
                      :response (fn [{:keys [authentication authorization body cookies] :as ctx}]
                                  (println "ctx" ctx)
                                  (println "cookies" cookies)
                                  (println "authentication" authentication)
                                  (println "authorization" authorization)
                                  ;(println "body" body)
                                  ;(println "request" (:request ctx))
                                  (let [query (get-in ctx [:parameters :form :query])]
                                    {:token "foo"
                                     :query query}))}}}))

kwladyka16:01:21

it should help to start

kwladyka16:01:50

this is how I test:

(defmacro with-server [handler build-url & body]
  `(let [listener# (yada/listener ~handler)
         close# (:close listener#)
         port# (:port listener#)
         ~build-url (fn [path#]
                      (str "" port# path#))]
     (try
       [email protected]
       (finally
         (close#)))))
(deftest graphql-test
  (with-server core/handler build-url
    (testing "Graphql"
      (is (= 405 (-> @(http/request
                        {:url (build-url "/graphql")
                         :method :get})
                     :status))
          "GET is not allowed.")
      (is (= 401 (-> @(http/request
                        {:url (build-url "/graphql")
                         :headers {"Content-Type" "application/json"}
                         :method :post
                         :body "{\"email\":\"\",\"password\":\"qwaszx\"}"})
                     :status))
          "Not authorized")
      #_(is (= 200 (-> @(http/request
                          {:url (build-url "/graphql")
                           :headers {"Content-Type" "application/json"}
                           :method :post
                           :params {:query "{ game_by_id(id: \"1237\") { name designers { name }}}"}})
                       :status))
            "Authentication by session")
      (is (= 200 (-> @(http/request
                        {:url (build-url "/graphql")
                         :headers {"Content-Type" "application/json"
                                   "Authorization" "bearer 89abddfb-2cff-4fda-83e6-13221f0c3d4f"}
                         :method :post
                         :body "{\"email\":\"\",\"password\":\"qwaszx\"}"})
                     :status))
          "Authentication by token"))))

jaihindhreddy16:01:47

I converged to something very similar yesterday. No graphql though.

jaihindhreddy16:01:02

Also, I tried to get juxt/edge working but couldn't

jaihindhreddy16:01:08

Is the documentation up to date?

dominicm17:01:53

@UBU6QCSJH the edge documentation is up to date, is that what you had issues with?

jaihindhreddy17:01:53

@U09LZR36F It felt like that to me because Adapting Edge for new projects comes after Dev guide in the documentation. My bad. Too much coffee.

dominicm17:01:36

@UBU6QCSJH I'll take that feedback on! It's quite difficult to figure out the structuring for this stuff. Thank you!

😅 1
dominicm17:01:52

Thanks, will try do this now

dominicm17:01:59

Actually, will have to wait. Making note now though.

kwladyka16:01:14

It is totally not up to date 🙂

kwladyka16:01:29

The worst part about yada is documentation

kwladyka16:01:47

If you want to figure out something read tests in git repository

jaihindhreddy16:01:03

That explains a couple of things

kwladyka16:01:06

you use it with :authentication-schemes but I didn’t finish this yet

kwladyka16:01:55

BTW :cookies auth from doc is not up to date, it doesn’t work. There is even not code for that in yada repo.

kwladyka19:01:01

https://github.com/juxt/yada/blob/master/ext/oauth2/src/yada/oauth.clj - I think this code is not up to date, especially {cookie "session"}. There is no way to set different cookie name. Probably doesn’t matter but can be confused.

kwladyka19:01:04

:yada.oauth2/secret this is confuse too, because there is no such ns

kwladyka19:01:23

oh the file was last modified in 2017

kwladyka19:01:44

Should I consider using oauth2 from yada?

kwladyka19:01:41

How do you use oauth2 in yada?