Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
Merge branch 'casino-4'
Browse files Browse the repository at this point in the history
  • Loading branch information
pencil committed May 15, 2015
2 parents 3339986 + fc431de commit 64d5ba0
Show file tree
Hide file tree
Showing 144 changed files with 2,601 additions and 3,854 deletions.
15 changes: 12 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
language: ruby
rvm:
- 1.9.3
- 2.0.0
- 2.1.0
- 1.9.3
- 2.0.0
- 2.1.0
notifications:
hipchat:
rooms:
secure: kXPfZwOtdwJM0NIOj2td/NoPOhzxWVlUfHQuke2N4fuoKDQ+nhz5ZV4btW5J+O5C5aC6qyBBFdm+FzA/8m1WiLMGX0DIE1X67zZts/udMwtIDRNoHV594hd2co4oA72QMUT5kdre7IvTpSnnJwkp/d3V0kB7DOHuEbDJsjipx8I=
template:
- '%{repository} <a href="%{build_url}">#%{build_number}</a> (%{branch} - <a href="%{compare_url}">%{commit}</a> : %{author}): %{message}'
format: html
on_failure: always
on_success: change
7 changes: 7 additions & 0 deletions app/api/casino/api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'grape'

class CASino::API < Grape::API
format :json

mount CASino::API::Resource::AuthTokenTickets
end
5 changes: 5 additions & 0 deletions app/api/casino/api/entity/auth_token_ticket.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'grape-entity'

class CASino::API::Entity::AuthTokenTicket < Grape::Entity
expose :ticket
end
12 changes: 12 additions & 0 deletions app/api/casino/api/resource/auth_token_tickets.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'grape'

class CASino::API::Resource::AuthTokenTickets < Grape::API
resource :auth_token_tickets do
desc 'Create an auth token ticket'
post do
@ticket = CASino::AuthTokenTicket.create
Rails.logger.debug "Created auth token ticket '#{@ticket.ticket}'"
present @ticket, with: CASino::API::Entity::AuthTokenTicket
end
end
end
10 changes: 8 additions & 2 deletions app/authenticators/casino/static_authenticator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,18 @@ def initialize(options)
def validate(username, password)
username = :"#{username}"
if @users.include?(username) && @users[username][:password] == password
load_user_data(username)
else
false
end
end

def load_user_data(username)
if @users.include?(username)
{
username: "#{username}",
extra_attributes: @users[username].except(:password)
}
else
false
end
end
end
24 changes: 24 additions & 0 deletions app/builders/casino/proxy_response_builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'builder'

class CASino::ProxyResponseBuilder
attr_reader :success, :options

def initialize(success, options)
@success = success
@options = options
end

def build
xml = Builder::XmlMarkup.new(indent: 2)
xml.cas :serviceResponse, 'xmlns:cas' => 'http://www.yale.edu/tp/cas' do |service_response|
if success
service_response.cas :proxySuccess do |proxy_success|
proxy_success.cas :proxyTicket, options[:proxy_ticket].ticket
end
else
service_response.cas :proxyFailure, options[:error_message], code: options[:error_code]
end
end
xml.target!
end
end
14 changes: 9 additions & 5 deletions app/builders/casino/ticket_validation_response_builder.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'builder'

class CASino::TicketValidationResponseBuilder
attr_reader :success, :options

def initialize(success, options)
@success = success
@options = options
Expand All @@ -9,8 +11,8 @@ def initialize(success, options)
def build
xml = Builder::XmlMarkup.new(indent: 2)
xml.cas :serviceResponse, 'xmlns:cas' => 'http://www.yale.edu/tp/cas' do |service_response|
if @success
ticket = @options[:ticket]
if success
ticket = options[:ticket]
if ticket.is_a?(CASino::ProxyTicket)
proxies = []
service_ticket = ticket
Expand Down Expand Up @@ -38,6 +40,8 @@ def serialize_extra_attribute(builder, key, value)
key = :"#{key}"
if value.kind_of?(String) || value.kind_of?(Numeric) || value.kind_of?(Symbol)
builder.cas key, "#{value}"
elsif value.kind_of?(Array)
value.each { |v| serialize_extra_attribute(builder, key, v) }
else
builder.cas key do |container|
container.cdata! value.to_yaml
Expand Down Expand Up @@ -65,8 +69,8 @@ def build_success_xml(service_response, ticket, service_ticket, ticket_granting_
end
end
end
if @options[:proxy_granting_ticket]
proxy_granting_ticket = @options[:proxy_granting_ticket]
if options[:proxy_granting_ticket]
proxy_granting_ticket = options[:proxy_granting_ticket]
authentication_success.cas :proxyGrantingTicket, proxy_granting_ticket.iou
end
if ticket.is_a?(CASino::ProxyTicket)
Expand All @@ -80,6 +84,6 @@ def build_success_xml(service_response, ticket, service_ticket, ticket_granting_
end

def build_failure_xml(service_response)
service_response.cas :authenticationFailure, @options[:error_message], code: @options[:error_code]
service_response.cas :authenticationFailure, options[:error_message], code: options[:error_code]
end
end
55 changes: 0 additions & 55 deletions app/controllers/casino/api/v1/tickets_controller.rb

This file was deleted.

22 changes: 0 additions & 22 deletions app/controllers/casino/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
require 'casino'
require 'http_accept_language'

class CASino::ApplicationController < ::ApplicationController
include ApplicationHelper

layout 'application'
before_filter :set_locale

unless Rails.env.development?
rescue_from ActionView::MissingTemplate, with: :missing_template
Expand All @@ -16,26 +14,6 @@ def cookies
end

protected
def processor(processor_name, listener_name = nil)
listener_name ||= processor_name
listener = CASino.const_get(:"#{listener_name}Listener").new(self)
@processor = CASino.const_get(:"#{processor_name}Processor").new(listener)
end

def set_locale
I18n.locale = extract_locale_from_accept_language_header || I18n.default_locale
end

def extract_locale_from_accept_language_header
if request.env['HTTP_ACCEPT_LANGUAGE']
http_accept_language.preferred_language_from(I18n.available_locales)
end
end

def http_accept_language
HttpAcceptLanguage::Parser.new request.env['HTTP_ACCEPT_LANGUAGE']
end

def missing_template(exception)
render plain: 'Format not supported', status: :not_acceptable
end
Expand Down
34 changes: 34 additions & 0 deletions app/controllers/casino/auth_tokens_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class CASino::AuthTokensController < CASino::ApplicationController
include CASino::SessionsHelper

def login
validation_result = validation_service.validation_result
return redirect_to_login unless validation_result
sign_in(validation_result)
end

private
def validation_service
@validation_service ||= CASino::AuthTokenValidationService.new(auth_token, auth_token_signature)
end

def redirect_to_login
redirect_to login_path(service: params[:service])
end

def auth_token_signature
@auth_token_signature ||= base64_decode(params[:ats])
end

def auth_token
@auth_token ||= base64_decode(params[:at])
end

def base64_decode(data)
begin
Base64.strict_decode64(data)
rescue
''
end
end
end
30 changes: 30 additions & 0 deletions app/controllers/casino/controller_concern/ticket_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module CASino::ControllerConcern::TicketValidator
extend ActiveSupport::Concern
include CASino::ServiceTicketProcessor
include CASino::ProxyGrantingTicketProcessor

def validate_ticket(ticket)
validation_result = validate_ticket_for_service(ticket, params[:service], renew: params[:renew])
if validation_result.success?
options = { ticket: ticket }
options[:proxy_granting_ticket] = acquire_proxy_granting_ticket(params[:pgtUrl], ticket) unless params[:pgtUrl].nil?
build_ticket_validation_response(true, options)
else
build_ticket_validation_response(false,
error_code: validation_result.error_code,
error_message: validation_result.error_message)
end
end

def build_ticket_validation_response(success, options = {})
render xml: CASino::TicketValidationResponseBuilder.new(success, options).build
end

def ensure_service_ticket_parameters_present
if params[:ticket].nil? || params[:service].nil?
build_ticket_validation_response(false,
error_code: 'INVALID_REQUEST',
error_message: '"ticket" and "service" parameters are both required')
end
end
end
44 changes: 42 additions & 2 deletions app/controllers/casino/proxy_tickets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@
class CASino::ProxyTicketsController < CASino::ApplicationController
include CASino::ControllerConcern::TicketValidator

before_action :load_ticket, only: [:proxy_validate]
before_action :ensure_service_ticket_parameters_present, only: [:proxy_validate]

before_action :load_proxy_granting_ticket, only: [:create]
before_action :ensure_proxy_parameters_present, only: [:create]

def proxy_validate
processor(:ProxyTicketValidator, :TicketValidator).process(params)
validate_ticket(@ticket)
end

def create
processor(:ProxyTicketProvider).process(params)
proxy_ticket = @proxy_granting_ticket.proxy_tickets.create!(service: params[:targetService])
build_proxy_response(true, proxy_ticket: proxy_ticket)
end

private
def load_ticket
@ticket = case params[:ticket]
when /\APT-/
CASino::ProxyTicket.where(ticket: params[:ticket]).first
when /\AST-/
CASino::ServiceTicket.where(ticket: params[:ticket]).first
end
end

def build_proxy_response(success, options = {})
render xml: CASino::ProxyResponseBuilder.new(success, options).build
end

def ensure_proxy_parameters_present
if params[:pgt].nil? || params[:targetService].nil?
build_proxy_response(false,
error_code: 'INVALID_REQUEST',
error_message: '"pgt" and "targetService" parameters are both required')
end
end

def load_proxy_granting_ticket
@proxy_granting_ticket = CASino::ProxyGrantingTicket.where(ticket: params[:pgt]).first if params[:pgt].present?
if @proxy_granting_ticket.nil?
build_proxy_response(false,
error_code: 'BAD_PGT',
error_message: 'PGT not found')
end
end
end
17 changes: 15 additions & 2 deletions app/controllers/casino/service_tickets_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
class CASino::ServiceTicketsController < CASino::ApplicationController
include CASino::ControllerConcern::TicketValidator

before_action :load_service_ticket
before_action :ensure_service_ticket_parameters_present, only: [:service_validate]

def validate
processor(:LegacyValidator).process(params)
if ticket_valid_for_service?(@service_ticket, params[:service], renew: params[:renew])
@username = @service_ticket.ticket_granting_ticket.user.username
end
render :validate, formats: [:text]
end

def service_validate
processor(:ServiceTicketValidator, :TicketValidator).process(params)
validate_ticket(@service_ticket)
end

private
def load_service_ticket
@service_ticket = CASino::ServiceTicket.where(ticket: params[:ticket]).first if params[:service].present?
end
end
Loading

0 comments on commit 64d5ba0

Please sign in to comment.