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

Pass auth header from client to loopback rest-connector #89

Open
ghost opened this issue Jan 10, 2017 · 17 comments
Open

Pass auth header from client to loopback rest-connector #89

ghost opened this issue Jan 10, 2017 · 17 comments

Comments

@ghost
Copy link

ghost commented Jan 10, 2017

Feature request

  • "x" Feature request

Description of feature (or steps to reproduce if bug)

Data can be passed to the rest-connector but it seems headers cannot. It would be useful to pass the Authorization header to the rest-connector for multi tenanted systems. Here is a link to what I am trying to do which promoted this request.

http://stackoverflow.com/questions/41573433/pass-basic-auth-from-angular-client-to-loopback-rest-connector

http://stackoverflow.com/questions/29899682/loopback-io-rest-connector-how-to-pass-through-oauth-token?

@bajtos

@raymondfeng
Copy link
Contributor

Have you tried to use connector observer - https://github.com/strongloop/loopback-connector-rest/blob/master/test/rest-loopback.test.js#L180? It allows you to intercept the http req/res for the outbound REST call.

@ghost
Copy link
Author

ghost commented Jan 10, 2017

Hi Raymond, Yes I have (I think). I have a connector,js boot script that has the following,

JBConnector.observe('before execute', function(ctx, next)
}

The ctx object does not have the Authorisation header in it although I am sending it in the http.post request.

@raymondfeng
Copy link
Contributor

Please note the ctx object is for the outbound http req/res to your REST API.

For your case, maybe the best option is as follows:

  1. Add custom remote method which has the authorization arg from the incoming http
  2. Configure your REST DS with authorization header as a variable in the template
  3. In your custom remote method impl, pass in the authorization arg to the REST model call

We'll probably come up a better solution in LoopBack.next

@ghost
Copy link
Author

ghost commented Jan 11, 2017

Hi Raymond, Thanks for the info re the ctx object. Please see this is for my attempt http://stackoverflow.com/questions/41573433/pass-basic-auth-from-angular-client-to-loopback-rest-connector which is wrong. I understand your steps as this is similar logic to what I was trying to do. The question really is how does this work. Best,

@ghost
Copy link
Author

ghost commented Jan 17, 2017

Hello both, Did you guys get anywhere with an example of best practice for this issue?

Best,

@bajtos
Copy link
Member

bajtos commented Jan 18, 2017

I think we may be able to leverage http://loopback.io/doc/en/lb3/Using-current-context.html to implement this feature:

  • When a model is attached to REST connector, the connector should override Model'screateOptionsFromRemotingContext method with a custom implementation that will include request headers in the options argument
  • Remote methods generated by REST connector should include options argument
  • The configuration of remote methods should allow the users to tell which of the request headers should be passed through.

@raymondfeng thoughts?

@bajtos bajtos self-assigned this Jan 18, 2017
@ghost
Copy link
Author

ghost commented Jan 20, 2017

Thanks for the input @bajtos. @raymondfeng are you able to weigh in with your thoughts on this?

Best,

@raymondfeng
Copy link
Contributor

@bajtos +1. Let's create a user story to implement such enhancements (probably for all http related connectors).

@ghost
Copy link
Author

ghost commented Jan 23, 2017

Hi @bajtos @raymondfeng. There context for why I am trying to do this is to allow the api to enable multi tenancy. As well as providing "local" models the api talks to another independent system. This system uses basic auth. The api may have to communicate with various instances of this independent system and hence requires the auth header to be dynamically created through the rest connector. Does that make sense? I can work with a work around if you guys can provide a code example of this? Best,

@kalpana-ideas2it
Copy link

Hi @raymondfeng, Is there any possibility to give include, filter options in the rest-connector template?
Thanks in advance:)

@bajtos bajtos removed their assignment Jan 16, 2018
@kreegr
Copy link

kreegr commented Jan 31, 2018

@NudgeTech -
For background, I have to request an access token and pass it in my auth header. Not wanting to re-write the default crud methods, I didn't want to use a template.

I was able to do this using the before execute hook @raymondfeng mentioned:

myDataSource.connector.observe('before execute', (ctx, next)=>{
   apiTokenPromise().then(ACCESS_TOKEN => {
     ctx.req.headers = {
        "authorization": `Bearer ${ACCESS_TOKEN}`
      };
      next();
  });
});

@kalpana-ideas2it
Copy link

Thank you @kreegr

@geocine
Copy link

geocine commented Dec 5, 2018

How to do this on loopback 4?

@dejibimbolaAyo
Copy link

Hi, is there a "non-hacky" way to do this in LB4?

Would really appreciate...

@arunkumarreddygoluguri
Copy link

@kreegr Hi Any ideas on implementing this for loopback 4

@kreegr
Copy link

kreegr commented Feb 13, 2019

I’m sorry - I don’t (I haven’t tried because I haven’t used it yet)

@jannyHou
Copy link
Contributor

I suggest we move the discussion to loopbackio/loopback-next#2381.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants