Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot skip required payload authentication with injected credentials #4214

Closed
jonathansamines opened this issue Jan 7, 2021 · 7 comments · Fixed by #4274
Closed

Cannot skip required payload authentication with injected credentials #4214

jonathansamines opened this issue Jan 7, 2021 · 7 comments · Fixed by #4274
Labels
support Questions, discussions, and general support

Comments

@jonathansamines
Copy link
Contributor

jonathansamines commented Jan 7, 2021

Support plan

  • is this issue currently blocking your project? (yes/no): No
  • is this issue affecting a production system? (yes/no): No

Context

  • node version: v12.19.1
  • module version with issue: 20.0.3
  • last module version without issue: I believe this has always behaved this way
  • environment (e.g. node, browser, native): node
  • used with (e.g. hapi application, another framework, standalone, ...): framework
  • any other relevant information:

What are you trying to achieve or the steps to reproduce?

When trying to inject credentials to an authentication scheme requiring payload authentication, then the payload authentication cannot be skipped.

'use strict';

const assert = require('assert');
const Hapi = require('@hapi/hapi');

async function run() {
  const server = Hapi.server();

  server.auth.scheme('scheme', () => ({
    authenticate() {
      throw new Error('authenticate error');
    },
    payload() {
      throw new Error('payload error');
    },
    options: {
      payload: true,
    },
  }));

  server.auth.strategy('default', 'scheme');

  server.route({
    method: 'POST',
    path: '/test',
    options: {
      auth: 'default',
      handler(request) {
        return request.auth.credentials;
      },
    },
  });

  const credentials = {
    hello: 'world',
  };

  const { result } = await server.inject({
    method: 'POST',
    url: '/test',
    auth: {
      strategy: 'default',
      credentials,
    },
  });

  assert.deepStrictEqual(result, credentials);
}

run()
  .catch((error) => {
    console.log(error);
    process.exit(1);
  });

What was the result you got?

The test fails because payload authentication could not be skipped, while the scheme authentication was.

What result did you expect?

Both the payload authentication and scheme authentication are skipped, when credentials are injected. If we update the authentication scheme to not require payload authentication, the test would pass.

I am not actually sure this is the expected behavior or not, for now I manually skip the payload authentication on my scheme when credentials are injected.

@jonathansamines jonathansamines added the support Questions, discussions, and general support label Jan 7, 2021
@cjihrig
Copy link
Contributor

cjihrig commented Jan 10, 2021

I am not actually sure this is the expected behavior or not

It's not entirely clear to me either, but from some quick testing, the test suite fails if payload validation is skipped on injected requests 😄

@jonathansamines
Copy link
Contributor Author

@cjihrig Is there any chance to change that and skip payload authentication when credentials are injected? Does it make sense?

@cjihrig
Copy link
Contributor

cjihrig commented Jan 11, 2021

I think the best way to support this without breaking any existing code would be to add a new option to server.inject() under the options.auth object, and have it default to the current behavior.

@jonathansamines
Copy link
Contributor Author

Nice, would the following API make sense?

options.auth

  • payload Disables payload authentication when set to false. Only required when an authentication strategy requires payload authentication. Defaults to true

Usage

const { result } = await server.inject({
    method: 'POST',
    url: '/test',
    auth: {
      strategy: 'default',
      credentials: {},
      payload: false,
    },
  });

@cjihrig
Copy link
Contributor

cjihrig commented Jan 12, 2021

That proposal makes sense to me.

@Nargonath
Copy link
Member

LGTM too.

@jonathansamines
Copy link
Contributor Author

Hi @Nargonath @cjihrig I recently created a pull request with these changes at #4274. Have some time to take a look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Questions, discussions, and general support
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
@Nargonath @jonathansamines @cjihrig and others