Skip to content

Commit 64122d3

Browse files
authored
Merge pull request #2915 from jerriep/rework-sso-docs
Rework SSO documentation
2 parents f69f180 + 17b7f8d commit 64122d3

File tree

6 files changed

+144
-44
lines changed

6 files changed

+144
-44
lines changed

articles/sso/index.md

+35-5
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,42 @@ SSO (Single Sign On) occurs when a user logs in to one Client and is then signed
99

1010
Google's implementation of login for their products, such as Gmail, YouTube, Google Analytics, and so on, is an example of SSO. Any user that is logged in to one of Google's products are automatically logged in to their other products as well.
1111

12-
## How SSO Works
13-
14-
SSO works by means of a *central service*. In the case of Google, this central service is [Google Accounts](https://accounts.google.com). When a user first logs in, Google Accounts creates a cookie, which persists with the user as they navigate to other Google-owned services. The process flow is as follows:
12+
Single Sign On usually makes use of a *Central Service* which orchestrates the single sign on between multiple clients. In the example of Google, this central service is [Google Accounts](https://accounts.google.com). When a user first logs in, Google Accounts creates a cookie, which persists with the user as they navigate to other Google-owned services. The process flow is as follows:
1513

1614
1. The user accesses the first Google product.
1715
2. The user receives a Google Accounts-generated cookie.
1816
3. The user navigates to another Google product.
1917
4. The user is redirected again to Google Accounts.
2018
5. Google Accounts sees that the user already has an authentication-related cookie, so it redirects the user to the requested product.
2119

20+
## An overview of how SSO works with Auth0
21+
22+
In the case of SSO with Auth0, the *Central Service* is the Auth0 Authorization Server.
23+
24+
Let's look at how the SSO flow looks when using Auth0 and a user visits your application for the first time:
25+
26+
1. Your application will redirect the user to the Auth0 Hosted Lock page where they can log in.
27+
2. Auth0 will check to see whether there is an existing SSO cookie.
28+
3. Because this is the first time the user visits this Hosted Lock page, and no SSO cookie is present, they may be presented with username and password fields and also possibly some Social Identity Providers such as LinkedIn, GitHub, etc. (The exact layout of the Lock screen will depend on the [Identity Providers](/identityproviders) you have configured.
29+
30+
![](/media/articles/sso/single-sign-on/lock-no-sso-cookie.png)
31+
32+
4. Once the user has logged in, Auth0 will set an SSO cookie
33+
5. Auth0 will also redirect back to your web application and will return an `id_token` containing the identity of the user.
34+
35+
Now let's look at flow when the user returns to your website for a subsequent visit:
36+
37+
1. Your application will redirect the user to the Auth0 Hosted Lock page where they can sign in.
38+
2. Auth0 will check to see whether there is an existing SSO cookie.
39+
3. This time Auth0 finds an SSO cookie and instead of displaying the normal Lock screen with the username and password fields, it will display a Lock screen which indicates that we know you the user is, as they have already logged in before. They can simply confirm that they want to log in with that same account.
40+
41+
![](/media/articles/sso/single-sign-on/lock-sso-cookie.png)
42+
43+
4. Auth0 will update the SSO cookie if required
44+
5. Auth0 will also redirect back to your web application and will return an `id_token` containing the identity of the user.
45+
46+
If an SSO cookie is present you can also sign the user in silently, i.e. without even displaying Lock so they can enter their credentials. This is covered in more detail in the next section.
47+
2248
## How to Implement SSO with Auth0
2349

2450
::: panel-info Enabling Identity Providers
@@ -33,9 +59,13 @@ Near the bottom of the *Settings* page, toggle **Use Auth0 instead of the IdP to
3359

3460
![](/media/articles/sso/single-sign-on/sso-flag.png)
3561

36-
You can set the Client's SSO flag using the [Auth0 Management API](/api/management/v2#!/Clients/patch_clients_by_id).
62+
Alternatively you can also set the Client's SSO flag using the [Auth0 Management API](/api/management/v2#!/Clients/patch_clients_by_id).
63+
64+
Once you have set the SSO flag for your Client in the Auth0 Dashboard, you must add logic to your application to check the user's SSO status. Checking the user's SSO status can only be done via JavaScript by making use of the [`getSSOData`](/libraries/auth0js#sso) function in the [auth0.js library](/libraries/auth0js).
65+
66+
The result of this function will indicate whether an SSO cookie is present, and if so it will return the SSO data of the user which can then subsequently be used to log the user in silently without even displaying Lock.
3767

38-
Once you have set the SSO flag in Auth0, you must add logic to your Client to check the user's SSO status. This logic can be implemented either client-side (using JavaScript) or server-side:
68+
For more detailed information on how to implement this, please refer to the following documents:
3969

4070
* [Client-Side SSO (Single Page Apps)](/sso/single-page-apps-sso)
4171
* [Server-Side SSO (Regular Web Apps)](/sso/regular-web-apps-sso)

articles/sso/regular-web-apps-sso.md

+25-14
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,40 @@ description: Server-side SSO with regular web applications.
44

55
# Server-side SSO (Regular Web Apps)
66

7-
Let's say we have three applications
7+
To log a user in silently (i.e. without displaying the Lock screen) the following conditions need to be met:
88

9-
* App 1: app1.com (single page app)
10-
* App 2: app2.com (single page app)
11-
* App 3: app3.com (regular web app)
9+
1. The Client needs to be configured to **Use Auth0 instead of the IdP to do Single Sign On** in the [Clients section of the Auth0 Management Dashboard](${manage_url}/#/clients)
10+
2. An SSO cookie must exist for the tenant's domain. In other words the user must have signed in previously, and the SSO cookie which was saved is still valid.
11+
3. When calling the Auth0 authentication endpoint, the connection name is passed along for which the user must be signed in. This connection name is the same as the one specified in the SSO cookie. You can pass the connection name along either as a parameter when calling the `signin` function of the [**auth0.js** Library](https://auth0.com/docs/libraries/auth0js), or by passing the `connection` query string parameter when calling the `/authorize` endpoint of the [Authentication API](/api/authentication)
1212

13-
> You can see an example of a Regular Web App configured to use SSO in [this github repository](https://github.com/auth0/auth0-sso-sample/tree/master/app3.com)
13+
## The SSO scenario
14+
15+
In our SSO scenario, let's say we have 3 applications
16+
17+
* App 1: app1.com (Single Page App)
18+
* App 2: app2.com (Single Page App)
19+
* App 3: app3.com (Regular Web app)
20+
21+
If a user logs in to any of these applications, and then subsequently navigates from this application to any of the other applications, we would want the user to be logged in automatically.
22+
23+
In this document we will be looking specifically how to achieve this in a Regular Web Application
1424

1525
## Case 1: The user is already logged in and clicks on a link that redirects to a specific URL app3.com
1626

17-
The user logs in on app1.com and clicks on a link that should take them to a particular URL on app3.com. In this case, you can create an endpoint on the target application (app3) that will redirect to the URL the user wanted to go after SSO. For example:
27+
The user logs in on one of the Single Page Applications and click on a link that should take them to a particular URL on app3.com. In this case, you can create an endpoint on the target application (app3) that will redirect to the URL the user wanted to go after SSO. For example:
1828

19-
```
20-
https://app3.com/sso?targetUrl=/foo/bar
29+
```text
30+
https://app3.com/sso?targetUrl=/foo/bar&connection=<connection name>
2131
```
2232

23-
This endpoint would check if the user is already logged in to this app. If they are, then redirect to the target URL. If the user is not logged in to the application, then redirect to Auth0 for SSO:
33+
This endpoint would check if the user is already logged in to this app. If they are, then redirect to the target URL. If the user is not logged in to the application, then redirect to Auth0 for SSO, passing along the name of the connection to use:
2434

25-
```
35+
```text
2636
handle("/sso")
2737
if (user is already logged in)
2838
redirect to targetUrl
2939
else
30-
redirect to "https://YOURS.auth0.com/authorize?client_id=…&redirect_uri=http://urlTo/callback&response_type=code&state=' + targetUrl
40+
redirect to "https://${account.namespace}/authorize?client_id=…&connection=<connection name>&redirect_uri=http://urlTo/callback&response_type=code&state=' + targetUrl
3141
```
3242

3343
Here is an example in node.js:
@@ -43,7 +53,8 @@ app.get('/sso', function(req,res, next) {
4353
} else {
4454
console.log("Authenticating with Auth0 for SSO");
4555
passport.authenticate('auth0', {
46-
state: req.query.targetUrl
56+
state: req.query.targetUrl,
57+
connection: req.query.connection
4758
})(req, res, next);
4859
}
4960
});
@@ -77,7 +88,7 @@ app.get('/callback',
7788
The user is logged in on app1.com and opens a new tab and goes to app3.com. You would expect the user to be automatically signed in. To do that, you need to redirect the user to the following URL in a filter or a middleware:
7889

7990
```
80-
https://YOURS.auth0.com/authorize?client_id=…&response_type=code&redirect_uri=http://urlTo/callback
91+
https://${account.namespace}/authorize?client_id=…&response_type=code&redirect_uri=http://urlTo/callback
8192
```
8293

8394
Here is an example in node.js:
@@ -99,5 +110,5 @@ If the user was already logged in before, then Auth0 will automatically redirect
99110
The user has never logged in to any app. In this case, the filter or middleware mentioned in the previous point checks if the user is authenticated or not, and in the case they're not, redirects the user to the following URL:
100111

101112
```
102-
https://YOURS.auth0.com/authorize?client_id=…&response_type=code&redirect_uri=http://urlTo/callback
113+
https://${account.namespace}/authorize?client_id=…&response_type=code&redirect_uri=http://urlTo/callback
103114
```

articles/sso/single-page-apps-sso.md

+82-22
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,95 @@ description: Client-side SSO with single page applications.
44

55
# Client-side SSO (Single Page Apps)
66

7-
Let's say you have three applications:
7+
To log a user in silently (i.e. without displaying the Lock screen) the following conditions need to be met:
88

9-
* App 1: app1.com (single page app)
10-
* App 2: app2.com (single page app)
11-
* App 3: app3.com (regular web app)
9+
1. The Client needs to be configured to **Use Auth0 instead of the IdP to do Single Sign On** in the [Clients section of the Auth0 Management Dashboard](${manage_url}/#/clients)
10+
2. An SSO cookie must exist for the tenant's domain. In other words the user must have signed in previously, and the SSO cookie which was saved is still valid.
11+
3. When calling the Auth0 authentication endpoint, the connection name is passed along for which the user must be signed in. This connection name is the same as the one specified in the SSO cookie. You can pass the connection name along either as a parameter when calling the `signin` function of the [**auth0.js** Library](/libraries/auth0js), or by passing the `connection` query string parameter when calling the `/authorize` endpoint of the [Authentication API](/api/authentication)
1212

13-
> You can see an example of a SPA configured to use SSO in [this github repository](https://github.com/auth0/auth0-sso-sample/tree/master/app1.com)
13+
## The SSO scenario
1414

15-
The user logs in on app1.com and tries to access app2.com. Since app2.com is a Single Page App you need to have some code like the following to do SSO. This code should be on every SPA you have (In this case App1 and App2).:
15+
In our SSO scenario, let's say we have 3 applications
16+
17+
* App 1: app1.com (Single Page App)
18+
* App 2: app2.com (Single Page App)
19+
* App 3: app3.com (Regular Web app)
20+
21+
If a user logs in to any of these applications, and then subsequently navigates from this application to any of the other applications, we would want the user to be logged in automatically.
22+
23+
In this document we will be looking specifically how to achieve this in a Single Page (JavaScript) Application
24+
25+
## Obtaining the SSO cookie information
26+
27+
If a users logs in to any of the applications and then subsequently tries to log in to any of the other applications, you can check to see whether an SSO session is active for that user by making use of the `getSSOData` function in the [auth0.js library](/libraries/auth0js#sso).
28+
29+
```js
30+
auth0.getSSOData(function (err, ssoData) {
31+
if (err) return console.log(err.message);
32+
expect(ssoData.sso).to.exist;
33+
});
34+
```
35+
36+
This function will return an `ssoData` object which will indicate whether an active SSO session exist. The `ssoData` object will contain the fields as per the following example:
37+
38+
```json
39+
{
40+
sso: true,
41+
sessionClients: [
42+
"jGMow0KO3WDJELW8XIxolqb1XIitjkYL"
43+
],
44+
lastUsedClientID: "jGMow0KO3WDJELW8XIxolqb1XIitjkYL",
45+
lastUsedUsername: "[email protected]",
46+
lastUsedConnection: {
47+
name: "Username-Password-Authentication",
48+
strategy: "auth0"
49+
}
50+
}
51+
```
52+
53+
## Passing the Connection Name when logging the user in
54+
55+
You can then use this information to call the `signin` function to log the user in. When you call the `signin` function you pass along the name of the connection used in the active SSO session as the `connection` parameter. Auth0 will determine that an SSO cookie is saved for that connection, and will log the user directly without displaying the Lock user interface.
56+
57+
58+
```js
59+
auth0.getSSOData(function (err, ssoData) {
60+
if (!err && ssoData.sso) {
61+
auth0.signin({
62+
connection: ssoData.lastUsedConnection.name,
63+
scope: 'openid name picture',
64+
params: {
65+
state: getQueryParameter('targetUrl')
66+
}
67+
});
68+
} else {
69+
// Display regular login option, e.g. a Login button which will invoke Lock
70+
}
71+
});
72+
```
73+
74+
## Full SSO Sample Code
75+
76+
Below is an expanded code sample of how you can implement this in a SPA application using jQuery to either show or hide the Login button or user information depending on whether a user is logged in or not.
77+
78+
The full code sample for both the SPA applications as well as the normal web application can be found in [this github repository](https://github.com/auth0/auth0-sso-sample/tree/master/app1.com)
1679

1780
```html
1881
<script type="text/javascript">
1982
// hide the page in case there is an SSO session (to avoid flickering)
2083
document.body.style.display = 'none';
2184
2285
// instantiate Lock
23-
var lock = new Auth0Lock(''${account.clientId}', ''${account.namespace}');
86+
var lock = new Auth0Lock('${account.clientId}', '${account.namespace}', {
87+
auth: {
88+
params: {
89+
scope: 'openid name picture'
90+
}
91+
}
92+
});
2493
var auth0 = new Auth0({
25-
domain: ''${account.namespace}',
26-
clientID: ''${account.clientId}',
94+
domain: '${account.namespace}',
95+
clientID: '${account.clientId}',
2796
callbackOnLocationHash: true
2897
});
2998
@@ -76,37 +145,28 @@ The user logs in on app1.com and tries to access app2.com. Since app2.com is a S
76145
// Showing Login
77146
$('.btn-login').click(function (e) {
78147
e.preventDefault();
79-
lock.show({
80-
auth: {
81-
params: {
82-
scope: 'openid name picture'
83-
}
84-
}
85-
});
148+
lock.show();
86149
});
87150
88151
</script>
89152

90153
<!-- Regular login -->
91154
<body>
92-
<button onclick="lock.show()">Login</button>
155+
<button class="btn-login">Login</button>
93156
</body>
94157
```
95158

96-
If the single sign on happens against app3.com (a regular web app), then you have to redirect to `app3.com/sso?targetUrl=/foo/bar`. Read more about this on [Single Sign On with Regular Web Apps](/sso/regular-web-apps-sso).
97-
98159
## Single Logout
99160

100-
If the user logged out from app1.com, then we want to clean up the token on app2.com (and app3.com). Read more about [Single Log Out](/logout).
161+
If the user logged out from app1.com, then we want to clean up the token on app2.com (and app3.com). Read more about [Single Log Out](/logout).
101162

102-
To do that, you have to check every X amount of time whether the SSO session is still alive in Auth0. If it is not, then remove the token from storage for the app.
163+
To do that, you have to check every X amount of time whether the SSO session is still alive in Auth0. If it is not, then remove the token from Local Storage for the application. This will ensure that the user's local session is cleared.
103164

104165
```js
105166
setInterval(function() {
106167
// if the token is not in local storage, there is nothing to check (i.e. the user is already logged out)
107168
if (!localStorage.getItem('userToken')) return;
108169

109-
110170
auth0.getSSOData(function (err, data) {
111171
// if there is still a session, do nothing
112172
if (err || (data && data.sso)) return;

config/sidebar.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,11 @@ articles:
145145
- title: "Overview"
146146
url: "/sso"
147147

148-
- title: "Server-side (Regular Web Apps)"
149-
url: "/sso/regular-web-apps-sso"
150-
151148
- title: "Client-side (Single Page Apps)"
152149
url: "/sso/single-page-apps-sso"
153150

151+
- title: "Server-side (Regular Web Apps)"
152+
url: "/sso/regular-web-apps-sso"
154153

155154
- title: "User Profile"
156155
url: "/user-profile"
Loading
Loading

0 commit comments

Comments
 (0)