-
Notifications
You must be signed in to change notification settings - Fork 12
Make Validator and ContextKey required input #90
Conversation
This addresses @jozsefsallai point in #86 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check the review comments
Maybe I have a misunderstanding: ContextKey is the name of the (locals) variable in which the actual key is saved and not the key itself, right? |
Correct, contextKey is the key under which the data is stored in the local variables |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree w/ the above, I don't think we should change anything in the context locals without the user's knowledge. There's no real reason for doing so anyway (that I can think of).
Hi all, But then where do we specify the actual key, does that mean Config should get a new argument or would the user have to write the key to the fiber context separetely? (at the moment we specify the password as contextKey and use that in the validateAPIKey function.) Or in other words, is this what you had in mind?: Add another element to Config eg |
The context key already exists in the config and is specified per configuration by the user |
Will try tomorrow to provide an example or a tutorial or change (depending on how much time I have tomorrow) |
At the moment, I would envision the functionality like so:
Then inside keyauth, the string "MySecretPassword" is saved to One Problem that I see, is that when we have multiple instances of the middleware, we need to store multiple ApiKeys and need to have multiple ContextKeys, and because the ContextKeys are different, we also need to have multiple Validator functions. Is it possible to make it easier, so that only the ApiKey must be provided even when multiple middlewares are used? |
Note that the last two commits would implement that idea |
I have updated the code (by removing most of the changes, except the panic in case no validator is provided, as well as I kept the tests). As far as I understood your intentions @ReneWerner87, your points should be taken care of. |
Note that the test cases for param needed some small changes in the key extraction code (see b759180). Notably, it uses twice (!) the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please use subtests in the loops
https://go.dev/blog/subtests
and explain the url escape case more
(cannot find the url with the bad character)
I am working on the subtest cases and will push later. Regarding the An example would be the following app:
which should authenticate with curl http://localhost:3000/key/specials%3A%20%21%24%25%2C.%23%22%21%3F~%60%3C%3E%40%24%5E%2A%28%29%7B%7D%5B%5D%7C%2F%5C123
#> Comparing keys:
#> GOT: 'specials: !$%,.#"!?~`<>@$^*(){}[]|/\123'
#> WANT: 'specials: !$%,.#"!?~`<>@$^*(){}[]|/\123'
#> ===> true
|
thx, after the change for the subtests i will merge it |
Tests are restructured to use subtests. The output looks a lot cleaner now.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@DavZim can you do the same for TestKeyAuth and TestMultipleKeyAuth in the loop
Revisiting the |
yes is ok for me |
All code is pushed. From my side we are good to merge! |
This PR makes the Validator function as well as the ContextKey (API Key) a required input for keyauth.
It also adds some basic tests to keyauth