Module: PandaPal::Platform::Canvas::OrgExtension

Extended by:
ActiveSupport::Concern
Defined in:
app/models/panda_pal/platform/canvas.rb

Instance Method Summary collapse

Instance Method Details

#_ensure_lti_v1p3_key(exists:) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/panda_pal/platform/canvas.rb', line 78

def _ensure_lti_v1p3_key(exists:)
  current_client_id = self.key.split('/')[0]

  existing_keys = []
  existing_keys += bearcat_client.get("api/v1/accounts/self/developer_keys?per_page=100").filter do |devkey|
    devkey[:id].to_s == current_client_id
  end
  existing_keys += bearcat_client.get("api/v1/accounts/self/developer_keys?per_page=100", inherited: true).filter do |devkey|
    devkey[:id].to_s == current_client_id
  end

  ekey = existing_keys[0]
  # if ekey && exists == :error
  #   raise "Tool with key #{self.key} already installed"
  # end

  lti_json_url = PandaPal::LaunchUrlHelpers.resolve_route(:v1p3_config_url, host: host)
  lti_json = JSON.parse(HTTParty.get(lti_json_url, format: :plain).body)

  if !ekey
    # Create New
    ekey = bearcat_client.post("api/lti/accounts/self/developer_keys/tool_configuration", {
      developer_key: {
        name: PandaPal.lti_options[:title],
        redirect_uris: [
          lti_json["target_link_uri"],
        ].join("\n")
      },
      tool_configuration: {
        settings: lti_json,
      },
    })
  elsif exists == :replace || exists == :update
    # Update Existing
    ekey = bearcat_client.put("api/lti/developer_keys/#{ekey[:id]}/tool_configuration", {
      developer_key: ekey[:developer_key],
      tool_configuration: lti_json,
    })
  end

  ekey = ekey[:developer_key] || ekey

  if ekey[:developer_key_account_binding][:workflow_state] == "off"
    bearcat_client.post("api/v1/accounts/self/developer_keys/#{ekey[:id]}/developer_key_account_bindings", {
      developer_key_account_binding: {
        workflow_state: "on",
      },
    })
  end

  ekey
end

#_find_existing_installs(exists:, &matcher) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/models/panda_pal/platform/canvas.rb', line 131

def _find_existing_installs(exists:, &matcher)
  existing_installs = bearcat_client.send(:"#{ctype}_external_tools", cid).all_pages_each.filter do |cet|
    matcher.call(cet)
  end

  if existing_installs.present?
    case exists
    when :error
      raise "Tool with key #{self.key} already installed"
    when :duplicate
    when :replace
      existing_installs.each do |install|
        bearcat_client.send(:"delete_#{ctype}_external_tool", cid, install[:id])
      end
    else
      raise "exists: #{exists} is not supported"
    end
  end

  existing_installs
end

#bearcat_clientObject



232
233
234
235
236
237
238
239
240
# File 'app/models/panda_pal/platform/canvas.rb', line 232

def bearcat_client
  return canvas_sync_client if defined?(canvas_sync_client)

  Bearcat::Client.new(
    prefix: canvas_url,
    token: canvas_token,
    master_rate_limit: (Rails.env.production? && !!defined?(Redis) && ENV['REDIS_URL'].present?),
  )
end

#canvas_api_tokenObject



212
213
214
215
216
217
218
219
220
221
# File 'app/models/panda_pal/platform/canvas.rb', line 212

def canvas_api_token
  PandaPal::Platform.find_org_setting([
    "canvas.api_token",
    "canvas.api_key",
    "canvas.token",
    "canvas_api_token",
    "canvas_token",
    "api_token",
  ], self)
end

#canvas_urlObject



202
203
204
205
206
207
208
209
210
# File 'app/models/panda_pal/platform/canvas.rb', line 202

def canvas_url
  PandaPal::Platform.find_org_setting([
    "canvas.base_url",
    "canvas_url",
    "canvas_base_url",
    "canvas.url",
    "base_url",
  ], self) || (Rails.env.development? && 'http://localhost:3000') || 'https://canvas.instructure.com'
end

#install_lti(host: nil, context: :root_account, version: 'v1p3', exists: :error, dedicated_deployment: false) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/panda_pal/platform/canvas.rb', line 31

def install_lti(host: nil, context: :root_account, version: 'v1p3', exists: :error, dedicated_deployment: false)
  raise "Automatically installing this LTI requires Bearcat." unless defined?(Bearcat)

  context = context.to_s
  context = 'account/self' if context == 'root_account'
  cid, ctype = context.split(/[\.\/]/).reverse
  ctype ||= 'account'

  if version == 'v1p0'
    existing_installs = _find_existing_installs(exists: exists) do |lti|
      lti[:consumer_key] == self.key
    end

    conf = {
      name: PandaPal.lti_options[:title],
      description: PandaPal.lti_options[:description],
      consumer_key: self.key,
      shared_secret: self.secret,
      privacy_level: "public",
      config_type: 'by_url',
      config_url: PandaPal::LaunchUrlHelpers.resolve_route(:v1p0_config_url, host: host),
    }

    bearcat_client.send(:"create_#{ctype}_external_tool", cid, conf)
  elsif version == 'v1p3'
    ekey = _ensure_lti_v1p3_key(exists: exists)

    existing_installs = _find_existing_installs(exists: exists) do |lti|
      lti[:developer_key_id] == (ekey[:id] % 10_000_000_000_000)
    end

    scope_tool = bearcat_client.send(:"create_#{ctype}_external_tool", cid, {
      client_id: ekey[:id],
    })

    new_client_id = "#{ekey[:id]}"
    new_client_id += "/#{scope_tool[:deployment_id]}" if dedicated_deployment

    self.key = new_client_id
    self.secret = ekey[:api_key]

    save! if changed!
  else
    raise "Unrecognized LTI Version #{version}"
  end
end

#lti_api_configuration(host: nil) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'app/models/panda_pal/platform/canvas.rb', line 157

def lti_api_configuration(host: nil)
  PandaPal::LaunchUrlHelpers.with_uri_host(host) do
    domain = PandaPal.lti_properties[:domain] || host.host
    launch_url = PandaPal.lti_options[:secure_launch_url] ||
      "#{domain}#{PandaPal.lti_options[:secure_launch_path]}" ||
      PandaPal.lti_options[:launch_url] ||
      "#{domain}#{PandaPal.lti_options[:launch_path]}" ||
      domain

    lti_json = {
      name: PandaPal.lti_options[:title],
      description: PandaPal.lti_options[:description],
      domain: host.host,
      url: launch_url,
      consumer_key: self.key,
      shared_secret: self.secret,
      privacy_level: "public",

      custom_fields: {},

      environments: PandaPal.lti_environments,
    }

    lti_json = lti_json.with_indifferent_access
    lti_json.merge!(PandaPal.lti_properties)

    (PandaPal.lti_options[:custom_fields] || []).each do |k, v|
      lti_json[:custom_fields][k] = v
    end

    PandaPal.lti_paths.each do |k, options|
      options = PandaPal::LaunchUrlHelpers.normalize_lti_launch_desc(options)
      options[:url] = PandaPal::LaunchUrlHelpers.absolute_launch_url(
        k.to_sym,
        host: host,
        launch_handler: :v1p0_launch_path,
        default_auto_launch: false
      ).to_s
      lti_json[k] = options
    end

    lti_json
  end
end

#reinstall_lti!(*args, **kwargs) ⇒ Object



153
154
155
# File 'app/models/panda_pal/platform/canvas.rb', line 153

def reinstall_lti!(*args, **kwargs)
  install_lti(*args, exists: :replace, **kwargs)
end

#root_account_infoObject



223
224
225
226
227
228
229
# File 'app/models/panda_pal/platform/canvas.rb', line 223

def 
  Rails.cache.fetch("panda_pal/org:#{name}/root_account_info", expires_in: 24.hours) do
    response = bearcat_client.("self")
    response = bearcat_client.(response[:root_account_id]) if response[:root_account_id].present?
    response
  end
end