-
Notifications
You must be signed in to change notification settings - Fork 157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cookies #2 #235
Cookies #2 #235
Conversation
Cool! I took a quick glance, and the new API looks nice to me. :) Is this a breaking change? |
Ah, yes forgot about that, I'll annotate the commits when I get a chance later (as well as test a beta build which seems to have broken from inference changes in 1.2?) |
This can be used to add some compile time dependencies for Middleware and Plugins. To fix code broken by this, you will need to introduce a type parameter for Request, Response, MiddlewareResult and NickelError. ``` // e.g. This fn foo<'a>(&mut Request, Response<'a>) -> MiddlewareResult<'a> // Should become: fn foo<'a, D>(&mut Request<D>, Response<'a, D>) -> MiddlewareResult<'a, D> ``` You can add bounds to `D` in the above to place compile-time restrictions on the server data (can be used for configuration). [breaking-change]
Also adds `on_send` which can be used to execute code when the Response headers are being sent.
Managed to get a build on 1.1 and beta (1.2)... which ICEs on 1.0. Sweet. |
The 1.2 beta has a regression for this kind of code, so being explicit should allow us to compile on all targets.
Alright, things work across rust versions and have an additional compile-fail test to check that the error message is 'reasonable' when using cookies without the appropriate bounds being met. cc @cburgdorf |
Sorry, for jumping on it late. I skimmed through the changes and it's quite a brain twister for me to follow ;) Looking at the example code I wonder what's the advantage of having the user to define a data structure for the cookie. Iron just defines There may be good reasons to implement it our way but I'd like to understand them better ;) |
That struct is needed to encrypt the Cookie, which Iron does not appear to be doing. So in Iron, the user would instead have to manually deal with encrypting whatever data they want to send, but as they don't have an example for that, Iron does come across are easier :) My suggestion is that we provide a struct so that the user doesn't have to create one in the most simple case. Then the example could be:
or something, and the user would not have to create a struct and initialize it. We could also add a case for those who prefer use the same key:
What do you think, @Ryman? |
That repo looks like it hasn't been updated in a year, but to be clear, the advantage is that this way will offer a compile-time guarantee, as you can see this error message is given when not handled. If we emulated the way a usual plugin would do it, then we get the dependency problem where you'll have a runtime crash due to the unwrap (see the first line in If we had specialization then I think we could provide a zero'd key for things which don't implement @simonpersson It does/did handle encryption (see here). But it does implement most of it by itself rather than leveraging cookie-rs (not sure if cookie-rs was available at the time though so it makes sense). I think I don't think generating a new key on every server restart is a good idea as that invalidates all old cookies, providing the Perhaps we need to consider how configuration will look for nickel in general, to place my vote, I'm in favor of sane implicit defaults (even so far as the server could autogenerate a config on first boot if no config file is available). This would put us nowhere near 'express like' though and align more with something like rails. |
@Ryman: It only signs the cookie, no encryption, no? Or am I missing something? Perhaps we can break the example out to two examples, one where we show the most simple use of Cookies and one where we demonstrate sharing of data. I suppose the I suppose I prefer that users who know nothing about encryption gets a safe key generated, and having the cookies become invalidated on a server restart, then having the same user provide an insufficiently strong key. Users who know what they're doing can still provide their own key. I very much like the idea of generating a config file though! Then we would get around that problem entirely by storing a generated key for the user. |
@simonpersson Ah yes I'm totally wrong about the encryption :P In the simple use of cookies, do you mean with no key, or with just passing something like Yeah, I would prefer a system where there are safe defaults rather than defaulting to something with a null key or no encryption. Perhaps we can elaborate on another ticket how we will handle simpler configuration, or do you think it's a blocking issue for cookies for now? |
@Ryman To be fair though, it is a bit odd to sign but not encrypt, I was also a bit confused at first. Yes, just passing a I don't think it is blocking cookies. We can always add that later. Feel free to open another ticket. |
Closing in favor of #272 |
cc @simonpersson
This took a bit more thinking than I'd anticipated, but it required something similar to solving #79 for some configuration without incurring ordering dependencies (which could be potentially insecure for signed cookies) that seems slightly obvious in hindsight (and is very similar to something you suggested there). Still feels a bit iffy though, but I think it's sound enough to be useful.
The last commit does a bit of weirdness with where clauses on
Self
to collapse theCookies
trait into a single trait, but I think it's a lot nicer for users to only have to worry about importing a single trait.cc #234 (original cookies implementation)