RejectedSoftware Forums

Sign up

How to get authenticathed user without a HTTPServerRequest parameter?

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?

Re: How to get authenticathed user without a HTTPServerRequest parameter?

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));
}

Re: How to get authenticathed user without a HTTPServerRequest parameter?

This works perfectly, thanks!

But... how does it work? I mean, how is authPred receiving the HTTPServerRequest and response parameters? I've looked at funcattr.d and I don't see how the magic is done (not that I understand much of that file yet, I'm learning the language as I go).

Re: How to get authenticathed user without a HTTPServerRequest parameter?

On Thu, 21 Aug 2014 22:03:58 GMT, Juanjo Álvarez wrote:

This works perfectly, thanks!

But... how does it work? I mean, how is authPred receiving the HTTPServerRequest and response parameters? I've looked at funcattr.d and I don't see how the magic is done (not that I understand much of that file yet, I'm learning the language as I go).

What basically is done is that registerRestInterface searches for @before attributes and then calls the associated function with (req, res) parameters. The code behind which this is hidden is indeed a little hardcore, even for template/CTFE code (it's createAttributedFunction which creates a wrapper function with the @before parameters properly set).

vibe.web.web handles @before in a little more direct way and this will be changed in vibe.web.rest at some point, too (it's necessary for supporting member functions for @before). Look for computeAttributedParameterCtx in vibe.web.web to get to the place where the parameter is set.

Re: How to get authenticathed user without a HTTPServerRequest parameter?

Oh, that was it, I should have looked to registerRestInterface then (now I see it). I'm learning a lot from your code alone.

On Thu, 21 Aug 2014 22:29:02 GMT, Sönke Ludwig wrote:

On Thu, 21 Aug 2014 22:03:58 GMT, Juanjo Álvarez wrote:

This works perfectly, thanks!

But... how does it work? I mean, how is authPred receiving the HTTPServerRequest and response parameters? I've looked at funcattr.d and I don't see how the magic is done (not that I understand much of that file yet, I'm learning the language as I go).

What basically is done is that registerRestInterface searches for @before attributes and then calls the associated function with (req, res) parameters. The code behind which this is hidden is indeed a little hardcore, even for template/CTFE code (it's createAttributedFunction which creates a wrapper function with the @before parameters properly set).

vibe.web.web handles @before in a little more direct way and this will be changed in vibe.web.rest at some point, too (it's necessary for supporting member functions for @before). Look for computeAttributedParameterCtx in vibe.web.web to get to the place where the parameter is set.