This example uses the @before annotation supported by the vibe.web.web framework for a concise and statically defined authentication approach.
import vibe.http.router; import vibe.http.server; import vibe.web.web; class MyWebService { private { UserManWebAuthenticator m_auth; } this(UserManController userman) { m_auth = new UserManWebAuthenticator(userman); } // this route can be accessed publicly (/) void getIndex() { //render!"welcome.dt" } // the @authenticated attribute (defined below) ensures that this route // (/private_page) can only ever be accessed when the user is logged in @authenticated void getPrivatePage(User _user) { // render a private page with some user specific information //render!("private_page.dt", _user); } // Define a custom attribute for authenticated routes private enum authenticated = before!performAuth("_user"); mixin PrivateAccessProxy; // needed so that performAuth can be private // our custom authentication routine, could return any other type, too private User performAuth(HTTPServerRequest req, HTTPServerResponse res) { return m_auth.performAuth(req, res); } } void registerMyService(URLRouter router, UserManController userman) { router.registerUserManWebInterface(userman); router.registerWebInterface(new MyWebService(userman)); }
An example using a plain vibe.http.router.URLRouter based authentication approach.
import std.functional; // toDelegate import vibe.http.router; import vibe.http.server; void getIndex(HTTPServerRequest req, HTTPServerResponse res) { //render!"welcome.dt" } void getPrivatePage(HTTPServerRequest req, HTTPServerResponse res, User user) { // render a private page with some user specific information //render!("private_page.dt", _user); } void registerMyService(URLRouter router, UserManController userman) { auto authenticator = new UserManWebAuthenticator(userman); router.registerUserManWebInterface(userman); router.get("/", &getIndex); router.any("/private_page", authenticator.auth(toDelegate(&getPrivatePage))); }
Used to privide request authentication for web applications.