Class: AtomicLti1v1::Lti1v1Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/atomic_lti_1v1/lti_1v1_middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Lti1v1Middleware

Returns a new instance of Lti1v1Middleware.



4
5
6
# File 'lib/atomic_lti_1v1/lti_1v1_middleware.rb', line 4

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/atomic_lti_1v1/lti_1v1_middleware.rb', line 14

def call(env)
  request = Rack::Request.new(env)
  if matches_path_prefixes?(request) && AtomicLti1v1::Lti1v1.is_lti_1v1?(request)
    oauth_consumer_key = request.params['oauth_consumer_key']

    lti_secret = nil
    begin
      lti_secret = AtomicLti1v1.secret_provider.call(oauth_consumer_key)
    rescue StandardError => e
      Rails.logger.error("Error looking up lti secret, #{e}")
    ensure
      if lti_secret.blank?
        Rails.logger.warn("No lti secret found for oauth_consumer_key: #{oauth_consumer_key}")
      end
    end

    if lti_secret.present? && AtomicLti1v1::Lti1v1.valid_lti_request?(request, lti_secret)
      env['atomic.validated.oauth_consumer_key'] = oauth_consumer_key
    elsif lti_secret.present? && !AtomicLti1v1::Lti1v1.valid_lti_request?(request, lti_secret)
      raise AtomicLti1v1::LtiValidationFailed, "Validation failed for oauth_consumer_key: #{oauth_consumer_key}"
    end

    # Let the frontend know there's no state to validate.  This is an LTI 1.3 thing.
    env["atomic.validated.state_validation"] = {
      state_verified: true,
    }
  end

  @app.call(env)
end

#matches_path_prefixes?(request) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
# File 'lib/atomic_lti_1v1/lti_1v1_middleware.rb', line 8

def matches_path_prefixes?(request)
  AtomicLti1v1.path_prefixes.any? do |prefix|
    request.path.starts_with? prefix
  end
end