How can I allow users to log into one domain and automatically be logged into my other domains without them having to submit a form on each domain?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
We all know that cookies are not accessible cross-domain as this presents a security risk. However, using some trickery, there are ways around this. Basically we are setting a cookie for the user on a central domain, checking for that cookie’s existence using a script, then using a JSON-P callback to copy that cookie onto the other domains. In more detail:
Logging In
Step 1
The
<form>displayed onmydomain.com(ormyotherdomain.com, etc) should POST tocentral.com/loginStep 2
On
central.com/login, the username and password are verified and a cookie is set on thecentral.comdomain containing a unique value for that user. The user is then redirected back tomydomain.comStep 3
Back on
mydomain.comwe embed a javascript call tocentral.com/check.Step 4
On
central.com/checkwe check if the unique cookie is set for the user. Then we embed a JavaScript callback (JSON-P) that informsmydomain.comthat the user is logged in. No sensitive user data is included, otherwisehacker.comcould embed this script and get the user’s information. (Setting appropriate Access-Control headers to only allow verified domains can alleviate this risk.) Instead, we create a disposable hash based on the timestamp, so thatmydomain.comcan verify the authentication.Step 5
The callback function is then executed, setting the cookie on
mydomain.com. Finally, we can either refresh the page or just alert the user using JavaScript that they are logged in (preferably both).mydomain.com/setcookieis similar to Step 2. Of course this assumes both sites have access to the same database (and code)Step 6
The next time the user refreshes the page, we can bypass the JSON-P callback
Logging Out
Step 7
The link on
mydomain.comshould go tocentral.com/logoutStep 8
On
central.com/logout, not only is the cookie deleted, but the unique value for that user is reset. The user is redirected back tomydomain.comStep 9
Now that the unique value is reset, Step 6 from above fails, the cookie is also deleted from
mydomain.com, and the user is effectively logged out.Notes
It is critical that
central.com/checkfrom Step 4 has thecorrect headers set so that it is not cached.
Steps 3-5 when the user is logging in may cause a slight delay. It’s wise to both refresh and show some kind of JavaScript alert that they are logged in. It’s also important for the script from Step 3 to be as close to the top of the page as possible.
In Step 5, you can optionally store a unique cookie value on each domain.
The separate
central.comdomain is not really necessary; you canjust use one of the other domains as the central domain if you wish.
The logic for that domain would obviously be different.
For this to work on Internet Explorer you will need a P3P policy
attached to your cookies.
As IvanGusev points out in the comments, one flaw of this approach is that if the user logs out of device A, it will also log them out of every other device.
Hope this is helpful to people. I’d be very interested to receive
feedback, especially if there are any security flaws from this
method. I think the worst a hacker could do is replicate Steps 3-5 and log you in to
mydomain.comwithout you knowing, but that would be harmless.