# Discourse não redireciona para return\_sso\_url após o usuário fazer login no site privado

**URL:** <https://meta.discourse.org/t/discourse-doesnt-redirect-to-return-sso-url-after-user-logs-in-on-private-site/97201>\
**Category:** Support\
**Created:** [14 Setembro , 2018 17:40 UTC](https://meta.discourse.org/t/discourse-doesnt-redirect-to-return-sso-url-after-user-logs-in-on-private-site/97201 "2018-09-14T17:40:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tdfischer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tdfischer/32/103856_2.png) [@tdfischer](https://meta.discourse.org/u/tdfischer)\
**Post date:** [14 Setembro , 2018 17:40 UTC](https://meta.discourse.org/t/discourse-doesnt-redirect-to-return-sso-url-after-user-logs-in-on-private-site/97201/1 "2018-09-14T17:40:22Z")

</div>

I have a discourse instance which requires you to login, and doesn’t support anonymous browsing.

I also have a separate django app that uses python-social-auth to login to Discourse using its SSO protocol.

When a user logs in to the django app using the [discourse SSO](https://meta.discourse.org/t/13045?silent=true) login, one of the two following sequences happen:

If the user is already logged in to discourse:

1. They click the login button in the django app
2. and are redirected to discourse’s SSO url
3. and are immediately redirected back to the django app

However, if they’re not already logged in to discourse:

1. They click the login button in the django app
2. and are redirected to discourse’s SSO url
3. which then redirects them to /login in the discourse app
4. the user logs in to discourse
5. they are redirected to the front discourse page
6. and are never redirected back to the django app, meaning they need to remember to go back to the app after logging in to discourse and /start over from step 1, but now following other sequence of steps I wrote above/

My django app’s [discourse SSO](https://meta.discourse.org/t/13045?silent=true) code can be found here:

> <https://github.com/tdfischer/organizer/blob/master/organizer/auth.py#L12>

I followed the steps here:

> [@Use Discourse as an identity provider (SSO, DiscourseConnect)](https://meta.discourse.org/t/using-discourse-as-a-sso-provider/32974):
>
> So you want to use Discourse as an identity provider for your own web app? Great! Let’s get started. Enable [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true) provider setting Under Discourse admin site settings (/admin/site\_settings) enable setting enable discourse connect provider and add a secret string to discourse connect provider secrets (used to hash SSO payloads). Implement [DiscourseConnect](https://meta.discourse.org/t/13045?silent=true) in your web app: Generate a random [nonce](https://en.wikipedia.org/wiki/Cryptographic_nonce). Let’s call this value NONCE. Save it temporarily so that you can verify it with the …

Am I missing something that isn’t documented? I see a lot of posts on [discourse.org](http://discourse.org) and a few merged pull requests in github that seem to allude to this having been fixed once before in 2015, but I can’t get it to work for some reason.

---

<div class="post-metadata">

**Author:** ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)\
**Post date:** [14 Setembro , 2018 20:34 UTC](https://meta.discourse.org/t/discourse-doesnt-redirect-to-return-sso-url-after-user-logs-in-on-private-site/97201/2 "2018-09-14T20:34:47Z")

</div>

I’ve just tested this out with the [WP Discourse](https://github.com/discourse/wp-discourse) plugin and using Discourse as the SSO Provider is working correctly for me on a private site. Users who are already logged into Discourse are automatically redirected back to the website. Users who are not logged into Discourse are redirect to the Discourse `/login` page. After clicking the Login button and entering their credentials, they are redirected back to the website.

You can see the [WP Discourse](https://github.com/discourse/wp-discourse) implementation here: [wp-discourse/lib/sso-client at main · discourse/wp-discourse · GitHub](https://github.com/discourse/wp-discourse/tree/master/lib/sso-client). The important part is how the payload is created: [wp-discourse/lib/sso-client/query-redirect.php at main · discourse/wp-discourse · GitHub](https://github.com/discourse/wp-discourse/blob/master/lib/sso-client/query-redirect.php#L93).

In the Discourse code, the difference between how logged in and non-logged in users is handled starts here: [discourse/app/controllers/session\_controller.rb at main · discourse/discourse · GitHub](https://github.com/discourse/discourse/blob/master/app/controllers/session_controller.rb#L56). A non-logged in user is redirected to `/login` with `cookies[:sso_payload]` set to the query string you sent from your django app. After being logged in, the `sso_provider` code should be being called again, with the payload set to the value of `cookies[:sso_payload]`. It seems that’s not happening for you.

---

<div class="post-metadata">

**Author:** ![tdfischer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tdfischer/32/103856_2.png) [@tdfischer](https://meta.discourse.org/u/tdfischer)\
**Post date:** [20 Setembro , 2018 04:05 UTC](https://meta.discourse.org/t/discourse-doesnt-redirect-to-return-sso-url-after-user-logs-in-on-private-site/97201/3 "2018-09-20T04:05:17Z")

</div>

Since you mentioned it, I decided to test the wp-discourse plugin as well. We currently use it for login to our wordpress site without issue, however having just now tested it inside of an incognito window I see that I’m getting the same exact behavior of no redirect back to wordpress after using the twitter login!

I’m really not sure what is different here. I used the chrome inspector to pick out the requests and here’s what I found:

- Clicking on the django app’s ‘login with discourse’ button redirects me to `/session/sso_provider?sig=bc16879aa51daa815318c1de6acdf8adb85f6e778d1e739334dd425a8b047b2d&amp;sso=bm9uY2U9ODEwOTg1NzkmcmV0dXJuX3Nzb191cmw9aHR0cHM6Ly9vcmdhbml6aW5nLmVhc3RiYXlmb3JldmVyeW9uZS5vcmcvY29tcGxldGUvZGlzY291cnNlLw%3D%3D`
- Discourse responds to that request with a 302 redirect to /login, and sets the `sso_payload` cookie to `sig%3Dbc16879aa51daa815318c1de6acdf8adb85f6e778d1e739334dd425a8b047b2d%26sso%3Dbm9uY2U9ODEwOTg1NzkmcmV0dXJuX3Nzb191cmw9aHR0cHM6Ly9vcmdhbml6aW5nLmVhc3RiYXlmb3JldmVyeW9uZS5vcmcvY29tcGxldGUvZGlzY291cnNlLw%253D%253D`
- Following the redirect, my browser sends `sig%3Dbc16879aa51daa815318c1de6acdf8adb85f6e778d1e739334dd425a8b047b2d%26sso%3Dbm9uY2U9ODEwOTg1NzkmcmV0dXJuX3Nzb191cmw9aHR0cHM6Ly9vcmdhbml6aW5nLmVhc3RiYXlmb3JldmVyeW9uZS5vcmcvY29tcGxldGUvZGlzY291cnNlLw%253D%253D` for the `sso_payload` cookie.
- At this point, I’m looking at the “please login to discourse” screen. I click the login button, then the twitter button in the pop-up.
- I sign in with my twitter account
- The same song and dance happens, and my browser ends up requesting `/` from the discourse app, while sending the `sso_payload` cookie of `sso_payload=sig%3Dbc16879aa51daa815318c1de6acdf8adb85f6e778d1e739334dd425a8b047b2d%26sso%3Dbm9uY2U9ODEwOTg1NzkmcmV0dXJuX3Nzb191cmw9aHR0cHM6Ly9vcmdhbml6aW5nLmVhc3RiYXlmb3JldmVyeW9uZS5vcmcvY29tcGxldGUvZGlzY291cnNlLw%253D%253D`
- I am never redirected to my django app.

In every step, the cookie seems intact and I can base64 decode it to generate the proper `sso_payload`. Here is one such payload: `nonce=81098579&return_sso_url=http://localhost:8000/complete/discourse/`

Should I be urlencoding the return\_sso\_url parameter as well? That isn’t mentioned in the documentation, so I’m currently not doing; modifying my django app to urlencode it also does not change this behavior and still gets me no redirects. That also wouldn’t explain why our wordpress plugin isn’t getting redirects either.

After re-reading your post though, I’m wondering if you’re testing using username+password logins instead of twitter logins. We have password logins turned off and the only way you can login to our discourse is through twitter. That might explain the differences.

---

<div class="post-metadata">

**Author:** ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)\
**Post date:** [20 Setembro , 2018 04:13 UTC](https://meta.discourse.org/t/discourse-doesnt-redirect-to-return-sso-url-after-user-logs-in-on-private-site/97201/4 "2018-09-20T04:13:07Z")

</div>

> [@tdfischer](#):
>
> After re-reading your post though, I’m wondering if you’re testing using username+password logins instead of twitter logins. We have password logins turned off and the only way you can login to our discourse is through twitter. That might explain the differences.

Yes, that’s it! I can reproduce the problem with the wp-discourse plugin when I use Facebook login. I’ll look into what’s going on.

---

<div class="post-metadata">

**Author:** ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)\
**Post date:** [4 Outubro , 2018 19:11 UTC](https://meta.discourse.org/t/discourse-doesnt-redirect-to-return-sso-url-after-user-logs-in-on-private-site/97201/8 "2018-10-04T19:11:40Z")

</div>

It’s fixed as per below commits. @tdfischer can you please check again.

[https://github.com/discourse/discourse/commit/ca7424665118e527eddbcb3c8bb764dd942585d7](https://github.com/discourse/discourse/commit/ca7424665118e527eddbcb3c8bb764dd942585d7)

[https://github.com/discourse/discourse/commit/be6404d6519d03cfb9194bc03e5c6b9593513c1d](https://github.com/discourse/discourse/commit/be6404d6519d03cfb9194bc03e5c6b9593513c1d)

---

<div class="post-metadata">

**Author:** ![vinothkannans](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/vinothkannans/32/86465_2.png) [@vinothkannans](https://meta.discourse.org/u/vinothkannans)\
**Post date:** [29 Novembro , 2018 18:33 UTC](https://meta.discourse.org/t/discourse-doesnt-redirect-to-return-sso-url-after-user-logs-in-on-private-site/97201/9 "2018-11-29T18:33:50Z")

</div>

This topic was automatically closed after 52 days. New replies are no longer allowed.
