# How to use SSO (Discourse on subdomain)

**URL:** https://meta.discourse.org/t/how-to-use-sso-discourse-on-subdomain/76630
**Category:** Self-hosting
**Created:** [22.Декабрь.2017 15:53:16 UTC](https://meta.discourse.org/t/how-to-use-sso-discourse-on-subdomain/76630 "2017-12-22T15:53:16Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Jean-Michel25](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@Jean-Michel25](https://meta.discourse.org/u/Jean-Michel25)
#### Post date: [22.Декабрь.2017 15:53:16 UTC](https://meta.discourse.org/t/how-to-use-sso-discourse-on-subdomain/76630/1 "2017-12-22T15:53:16Z")

</div>

Hello

Many time to read topics, but I cannot undestand how to do.  
Maybe you can help me ?

We installed a Discourse forum on OVH VPS, subdomain of our site ([forum.mysite.com](http://forum.mysite.com))

Old users will be imported into Discourse Forum.  
New users will create account by Discourse Forum.  
All users will sign on with Discourse.  
OK

Our main site ([www.mysite.com](http://www.mysite.com)) is PHP/MySQL.  
My php pages have to know if the user is logged, his username, his group.  
How to do ?  
I can create PHP scripts, but I don’t know Ruby.

Maybe read session (cookie) ?

I found this but cannot find what to do with :

> <https://gist.github.com/paxmanchris/e93018a3e8fbdfced039>

Your help is welcome.  
Discourse is new for me…

Many thanks

---

<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: [22.Декабрь.2017 16:18:14 UTC](https://meta.discourse.org/t/how-to-use-sso-discourse-on-subdomain/76630/2 "2017-12-22T16:18:14Z")

</div>

> [@Jean-Michel25](#):
>
> How to do ?  
> I can create PHP scripts, but I don’t know Ruby.

SSO provider feature is inbuilt in Discourse core. So you don’t have to worry about it. Follow this #howto [Use Discourse as an identity provider (SSO, DiscourseConnect)](https://meta.discourse.org/t/using-discourse-as-a-sso-provider/32974).

In [this line](https://gist.github.com/paxmanchris/e93018a3e8fbdfced039#file-sso_login_discourse-php-L37-L38) you will receive logged-in user details if you configured everything _(php code + settings)_ correctly.

---

<div class="post-metadata">

### Author: ![Jean-Michel25](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@Jean-Michel25](https://meta.discourse.org/u/Jean-Michel25)
#### Post date: [22.Декабрь.2017 16:31:34 UTC](https://meta.discourse.org/t/how-to-use-sso-discourse-on-subdomain/76630/3 "2017-12-22T16:31:34Z")

</div>

OK thanks.

I see it’s the solution, but I don’t andertand howto use.

The way with this script :  
1/ On external site, user click the link `<a href='$discourse_url/session/sso_provider?$query'>sign in with discourse</a>`  
2/ Discourse returns user info  
3/ external site open a session ?

I’m ok or not ?

I’m not far, but have not yet all keys…  
Migration stopped until I anderstand how to use the script…  
Many thanks !

---

<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: [22.Декабрь.2017 16:39:11 UTC](https://meta.discourse.org/t/how-to-use-sso-discourse-on-subdomain/76630/4 "2017-12-22T16:39:11Z")

</div>

> [@Jean-Michel25](#):
>
> 3/ external site open a session ?

Here external site received successful logged-in user details. Yes you can start a session.

> [@Jean-Michel25](#):
>
> what about set\_key(
> 
> I cannot find this function in this script…

It is just a placeholder. You have to replace your own code to create a new session in your website.

_Also since the script you referred is not official I don’t know whether it is valid working one or not._

---

<div class="post-metadata">

### Author: ![Jean-Michel25](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@Jean-Michel25](https://meta.discourse.org/u/Jean-Michel25)
#### Post date: [22.Декабрь.2017 16:42:53 UTC](https://meta.discourse.org/t/how-to-use-sso-discourse-on-subdomain/76630/5 "2017-12-22T16:42:53Z")

</div>

It’s more clearn now for me.

I read this several times and then try to do this

Thanks for helping

---

<div class="post-metadata">

### Author: ![Jean-Michel25](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@Jean-Michel25](https://meta.discourse.org/u/Jean-Michel25)
#### Post date: [22.Декабрь.2017 17:14:20 UTC](https://meta.discourse.org/t/how-to-use-sso-discourse-on-subdomain/76630/6 "2017-12-22T17:14:20Z")

</div>

Last question please about get\_key() and set\_key() functions

> // pretend that set\_key is a function that saves key value data in a database

Initially we store the nonce in a database (a single column table ?)  
When Discourse respond, we just check if nonce exists in the table ?  
If yes, we store login infos received from Discourse with the $\_GET  
(then store in PHP session…)

Is it ok ?

---

<div class="post-metadata">

### Author: ![Jean-Michel25](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@Jean-Michel25](https://meta.discourse.org/u/Jean-Michel25)
#### Post date: [22.Декабрь.2017 17:48:10 UTC](https://meta.discourse.org/t/how-to-use-sso-discourse-on-subdomain/76630/7 "2017-12-22T17:48:10Z")

</div>

I just create a table with column “nonce”, and it’s ok.

Now I have to purge the table to delete old nonce, I have to set session variables, and all is ok.

Thanks again

---

<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: [22.Декабрь.2017 18:06:26 UTC](https://meta.discourse.org/t/how-to-use-sso-discourse-on-subdomain/76630/8 "2017-12-22T18:06:26Z")

</div>

I think you don’t need to store your nonce in db. It is not like an API key.

---

<div class="post-metadata">

### Author: ![Jean-Michel25](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@Jean-Michel25](https://meta.discourse.org/u/Jean-Michel25)
#### Post date: [22.Декабрь.2017 18:09:20 UTC](https://meta.discourse.org/t/how-to-use-sso-discourse-on-subdomain/76630/9 "2017-12-22T18:09:20Z")

</div>

Only in a session variable, so I can check it with value sent by Discourse ?

---

<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: [22.Декабрь.2017 18:15:32 UTC](https://meta.discourse.org/t/how-to-use-sso-discourse-on-subdomain/76630/10 "2017-12-22T18:15:32Z")

</div>

> [@Use Discourse as an identity provider (SSO, DiscourseConnect)](https://meta.discourse.org/t/use-discourse-as-an-identity-provider-sso-discourseconnect/32974/1):
>
> Generate a random nonce. Save it temporarily so that you can verify it with returned nonce value

Yes. anywhere temporarily.

---

<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: [22.Декабрь.2017 18:28:09 UTC](https://meta.discourse.org/t/how-to-use-sso-discourse-on-subdomain/76630/11 "2017-12-22T18:28:09Z")

</div>

I misread what is being done here. To use Discourse as the SSO provider, the nonce will have to be stored somewhere on the website. The wp-discourse plugin does it by saving the value to the database.

---

<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: [22.Декабрь.2017 18:46:39 UTC](https://meta.discourse.org/t/how-to-use-sso-discourse-on-subdomain/76630/13 "2017-12-22T18:46:39Z")

</div>

Yes. @Jean-Michel25 you can use either way which is easier to you.

---

<div class="post-metadata">

### Author: ![Jean-Michel25](https://avatars.discourse-cdn.com/v4/letter/j/e19adc/32.png) [@Jean-Michel25](https://meta.discourse.org/u/Jean-Michel25)
#### Post date: [10.Октябрь.2019 11:50:30 UTC](https://meta.discourse.org/t/how-to-use-sso-discourse-on-subdomain/76630/14 "2019-10-10T11:50:30Z")

</div>

Здравствуйте

Хотел подтвердить это для будущих читателей.

Я использую переменные сессии для хранения nonce, а затем сравниваю их со строкой, возвращаемой SSO-сервером.  
Всё это работает отлично.

Спасибо за помощь!
