Sha256: 5e2c43f02e5966573d9aee5012d80177570535514929fd0494a1b82cb3601d63

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

require 'oauth/request_proxy/rack_request'

module LtiProvider
  class LtiController < LtiProvider::LtiApplicationController
    skip_before_filter :require_lti_launch

    def launch
      provider = IMS::LTI::ToolProvider.new(params['oauth_consumer_key'], LtiProvider::Config.secret, params)
      launch = Launch.initialize_from_request(provider, request)

      if !launch.valid_provider?
        msg = "#{launch.lti_errormsg} Please be sure you are launching this tool from the link provided in Canvas."
        return show_error msg
      elsif launch.save
        session[:cookie_test] = true
        redirect_to cookie_test_path(nonce: launch.nonce)
      else
        return show_error "Unable to launch #{LtiProvider::XmlConfig.tool_title}. Please check your External Tools configuration and try again."
      end
    end

    def cookie_test
      if session[:cookie_test]
        # success!!! we've got a session!
        consume_launch
      else
        render
      end
    end

    def consume_launch
      launch = Launch.where("created_at > ?", 5.minutes.ago).find_by_nonce(params[:nonce])

      if launch
        [:account_id, :course_name, :course_id, :canvas_url, :tool_consumer_instance_guid,
         :user_id, :user_name, :user_roles, :user_avatar_url].each do |attribute|
          session[attribute] = launch.public_send(attribute)
        end

        launch.destroy

        redirect_to main_app.root_path
      else
        return show_error "The tool was not launched successfully. Please try again."
      end
    end

    def configure
      respond_to do |format|
        format.xml do
          render xml: Launch.xml_config(lti_launch_url)
        end
      end
    end

    protected
      def show_error(message)
        render text: message
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lti_provider_engine-0.0.6 app/controllers/lti_provider/lti_controller.rb
lti_provider_engine-0.0.5 app/controllers/lti_provider/lti_controller.rb