zohararad / sails-rest

Use a RESTful API as an ORM adapter in Sails.js
MIT License
144 stars 49 forks source link

Attach authentication header to outgoing request #67

Open thebigredgeek opened 9 years ago

thebigredgeek commented 9 years ago

Hi,

I am using SailsJS as a stateless facade middle tier. The client makes requests to SailsJS, and SailsJS makes requests to an SOA RESTful backend. With each request, the client passes an authentication token to Sails. So far, I have built a service-type wrapper for each SOA API endpoint and I can said wrapper from each controller as "pretend models". These services then make requests to the SOA backend and pass the user's authentication token with each request. I'd like to switch to sails-rest to reduce boilerplate and leverage waterline's real time functionality via model subscriptions. The problem I am having is that I have no idea how to dynamically insert an authentication header per-tenant with sails-rest. I want sails-rest to make HTTP requests to my SOA backend by attaching the user's authentication token (present in the req object passed into the controller) to each outgoing request that it makes. Is there a way to accomplish this?

Thanks!

zohararad commented 9 years ago

I think this is a good use-case of using a before hook to modify the request headers. Did you try that?

If you can provide an example call to a sails-rest model from one of your controllers I can try and help you figure it out

fokkerone commented 8 years ago

Any plans to add request/access tokens for Rest Api authentication?

zohararad commented 8 years ago

@fokkerone you mean tokens to the backend API you're calling? Can you not use a before hook?

fokkerone commented 8 years ago

Hmm, how would you log in into a MySQL db via hook or via conf ?

zohararad commented 8 years ago

I wouldn't, but then again, MySQL is not a REST service.

I need an example of what you want to do, and why you cant use the current mechanisms

ykcodebook commented 6 years ago

Hi,

I would like to forward the req.header that i receive through the sails-rest adapter instead of creating or appending a new header. How do i do this?

zohararad commented 6 years ago

Hmm... not sure I completely understand what you're trying to do, but I think using before/after hooks should do the trick. Did you try that?

ykcodebook commented 6 years ago

Well to elaborate, this is my issue. I'm trying to build a microservice based app. Lets say i have two services i.e two sails app running. Lets name it ApiService and UserService. My entry point is ApiService where i send requests with Authorization header. I want the ApiService to forward the request to UserService without altering the header information. I'm using sails-rest at ApiService to do so, but it is removing all my headers. Before/After hooks does not have information of the request received so i cant even append headers to the new sails-rest request that is being sent to UserService. I hope this scenario gave you more clarity. Thanks!!

zohararad commented 6 years ago

Right.... that is a bit complicated, and I don't see an easy way to achieve this kind of behaviour. It might make more sense to issue plain HTTP requests to your backend, rather than using sails-rest. There's an example of how to do that in the examples folder under User.js

ykcodebook commented 6 years ago

Yes. I'm looking into it. Its quite helpful. Thanks!!