casino_core 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.4
1
+ 1.0.5
data/casino_core.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "casino_core"
8
- s.version = "1.0.4"
8
+ s.version = "1.0.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Nils Caspar"]
@@ -56,6 +56,7 @@ Gem::Specification.new do |s|
56
56
  "lib/casino_core/builder.rb",
57
57
  "lib/casino_core/builder/ticket_validation_response.rb",
58
58
  "lib/casino_core/helper.rb",
59
+ "lib/casino_core/helper/authentication.rb",
59
60
  "lib/casino_core/helper/browser.rb",
60
61
  "lib/casino_core/helper/logger.rb",
61
62
  "lib/casino_core/helper/login_tickets.rb",
@@ -72,6 +73,10 @@ Gem::Specification.new do |s|
72
73
  "lib/casino_core/model/service_ticket/single_sign_out_notifier.rb",
73
74
  "lib/casino_core/model/ticket_granting_ticket.rb",
74
75
  "lib/casino_core/processor.rb",
76
+ "lib/casino_core/processor/api.rb",
77
+ "lib/casino_core/processor/api/login_credential_acceptor.rb",
78
+ "lib/casino_core/processor/api/logout.rb",
79
+ "lib/casino_core/processor/api/service_ticket_provider.rb",
75
80
  "lib/casino_core/processor/legacy_validator.rb",
76
81
  "lib/casino_core/processor/login_credential_acceptor.rb",
77
82
  "lib/casino_core/processor/login_credential_requestor.rb",
@@ -93,6 +98,9 @@ Gem::Specification.new do |s|
93
98
  "spec/model/service_ticket/single_sign_out_notifier_spec.rb",
94
99
  "spec/model/service_ticket_spec.rb",
95
100
  "spec/model/ticket_granting_ticket_spec.rb",
101
+ "spec/processor/api/login_credential_acceptor_spec.rb",
102
+ "spec/processor/api/logout_spec.rb",
103
+ "spec/processor/api/service_ticket_provider_spec.rb",
96
104
  "spec/processor/legacy_validator_spec.rb",
97
105
  "spec/processor/login_credential_acceptor_spec.rb",
98
106
  "spec/processor/login_credential_requestor_spec.rb",
@@ -0,0 +1,20 @@
1
+ module CASinoCore
2
+ module Helper
3
+ module Authentication
4
+
5
+ def validate_login_credentials(username, password)
6
+ authentication_result = nil
7
+ CASinoCore::Settings.authenticators.each do |authenticator_name, authenticator|
8
+ data = authenticator.validate(username, password)
9
+ if data
10
+ authentication_result = { authenticator: authenticator_name, user_data: data }
11
+ logger.info("Credentials for username '#{data[:username]}' successfully validated using authenticator '#{authenticator_name}' (#{authenticator.class})")
12
+ break
13
+ end
14
+ end
15
+ authentication_result
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -3,6 +3,7 @@ require 'addressable/uri'
3
3
  module CASinoCore
4
4
  module Helper
5
5
  module TicketGrantingTickets
6
+
6
7
  include CASinoCore::Helper::Browser
7
8
  include CASinoCore::Helper::Logger
8
9
 
@@ -19,6 +20,25 @@ module CASinoCore
19
20
  end
20
21
  end
21
22
  end
23
+
24
+ def acquire_ticket_granting_ticket(authentication_result, user_agent = nil)
25
+ user_data = authentication_result[:user_data]
26
+ CASinoCore::Model::TicketGrantingTicket.create!({
27
+ ticket: random_ticket_string('TGC'),
28
+ authenticator: authentication_result[:authenticator],
29
+ username: user_data[:username],
30
+ extra_attributes: user_data[:extra_attributes],
31
+ user_agent: user_agent
32
+ })
33
+ end
34
+
35
+ def remove_ticket_granting_ticket(ticket_granting_ticket, user_agent = nil)
36
+ tgt = find_valid_ticket_granting_ticket(ticket_granting_ticket, user_agent)
37
+ unless tgt.nil?
38
+ tgt.destroy
39
+ end
40
+ end
41
+
22
42
  end
23
43
  end
24
44
  end
@@ -3,6 +3,7 @@ require 'useragent'
3
3
 
4
4
  module CASinoCore
5
5
  module Helper
6
+ autoload :Authentication, 'casino_core/helper/authentication.rb'
6
7
  autoload :Browser, 'casino_core/helper/browser.rb'
7
8
  autoload :Logger, 'casino_core/helper/logger.rb'
8
9
  autoload :LoginTickets, 'casino_core/helper/login_tickets.rb'
@@ -0,0 +1,49 @@
1
+ require 'casino_core/processor/api'
2
+ require 'casino_core/helper'
3
+
4
+ # This processor should be used for API calls: POST /cas/v1/tickets
5
+ class CASinoCore::Processor::API::LoginCredentialAcceptor < CASinoCore::Processor
6
+ include CASinoCore::Helper::Logger
7
+ include CASinoCore::Helper::ServiceTickets
8
+ include CASinoCore::Helper::Authentication
9
+ include CASinoCore::Helper::TicketGrantingTickets
10
+
11
+ # Use this method to process the request. It expects the username in the parameter "username" and the password
12
+ # in "password".
13
+ #
14
+ # The method will call one of the following methods on the listener:
15
+ # * `#user_logged_in_via_api`: First and only argument is a String with the TGT-id
16
+ # * `#invalid_login_credentials_via_api`: No argument
17
+ #
18
+ # @param [Hash] login_data parameters supplied by user (username and password)
19
+ def process(login_data)
20
+ @login_data = login_data
21
+
22
+ validate_login_data
23
+
24
+ unless @authentication_result.nil?
25
+ generate_ticket_granting_ticket
26
+ callback_user_logged_in
27
+ else
28
+ callback_invalid_login_credentials
29
+ end
30
+ end
31
+
32
+ private
33
+ def validate_login_data
34
+ @authentication_result = validate_login_credentials(@login_data[:username], @login_data[:password])
35
+ end
36
+
37
+ def callback_user_logged_in
38
+ @listener.user_logged_in_via_api @ticket_granting_ticket.ticket
39
+ end
40
+
41
+ def generate_ticket_granting_ticket
42
+ @ticket_granting_ticket = acquire_ticket_granting_ticket(@authentication_result)
43
+ end
44
+
45
+ def callback_invalid_login_credentials
46
+ @listener.invalid_login_credentials_via_api
47
+ end
48
+
49
+ end
@@ -0,0 +1,22 @@
1
+ require 'casino_core/processor'
2
+ require 'casino_core/helper'
3
+ require 'casino_core/model'
4
+
5
+ # The Logout processor should be used to process API DELET requests to /cas/v1/tickets/TGT-fdsjfsdfjkalfewrihfdhfaie
6
+ class CASinoCore::Processor::API::Logout < CASinoCore::Processor
7
+ include CASinoCore::Helper::TicketGrantingTickets
8
+
9
+ # This method will call `#user_logged_out_via_api`
10
+ #
11
+ # @param [String] ticket_granting_ticket Ticket granting ticket to logout
12
+ def process(ticket_granting_ticket)
13
+
14
+ remove_ticket_granting_ticket(ticket_granting_ticket)
15
+ callback_user_logged_out
16
+ end
17
+
18
+ def callback_user_logged_out
19
+ @listener.user_logged_out_via_api
20
+ end
21
+
22
+ end
@@ -0,0 +1,61 @@
1
+ require 'casino_core/processor'
2
+ require 'casino_core/helper'
3
+ require 'casino_core/model'
4
+ require 'casino_core/builder'
5
+
6
+ # The ServiceTicketProvider processor should be used to handle API calls: POST requests to /cas/v1/tickets/<ticket_granting_ticket>
7
+ class CASinoCore::Processor::API::ServiceTicketProvider < CASinoCore::Processor
8
+ include CASinoCore::Helper::ServiceTickets
9
+ include CASinoCore::Helper::TicketGrantingTickets
10
+
11
+ # Use this method to process the request.
12
+ #
13
+ # The method will call one of the following methods on the listener:
14
+ # * `#granted_service_ticket_via_api`: First and only argument is a String with the ST-id
15
+ # * `#invalid_ticket_granting_ticket_via_api`: No argument
16
+ # * `#no_service_provided_via_api`: No argument
17
+ #
18
+ # @param [String] ticket_granting_ticket ticket_granting_ticket supplied by the user in the URL
19
+ # @param [Hash] parameters parameters supplied by user (ticket granting ticket and service url)
20
+ def process(ticket_granting_ticket, parameters)
21
+ @client_ticket_granting_ticket = ticket_granting_ticket
22
+ @service_url = parameters[:service]
23
+
24
+ fetch_valid_ticket_granting_ticket
25
+ handle_ticket_granting_ticket
26
+ end
27
+
28
+ private
29
+ def fetch_valid_ticket_granting_ticket
30
+ @ticket_granting_ticket = find_valid_ticket_granting_ticket(@client_ticket_granting_ticket, nil)
31
+ end
32
+
33
+ def handle_ticket_granting_ticket
34
+ case
35
+ when (@service_url and @ticket_granting_ticket)
36
+ create_service_ticket
37
+ callback_granted_service_ticket
38
+ when (@service_url and not @ticket_granting_ticket)
39
+ callback_invalid_tgt
40
+ when (not @service_url and @ticket_granting_ticket)
41
+ callback_empty_service
42
+ end
43
+ end
44
+
45
+ def create_service_ticket
46
+ @service_ticket = acquire_service_ticket(@ticket_granting_ticket, @service_url)
47
+ end
48
+
49
+ def callback_granted_service_ticket
50
+ @listener.granted_service_ticket_via_api @service_ticket.ticket
51
+ end
52
+
53
+ def callback_invalid_tgt
54
+ @listener.invalid_ticket_granting_ticket_via_api
55
+ end
56
+
57
+ def callback_empty_service
58
+ @listener.no_service_provided_via_api
59
+ end
60
+
61
+ end
@@ -0,0 +1,9 @@
1
+ module CASinoCore
2
+ class Processor
3
+ module API
4
+ autoload :LoginCredentialAcceptor, 'casino_core/processor/api/login_credential_acceptor.rb'
5
+ autoload :ServiceTicketProvider, 'casino_core/processor/api/service_ticket_provider.rb'
6
+ autoload :Logout, 'casino_core/processor/api/logout.rb'
7
+ end
8
+ end
9
+ end
@@ -6,6 +6,8 @@ class CASinoCore::Processor::LoginCredentialAcceptor < CASinoCore::Processor
6
6
  include CASinoCore::Helper::Logger
7
7
  include CASinoCore::Helper::LoginTickets
8
8
  include CASinoCore::Helper::ServiceTickets
9
+ include CASinoCore::Helper::Authentication
10
+ include CASinoCore::Helper::TicketGrantingTickets
9
11
 
10
12
  # Use this method to process the request. It expects the username in the parameter "username" and the password
11
13
  # in "password".
@@ -54,27 +56,4 @@ class CASinoCore::Processor::LoginCredentialAcceptor < CASinoCore::Processor
54
56
  end
55
57
  end
56
58
 
57
- def validate_login_credentials(username, password)
58
- authentication_result = nil
59
- CASinoCore::Settings.authenticators.each do |authenticator_name, authenticator|
60
- data = authenticator.validate(username, password)
61
- if data
62
- authentication_result = { authenticator: authenticator_name, user_data: data }
63
- logger.info("Credentials for username '#{data[:username]}' successfully validated using authenticator '#{authenticator_name}' (#{authenticator.class})")
64
- break
65
- end
66
- end
67
- authentication_result
68
- end
69
-
70
- def acquire_ticket_granting_ticket(authentication_result, user_agent = nil)
71
- user_data = authentication_result[:user_data]
72
- CASinoCore::Model::TicketGrantingTicket.create!({
73
- ticket: random_ticket_string('TGC'),
74
- authenticator: authentication_result[:authenticator],
75
- username: user_data[:username],
76
- extra_attributes: user_data[:extra_attributes],
77
- user_agent: user_agent
78
- })
79
- end
80
59
  end
@@ -17,10 +17,7 @@ class CASinoCore::Processor::Logout < CASinoCore::Processor
17
17
  def process(params = nil, cookies = nil, user_agent = nil)
18
18
  params ||= {}
19
19
  cookies ||= {}
20
- ticket_granting_ticket = find_valid_ticket_granting_ticket(cookies[:tgt], user_agent)
21
- unless ticket_granting_ticket.nil?
22
- ticket_granting_ticket.destroy
23
- end
20
+ remove_ticket_granting_ticket(cookies[:tgt], user_agent)
24
21
  @listener.user_logged_out(params[:url])
25
22
  end
26
23
  end
@@ -12,6 +12,8 @@ module CASinoCore
12
12
  autoload :SessionDestroyer, 'casino_core/processor/session_destroyer.rb'
13
13
  autoload :SessionOverview, 'casino_core/processor/session_overview.rb'
14
14
 
15
+ autoload :API, 'casino_core/processor/api.rb'
16
+
15
17
  def initialize(listener)
16
18
  @listener = listener
17
19
  end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe CASinoCore::Processor::API::LoginCredentialAcceptor do
4
+ describe '#process' do
5
+ let(:listener) { Object.new }
6
+ let(:processor) { described_class.new(listener) }
7
+
8
+ context 'with invalid credentials' do
9
+ let(:login_data) { {username: 'testuser', password: 'wrong'} }
10
+
11
+ it 'calls the #invalid_login_credentials method on the listener' do
12
+ listener.should_receive(:invalid_login_credentials_via_api)
13
+ processor.process(login_data).should be_false
14
+ end
15
+ end
16
+
17
+ context 'with valid credentials' do
18
+ let(:login_data) { {username: 'testuser', password: 'foobar123'} }
19
+
20
+ before(:each) do
21
+ listener.stub(:user_logged_in)
22
+ end
23
+
24
+ it 'calls the #user_logged_in method on the listener' do
25
+ listener.should_receive(:user_logged_in_via_api).with(/^TGC\-/)
26
+ processor.process(login_data)
27
+ end
28
+
29
+ it 'generates a ticket-granting ticket' do
30
+ listener.should_receive(:user_logged_in_via_api).with(/^TGC\-/)
31
+ expect {
32
+ processor.process(login_data)
33
+ }.to change(CASinoCore::Model::TicketGrantingTicket, :count).by(1)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe CASinoCore::Processor::API::Logout do
4
+ describe '#process' do
5
+ let(:listener) { Object.new }
6
+ let(:processor) { described_class.new(listener) }
7
+
8
+ context 'with an existing ticket-granting ticket' do
9
+ let(:ticket_granting_ticket) { FactoryGirl.create(:ticket_granting_ticket, user_agent: nil) }
10
+
11
+ it 'deletes the ticket-granting ticket' do
12
+ listener.should_receive(:user_logged_out_via_api)
13
+ processor.process(ticket_granting_ticket.ticket)
14
+ CASinoCore::Model::TicketGrantingTicket.where(id: ticket_granting_ticket.id).first.should == nil
15
+ end
16
+
17
+ it 'calls the #user_logged_out_via_api method on the listener' do
18
+ listener.should_receive(:user_logged_out_via_api)
19
+ processor.process(ticket_granting_ticket)
20
+ end
21
+
22
+ end
23
+
24
+ context 'with an invlaid ticket-granting ticket' do
25
+ let(:tgt) { 'TGT-lalala' }
26
+
27
+ it 'calls the #user_logged_out method on the listener' do
28
+ listener.should_receive(:user_logged_out_via_api)
29
+ processor.process(tgt)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe CASinoCore::Processor::API::ServiceTicketProvider do
4
+ describe '#process' do
5
+ let(:listener) { Object.new }
6
+ let(:processor) { described_class.new(listener) }
7
+
8
+ let(:parameters) { { service: 'http://example.org/' } }
9
+
10
+ context 'with an invalid ticket-granting ticket' do
11
+ let(:ticket_granting_ticket) { 'TGT-INVALID' }
12
+
13
+ it 'calls the #invalid_tgt_via_api method on the listener' do
14
+ listener.should_receive(:invalid_ticket_granting_ticket_via_api)
15
+ processor.process(ticket_granting_ticket, parameters).should be_false
16
+ end
17
+ end
18
+
19
+ context 'with a valid ticket-granting ticket' do
20
+ let(:ticket_granting_ticket) { FactoryGirl.create(:ticket_granting_ticket, user_agent: nil).ticket }
21
+
22
+ it 'calls the #granted_service_ticket_via_api method on the listener' do
23
+ listener.should_receive(:granted_service_ticket_via_api).with(/^ST\-/)
24
+ processor.process(ticket_granting_ticket, parameters)
25
+ end
26
+
27
+ it 'generates a ticket-granting ticket' do
28
+ listener.should_receive(:granted_service_ticket_via_api).with(/^ST\-/)
29
+ expect {
30
+ processor.process(ticket_granting_ticket, parameters)
31
+ }.to change(CASinoCore::Model::ServiceTicket, :count).by(1)
32
+ end
33
+
34
+ context 'without a service' do
35
+ let(:parameters) { { } }
36
+
37
+ it 'calls the #no_service_provided_via_api method on the listener' do
38
+ listener.should_receive(:no_service_provided_via_api)
39
+ processor.process(ticket_granting_ticket, parameters)
40
+ end
41
+ end
42
+
43
+ end
44
+ end
45
+ end
46
+
@@ -46,4 +46,4 @@ describe CASinoCore::Processor::Logout do
46
46
  end
47
47
  end
48
48
  end
49
- end
49
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: casino_core
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.4
5
+ version: 1.0.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Nils Caspar
@@ -215,6 +215,7 @@ files:
215
215
  - lib/casino_core/builder.rb
216
216
  - lib/casino_core/builder/ticket_validation_response.rb
217
217
  - lib/casino_core/helper.rb
218
+ - lib/casino_core/helper/authentication.rb
218
219
  - lib/casino_core/helper/browser.rb
219
220
  - lib/casino_core/helper/logger.rb
220
221
  - lib/casino_core/helper/login_tickets.rb
@@ -231,6 +232,10 @@ files:
231
232
  - lib/casino_core/model/service_ticket/single_sign_out_notifier.rb
232
233
  - lib/casino_core/model/ticket_granting_ticket.rb
233
234
  - lib/casino_core/processor.rb
235
+ - lib/casino_core/processor/api.rb
236
+ - lib/casino_core/processor/api/login_credential_acceptor.rb
237
+ - lib/casino_core/processor/api/logout.rb
238
+ - lib/casino_core/processor/api/service_ticket_provider.rb
234
239
  - lib/casino_core/processor/legacy_validator.rb
235
240
  - lib/casino_core/processor/login_credential_acceptor.rb
236
241
  - lib/casino_core/processor/login_credential_requestor.rb
@@ -252,6 +257,9 @@ files:
252
257
  - spec/model/service_ticket/single_sign_out_notifier_spec.rb
253
258
  - spec/model/service_ticket_spec.rb
254
259
  - spec/model/ticket_granting_ticket_spec.rb
260
+ - spec/processor/api/login_credential_acceptor_spec.rb
261
+ - spec/processor/api/logout_spec.rb
262
+ - spec/processor/api/service_ticket_provider_spec.rb
255
263
  - spec/processor/legacy_validator_spec.rb
256
264
  - spec/processor/login_credential_acceptor_spec.rb
257
265
  - spec/processor/login_credential_requestor_spec.rb
@@ -280,7 +288,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
280
288
  requirements:
281
289
  - - ">="
282
290
  - !ruby/object:Gem::Version
283
- hash: -1912000289096139821
291
+ hash: -2705901659147789141
284
292
  segments:
285
293
  - 0
286
294
  version: "0"