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

Commit

Permalink
Merge pull request #1 from pencil/api-login-processor
Browse files Browse the repository at this point in the history
API login processor
  • Loading branch information
pencil committed Dec 30, 2012
2 parents b17969d + e94bd23 commit 593a46a
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 23 deletions.
1 change: 1 addition & 0 deletions lib/casino_core/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

module CASinoCore
module Helper
autoload :Authentication, 'casino_core/helper/authentication.rb'
autoload :Browser, 'casino_core/helper/browser.rb'
autoload :Logger, 'casino_core/helper/logger.rb'
autoload :LoginTickets, 'casino_core/helper/login_tickets.rb'
Expand Down
20 changes: 20 additions & 0 deletions lib/casino_core/helper/authentication.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module CASinoCore
module Helper
module Authentication

def validate_login_credentials(username, password)
authentication_result = nil
CASinoCore::Settings.authenticators.each do |authenticator_name, authenticator|
data = authenticator.validate(username, password)
if data
authentication_result = { authenticator: authenticator_name, user_data: data }
logger.info("Credentials for username '#{data[:username]}' successfully validated using authenticator '#{authenticator_name}' (#{authenticator.class})")
break
end
end
authentication_result
end

end
end
end
13 changes: 13 additions & 0 deletions lib/casino_core/helper/ticket_granting_tickets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
module CASinoCore
module Helper
module TicketGrantingTickets

include CASinoCore::Helper::Browser
include CASinoCore::Helper::Logger

Expand All @@ -19,6 +20,18 @@ def find_valid_ticket_granting_ticket(tgt, user_agent)
end
end
end

def acquire_ticket_granting_ticket(authentication_result, user_agent = nil)
user_data = authentication_result[:user_data]
CASinoCore::Model::TicketGrantingTicket.create!({
ticket: random_ticket_string('TGC'),
authenticator: authentication_result[:authenticator],
username: user_data[:username],
extra_attributes: user_data[:extra_attributes],
user_agent: user_agent
})
end

end
end
end
2 changes: 2 additions & 0 deletions lib/casino_core/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Processor
autoload :SessionDestroyer, 'casino_core/processor/session_destroyer.rb'
autoload :SessionOverview, 'casino_core/processor/session_overview.rb'

autoload :API, 'casino_core/processor/api.rb'

def initialize(listener)
@listener = listener
end
Expand Down
7 changes: 7 additions & 0 deletions lib/casino_core/processor/api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module CASinoCore
class Processor
module API
autoload :LoginCredentialAcceptor, 'casino_core/processor/api/login_credential_acceptor.rb'
end
end
end
49 changes: 49 additions & 0 deletions lib/casino_core/processor/api/login_credential_acceptor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'casino_core/processor/api'
require 'casino_core/helper'

# This processor should be used for API calls: POST /cas/v1/tickets
class CASinoCore::Processor::API::LoginCredentialAcceptor < CASinoCore::Processor
include CASinoCore::Helper::Logger
include CASinoCore::Helper::ServiceTickets
include CASinoCore::Helper::Authentication
include CASinoCore::Helper::TicketGrantingTickets

# Use this method to process the request. It expects the username in the parameter "username" and the password
# in "password".
#
# The method will call one of the following methods on the listener:
# * `#user_logged_in_via_api`: First and only argument is a String with the TGT-id
# * `#invalid_login_credentials_via_api`: No argument
#
# @param [Hash] login_data parameters supplied by user (username and password)
def process(login_data)
@login_data = login_data

validate_login_data

unless @authentication_result.nil?
generate_ticket_granting_ticket
callback_user_logged_in
else
callback_invalid_login_credentials
end
end

private
def validate_login_data
@authentication_result = validate_login_credentials(@login_data[:username], @login_data[:password])
end

def callback_user_logged_in
@listener.user_logged_in_via_api @ticket_granting_ticket.ticket
end

def generate_ticket_granting_ticket
@ticket_granting_ticket = acquire_ticket_granting_ticket(@authentication_result)
end

def callback_invalid_login_credentials
@listener.invalid_login_credentials_via_api
end

end
25 changes: 2 additions & 23 deletions lib/casino_core/processor/login_credential_acceptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class CASinoCore::Processor::LoginCredentialAcceptor < CASinoCore::Processor
include CASinoCore::Helper::Logger
include CASinoCore::Helper::LoginTickets
include CASinoCore::Helper::ServiceTickets
include CASinoCore::Helper::Authentication
include CASinoCore::Helper::TicketGrantingTickets

# Use this method to process the request. It expects the username in the parameter "username" and the password
# in "password".
Expand Down Expand Up @@ -54,27 +56,4 @@ def login_ticket_valid?(lt)
end
end

def validate_login_credentials(username, password)
authentication_result = nil
CASinoCore::Settings.authenticators.each do |authenticator_name, authenticator|
data = authenticator.validate(username, password)
if data
authentication_result = { authenticator: authenticator_name, user_data: data }
logger.info("Credentials for username '#{data[:username]}' successfully validated using authenticator '#{authenticator_name}' (#{authenticator.class})")
break
end
end
authentication_result
end

def acquire_ticket_granting_ticket(authentication_result, user_agent = nil)
user_data = authentication_result[:user_data]
CASinoCore::Model::TicketGrantingTicket.create!({
ticket: random_ticket_string('TGC'),
authenticator: authentication_result[:authenticator],
username: user_data[:username],
extra_attributes: user_data[:extra_attributes],
user_agent: user_agent
})
end
end
37 changes: 37 additions & 0 deletions spec/processor/api/login_credential_acceptor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'spec_helper'

describe CASinoCore::Processor::API::LoginCredentialAcceptor do
describe '#process' do
let(:listener) { Object.new }
let(:processor) { described_class.new(listener) }

context 'with invalid credentials' do
let(:login_data) { {username: 'testuser', password: 'wrong'} }

it 'calls the #invalid_login_credentials method on the listener' do
listener.should_receive(:invalid_login_credentials_via_api)
processor.process(login_data).should be_false
end
end

context 'with valid credentials' do
let(:login_data) { {username: 'testuser', password: 'foobar123'} }

before(:each) do
listener.stub(:user_logged_in)
end

it 'calls the #user_logged_in method on the listener' do
listener.should_receive(:user_logged_in_via_api).with(/^TGC\-/)
processor.process(login_data)
end

it 'generates a ticket-granting ticket' do
listener.should_receive(:user_logged_in_via_api).with(/^TGC\-/)
expect {
processor.process(login_data)
}.to change(CASinoCore::Model::TicketGrantingTicket, :count).by(1)
end
end
end
end

0 comments on commit 593a46a

Please sign in to comment.