yada

jaihindhreddy 2019-01-25T16:17:09.363Z

^ Thank you for that.

kwladyka 2019-01-25T16:23:34.363400Z

ok, so how can I help?

kwladyka 2019-01-25T16:24:14.363800Z

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}))}}}))

kwladyka 2019-01-25T16:24:21.364Z

it should help to start

kwladyka 2019-01-25T16:24:50.364400Z

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 "<http://localhost:>" port# path#))]
     (try
       ~@body
       (finally
         (close#)))))
(deftest graphql-test
  (with-server core/handler build-url
    (testing "Graphql"
      (is (= 405 (-&gt; @(http/request
                        {:url (build-url "/graphql")
                         :method :get})
                     :status))
          "GET is not allowed.")
      (is (= 401 (-&gt; @(http/request
                        {:url (build-url "/graphql")
                         :headers {"Content-Type" "application/json"}
                         :method :post
                         :body "{\"email\":\"<mailto:foo@example.com|foo@example.com>\",\"password\":\"qwaszx\"}"})
                     :status))
          "Not authorized")
      #_(is (= 200 (-&gt; @(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 (-&gt; @(http/request
                        {:url (build-url "/graphql")
                         :headers {"Content-Type" "application/json"
                                   "Authorization" "bearer 89abddfb-2cff-4fda-83e6-13221f0c3d4f"}
                         :method :post
                         :body "{\"email\":\"<mailto:foo@example.com|foo@example.com>\",\"password\":\"qwaszx\"}"})
                     :status))
          "Authentication by token"))))

jaihindhreddy 2019-01-25T16:25:47.364800Z

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

jaihindhreddy 2019-01-25T16:26:02.365300Z

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

jaihindhreddy 2019-01-25T16:26:08.365500Z

Is the documentation up to date?

kwladyka 2019-01-25T16:27:14.365700Z

It is totally not up to date 🙂

kwladyka 2019-01-25T16:27:29.366Z

The worst part about yada is documentation

kwladyka 2019-01-25T16:27:47.366500Z

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

jaihindhreddy 2019-01-25T16:27:47.366600Z

Oohhh

jaihindhreddy 2019-01-25T16:28:03.366900Z

That explains a couple of things

jaihindhreddy 2019-01-25T16:28:07.367100Z

Thanks!

kwladyka 2019-01-25T16:28:43.367400Z

https://github.com/juxt/yada/search?q=defmethod+verify&amp;unscoped_q=defmethod+verify how to write authentication code

kwladyka 2019-01-25T16:30:06.367700Z

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

kwladyka 2019-01-25T16:33:55.369Z

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

dominicm 2019-01-25T17:07:53.369400Z

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

jaihindhreddy 2019-01-25T17:13:53.369600Z

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

dominicm 2019-01-25T17:14:36.370Z

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

😅 1
jaihindhreddy 2019-01-25T17:15:38.370300Z

Also, s/discreet/discrete/g on https://juxt.pro/edge/docs/why-edge.html

dominicm 2019-01-25T17:21:52.370700Z

Thanks, will try do this now

dominicm 2019-01-25T17:21:59.370900Z

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

kwladyka 2019-01-25T19:15:01.371900Z

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.

kwladyka 2019-01-25T19:19:04.372500Z

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

kwladyka 2019-01-25T19:19:23.372800Z

oh the file was last modified in 2017

kwladyka 2019-01-25T19:19:44.373400Z

Should I consider using oauth2 from yada?

kwladyka 2019-01-25T19:21:41.373700Z

How do you use oauth2 in yada?