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

Attach authentication header to outgoing request #67

Open
thebigredgeek opened this issue Oct 16, 2015 · 10 comments
Open

Attach authentication header to outgoing request #67

thebigredgeek opened this issue Oct 16, 2015 · 10 comments

Comments

@thebigredgeek
Copy link

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
Copy link
Owner

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
Copy link

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

@zohararad
Copy link
Owner

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

@fokkerone
Copy link

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

@zohararad
Copy link
Owner

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
Copy link

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
Copy link
Owner

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
Copy link

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
Copy link
Owner

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
Copy link

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

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

No branches or pull requests

4 participants