36

I don't understand how google achieve the following mechanism of single sign on:

  1. I login in gmail for example (I suppose this creates a cookie withmy authorization)
  2. I open a new tab and direct type the url of "youtube"
  3. Then I enter youtube logged in.

How can this second site detect that I've already been logged in. They are different domains. Youtube can't read the cookie of Gmail.

All the solutions I've read about Single sign on don't allow this. The client always ask permission to a central login app. In my example YouTube doesn't know I am the same user logged in Gmail (actually it does know, but I don't understand how)

Note that I type the url of "youtube" by hand. I don't clic the youtube icon from the upper toolbar of gmail (In that case gmail may pass some auth params through the url for example).

user1518048
  • 451
  • 2
  • 5
  • 4
  • Maybe the answer is in this SO question? http://stackoverflow.com/questions/44509/single-sign-on-across-multiple-domains – mxro Oct 23 '12 at 01:58
  • 1
    Were you able to find out how they do this? I am stumped because I have been looking at firebug and if I am signed in at `google.com`, going to `youtube.com` never redirects to something like `sso.google.com`. It somehow magically knows that you are logged on. – F21 Nov 29 '13 at 10:48
  • Did you find it how it works? – chanchal118 Jun 01 '16 at 05:05
  • I have been looking for the answer to this for over a year, I get some pointers to SAML, but it is basically the same thing you explained, the IDP stores the cookies and users have to be at least redirected to the IDP web page for the cookie to be read. In youtube there is no redirection happening. – George Apr 21 '20 at 07:59
  • @James Okpe George you can not see the redirection with your eyes . You need to use web debugger – Hichem Jan 25 '21 at 10:59

5 Answers5

19

The cookies are set on specific domains. Ex:

setcookie(name,value,expire,path,domain) 

When you log in on gmail, before "mail.google.com", you have been redirected to "accounts.google.com" then to "mail.google.com" so the cookies are on "accounts.google.com" too.

In this case, the domain is "accounts.google.com" and the path is "/" (the home path).

When you request "www.youtube.com" then you click on "connection" it requests "accounts.google.com" fast so you can't see this redirection and checks if you have cookies on "accounts.google.com". If so, it checks if the cookies are valid and not expired, or user not banned... Then it redirects you to "www.youtube.com/signin?loginthisSession=Sessionid". This request contains the value of the of sessionid cookie catched from the cookies of "accounts.google.com".

In the last step, "www.youtube.com" logs you and set its own cookie on the domain "www.youtube.com" and saves them.

So the trick is on the 302 HTTP redirect.

Update

i do not know why people keep mentioning iframe take a look at the date whene this questions was posted on 2016 google was not using then iframe as i mentioned the capture of web traffic as you can see SetSID wich means set the cookie of SESSION_ID from accounts.google.dz(com) then redirects to youtube.com it can not be used trought iframe differant domains security measure you can not be redirected from domain to domain trought iframe neither please read this before posting

enter image description here

Hichem
  • 1,111
  • 11
  • 30
  • 1
    How the redirection is implemented without we see on Firebug? – chanchal118 Jun 01 '16 at 05:06
  • Is that how it actually works? Including the session ID in the URL is usually considered insecure (yes _it is_ encrypted if HTTPS is used, but it can be logged by servers, proxies, browser history or leaked by the HTTP referrer header). – SilverlightFox Jun 02 '16 at 09:39
  • @chanchal118 please open your eyes on this screenshot or just do it via proxy/httpfox and you will see http://i.imgur.com/aDIcYJb.png – Hichem Jun 02 '16 at 18:01
  • @SilverlightFox please open your eyes on this screenshot or just do it via proxy/httpfox and you will see http://i.imgur.com/aDIcYJb.png – Hichem Jun 02 '16 at 18:01
  • 1
    @K3rnel31 Is this screen shot taken while logging into youtube? – chanchal118 Jun 03 '16 at 13:29
  • @chanchal118 Exactly , taken yesterday at same time , do it by yourself and you will see :) , those requests are all from 302 , each one redirect to the other , till ends up at youtube.com – Hichem Jun 03 '16 at 17:41
  • @K3rnel31 Do you see any request to youtube when you logged into gmail? – chanchal118 Jun 04 '16 at 09:52
  • @chanchal118 yeah gmail in this case its youtube , same thing , before you got logged in to gmail , u will be steped from accounts.google.com , then same process , untill you end up at gmail , the question was montioned gmail , but basicly its not gmail who made youtube loggedin , its accounts.google.com what made gmail logged in , then youtube logged in ... – Hichem Jun 05 '16 at 13:04
  • @chanchal118 @k3rnel31 If you go from gmail to youtube it doesn't do any redirects. It seems to use an iframe as suggested by @pedrofb in [his answer](http://stackoverflow.com/questions/11434866/automatic-cookie-single-sign-on-on-multiple-domains-like-google#answer-37405389), if I switch the frame in the console from to one of the – SgtPepper43 Aug 16 '16 at 20:32
  • @sgtpepper43 question is asked on 2012 .. there was no iframe as i mentioned on the picture google was working by 302 redirect .. – Hichem Dec 08 '21 at 16:45
  • Sure that's how it worked in 2012, but the comments I was responding to are from 2016, and in 2016, they were using iframes. The question was "how to do SSO across domains like google", not "how did google do SSO across domains in 2012". If a stack overflow post is to remain useful, it should stay updated. – SgtPepper43 May 16 '22 at 19:10
  • Regarding your update, though, they were never _redirecting_ using iframes. They were most likely using the `postMessage` api, which allows you to push _anything_ you want between an iframe and the parent. Anything the website in the iframe has access to (cookies, local storage, etc) can be pushed to the parent with `postMessage`, as long as the parent is listening for it, as demonstrated here https://stackoverflow.com/a/37565692/2749072#answer-37565692 – SgtPepper43 May 16 '22 at 19:10
  • @SgtPepper43 you need to understand one thing , the question was about being signed in to youtube from gmail domain redirection on the address bar wich you can never do it with iframe – Hichem Jul 03 '22 at 21:32
  • @Hichem Reread the question, it has nothing to do with redirection. They're logged in to gmail, then they type in youtube.com, and they're automatically logged in *without any redirection*. And again, you really need to read about the `postMessage` api. You can do anything with iframes. – SgtPepper43 Jul 18 '22 at 19:30
5

Cookies and localStorage can be shared between domains using an intermediate domain. On the home page is embedded an "iframe ', which accesses cookies and sends messages to the main.

mail.google.com and youtube.com can share the cookies using accounts.google.es. Open Chrome->Inspect->Resources->Local storage and you will see in accounts.google.com the authentication token in JWT format.

I have detailed the technical steps in this answer: https://stackoverflow.com/a/37565692/6371459. Also take a look at https://github.com/Aralink/ssojwt to see an implementation of a Single Sign On using JWT in a central domain

Community
  • 1
  • 1
pedrofb
  • 37,271
  • 5
  • 94
  • 142
  • 2
    Note that adding iframe to share cookie between domain are called `3rd party cookie` and are being obsolete moving forward in new browser, check this: https://blog.chromium.org/2020/01/building-more-private-web-path-towards.html – Ng Sek Long Jun 20 '20 at 13:25
  • 1
    Seems it will affect third party cookies that do not include the SameSite tag or do not use https. This affects tracking cookies but not the use explained in the answer and in no case to `localStorage`. The content is shared using `postMessage` between iframes that is a secure and accepted method – pedrofb Jun 25 '20 at 19:16
2

Check this out.. http://www.codeproject.com/Articles/106439/Single-Sign-On-SSO-for-cross-domain-ASP-NET-applic. The article consist explanation and sample of SSO cross domain.

Nurkartiko
  • 183
  • 2
  • 10
0

As far as I remember, if I am not wrong, cookies contains a specified field that contains the domain that can read and get such cookie. That is made in order to prevent certain web sites to read all your cookie list and make your own business. You should be able to see which kind of sites can 'see' your gmail cookie.

Correct me if I am wrong, this should compile the answer given regarding the SID and gmail-YouTube example..

LMG
  • 966
  • 1
  • 12
  • 28
0

While evaluating this cross domain SSO topic, I have come up with possible a new SSO synchronization flow using cookie with timestamp. Although it is not a flow used by Google, I think this flow is possible to implement for system with limited number of domains.

This flow do not use 3rd party cookie

This is going to be a long post :)

domains

To make an example, let say we have these domains for our example pet forums:

Change to https://account.domain1.com:

Login Steps:

  1. User go to dog.domain2.com, user have not sign in yet.
  2. User click the Login button in dog.domain2.com
  3. User get redirect to account.domain1.com for login
  4. User Input username & password, login success
  5. New step, before redirect back to https://dog.domain2.com, set cookies on all domains
    1. Redirect browser to https://accounts.domain2.com?...
    2. Set a cookie on the .domains2.com domain (More on the cookie value later)
    3. Redirect browser to https://accounts.domain2.com?...
    4. Set a cookie on the .domains3.com domain
    5. Redirect browser to https://accounts.domain1.com?...
    6. Set a cookie on the .domains1.com domain
    7. Redirect back to original flow
  6. Redirect user back to their original service, i.e. https://dog.domain2.com

Now, right after login flow we have cookies over all 3 domains. Any of our service (e.g. https://cat.domain1.com / https://dog.domain2.com / https://rabbit.domain2.com ) can access this cookie under their own domain.

Cookie Content

  • The content of the cookie, should allows for any webpage to look at it, and determine if SSO sync is needed
  • Different types of cookie content can be stored, including
    • Boolean indicate user logined or not
    • User ID
    • Expired At timestamp

Boolean indicate user logined or not

User ID

  • While it is tempting to just stored the user_id on those cookie, and let all the domain to see them and set the user accordingly.
    • This is way too dangerous, since the cookie is set at the parent domain,
    • if any of the website under your domain been hacked, impersonation might happen (Copying any of the user_id, pasting it to their own browser cookie).

Expired At Timestamp

  • What I suggest, is for the cookie value to set as the SSO expired time, and set the type as session cookie, this have the following benefits:
    • An expired time have minimal security impact if leaked / altered
    • Our website can check the expired time to know if user need to relogin
    • As for why session cookie, is for when user close them browser, and tried to login again, the cookie will be deleted hence logout the user as well
  • Any webpage that use the SSO, should also stored a cookie themselves with the same expired time
    • There will be cases that, User A Login, visit https://cat.domains1.com Then User B Login
    • Since User A and User B will have a different login expired time, storing and compare that timestamp will tell the user to sync with SSO again

Example checking implement for your service

E.g. On https://cat.domains1.com, you can add this to the top of your page load

<?php
$sso_expired_time = $_COOKIE["sso_expired_time "] ?? 0;
$website_expired_time = $_COOKIE["website_expired_time "] ?? 0;

if( (int) $sso_expired_time  < time() || $sso_expired_time  !== $website_expired_time ) {
    // User not sync, perform sync
    setcookie("website_expired_time", $website_expired_time,0,"/", $_SERVER['SERVER_NAME'], true, true);
    // Redirect to https://account.domain1.com for Login
    // Or, Initiate the login sequence for your selected login protocol
    header("Location: https://account.domain1.com/.....") 
    exit;
}
// User is sync

// Page load success, continue other operation

Logout

Login is very similar to Login, basically:

  • Before logout goes through, redirect to all 3 domains just like login
  • Remove the SSO cookie
  • Continue the normal logout flow

Pro and cons for the methods:

  • Pro: All domain sync possible
  • Pro: No need to relies on 3rd party cookie
  • Cons: First time login longer (around 50ms longer)
  • Cons: Customization on every website is needed for the sync to works
Ng Sek Long
  • 4,233
  • 2
  • 31
  • 38
  • In this implementation if a domain is unavailable, it won't redirect to the next one and subsequent domains won't log in/out, right? Is there an easy way to go around that other than opening multiple tabs/popups for each domain? – Lauro May 04 '22 at 18:23
  • 1
    You can check if the page is up or not using this image trick: https://stackoverflow.com/a/63897923/6463291. – Ng Sek Long May 05 '22 at 02:06