yada

martinklepsch 2018-01-30T08:44:13.000291Z

Hey there. I’m using yada/lean with a handler like this:

(def webhook-handler
  (yada/handler
   (yada/resource
    {:methods
     {:post
      {:consumes #{"application/json"}
       :produces "text/plain"
       :response (fn [ctx] "worked")}}})))
Now when I make a request using curl:
curl -X POST -d '{"some":"thing"}' -H "Content-Type: application/json" localhost:3000/webhook
I get 415 Unsupported Media Type@dominicm mentioned this may be because yada/lean doesn’t have yada/json but since I’m specifying that I’m able to handle json in the :consumes key I feel like that handler should accept json?

martinklepsch 2018-01-30T09:16:02.000180Z

[deleted]

martinklepsch 2018-01-30T09:23:30.000031Z

I’m having some trouble getting :access-control to do what I want. I use the following function as :verify

(defn auth [[user password]]
  (let [m {"cljdoc" "cljdoc"}]
    (when (get m user)
      {:user user})))
Now when I send a request with -u cljdoc:cljdoc there is no WWW-Authenticate header returned. In contrast when I send a request with -u foo:bar there is a WWW-Authenticate header returned. In both cases the handler runs as if authentication returned a value which, i.e. the handler is run. This is confusing with regards to this quote from the yada docs: > However, if you return nil, this will be treated as no credentials being sent and a 401 Unauthorized response will be returned.

martinklepsch 2018-01-30T09:33:22.000122Z

I get it to work if I set :authorization for the given method but I’m not sure if using these two in combination is required? The docs seem to treat them as separate concerns?

dominicm 2018-01-30T09:41:24.000235Z

:authorization is about a minimum set of requirements for who is/isn't authenticated. You could say that unauthenticated users are :user, normal users have :user, and admins have :admin.

malcolmsparks 2018-01-30T11:50:52.000039Z

Hi @martinklepsch sorry this is late. I'm a bit confused - your auth function is returning non-nil with cljdoc:cljdoc, so that means it's passing the authentication check. That means there won't be a 401, so no WWW-Authenticate header

malcolmsparks 2018-01-30T11:51:58.000459Z

there is a built-in "Basic" defmethod, where "Basic" will be sent back in the WWW-Authenticate header. If your defmethod uses a string rather than a keyword then the behaviour is slight different

malcolmsparks 2018-01-30T11:52:49.000224Z

So in summary I'm a little confused -could you expand on what you're seeing?

martinklepsch 2018-01-30T12:02:41.000429Z

@malcolmsparks I was expecting that I could restrict access to a resource using just the :verify/ Authentication functionality. It seems that this kind of thing requires the user of :authorization though.

martinklepsch 2018-01-30T12:03:44.000352Z

basically there does not seem to be a way to return 403 just by returning some value from :verify

malcolmsparks 2018-01-30T12:17:00.000047Z

Ah. I understand now. You cannot restrict a resource with authentication only. Its only a step of establishing credentials. Think of this as a passport. If you want to restrict access you need an authorisation step. Think of this as a border guard who checks the passport and visa and possibly other credentials and documents

malcolmsparks 2018-01-30T12:18:10.000113Z

This is not how other Web frameworks interpret http

malcolmsparks 2018-01-30T12:18:14.000060Z

https://juxt.pro/blog/posts/yada-authentication.html

martinklepsch 2018-01-30T12:18:14.000457Z

Got it, I kind of figured that this may be the thinking here but I found the docs a bit confusing then

martinklepsch 2018-01-30T12:18:57.000215Z

specifically this bit: https://cljdoc.martinklepsch.org/yada/yada/1.2.10/doc/basics/security/#basic-authentication

malcolmsparks 2018-01-30T12:19:07.000189Z

The blog article is better than the docs right now. But we're going to improve the docs soon especially with cljdoc right.?

martinklepsch 2018-01-30T12:19:14.000488Z

> If you return an empty map (a truthy value) and the resource requires credentials that aren’t in the map, a 403 Forbidden response will be returned. However, if you return nil, this will be treated as no credentials being sent and a 401 Unauthorized response will be returned.

martinklepsch 2018-01-30T12:19:53.000009Z

Didn’t even look for a blog post 😛

malcolmsparks 2018-01-30T12:20:01.000129Z

Ah that's wrong. I need to rewrite that section

martinklepsch 2018-01-30T12:20:32.000255Z

unrelated other newb question: how to return empty body with 200 OK?

martinklepsch 2018-01-30T12:21:09.000413Z

Dominic mentioned just (assoc-in ctx [:response :status] 200) but with text/plain that will print the entire context 🐵

danielcompton 2018-01-30T12:31:38.000251Z

@martinklepsch you want more like (assoc (:response ctx) :status 200). :response is a record in Yada, so it can be distinguished from other maplike objects

2018-01-30T13:02:43.000207Z

if you want to be http compliant, you should probably return 204, so that it's clear that client shouldn't expect any other data from the server

martinklepsch 2018-01-30T15:49:45.000413Z

@danielcompton that does the trick! @wdullaer thanks good point 🙂

dominicm 2018-01-30T16:37:10.000564Z

I wonder if yada handles "" as 204 :thinking_face:

martinklepsch 2018-01-30T18:01:04.000041Z

Is there a go-to way to print exceptions somewhere?

martinklepsch 2018-01-30T18:01:24.000458Z

Do I need to write an interceptor or is there something basic built-in?

dominicm 2018-01-30T18:05:04.000163Z

There's a logging chain, which might be useful

martinklepsch 2018-01-30T18:16:52.000567Z

@dominicm can you think of any example code that I may be able to steal from? 😛

dominicm 2018-01-30T18:52:58.000130Z

We use it internally, I don't know of anything public. Unless someone added tests for the feature.

martinklepsch 2018-01-30T19:18:47.000594Z

Something seems to be really broken in my project in that I can’t log anything even just using plain log/info at the repl 🙈

2018-01-30T19:32:07.000033Z

if you assoc it, it'll set it. I seem to remember that if you send an empty reply it does just set 204 as the status, but it's been a while. seems easy enough to test 🙂

malcolmsparks 2018-01-30T19:33:14.000498Z

Hmm. That could be the async nature of manifold, and binding conveyance.

malcolmsparks 2018-01-30T19:33:38.000640Z

Can you log to a file or via logback?

malcolmsparks 2018-01-30T19:35:19.000242Z

@martinklepsch yes. That logging interceptor was added for a juxt client. You can use it.

malcolmsparks 2018-01-30T19:35:51.000050Z

From the code you add :logger of course

malcolmsparks 2018-01-30T19:37:59.000614Z

It is a problem that yada only really shines for the devs who get it and have figured out how it hangs together. Often you need some familiarity with the source.

malcolmsparks 2018-01-30T19:38:11.000448Z

Docs will help

malcolmsparks 2018-01-30T19:39:01.000547Z

(I am resolved to redouble my efforts to provide better docs)

martinklepsch 2018-01-30T19:39:26.000253Z

You say this and yada already has some of the nicest docs on the block 😛

malcolmsparks 2018-01-30T19:39:53.000220Z

Too kind. The docs are rather incomplete