Incorporating Discourse SSO with Existing Rails Site with Devise

后端 未结 1 1710
野的像风
野的像风 2021-01-30 07:39

I have an existing rails app that is using devise as it\'s user authentication. I added a discourse forum and everything went smoothly and it resides on a subdomain. I have read

相关标签:
1条回答
  • 2021-01-30 08:20

    This is pretty straightforward. Following on from the instructions at https://meta.discourse.org/t/official-single-sign-on-for-discourse/13045 and extrapolating a little, I have this working:

    1) Put the reference implementation - https://github.com/discourse/discourse/blob/master/lib/single_sign_on.rb - in your #{Rails.root}/lib directory

    2) Add this route to routes.rb

    get 'discourse/sso' => 'discourse_sso#sso'
    

    3) Put this controller in your app/controllers directory

    require 'single_sign_on'
    
    class DiscourseSsoController < ApplicationController
      before_action :authenticate_user! # ensures user must login
    
      def sso
        secret = "MY_SECRET_STRING"
        sso = SingleSignOn.parse(request.query_string, secret)
        sso.email = current_user.email # from devise
        sso.name = current_user.full_name # this is a custom method on the User class
        sso.username = current_user.email # from devise
        sso.external_id = current_user.id # from devise
        sso.sso_secret = secret
    
        redirect_to sso.to_url("http://your_discource_server/session/sso_login")
      end
    end
    

    4) Set up the SSO config in discourse to have the following

    sso url: http://your_rails_server/discourse/sso
    sso secret : what you set as MY_SECRET_STRING above
    

    5) Disable other login types in discourse.

    6) Try to login in discourse. It should work...

    0 讨论(0)
提交回复
热议问题