outmoded / lout

API documentation generator
Other
276 stars 49 forks source link

Not easy to get a custom error from authentication failure #146

Closed sazzer closed 8 years ago

sazzer commented 8 years ago

I'm writing my own Authentication Scheme to use with something I'm doing. For now it's just based off of hapi-auth-basic but that's just to get the hang of things.

I'm trying to get it so that I can return a custom payload on authentication failure, but at the same time support the route stating that authentication is optional. The best that I can find to support this is:

    if (!authorization) {
        // No Authorization header
        const error = Boom.unauthorized(null, 'Basic');
        error.output.payload = {error: 'no_authentication_provided', error_description: 'No authentication provided'};
        error.isMissing = false;
        return reply(error);
    }

If error.isMissing is set to true - which it is automatically for Boom.unauthorized() - then it doesn't work correctly when the authorization fails (It instead rewrites the payload and you end up with a more typical Boom JSON structure than the one that I have provided here)

If you don't provide a Boom object at all - or an Error object which is automatically wrapped - then you can't have the authentication be optional - failure is always an error regardless of how the route is configured.

Is there some cleaner way to specify a custom error payload for failed authentication?

Marsup commented 8 years ago

This has nothing to do with lout, bring it to hapijs/discuss.

sazzer commented 8 years ago

Apologies. I thought I was on discuss but must have gone from the wrong tab instead :-( sorry about that!

On Mon, 4 Jan 2016 22:38 Nicolas Morel notifications@github.com wrote:

Closed #146 https://github.com/hapijs/lout/issues/146.

— Reply to this email directly or view it on GitHub https://github.com/hapijs/lout/issues/146#event-505319887.