Skip to content

Commit

Permalink
refine and organize code
Browse files Browse the repository at this point in the history
  • Loading branch information
hlxwell committed Nov 20, 2011
1 parent f2c4bd0 commit a41b0b4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 75 deletions.
60 changes: 15 additions & 45 deletions app/controllers/application_controller.rb
@@ -1,70 +1,40 @@
class ApplicationController < ActionController::Base
protect_from_forgery

# before_filter :require_login, :except => [:not_authenticated]
helper_method :current_users_list

def render_validation_error(code, message = nil)
xml = Nokogiri::XML::Builder.new do |xml|
xml.serviceResponse("xmlns:cas" => "http://www.yale.edu/tp/cas") {
xml.parent.namespace = xml.parent.namespace_definitions.first
xml['cas'].authenticationFailure(message, :code => code.to_s.upcase){
}
}
end
render :xml => xml.to_xml
end

def render_validation_success(username)
xml = Nokogiri::XML::Builder.new do |xml|
xml.serviceResponse("xmlns:cas" => "http://www.yale.edu/tp/cas") {
xml.parent.namespace = xml.parent.namespace_definitions.first
xml['cas'].authenticationSuccess {
xml['cas'].user username
# append_user_info(username, xml)
}
}
end
render :xml => xml.to_xml
end

def get_url_host url
URI::parse(CGI.unescape(url.to_s)).host
url = URI::parse(CGI.unescape(url.to_s))
"#{url.host}:#{url.port}"
end

def has_service_info?
cookies[:service].present? and cookies[:service_back_url].present?
end

protected
def current_tgt
# unconsumed is meaningless, since it will be deleted after logout
TicketGrantingTicket.where(:ticket => cookies.signed[:tgt]).first
end

def has_valid_tgt
TicketGrantingTicket.where(:ticket => cookies[:tgt]).first # unconsumed is meaningless, since it will be deleted after logout
def not_authenticated
redirect_to root_path, :alert => "Please login first."
end

# issue a Service Ticket and redirect back
def issue_service_ticket
if tgt = has_valid_tgt and has_service_info?
# Issue a Service Ticket and return a url with this st
def back_url_with_service_ticket
if tgt = current_tgt and has_service_info?
st = ServiceTicket.create(
:service => cookies[:service],
:username => current_user.id,
:granted_by_tgt_id => tgt.id
)

service_back_url = cookies[:service_back_url]
service_back_url = cookies[:service_back_url] + "?ticket=#{st.ticket}"

# remove service info
cookies.delete :service
cookies.delete :service_back_url

return service_back_url + "?ticket=#{st.ticket}"
service_back_url
end
end

def not_authenticated
redirect_to root_path, :alert => "Please login first."
end

def current_users_list
current_users.map {|u| u.email}.join(", ")
end

end
8 changes: 6 additions & 2 deletions app/controllers/sso_api_controller.rb
@@ -1,3 +1,7 @@
###
# SSO API is used to provide LoginTicket and ServiceTicket to SSO client by JSONP.
# That other services can login through their website, without being redirected to UserCenter
#
class SsoApiController < ApplicationController
respond_to :json

Expand All @@ -18,8 +22,8 @@ def get_service_ticket
if !LoginTicket.validate_ticket(params[:lt])
render :json => { :sts => nil, :message => "Wrong login ticket" }, :callback => params[:callback]
elsif user = login(params[:email], params[:password], params[:remember])
tgt = has_valid_tgt || TicketGrantingTicket.create
cookies[:tgt] = tgt.to_s if cookies[:tgt].blank?
tgt = current_tgt || TicketGrantingTicket.create
cookies.signed[:tgt] = tgt.to_s if cookies.signed[:tgt].blank?

# issue service ticket
sts = ServiceTicket::SERVICES.map do |service|
Expand Down
30 changes: 5 additions & 25 deletions app/controllers/users_controller.rb
Expand Up @@ -5,45 +5,25 @@ def new
@user = User.new
end

def edit
@user = User.find(params[:id])
end

def create
@user = User.new params[:user]

respond_to do |format|
format.html {
if @user.save
auto_login @user
if cookies[:service]
issue_service_ticket
else
redirect_to root_url, :notice => 'Registration successfull. Check your email for activation instructions.'
end
# auto_login @user
redirect_to root_url, :notice => 'Registration successfull. Check your email for activation instructions.'
else
render :action => "new"
end
}
end
end

def update
@user = User.find params[:id]
respond_to do |format|
format.html {
if @user.update_attributes(params[:user])
redirect_to(edit_user_path(@user), :notice => 'User was successfully updated.')
else
render :action => "edit"
end
}
end
end

def activate
if @user = User.load_from_activation_token(params[:id])
if @user = User.load_from_activation_token(params[:token])
@user.activate!
redirect_to(login_path, :notice => 'User was successfully activated.')
redirect_to(login_path, :notice => 'User was successfully activated. You can login this account now.')
else
not_authenticated
end
Expand Down
31 changes: 28 additions & 3 deletions app/controllers/validators_controller.rb
@@ -1,16 +1,17 @@
##
# Validator is used to validate ServiceTicket
#
class ValidatorsController < ApplicationController
before_filter :set_variables

def serviceValidate
# check the existance of service_url and ticket
render_validation_error(:invalid_request) and return if @service.blank? or @ticket.blank?

# find the ST from DB
render_validation_error(:invalid_ticket, "ticket #{@ticket} not recognized") and return unless @st

# validate if current ST is for current service.
render_validation_error(:invalid_service) and return unless @st.valid_for_service?(@service)

# if ServiceTicket is unused return success
render_validation_success @st.username and return if @st.unused?

render_validation_error(:invalid_request)
Expand All @@ -23,4 +24,28 @@ def set_variables
@ticket = params[:ticket]
@st = ServiceTicket.where(:ticket => @ticket).first
end

def render_validation_error(code, message = nil)
xml = Nokogiri::XML::Builder.new do |xml|
xml.serviceResponse("xmlns:cas" => "http://www.yale.edu/tp/cas") {
xml.parent.namespace = xml.parent.namespace_definitions.first
xml['cas'].authenticationFailure(message, :code => code.to_s.upcase){
}
}
end
render :xml => xml.to_xml
end

def render_validation_success(username)
xml = Nokogiri::XML::Builder.new do |xml|
xml.serviceResponse("xmlns:cas" => "http://www.yale.edu/tp/cas") {
xml.parent.namespace = xml.parent.namespace_definitions.first
xml['cas'].authenticationSuccess {
xml['cas'].user username
# append_user_info(username, xml)
}
}
end
render :xml => xml.to_xml
end
end

0 comments on commit a41b0b4

Please sign in to comment.