casino 3.0.4 → 4.0.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. checksums.yaml +7 -0
  2. data/.travis.yml +12 -3
  3. data/app/api/casino/api.rb +7 -0
  4. data/app/api/casino/api/entity/auth_token_ticket.rb +5 -0
  5. data/app/api/casino/api/resource/auth_token_tickets.rb +12 -0
  6. data/app/assets/javascripts/casino/{application.js → application.js.erb} +1 -1
  7. data/app/authenticators/casino/static_authenticator.rb +8 -2
  8. data/app/builders/casino/proxy_response_builder.rb +24 -0
  9. data/app/builders/casino/ticket_validation_response_builder.rb +9 -5
  10. data/app/controllers/casino/application_controller.rb +0 -22
  11. data/app/controllers/casino/auth_tokens_controller.rb +34 -0
  12. data/app/controllers/casino/controller_concern/ticket_validator.rb +30 -0
  13. data/app/controllers/casino/proxy_tickets_controller.rb +42 -2
  14. data/app/controllers/casino/service_tickets_controller.rb +15 -2
  15. data/app/controllers/casino/sessions_controller.rb +59 -8
  16. data/app/controllers/casino/two_factor_authenticators_controller.rb +28 -3
  17. data/app/helpers/casino/sessions_helper.rb +75 -0
  18. data/app/helpers/casino/two_factor_authenticators_helper.rb +12 -0
  19. data/app/models/casino/auth_token_ticket.rb +15 -0
  20. data/app/models/casino/login_ticket.rb +7 -4
  21. data/app/models/casino/model_concern/consumable_ticket.rb +20 -0
  22. data/app/models/casino/model_concern/ticket.rb +28 -0
  23. data/app/models/casino/proxy_granting_ticket.rb +12 -0
  24. data/app/models/casino/proxy_ticket.rb +4 -0
  25. data/app/models/casino/service_ticket.rb +5 -4
  26. data/app/models/casino/ticket_granting_ticket.rb +5 -1
  27. data/app/models/casino/two_factor_authenticator.rb +2 -0
  28. data/app/processors/casino/authentication_processor.rb +73 -0
  29. data/app/processors/casino/browser_processor.rb +12 -0
  30. data/app/processors/casino/proxy_granting_ticket_processor.rb +37 -0
  31. data/app/processors/casino/service_ticket_processor.rb +81 -0
  32. data/app/processors/casino/ticket_granting_ticket_processor.rb +56 -0
  33. data/app/processors/casino/two_factor_authenticator_processor.rb +18 -0
  34. data/app/services/casino/auth_token_validation_service.rb +66 -0
  35. data/app/views/casino/sessions/index.html.erb +2 -2
  36. data/app/views/casino/sessions/new.html.erb +1 -1
  37. data/app/views/casino/sessions/validate_otp.html.erb +1 -1
  38. data/app/views/casino/two_factor_authenticators/new.html.erb +6 -3
  39. data/app/views/layouts/application.html.erb +0 -1
  40. data/casino.gemspec +4 -2
  41. data/config/locales/en.yml +35 -0
  42. data/config/locales/zh-CN.yml +88 -0
  43. data/config/locales/zh-TW.yml +88 -0
  44. data/config/routes.rb +3 -10
  45. data/db/migrate/20140831205255_create_auth_token_tickets.rb +10 -0
  46. data/lib/casino.rb +4 -1
  47. data/lib/casino/tasks/cleanup.rake +13 -1
  48. data/lib/casino/version.rb +1 -1
  49. data/spec/controllers/auth_tokens_controller_spec.rb +75 -0
  50. data/spec/controllers/proxy_tickets_controller_spec.rb +120 -14
  51. data/spec/controllers/service_and_proxy_tickets_controller_spec.rb +224 -0
  52. data/spec/controllers/service_tickets_controller_spec.rb +62 -16
  53. data/spec/controllers/sessions_controller_spec.rb +622 -36
  54. data/spec/controllers/two_factor_authenticators_controller_spec.rb +217 -18
  55. data/spec/dummy/config/cas.yml +3 -0
  56. data/spec/dummy/config/environments/development.rb +0 -4
  57. data/spec/dummy/db/migrate/{20130910094259_create_base_models.casino.rb → 20140831214845_create_core_schema.casino.rb} +55 -32
  58. data/spec/dummy/db/migrate/20140831214846_rename_base_models.casino.rb +102 -0
  59. data/spec/dummy/db/migrate/20140831214847_cleanup_indexes.casino.rb +28 -0
  60. data/spec/dummy/db/migrate/20140831214848_fix_long_index_names.casino.rb +13 -0
  61. data/spec/dummy/db/migrate/20140831214849_change_service_to_text.casino.rb +7 -0
  62. data/spec/dummy/db/migrate/20140831214850_change_user_agent_to_text.casino.rb +6 -0
  63. data/spec/dummy/db/migrate/20140831214851_fix_length_of_text_fields.casino.rb +8 -0
  64. data/spec/dummy/db/migrate/20140831214852_create_auth_token_tickets.casino.rb +11 -0
  65. data/spec/dummy/db/schema.rb +79 -70
  66. data/spec/features/login_spec.rb +0 -9
  67. data/spec/model/auth_token_ticket_spec.rb +23 -0
  68. data/spec/services/auth_token_validation_service_spec.rb +83 -0
  69. data/spec/support/sign_in.rb +4 -0
  70. metadata +139 -210
  71. data/app/controllers/casino/api/v1/tickets_controller.rb +0 -55
  72. data/app/helpers/service_tickets_helper.rb +0 -2
  73. data/app/listeners/casino/legacy_validator_listener.rb +0 -11
  74. data/app/listeners/casino/listener.rb +0 -16
  75. data/app/listeners/casino/login_credential_acceptor_listener.rb +0 -38
  76. data/app/listeners/casino/login_credential_requestor_listener.rb +0 -21
  77. data/app/listeners/casino/logout_listener.rb +0 -12
  78. data/app/listeners/casino/other_sessions_destroyer_listener.rb +0 -7
  79. data/app/listeners/casino/proxy_ticket_provider_listener.rb +0 -11
  80. data/app/listeners/casino/second_factor_authentication_acceptor_listener.rb +0 -26
  81. data/app/listeners/casino/session_destroyer_listener.rb +0 -11
  82. data/app/listeners/casino/session_overview_listener.rb +0 -11
  83. data/app/listeners/casino/ticket_validator_listener.rb +0 -11
  84. data/app/listeners/casino/two_factor_authenticator_activator_listener.rb +0 -23
  85. data/app/listeners/casino/two_factor_authenticator_destroyer_listener.rb +0 -16
  86. data/app/listeners/casino/two_factor_authenticator_overview_listener.rb +0 -11
  87. data/app/listeners/casino/two_factor_authenticator_registrator_listener.rb +0 -11
  88. data/app/processors/casino/api/login_credential_acceptor_processor.rb +0 -46
  89. data/app/processors/casino/api/logout_processor.rb +0 -17
  90. data/app/processors/casino/api/service_ticket_provider_processor.rb +0 -69
  91. data/app/processors/casino/legacy_validator_processor.rb +0 -19
  92. data/app/processors/casino/login_credential_acceptor_processor.rb +0 -63
  93. data/app/processors/casino/login_credential_requestor_processor.rb +0 -70
  94. data/app/processors/casino/logout_processor.rb +0 -23
  95. data/app/processors/casino/other_sessions_destroyer_processor.rb +0 -26
  96. data/app/processors/casino/processor.rb +0 -5
  97. data/app/processors/casino/processor_concern/authentication.rb +0 -87
  98. data/app/processors/casino/processor_concern/browser.rb +0 -14
  99. data/app/processors/casino/processor_concern/login_tickets.rb +0 -28
  100. data/app/processors/casino/processor_concern/proxy_granting_tickets.rb +0 -43
  101. data/app/processors/casino/processor_concern/proxy_tickets.rb +0 -56
  102. data/app/processors/casino/processor_concern/service_tickets.rb +0 -50
  103. data/app/processors/casino/processor_concern/ticket_granting_tickets.rb +0 -65
  104. data/app/processors/casino/processor_concern/tickets.rb +0 -17
  105. data/app/processors/casino/processor_concern/two_factor_authenticators.rb +0 -23
  106. data/app/processors/casino/proxy_ticket_provider_processor.rb +0 -41
  107. data/app/processors/casino/proxy_ticket_validator_processor.rb +0 -22
  108. data/app/processors/casino/second_factor_authentication_acceptor_processor.rb +0 -45
  109. data/app/processors/casino/service_ticket_validator_processor.rb +0 -46
  110. data/app/processors/casino/session_destroyer_processor.rb +0 -25
  111. data/app/processors/casino/session_overview_processor.rb +0 -21
  112. data/app/processors/casino/two_factor_authenticator_activator_processor.rb +0 -41
  113. data/app/processors/casino/two_factor_authenticator_destroyer_processor.rb +0 -33
  114. data/app/processors/casino/two_factor_authenticator_overview_processor.rb +0 -20
  115. data/app/processors/casino/two_factor_authenticator_registrator_processor.rb +0 -24
  116. data/spec/controllers/api/v1/tickets_controller_spec.rb +0 -114
  117. data/spec/controllers/listener/legacy_validator_spec.rb +0 -22
  118. data/spec/controllers/listener/login_credential_acceptor_spec.rb +0 -108
  119. data/spec/controllers/listener/login_credential_requestor_spec.rb +0 -57
  120. data/spec/controllers/listener/logout_spec.rb +0 -38
  121. data/spec/controllers/listener/other_sessions_destroyer_spec.rb +0 -19
  122. data/spec/controllers/listener/proxy_ticket_provider_spec.rb +0 -22
  123. data/spec/controllers/listener/second_factor_authentication_acceptor_spec.rb +0 -74
  124. data/spec/controllers/listener/session_destroyer_spec.rb +0 -25
  125. data/spec/controllers/listener/session_overview_spec.rb +0 -26
  126. data/spec/controllers/listener/ticket_validator_spec.rb +0 -22
  127. data/spec/controllers/listener/two_factor_authenticator_activator_spec.rb +0 -64
  128. data/spec/controllers/listener/two_factor_authenticator_destroyer_spec.rb +0 -40
  129. data/spec/controllers/listener/two_factor_authenticator_overview_spec.rb +0 -16
  130. data/spec/controllers/listener/two_factor_authenticator_registrator_spec.rb +0 -27
  131. data/spec/processor/api/login_credential_acceptor_spec.rb +0 -52
  132. data/spec/processor/api/logout_spec.rb +0 -34
  133. data/spec/processor/api/service_ticket_provider_spec.rb +0 -61
  134. data/spec/processor/legacy_validator_spec.rb +0 -78
  135. data/spec/processor/login_credential_acceptor_spec.rb +0 -164
  136. data/spec/processor/login_credential_requestor_spec.rb +0 -145
  137. data/spec/processor/logout_other_sessions_spec.rb +0 -53
  138. data/spec/processor/logout_spec.rb +0 -72
  139. data/spec/processor/processor_concern/service_tickets_spec.rb +0 -49
  140. data/spec/processor/proxy_ticket_provider_spec.rb +0 -66
  141. data/spec/processor/proxy_ticket_validator_spec.rb +0 -65
  142. data/spec/processor/second_factor_authenticaton_acceptor_spec.rb +0 -94
  143. data/spec/processor/session_destroyer_spec.rb +0 -75
  144. data/spec/processor/session_overview_spec.rb +0 -49
  145. data/spec/processor/ticket_validator_spec.rb +0 -214
  146. data/spec/processor/two_factor_authenticator_activator_spec.rb +0 -122
  147. data/spec/processor/two_factor_authenticator_destroyer_spec.rb +0 -71
  148. data/spec/processor/two_factor_authenticator_overview_spec.rb +0 -56
  149. data/spec/processor/two_factor_authenticator_registrator_spec.rb +0 -48
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 71cfd75e5672f90469a0c6d4fdd0f2d3a8aa8c33
4
+ data.tar.gz: f21e697ce6f06d1b2212e503e34dd096f6128f3c
5
+ SHA512:
6
+ metadata.gz: a9208fbd8c620d512e0ee8ef82ec7bdec667003ba6f7eaff1833fe69b1fc85f16a353201636d385b1eb98140996d0e4f337a9d89ab8219fc88c3510b7b8d5b32
7
+ data.tar.gz: e24bc5645aadd311e410fd6da88996672faa77014ef4981ed5c03d80a12d9a6fcaeec5af700edd68511378c2647a5c42ccc4617912ca7f7856ef6d0dd12ecf61
@@ -1,5 +1,14 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
- - 2.0.0
5
- - 2.1.0
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
6
+ notifications:
7
+ hipchat:
8
+ rooms:
9
+ secure: kXPfZwOtdwJM0NIOj2td/NoPOhzxWVlUfHQuke2N4fuoKDQ+nhz5ZV4btW5J+O5C5aC6qyBBFdm+FzA/8m1WiLMGX0DIE1X67zZts/udMwtIDRNoHV594hd2co4oA72QMUT5kdre7IvTpSnnJwkp/d3V0kB7DOHuEbDJsjipx8I=
10
+ template:
11
+ - '%{repository} <a href="%{build_url}">#%{build_number}</a> (%{branch} - <a href="%{compare_url}">%{commit}</a> : %{author}): %{message}'
12
+ format: html
13
+ on_failure: always
14
+ on_success: change
@@ -0,0 +1,7 @@
1
+ require 'grape'
2
+
3
+ class CASino::API < Grape::API
4
+ format :json
5
+
6
+ mount CASino::API::Resource::AuthTokenTickets
7
+ end
@@ -0,0 +1,5 @@
1
+ require 'grape-entity'
2
+
3
+ class CASino::API::Entity::AuthTokenTicket < Grape::Entity
4
+ expose :ticket
5
+ end
@@ -0,0 +1,12 @@
1
+ require 'grape'
2
+
3
+ class CASino::API::Resource::AuthTokenTickets < Grape::API
4
+ resource :auth_token_tickets do
5
+ desc 'Create an auth token ticket'
6
+ post do
7
+ @ticket = CASino::AuthTokenTicket.create
8
+ Rails.logger.debug "Created auth token ticket '#{@ticket.ticket}'"
9
+ present @ticket, with: CASino::API::Entity::AuthTokenTicket
10
+ end
11
+ end
12
+ end
@@ -1,7 +1,7 @@
1
1
  // Place all the behaviors and hooks related to the matching controller here.
2
2
  (function(win) {
3
3
  if(!win.CASino) {
4
- win.CASino = { baseUrl: '/' };
4
+ win.CASino = { baseUrl: '<%= CASino::Engine.routes.url_helpers.root_path %>' };
5
5
  }
6
6
 
7
7
  win.CASino.url = function(path) {
@@ -12,12 +12,18 @@ class CASino::StaticAuthenticator < CASino::Authenticator
12
12
  def validate(username, password)
13
13
  username = :"#{username}"
14
14
  if @users.include?(username) && @users[username][:password] == password
15
+ load_user_data(username)
16
+ else
17
+ false
18
+ end
19
+ end
20
+
21
+ def load_user_data(username)
22
+ if @users.include?(username)
15
23
  {
16
24
  username: "#{username}",
17
25
  extra_attributes: @users[username].except(:password)
18
26
  }
19
- else
20
- false
21
27
  end
22
28
  end
23
29
  end
@@ -0,0 +1,24 @@
1
+ require 'builder'
2
+
3
+ class CASino::ProxyResponseBuilder
4
+ attr_reader :success, :options
5
+
6
+ def initialize(success, options)
7
+ @success = success
8
+ @options = options
9
+ end
10
+
11
+ def build
12
+ xml = Builder::XmlMarkup.new(indent: 2)
13
+ xml.cas :serviceResponse, 'xmlns:cas' => 'http://www.yale.edu/tp/cas' do |service_response|
14
+ if success
15
+ service_response.cas :proxySuccess do |proxy_success|
16
+ proxy_success.cas :proxyTicket, options[:proxy_ticket].ticket
17
+ end
18
+ else
19
+ service_response.cas :proxyFailure, options[:error_message], code: options[:error_code]
20
+ end
21
+ end
22
+ xml.target!
23
+ end
24
+ end
@@ -1,6 +1,8 @@
1
1
  require 'builder'
2
2
 
3
3
  class CASino::TicketValidationResponseBuilder
4
+ attr_reader :success, :options
5
+
4
6
  def initialize(success, options)
5
7
  @success = success
6
8
  @options = options
@@ -9,8 +11,8 @@ class CASino::TicketValidationResponseBuilder
9
11
  def build
10
12
  xml = Builder::XmlMarkup.new(indent: 2)
11
13
  xml.cas :serviceResponse, 'xmlns:cas' => 'http://www.yale.edu/tp/cas' do |service_response|
12
- if @success
13
- ticket = @options[:ticket]
14
+ if success
15
+ ticket = options[:ticket]
14
16
  if ticket.is_a?(CASino::ProxyTicket)
15
17
  proxies = []
16
18
  service_ticket = ticket
@@ -38,6 +40,8 @@ class CASino::TicketValidationResponseBuilder
38
40
  key = :"#{key}"
39
41
  if value.kind_of?(String) || value.kind_of?(Numeric) || value.kind_of?(Symbol)
40
42
  builder.cas key, "#{value}"
43
+ elsif value.kind_of?(Array)
44
+ value.each { |v| serialize_extra_attribute(builder, key, v) }
41
45
  else
42
46
  builder.cas key do |container|
43
47
  container.cdata! value.to_yaml
@@ -65,8 +69,8 @@ class CASino::TicketValidationResponseBuilder
65
69
  end
66
70
  end
67
71
  end
68
- if @options[:proxy_granting_ticket]
69
- proxy_granting_ticket = @options[:proxy_granting_ticket]
72
+ if options[:proxy_granting_ticket]
73
+ proxy_granting_ticket = options[:proxy_granting_ticket]
70
74
  authentication_success.cas :proxyGrantingTicket, proxy_granting_ticket.iou
71
75
  end
72
76
  if ticket.is_a?(CASino::ProxyTicket)
@@ -80,6 +84,6 @@ class CASino::TicketValidationResponseBuilder
80
84
  end
81
85
 
82
86
  def build_failure_xml(service_response)
83
- service_response.cas :authenticationFailure, @options[:error_message], code: @options[:error_code]
87
+ service_response.cas :authenticationFailure, options[:error_message], code: options[:error_code]
84
88
  end
85
89
  end
@@ -1,11 +1,9 @@
1
1
  require 'casino'
2
- require 'http_accept_language'
3
2
 
4
3
  class CASino::ApplicationController < ::ApplicationController
5
4
  include ApplicationHelper
6
5
 
7
6
  layout 'application'
8
- before_filter :set_locale
9
7
 
10
8
  unless Rails.env.development?
11
9
  rescue_from ActionView::MissingTemplate, with: :missing_template
@@ -16,26 +14,6 @@ class CASino::ApplicationController < ::ApplicationController
16
14
  end
17
15
 
18
16
  protected
19
- def processor(processor_name, listener_name = nil)
20
- listener_name ||= processor_name
21
- listener = CASino.const_get(:"#{listener_name}Listener").new(self)
22
- @processor = CASino.const_get(:"#{processor_name}Processor").new(listener)
23
- end
24
-
25
- def set_locale
26
- I18n.locale = extract_locale_from_accept_language_header || I18n.default_locale
27
- end
28
-
29
- def extract_locale_from_accept_language_header
30
- if request.env['HTTP_ACCEPT_LANGUAGE']
31
- http_accept_language.preferred_language_from(I18n.available_locales)
32
- end
33
- end
34
-
35
- def http_accept_language
36
- HttpAcceptLanguage::Parser.new request.env['HTTP_ACCEPT_LANGUAGE']
37
- end
38
-
39
17
  def missing_template(exception)
40
18
  render plain: 'Format not supported', status: :not_acceptable
41
19
  end
@@ -0,0 +1,34 @@
1
+ class CASino::AuthTokensController < CASino::ApplicationController
2
+ include CASino::SessionsHelper
3
+
4
+ def login
5
+ validation_result = validation_service.validation_result
6
+ return redirect_to_login unless validation_result
7
+ sign_in(validation_result)
8
+ end
9
+
10
+ private
11
+ def validation_service
12
+ @validation_service ||= CASino::AuthTokenValidationService.new(auth_token, auth_token_signature)
13
+ end
14
+
15
+ def redirect_to_login
16
+ redirect_to login_path(service: params[:service])
17
+ end
18
+
19
+ def auth_token_signature
20
+ @auth_token_signature ||= base64_decode(params[:ats])
21
+ end
22
+
23
+ def auth_token
24
+ @auth_token ||= base64_decode(params[:at])
25
+ end
26
+
27
+ def base64_decode(data)
28
+ begin
29
+ Base64.strict_decode64(data)
30
+ rescue
31
+ ''
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,30 @@
1
+ module CASino::ControllerConcern::TicketValidator
2
+ extend ActiveSupport::Concern
3
+ include CASino::ServiceTicketProcessor
4
+ include CASino::ProxyGrantingTicketProcessor
5
+
6
+ def validate_ticket(ticket)
7
+ validation_result = validate_ticket_for_service(ticket, params[:service], renew: params[:renew])
8
+ if validation_result.success?
9
+ options = { ticket: ticket }
10
+ options[:proxy_granting_ticket] = acquire_proxy_granting_ticket(params[:pgtUrl], ticket) unless params[:pgtUrl].nil?
11
+ build_ticket_validation_response(true, options)
12
+ else
13
+ build_ticket_validation_response(false,
14
+ error_code: validation_result.error_code,
15
+ error_message: validation_result.error_message)
16
+ end
17
+ end
18
+
19
+ def build_ticket_validation_response(success, options = {})
20
+ render xml: CASino::TicketValidationResponseBuilder.new(success, options).build
21
+ end
22
+
23
+ def ensure_service_ticket_parameters_present
24
+ if params[:ticket].nil? || params[:service].nil?
25
+ build_ticket_validation_response(false,
26
+ error_code: 'INVALID_REQUEST',
27
+ error_message: '"ticket" and "service" parameters are both required')
28
+ end
29
+ end
30
+ end
@@ -1,9 +1,49 @@
1
1
  class CASino::ProxyTicketsController < CASino::ApplicationController
2
+ include CASino::ControllerConcern::TicketValidator
3
+
4
+ before_action :load_ticket, only: [:proxy_validate]
5
+ before_action :ensure_service_ticket_parameters_present, only: [:proxy_validate]
6
+
7
+ before_action :load_proxy_granting_ticket, only: [:create]
8
+ before_action :ensure_proxy_parameters_present, only: [:create]
9
+
2
10
  def proxy_validate
3
- processor(:ProxyTicketValidator, :TicketValidator).process(params)
11
+ validate_ticket(@ticket)
4
12
  end
5
13
 
6
14
  def create
7
- processor(:ProxyTicketProvider).process(params)
15
+ proxy_ticket = @proxy_granting_ticket.proxy_tickets.create!(service: params[:targetService])
16
+ build_proxy_response(true, proxy_ticket: proxy_ticket)
17
+ end
18
+
19
+ private
20
+ def load_ticket
21
+ @ticket = case params[:ticket]
22
+ when /\APT-/
23
+ CASino::ProxyTicket.where(ticket: params[:ticket]).first
24
+ when /\AST-/
25
+ CASino::ServiceTicket.where(ticket: params[:ticket]).first
26
+ end
27
+ end
28
+
29
+ def build_proxy_response(success, options = {})
30
+ render xml: CASino::ProxyResponseBuilder.new(success, options).build
31
+ end
32
+
33
+ def ensure_proxy_parameters_present
34
+ if params[:pgt].nil? || params[:targetService].nil?
35
+ build_proxy_response(false,
36
+ error_code: 'INVALID_REQUEST',
37
+ error_message: '"pgt" and "targetService" parameters are both required')
38
+ end
39
+ end
40
+
41
+ def load_proxy_granting_ticket
42
+ @proxy_granting_ticket = CASino::ProxyGrantingTicket.where(ticket: params[:pgt]).first if params[:pgt].present?
43
+ if @proxy_granting_ticket.nil?
44
+ build_proxy_response(false,
45
+ error_code: 'BAD_PGT',
46
+ error_message: 'PGT not found')
47
+ end
8
48
  end
9
49
  end
@@ -1,9 +1,22 @@
1
1
  class CASino::ServiceTicketsController < CASino::ApplicationController
2
+ include CASino::ControllerConcern::TicketValidator
3
+
4
+ before_action :load_service_ticket
5
+ before_action :ensure_service_ticket_parameters_present, only: [:service_validate]
6
+
2
7
  def validate
3
- processor(:LegacyValidator).process(params)
8
+ if ticket_valid_for_service?(@service_ticket, params[:service], renew: params[:renew])
9
+ @username = @service_ticket.ticket_granting_ticket.user.username
10
+ end
11
+ render :validate, formats: [:text]
4
12
  end
5
13
 
6
14
  def service_validate
7
- processor(:ServiceTicketValidator, :TicketValidator).process(params)
15
+ validate_ticket(@service_ticket)
16
+ end
17
+
18
+ private
19
+ def load_service_ticket
20
+ @service_ticket = CASino::ServiceTicket.where(ticket: params[:ticket]).first if params[:service].present?
8
21
  end
9
22
  end
@@ -1,32 +1,83 @@
1
1
  class CASino::SessionsController < CASino::ApplicationController
2
2
  include CASino::SessionsHelper
3
+ include CASino::AuthenticationProcessor
4
+ include CASino::TwoFactorAuthenticatorProcessor
5
+
6
+ before_action :validate_login_ticket, only: [:create]
7
+ before_action :ensure_service_allowed, only: [:new, :create]
8
+ before_action :load_ticket_granting_ticket_from_parameter, only: [:validate_otp]
9
+ before_action :ensure_signed_in, only: [:index, :destroy]
3
10
 
4
11
  def index
5
- processor(:TwoFactorAuthenticatorOverview).process(cookies, request.user_agent)
6
- processor(:SessionOverview).process(cookies, request.user_agent)
12
+ @ticket_granting_tickets = current_user.ticket_granting_tickets.active
13
+ @two_factor_authenticators = current_user.two_factor_authenticators.active
7
14
  end
8
15
 
9
16
  def new
10
- processor(:LoginCredentialRequestor).process(params, cookies, request.user_agent)
17
+ tgt = current_ticket_granting_ticket
18
+ handle_signed_in(tgt) unless params[:renew] || tgt.nil?
19
+ redirect_to(params[:service]) if params[:gateway] && params[:service].present?
11
20
  end
12
21
 
13
22
  def create
14
- processor(:LoginCredentialAcceptor).process(params, request.user_agent)
23
+ validation_result = validate_login_credentials(params[:username], params[:password])
24
+ if !validation_result
25
+ show_login_error I18n.t('login_credential_acceptor.invalid_login_credentials')
26
+ else
27
+ sign_in(validation_result, long_term: params[:rememberMe], credentials_supplied: true)
28
+ end
15
29
  end
16
30
 
17
31
  def destroy
18
- processor(:SessionDestroyer).process(params, cookies, request.user_agent)
32
+ tickets = current_user.ticket_granting_tickets.where(id: params[:id])
33
+ tickets.first.destroy if tickets.any?
34
+ redirect_to sessions_path
19
35
  end
20
36
 
21
37
  def destroy_others
22
- processor(:OtherSessionsDestroyer).process(params, cookies, request.user_agent)
38
+ current_user
39
+ .ticket_granting_tickets
40
+ .where('id != ?', current_ticket_granting_ticket.id)
41
+ .destroy_all if signed_in?
42
+ redirect_to params[:service] || sessions_path
23
43
  end
24
44
 
25
45
  def logout
26
- processor(:Logout).process(params, cookies, request.user_agent)
46
+ sign_out
47
+ @url = params[:url]
48
+ if params[:service].present? && service_allowed?(params[:service])
49
+ redirect_to params[:service], status: :see_other
50
+ end
27
51
  end
28
52
 
29
53
  def validate_otp
30
- processor(:SecondFactorAuthenticationAcceptor).process(params, request.user_agent)
54
+ validation_result = validate_one_time_password(params[:otp], @ticket_granting_ticket.user.active_two_factor_authenticator)
55
+ return flash.now[:error] = I18n.t('validate_otp.invalid_otp') unless validation_result.success?
56
+ @ticket_granting_ticket.update_attribute(:awaiting_two_factor_authentication, false)
57
+ set_tgt_cookie(@ticket_granting_ticket)
58
+ handle_signed_in(@ticket_granting_ticket)
59
+ end
60
+
61
+ private
62
+ def show_login_error(message)
63
+ flash.now[:error] = message
64
+ render :new, status: :forbidden
65
+ end
66
+
67
+ def validate_login_ticket
68
+ unless CASino::LoginTicket.consume(params[:lt])
69
+ show_login_error I18n.t('login_credential_acceptor.invalid_login_ticket')
70
+ end
71
+ end
72
+
73
+ def ensure_service_allowed
74
+ if params[:service].present? && !service_allowed?(params[:service])
75
+ render 'service_not_allowed', status: :forbidden
76
+ end
77
+ end
78
+
79
+ def load_ticket_granting_ticket_from_parameter
80
+ @ticket_granting_ticket = find_valid_ticket_granting_ticket(params[:tgt], request.user_agent, ignore_two_factor: true)
81
+ redirect_to login_path if @ticket_granting_ticket.nil?
31
82
  end
32
83
  end
@@ -1,15 +1,40 @@
1
+ require 'rotp'
2
+
1
3
  class CASino::TwoFactorAuthenticatorsController < CASino::ApplicationController
2
4
  include CASino::SessionsHelper
5
+ include CASino::TwoFactorAuthenticatorsHelper
6
+ include CASino::TwoFactorAuthenticatorProcessor
7
+
8
+ before_action :ensure_signed_in
3
9
 
4
10
  def new
5
- processor(:TwoFactorAuthenticatorRegistrator).process(cookies, request.user_agent)
11
+ @two_factor_authenticator = current_user.two_factor_authenticators.create! secret: ROTP::Base32.random_base32
6
12
  end
7
13
 
8
14
  def create
9
- processor(:TwoFactorAuthenticatorActivator).process(params, cookies, request.user_agent)
15
+ @two_factor_authenticator = current_user.two_factor_authenticators.where(id: params[:id]).first
16
+ validation_result = validate_one_time_password(params[:otp], @two_factor_authenticator)
17
+ case
18
+ when validation_result.success?
19
+ current_user.two_factor_authenticators.where(active: true).delete_all
20
+ @two_factor_authenticator.update_attribute(:active, true)
21
+ flash[:notice] = I18n.t('two_factor_authenticators.successfully_activated')
22
+ redirect_to sessions_path
23
+ when validation_result.error_code == 'INVALID_OTP'
24
+ flash.now[:error] = I18n.t('two_factor_authenticators.invalid_one_time_password')
25
+ render :new
26
+ else
27
+ flash[:error] = I18n.t('two_factor_authenticators.invalid_two_factor_authenticator')
28
+ redirect_to new_two_factor_authenticator_path
29
+ end
10
30
  end
11
31
 
12
32
  def destroy
13
- processor(:TwoFactorAuthenticatorDestroyer).process(params, cookies, request.user_agent)
33
+ authenticators = current_user.two_factor_authenticators.where(id: params[:id])
34
+ if authenticators.any?
35
+ authenticators.first.destroy
36
+ flash[:notice] = I18n.t('two_factor_authenticators.successfully_deleted')
37
+ end
38
+ redirect_to sessions_path
14
39
  end
15
40
  end