Using Discourse as an SSO provider is documented here:
That howto also links to a PHP script by @paxmanchris that implements Discourse as SSO provider: