Commit ed08bc4

mo <mo@mokhan.ca>
2017-11-24 19:45:07
reduce all actions down to the registrations controller.
1 parent 7aafda9
airport/app/controllers/application_controller.rb
@@ -1,18 +1,13 @@
 class ApplicationController < ActionController::Base
   protect_from_forgery with: :exception
-  helper_method :current_user
-  before_action :authenticate!
+  helper_method :current_user, :current_user?
 
-  def current_user
-    return nil unless session[:user].present?
-    @current_user ||= User.new(session[:user].with_indifferent_access)
+  def current_user(issuer = params[:entity_id])
+    return nil unless session[issuer].present?
+    User.new(session[issuer].with_indifferent_access)
   end
 
-  def current_user?
-    current_user.present?
-  end
-
-  def authenticate!
-    redirect_to new_session_path unless current_user?
+  def current_user?(issuer)
+    current_user(issuer).present?
   end
 end
airport/app/controllers/assertions_controller.rb
@@ -1,6 +1,5 @@
 class AssertionsController < ApplicationController
   skip_before_action :verify_authenticity_token, only: [:create, :destroy]
-  skip_before_action :authenticate!, only: [:create, :destroy]
 
   def create
     saml_binding = sp.assertion_consumer_service_for(binding: :http_post)
@@ -8,8 +7,8 @@ class AssertionsController < ApplicationController
     logger.debug(@saml_response.to_xml(pretty: true))
     return render :error, status: :forbidden if @saml_response.invalid?
 
-    session[:user] = { id: @saml_response.name_id }.merge(@saml_response.attributes)
-    redirect_to dashboard_path
+    session[@saml_response.issuer] = { id: @saml_response.name_id }.merge(@saml_response.attributes)
+    redirect_to registrations_path
   end
 
   def destroy
@@ -19,8 +18,8 @@ class AssertionsController < ApplicationController
       saml_binding = sp.single_logout_service_for(binding: :http_post)
       saml_response = saml_binding.deserialize(params)
       raise ActiveRecordRecordInvalid.new(saml_response) if saml_response.invalid?
-      reset_session
-      redirect_to new_session_path
+      session[saml_response.issuer] = nil
+      redirect_to registrations_path
     end
   end
 
airport/app/controllers/metadata_controller.rb
@@ -1,6 +1,6 @@
 class MetadataController < ApplicationController
   force_ssl if: :ssl_configured?
-  skip_before_action :authenticate!
+  #skip_before_action :authenticate!
 
   def show
     render xml: to_xml, content_type: "application/samlmetadata+xml"
airport/app/controllers/registrations_controller.rb
@@ -1,5 +1,5 @@
 class RegistrationsController < ApplicationController
-  skip_before_action :authenticate!
+  #skip_before_action :authenticate!
 
   def index
     @metadatum = Metadatum.all.limit(10)
airport/app/controllers/sessions_controller.rb
@@ -1,6 +1,4 @@
 class SessionsController < ApplicationController
-  skip_before_action :authenticate!, only: [:new, :create]
-
   def new
     @metadatum = Metadatum.all
   end
@@ -17,7 +15,8 @@ class SessionsController < ApplicationController
   end
 
   def destroy
-    saml_binding = idp.single_logout_service_for(binding: :http_post)
+    binding = :http_redirect == params[:binding].to_sym ? :http_redirect : :http_post
+    saml_binding = idp.single_logout_service_for(binding: binding)
     @url, @saml_params = saml_binding.serialize(builder_for(:logout))
     render layout: "spinner"
   end
@@ -32,7 +31,7 @@ class SessionsController < ApplicationController
     JSON.generate(redirect_to: '/')
   end
 
-  def builder_for(type)
+  def builder_for(type, entity_id: nil)
     case type
     when :login
       builder = Saml::Kit::AuthenticationRequest::Builder.new
airport/app/views/dashboard/show.html.erb
@@ -1,13 +1,6 @@
 <div class="container">
   <div class="row">
     <div class="col">
-      <h1>Welcome <%= current_user.email %></h1>
-      <ul>
-        <% current_user.attributes.each do |attribute| %>
-          <li> <%= attribute %> </li>
-        <% end %>
-      </ul>
-      <%= link_to "logout", session_path, method: :delete %>
     </div>
   </div>
 </div>
airport/app/views/registrations/index.html.erb
@@ -3,21 +3,52 @@
     <div class="col">
       <%= link_to "register", new_registration_path %>
       <table class="table">
-        <tbody>
-        <% @metadatum.each do |metadata| %>
+        <thead>
           <tr>
-            <td><%= link_to metadata.entity_id, registration_path(metadata) %></td>
-            <% metadata.to_saml.single_sign_on_services.each do |service| %>
+            <th>Entity Id</th>
+            <th>Login</th>
+            <th>Logout</th>
+          </tr>
+        </thead>
+        <tbody>
+          <% @metadatum.each do |metadata| %>
+            <tr>
               <td>
-                <%= form_with url: session_path, data: { remote: false } do |form| %>
-                  <%= form.hidden_field :entity_id, value: metadata.entity_id %>
-                  <%= form.hidden_field :binding, value: Saml::Kit::Bindings.to_symbol(service.binding) %>
-                  <%= form.submit service.binding %>
-                <% end %>
+                <%= link_to metadata.entity_id, registration_path(metadata) %>
+                <ul>
+                  <% current_user(metadata.entity_id).attributes.each do |attribute| %>
+                    <li> <%= attribute %> </li>
+                  <% end if current_user?(metadata.entity_id) %>
+                </ul>
               </td>
-            <% end %>
-          </tr>
-        <% end %>
+              <td>
+                <ul>
+                  <% metadata.to_saml.single_sign_on_services.each do |service| %>
+                    <li>
+                      <%= form_with url: session_path, data: { remote: false } do |form| %>
+                        <%= form.hidden_field :entity_id, value: metadata.entity_id %>
+                        <%= form.hidden_field :binding, value: Saml::Kit::Bindings.to_symbol(service.binding) %>
+                        <%= form.submit service.binding %>
+                      <% end %>
+                    </li>
+                  <% end %>
+                </ul>
+              </td>
+              <td>
+                <ul>
+                  <% metadata.to_saml.single_logout_services.each do |service| %>
+                    <li>
+                      <%= form_with url: session_path, method: :delete, data: { remote: false } do |form| %>
+                        <%= form.hidden_field :entity_id, value: metadata.entity_id %>
+                        <%= form.hidden_field :binding, value: Saml::Kit::Bindings.to_symbol(service.binding) %>
+                        <%= form.submit service.binding %>
+                      <% end %>
+                    </li>
+                  <% end %>
+                </ul>
+              </td>
+            </tr>
+          <% end %>
         </tbody>
       </table>
     </div>
airport/config/routes.rb
@@ -6,5 +6,5 @@ Rails.application.routes.draw do
   post "/assertions/logout" => "assertions#destroy", as: :logout
   resource :metadata, only: [:show]
   resources :registrations, only: [:index, :show, :new, :create]
-  root to: "sessions#new"
+  root to: "registrations#index"
 end