lti_provider_engine 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +145 -0
  3. data/Rakefile +28 -0
  4. data/app/controllers/lti_provider/application_controller.rb +5 -0
  5. data/app/controllers/lti_provider/lti_controller.rb +61 -0
  6. data/app/models/lti_provider/launch.rb +105 -0
  7. data/app/views/layouts/lti_provider/application.html.erb +12 -0
  8. data/app/views/lti_provider/lti/cookie_test.html.erb +4 -0
  9. data/config/lti.yml.example +13 -0
  10. data/config/lti_xml.yml.example +22 -0
  11. data/config/routes.rb +6 -0
  12. data/db/migrate/20130319050003_create_lti_provider_launches.rb +11 -0
  13. data/lib/lti_provider.rb +20 -0
  14. data/lib/lti_provider/config.rb +1 -0
  15. data/lib/lti_provider/engine.rb +19 -0
  16. data/lib/lti_provider/lti_application.rb +56 -0
  17. data/lib/lti_provider/lti_config.rb +28 -0
  18. data/lib/lti_provider/lti_xml_config.rb +23 -0
  19. data/lib/lti_provider/version.rb +3 -0
  20. data/lib/lti_provider/xml_config.rb +1 -0
  21. data/lib/tasks/lti_provider_tasks.rake +4 -0
  22. data/spec/controllers/lti_provider/lti_controller_spec.rb +141 -0
  23. data/spec/dummy/Rakefile +7 -0
  24. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  25. data/spec/dummy/app/controllers/welcome_controller.rb +5 -0
  26. data/spec/dummy/config.ru +4 -0
  27. data/spec/dummy/config/application.rb +58 -0
  28. data/spec/dummy/config/boot.rb +10 -0
  29. data/spec/dummy/config/cucumber.yml +8 -0
  30. data/spec/dummy/config/database.yml +25 -0
  31. data/spec/dummy/config/environment.rb +5 -0
  32. data/spec/dummy/config/environments/development.rb +37 -0
  33. data/spec/dummy/config/environments/production.rb +67 -0
  34. data/spec/dummy/config/environments/test.rb +37 -0
  35. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  36. data/spec/dummy/config/initializers/inflections.rb +15 -0
  37. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  38. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  39. data/spec/dummy/config/initializers/session_store.rb +8 -0
  40. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  41. data/spec/dummy/config/locales/en.yml +5 -0
  42. data/spec/dummy/config/lti.yml +13 -0
  43. data/spec/dummy/config/lti_xml.yml +24 -0
  44. data/spec/dummy/config/routes.rb +4 -0
  45. data/spec/dummy/db/development.sqlite3 +0 -0
  46. data/spec/dummy/db/migrate/20130319050206_create_lti_provider_launches.lti_provider.rb +12 -0
  47. data/spec/dummy/db/schema.rb +24 -0
  48. data/spec/dummy/db/test.sqlite3 +0 -0
  49. data/spec/dummy/log/development.log +266 -0
  50. data/spec/dummy/log/test.log +3643 -0
  51. data/spec/dummy/public/404.html +26 -0
  52. data/spec/dummy/public/422.html +26 -0
  53. data/spec/dummy/public/500.html +25 -0
  54. data/spec/dummy/public/favicon.ico +0 -0
  55. data/spec/dummy/public/robots.txt +5 -0
  56. data/spec/dummy/script/rails +6 -0
  57. data/spec/models/lti_provider/launch_spec.rb +80 -0
  58. data/spec/spec_helper.rb +55 -0
  59. metadata +337 -0
@@ -0,0 +1,20 @@
1
+ require 'ostruct'
2
+
3
+ require 'ims'
4
+
5
+ require "lti_provider/config"
6
+ require "lti_provider/lti_application"
7
+ require 'lti_provider/lti_config'
8
+ require 'lti_provider/lti_xml_config'
9
+ require "lti_provider/xml_config"
10
+
11
+ module LtiProvider
12
+ mattr_accessor :app_root
13
+
14
+ def self.setup
15
+ yield self
16
+ end
17
+ end
18
+
19
+ require "lti_provider/engine"
20
+
@@ -0,0 +1 @@
1
+ LtiProvider::Config = OpenStruct.new
@@ -0,0 +1,19 @@
1
+ module LtiProvider
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace LtiProvider
4
+
5
+ initializer "lti_provider.load_app_instance_data" do |app|
6
+ LtiProvider.setup do |config|
7
+ config.app_root = app.root
8
+ end
9
+ end
10
+
11
+ initializer "lti_provider.lti_config" do |app|
12
+ LtiProvider::LtiConfig.setup!
13
+ end
14
+
15
+ initializer "lti_provider.lti_xml_config" do |app|
16
+ LtiProvider::LtiXmlConfig.setup!
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,56 @@
1
+ module LtiProvider
2
+ module LtiApplication
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ end
7
+
8
+ included do
9
+ before_filter :require_lti_launch
10
+ end
11
+
12
+ protected
13
+ def require_lti_launch
14
+ if canvas_url.blank? || user_id.blank?
15
+ reset_session
16
+ prompt_for_launch
17
+ end
18
+ end
19
+
20
+ def prompt_for_launch
21
+ render text: 'Please launch this tool from Canvas and then try again.'
22
+ end
23
+
24
+ def canvas_url
25
+ session[:canvas_url]
26
+ end
27
+
28
+ def user_id
29
+ session[:user_id]
30
+ end
31
+
32
+ def current_course_id
33
+ session[:course_id]
34
+ end
35
+
36
+ def tool_consumer_instance_guid
37
+ session[:tool_consumer_instance_guid]
38
+ end
39
+
40
+ def course_launch?
41
+ current_course_id.present?
42
+ end
43
+
44
+ def current_account_id
45
+ session[:account_id]
46
+ end
47
+
48
+ def account_launch?
49
+ current_account_id.present?
50
+ end
51
+
52
+ def not_acceptable
53
+ render text: "Unable to process request", status: 406
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,28 @@
1
+ module LtiProvider
2
+ module LtiConfig
3
+ def self.load_config
4
+ YAML::load(File.open(config_file))[Rails.env]
5
+ end
6
+
7
+ def self.config_file
8
+ LtiProvider.app_root.join('config/lti.yml')
9
+ end
10
+
11
+ def self.setup!
12
+ config = LtiProvider::Config
13
+ if File.exists?(config_file)
14
+ Rails.logger.info "Initializing LTI key and secret using configuration in #{config_file}"
15
+ load_config.each do |k,v|
16
+ config.send("#{k}=", v)
17
+ end
18
+ elsif ENV['LTI_KEY'].present? && ENV['LTI_SECRET'].present?
19
+ Rails.logger.info "Initializing LTI key and secret using environment vars LTI_KEY and LTI_SECRET"
20
+ config.key = ENV['LTI_KEY']
21
+ config.secret = ENV['LTI_SECRET']
22
+ config.require_canvas = !!ENV['LTI_REQUIRE_CANVAS']
23
+ else
24
+ raise "Warning: LTI key and secret not configured for #{Rails.env})."
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,23 @@
1
+ module LtiProvider
2
+ module LtiXmlConfig
3
+ def self.load_config
4
+ YAML::load(File.open(config_file))[Rails.env]
5
+ end
6
+
7
+ def self.config_file
8
+ LtiProvider.app_root.join('config/lti_xml.yml')
9
+ end
10
+
11
+ def self.setup!
12
+ config = LtiProvider::XmlConfig
13
+ if File.exists?(config_file)
14
+ Rails.logger.info "Initializing LTI XML config using configuration in #{config_file}"
15
+ load_config.each do |k,v|
16
+ config.send("#{k}=", v)
17
+ end
18
+ else
19
+ raise "Warning: LTI XML config not configured for #{Rails.env})."
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,3 @@
1
+ module LtiProvider
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1 @@
1
+ LtiProvider::XmlConfig = OpenStruct.new
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :lti_provider do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,141 @@
1
+ require 'spec_helper'
2
+
3
+ describe LtiProvider::LtiController do
4
+ let(:user_id) { "1" }
5
+ let(:parameters) {
6
+ {
7
+ 'launch_url' => "http://#{request.host}",
8
+ 'custom_canvas_user_id' => user_id,
9
+ 'launch_presentation_return_url' => "http://test.canvas",
10
+
11
+ 'lti_version' => 'LTI-1p0',
12
+ 'lti_message_type' => 'basic-lti-launch-request',
13
+ 'action' => 'launch',
14
+ 'controller' => 'lti_provider/lti'
15
+ }
16
+ }
17
+
18
+ def create_consumer(key, secret)
19
+ consumer = IMS::LTI::ToolConsumer.new(key, secret, parameters)
20
+ consumer.resource_link_id = 'abc'
21
+ consumer
22
+ end
23
+
24
+ def post_lti_request!(key, secret)
25
+ consumer = create_consumer(key, secret)
26
+
27
+ # the oauth rack request proxy doesn't know to strip the 'action' and
28
+ # 'controller' parameters, so we need to stub them here so the request also
29
+ # gets signed with them
30
+ consumer.stubs(:to_params).returns(parameters)
31
+
32
+ data = consumer.generate_launch_data
33
+ request.env['RAW_POST_DATA'] = data.to_query
34
+ request.env['CONTENT_TYPE'] = 'application/x-www-form-urlencoded'
35
+ request.env['HTTP_REFERER'] = 'http://test.canvas/external_tools/1'
36
+
37
+ post :launch, data.merge(use_route: :lti_provider)
38
+ end
39
+
40
+ describe "GET cookie_test" do
41
+ context "when successful" do
42
+ it "proceeds to oauth" do
43
+ controller.session[:cookie_test] = true
44
+ controller.should_receive(:consume_launch)
45
+ get :cookie_test, use_route: :lti_provider
46
+ end
47
+ end
48
+
49
+ context "when failed" do
50
+ it "renders a message" do
51
+ get :cookie_test, use_route: :lti_provider
52
+ response.should render_template('cookie_test')
53
+ end
54
+ end
55
+ end
56
+
57
+ describe "POST launch" do
58
+ context "with a valid key" do
59
+ before do
60
+ post_lti_request!(LtiProvider::Config.key, LtiProvider::Config.secret)
61
+ end
62
+
63
+ it "performs a cookie test and passes along the nonce" do
64
+ response.redirect_url.should include(lti_provider.cookie_test_url(nonce: '', host: request.host))
65
+ end
66
+
67
+ it "saves the launch record" do
68
+ LtiProvider::Launch.first.user_id.should == user_id
69
+ end
70
+ end
71
+
72
+ context "without a key" do
73
+ it "renders an error message" do
74
+ post_lti_request!('', '')
75
+ response.body.should match "Consumer key not provided."
76
+ end
77
+ end
78
+
79
+ context "with an invalid secret" do
80
+ it "renders an error message" do
81
+ post_lti_request!(LtiProvider::Config.key, 'invalid')
82
+ response.body.should match "The OAuth signature was invalid."
83
+ end
84
+ end
85
+ end
86
+
87
+ describe "consume_launch" do
88
+ let!(:launch) do
89
+ LtiProvider::Launch.create!({
90
+ canvas_url: 'http://canvas',
91
+ nonce: 'abcd',
92
+ provider_params: {
93
+ 'custom_canvas_course_id' => 1,
94
+ 'custom_canvas_user_id' => 2,
95
+ 'tool_consumer_instance_guid' => '123abc'
96
+ }
97
+ },
98
+ without_protection: true)
99
+ end
100
+
101
+ describe "a successful launch" do
102
+ it "sets the session params" do
103
+ get :consume_launch, nonce: 'abcd', use_route: :lti_provider
104
+ session[:course_id].should == 1
105
+ session[:user_id].should == 2
106
+ session[:canvas_url].should == 'http://canvas'
107
+ session[:tool_consumer_instance_guid].should == '123abc'
108
+ end
109
+
110
+ it "destroys the launch" do
111
+ get :consume_launch, nonce: 'abcd', use_route: :lti_provider
112
+ LtiProvider::Launch.count.should == 0
113
+ end
114
+ end
115
+
116
+ describe "an expired nonce" do
117
+ before do
118
+ launch.update_attribute(:created_at, 10.minutes.ago)
119
+ end
120
+
121
+ it "shows an error" do
122
+ get :consume_launch, nonce: 'abcd', use_route: :lti_provider
123
+ response.body.should =~ /not launched successfully/
124
+ end
125
+ end
126
+
127
+ describe "a failed launch" do
128
+ it "shows an error" do
129
+ get :consume_launch, nonce: 'invalid', use_route: :lti_provider
130
+ response.body.should =~ /not launched successfully/
131
+ end
132
+ end
133
+ end
134
+
135
+ describe "configure.xml" do
136
+ it "should succeed" do
137
+ get :configure, format: :xml, use_route: :lti_provider
138
+ response.should be_success
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
3
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4
+
5
+ require File.expand_path('../config/application', __FILE__)
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ include LtiProvider::LtiApplication
3
+
4
+ protect_from_forgery
5
+ end
@@ -0,0 +1,5 @@
1
+ class WelcomeController < ApplicationController
2
+ def index
3
+ render text: "Hello LTI"
4
+ end
5
+ end
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Dummy::Application
@@ -0,0 +1,58 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ Bundler.require
6
+ require 'lti_provider'
7
+
8
+ module Dummy
9
+ class Application < Rails::Application
10
+ # Settings in config/environments/* take precedence over those specified here.
11
+ # Application configuration should go into files in config/initializers
12
+ # -- all .rb files in that directory are automatically loaded.
13
+
14
+ # Custom directories with classes and modules you want to be autoloadable.
15
+ # config.autoload_paths += %W(#{config.root}/extras)
16
+
17
+ # Only load the plugins named here, in the order given (default is alphabetical).
18
+ # :all can be used as a placeholder for all plugins not explicitly named.
19
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
20
+
21
+ # Activate observers that should always be running.
22
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
23
+
24
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
25
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
26
+ # config.time_zone = 'Central Time (US & Canada)'
27
+
28
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
29
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
30
+ # config.i18n.default_locale = :de
31
+
32
+ # Configure the default encoding used in templates for Ruby 1.9.
33
+ config.encoding = "utf-8"
34
+
35
+ # Configure sensitive parameters which will be filtered from the log file.
36
+ config.filter_parameters += [:password]
37
+
38
+ # Enable escaping HTML in JSON.
39
+ config.active_support.escape_html_entities_in_json = true
40
+
41
+ # Use SQL instead of Active Record's schema dumper when creating the database.
42
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
43
+ # like if you have constraints or database-specific column types
44
+ # config.active_record.schema_format = :sql
45
+
46
+ # Enforce whitelist mode for mass assignment.
47
+ # This will create an empty whitelist of attributes available for mass-assignment for all models
48
+ # in your app. As such, your models will need to explicitly whitelist or blacklist accessible
49
+ # parameters by using an attr_accessible or attr_protected declaration.
50
+ config.active_record.whitelist_attributes = true
51
+
52
+ # Enable the asset pipeline
53
+ config.assets.enabled = true
54
+
55
+ # Version of your assets, change this if you want to expire all your assets
56
+ config.assets.version = '1.0'
57
+ end
58
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ gemfile = File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ if File.exist?(gemfile)
5
+ ENV['BUNDLE_GEMFILE'] = gemfile
6
+ require 'bundler'
7
+ Bundler.setup
8
+ end
9
+
10
+ $:.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,8 @@
1
+ <%
2
+ rerun = File.file?('rerun.txt') ? IO.read('rerun.txt') : ""
3
+ rerun_opts = rerun.to_s.strip.empty? ? "--format #{ENV['CUCUMBER_FORMAT'] || 'progress'} features" : "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} #{rerun}"
4
+ std_opts = "--format #{ENV['CUCUMBER_FORMAT'] || 'pretty'} --strict --tags ~@wip"
5
+ %>
6
+ default: <%= std_opts %> features
7
+ wip: --tags @wip:3 --wip features
8
+ rerun: <%= rerun_opts %> --format rerun --out rerun.txt --strict --tags ~@wip
@@ -0,0 +1,25 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3
3
+ #
4
+ # Ensure the SQLite 3 gem is defined in your Gemfile
5
+ # gem 'sqlite3'
6
+ development:
7
+ adapter: sqlite3
8
+ database: db/development.sqlite3
9
+ pool: 5
10
+ timeout: 5000
11
+
12
+ # Warning: The database defined as "test" will be erased and
13
+ # re-generated from your development database when you run "rake".
14
+ # Do not set this db to the same as development or production.
15
+ test:
16
+ adapter: sqlite3
17
+ database: db/test.sqlite3
18
+ pool: 5
19
+ timeout: 5000
20
+
21
+ production:
22
+ adapter: sqlite3
23
+ database: db/production.sqlite3
24
+ pool: 5
25
+ timeout: 5000