loopback-next icon indicating copy to clipboard operation
loopback-next copied to clipboard

authenticateRequest not calling strategy for non get requests

Open mmbabel opened this issue 7 years ago • 5 comments

I hope I did not get anything wrong, but this seems wrong A UserProfile in the sequence is only returned on get requests, not on post, put etc

Steps to reproduce

Create a authentication strategy and add something like that to the sequence

try {
        await this.authenticateRequest(request);
      } catch (e) {
        if(e.code === AUTHENTICATION_STRATEGY_NOT_FOUND || e.code === USER_PROFILE_NOT_FOUND) {
          throw new HttpErrors.Unauthorized(e.message);
        } else {
          throw e;
        }
      }

Current Behavior

await this.authenticateRequest(request); 

will only return a UserProfile on get requests as well as the authenticate function of the strategy is not getting called.

Expected Behavior

for every request the authenticate function should get called and the UserProfile should be returned.

Additional information

The authetnicate function can be called manually as a work around.

        await this.authenticateRequest(request);
        let ba = new BasicAuthenticationStrategy();
        user = await ba.authenticate(request);

mmbabel avatar Jul 15 '19 11:07 mmbabel

@mmbabel , if you can provide a link to your github repo, we can help you better.

It is not clear that you've decorated your controller functions with the @authenticate(strategyName, options?) decorator.

emonddr avatar Jul 15 '19 12:07 emonddr

@mmbabel , please see : https://loopback.io/doc/en/lb4/Loopback-component-authentication.html#using-the-authentication-decorator

emonddr avatar Jul 15 '19 12:07 emonddr

@mmbabel

I cloned our shopping cart example

https://github.com/strongloop/loopback4-example-shopping

In /loopback4-example-shopping/packages/shopping/src/controllers/user.controller.ts

I added

 @post('/users/uppercase/{textString}', {
    responses: {
      '200': {
        description: '',
        schema: {
          type: 'string',
        },
      },
    },
  })
  @authenticate('jwt')
  async upperCase(@param.path.string('textString') textString: string): Promise<string> {
    return textString.toUpperCase();
  }

I tried it out, and it works.

emonddr avatar Jul 15 '19 13:07 emonddr

Thanks @emonddr you were totally right, I was missing the controller annotation, sorry for this report

mmbabel avatar Jul 15 '19 14:07 mmbabel

Thanks @emonddr you were totally right, I was missing the controller annotation, sorry for this report

No problem. :)

emonddr avatar Jul 16 '19 16:07 emonddr