Goal. My server should direct non-users to the landing / home page and register users in a real application. When the application is downloaded, it will make authenticated HTTP RESTful API requests (via Ajax).
I have a RESTful API that needs authentication. On another server, I have my website, which also requires authentication, so I can determine whether to show the landing / home page for non-users or the application for registered users.
Initially, I thought that it would be enough to implement HTTP Basic Auth for the RESTful API. However, in order to get authentication for my website, I also need to configure authentication there, which would mean duplicating low-level code to verify credentials in the database in both the REST API and the website’s servers.
As an alternative, I wondered if the website can authenticate through the RESTful API. For example, in my request handler for POST /login I could make a GET request to my API by passing user credentials from the request body. If the request returns 200 OK , I can sign up for a user session, thereby authenticating them. From there, Ajax requests to the REST API should be authenticated with the same credentials, so I could:
- set a cookie containing credentials, which allows JavaScript to get credentials before executing the request (OK with SSL?)
- reset credentials in served HTML for a web application, allowing JavaScript to get credentials before executing the request (OK with SSL?)
- API proxy through the web application server, where can I get the credentials from the session and add them to the
Authorization header of the proxied request?
Alternatively, I suppose I could just share the session between the two servers, although Ive heard that this is bad practice for RESTful design.
What would be wrong with that? Is there a better way to achieve my goal?
user1082754
source share