On Tue, 19 Aug 2014 14:19:26 GMT, Juanjo Álvarez wrote:

Hi!

I'm writing a rest API using Vibe.d. I'm registering the interface class below a call to performBasicAuth like:

router.any("*", performBasicAuth("Site Realm",   toDelegate(&checkAuth)));
router.registerRestInterface(new ApiImpl);

Now, on a method of ApiImpl I want to check the authenticated user, but AFAIK these methods doesn't receive an HTTPServerRequest object like the normal "web" delegates. Is there any way to get the user? (or any other HTTP data from the request) on these API methods?

A nice way to get data from the request to the handler method is the @before annotation, which uses a function taking the request and response object to assign arbitrary values to a certain parameter. This can also be used in conjunction with alias or enum to define custom attributes.

Something along these lines should work:

interface MyService {
    @auth void getIndex(string _authUserName);
}

enum auth = before!authPred("_authUserName");
string authPred(HTTPServerRequest req, HTTPServerResponse res)
{
    import vibe.http.auth.basic_auth;
    return performBasicAuth(req, res, "Site Realm", toDelegate(checkAuth));
}