Skip to content

Instantly share code, notes, and snippets.

@gogojimmy
Created September 26, 2013 04:03
Show Gist options
  • Save gogojimmy/6709744 to your computer and use it in GitHub Desktop.
Save gogojimmy/6709744 to your computer and use it in GitHub Desktop.
Simple SSO
class ApplicationController < ActionController::Base
before_action :auth_via_sso_cookie
private
def auth_via_sso_cookie
if !current_user_id && cookies[:sso_from] && cookies[:sso_token] && cookies[:sso_env] == Rails.env
verifier = ActiveSupport::MessageVerifier.new( SSO_SECRET_TOKEN.fetch(cookies[:sso_from]) )
user_id = verifier.verify( cookies[:sso_token] )
session[:current_user_id] = user_id
end
end
def add_sso_cookie(user_id)
verifier = ActiveSupport::MessageVerifier.new( WebSite::Application.config.secret_token )
value = verifier.generate(user_id)
cookies[:sso_from] = { value: "website", domain: SSO_DOMAIN }
cookies[:sso_env] = { value: Rails.env, domain: SSO_DOMAIN }
cookies[:sso_token] = { value: value, domain: SSO_DOMAIN }
end
def remove_sso_cookie
cookies.delete(:sso_from, domain: SSO_DOMAIN)
cookies.delete(:sso_env, domain: SSO_DOMAIN)
cookies.delete(:sso_token, domain: SSO_DOMAIN)
end
end
SSO_DOMAIN = "website.com"
SSO_SECRET_TOKEN = {
"website" => 'blahblahblah'
}
class SessionsController < ApplicationController
def create
#..after sign in
add_sso_cookie( user["id"] )
end
def destroy
#..after sign out
remove_sso_cookie
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment