Need basic authentication example
Following discussion at #144, this issue is a reminder to create a basic authentication example using Crow.
Ideally a login page to enter user+password is welcomed then a simple "hello world {{ user }}" to demonstrate how to achieve this feature :-)
Joel
This issue is waiting on #406, and the auth middleware to be built on top.
Hello @The-EDev Any news here ? :-) Thanks for the update, Joel
@joelguittet Hi. There are!
As for now, Session middleware is implemented as planned. It allows you to associate data with clients via cookies (securely). This way you can store whether the user is logged in and what role he has. I've even made a small example to show how to use it for login functionality.
The only other thing you would have to implement is user storage - password hasing, registration, etc. Storage is up to you. Hashing could be done with a custom hasher or crows SHA-1 (which is cryptographically broken in modern days). Because there isn't much more for basic authentication, I even doubt we need a separate middleware for this task - it would be too specific to be flexible.
If you have more questions regarding sessions or possible implementations feel free to ask! Also let me know if you have some thoughts or ideas on how to make a flexible authentication middleware 😄
Hi! Thanks for the quick reply. I was aware development was almost done, what I was looking for was the example you just indicated! Looks really great!! Yes sure hahding and storage on my side this is normal. I will do my login page and keep here updated to indicate my progress on this topic of my project :-)
Again thanks for your help, Regards
Hello @The-EDev
Just tried to build the example https://gist.github.com/dranikpg/fe215f26f825225a79b34e16b3a7d5e9 Failing. Can you indicate proper command line to do it ? Actually I have tried (crow and asio cloned locally):
ubuntu@ubuntu-VirtualBox:~/test_crow$ gcc example.cpp -I Crow/include -I asio/asio/include -lpthread -o example
In file included from example.cpp:2:
Crow/include/crow/middlewares/session.h: In instantiation of ‘crow::SessionMiddleware<Store>::SessionMiddleware(crow::CookieParser::Cookie, int, Ts ...) [with Ts = {const char*, crow::FileStore}; Store = crow::FileStore]’:
Crow/include/crow/middlewares/session.h:363:36: required from ‘crow::SessionMiddleware<Store>::SessionMiddleware(Ts ...) [with Ts = {const char*, crow::FileStore}; Store = crow::FileStore]’
example.cpp:30:5: required from here
Crow/include/crow/middlewares/session.h:355:67: error: no matching function for call to ‘crow::FileStore::FileStore(const char*, crow::FileStore)’
355 | store_(std::forward<Ts>(ts)...), mutex_(new std::mutex{})
| ^
Crow/include/crow/middlewares/session.h:509:9: note: candidate: ‘crow::FileStore::FileStore(const string&, uint64_t)’
509 | FileStore(const std::string& folder, uint64_t expiration_seconds = /*month*/ 30 * 24 * 60 * 60):
| ^~~~~~~~~
Crow/include/crow/middlewares/session.h:509:55: note: no known conversion for argument 2 from ‘crow::FileStore’ to ‘uint64_t’ {aka ‘long unsigned int’}
509 | FileStore(const std::string& folder, uint64_t expiration_seconds = /*month*/ 30 * 24 * 60 * 60):
| ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Crow/include/crow/middlewares/session.h:507:12: note: candidate: ‘crow::FileStore::FileStore(const crow::FileStore&)’
507 | struct FileStore
| ^~~~~~~~~
Crow/include/crow/middlewares/session.h:507:12: note: candidate expects 1 argument, 2 provided
Joel
@joelguittet
Replace the constructor to be the following: (remove the secret key)
// Init the app
crow::App<crow::CookieParser, Session> app {Session{
crow::FileStore{"./sessions"} // make sure this folder exists!!
}};
I've made this example during the PR, it changed a bit by the end - this is why its outdated. You can also check examples/session for the InMemoryStore - they're always up-to-date.
PS: you can tag me anytime - I'll try to respond quickly 😃 . The-EDev has been somewhat busy lately
Hello @dranikpg
Thanks for the quick reply and sorry to ping The-EDev, I understand and apologize :-)
Modification tested and working. I also fix myself an error in the command provided above: of course this is C++ and gcc should not be used. g++ is better :-)
Feel free to close this issue right now if you want, or maybe after an update of the gist?
Thanks again for the help, I really appreciated ! Joel
Feel free to close this issue right now if you want
I'll close it then. You can open new issues if you notice something with the new sessions feature - it hasn't been battle-tested that much.
after an update of the gist
Did it. I hope v1.1 will have some more docs on the new features - probably the auth page on the website.