Created
September 26, 2013 04:03
-
-
Save gogojimmy/6709744 to your computer and use it in GitHub Desktop.
Simple SSO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SSO_DOMAIN = "website.com" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SSO_SECRET_TOKEN = { | |
"website" => 'blahblahblah' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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