This repository has been archived by the owner on Feb 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from pencil/api-login-processor
API login processor
- Loading branch information
Showing
8 changed files
with
131 additions
and
23 deletions.
There are no files selected for viewing
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
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
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 |
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
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
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
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
49
lib/casino_core/processor/api/login_credential_acceptor.rb
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
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 |
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
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
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 |