Module: PandaPal::Platform::Canvas::OrgExtension
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/panda_pal/platform/canvas.rb
Instance Method Summary collapse
- #_ensure_lti_v1p3_key(exists:, host:) ⇒ Object
- #_find_existing_installs(context, exists: nil, &matcher) ⇒ Object
- #_install_lti(host: nil, context: :root_account, version: nil, exists: :error, dedicated_deployment: false) ⇒ Object
- #_parse_lti_context(context) ⇒ Object
- #bearcat_client ⇒ Object
- #canvas_api_token ⇒ Object
- #canvas_lti_launch_url(placement_or_context, context: nil, host: nil, params: {}, lti_id: nil) ⇒ Object
- #canvas_url ⇒ Object
- #install_lti(*args, **kwargs) ⇒ Object
- #lti_api_configuration(host: nil) ⇒ Object
- #lti_installations(context = :root_account) ⇒ Object
- #reinstall_lti!(*args, **kwargs) ⇒ Object
- #root_account_info ⇒ Object
Instance Method Details
#_ensure_lti_v1p3_key(exists:, host:) ⇒ Object
151 152 153 154 155 156 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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'app/models/panda_pal/platform/canvas.rb', line 151 def _ensure_lti_v1p3_key(exists:, host:) 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] lti_json_url = PandaPal::LaunchUrlHelpers.resolve_route(:v1p3_config_url, host: host) lti_json = JSON.parse(HTTParty.get(lti_json_url, format: :plain).body) valid_redirect_uris = [ lti_json["target_link_uri"] ] canvas_ext = (lti_json["extensions"] || []).find { |ext| ext["platform"] == "canvas.instructure.com" } placements = canvas_ext&.dig("settings", "placements") || [] placements.each do |p| next unless p["target_link_uri"] valid_redirect_uris << p["target_link_uri"].gsub(/\?.*$/, "") end valid_redirect_uris.uniq! prod_domain = PandaPal.lti_environments[:domain] if prod_domain.present? PandaPal.lti_environments.each do |env, domain| env = env.to_s next unless env.ends_with?("_domain") env = env.split('_')[0] valid_redirect_uris += valid_redirect_uris.map{|uri| uri.gsub(prod_domain, domain)} end end valid_redirect_uris.uniq! if !ekey # Create New ekey = bearcat_client.post("api/lti/accounts/self/developer_keys/tool_configuration", { developer_key: { name: PandaPal.[:title], redirect_uris: valid_redirect_uris.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: { redirect_uris: valid_redirect_uris.join("\n"), }, # tool_configuration: lti_json, tool_configuration: { settings: 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(context, exists: nil, &matcher) ⇒ Object
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'app/models/panda_pal/platform/canvas.rb', line 229 def _find_existing_installs(context, exists: nil, &matcher) ctype, cid = _parse_lti_context(context) existing_installs = bearcat_client.send(:"#{ctype}_external_tools", cid).all_pages_each.filter do |cet| matcher.call(cet) end if exists.present? && 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 [] when :update existing_installs else raise "exists: #{exists} is not supported" end else existing_installs end end |
#_install_lti(host: nil, context: :root_account, version: nil, exists: :error, dedicated_deployment: false) ⇒ 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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'app/models/panda_pal/platform/canvas.rb', line 78 def _install_lti(host: nil, context: :root_account, version: nil, exists: :error, dedicated_deployment: false) raise "Automatically installing this LTI requires Bearcat." unless defined?(Bearcat) version = version || PandaPal.[:lti_spec_version] || 'v1p3' version = version.to_s if (vs = version.split(".")).count > 1 version = "v#{vs[0]}p#{vs[0]}" end # TODO Ensure host is actually this LTI run_callbacks :lti_install do ctype, cid = _parse_lti_context(context) if version == 'v1p0' existing_installs = _find_existing_installs(context, exists: exists) do |lti| lti[:consumer_key] == self.key end conf = { name: PandaPal.[:title], description: PandaPal.[: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), } api_result = existing_installs[0] ? bearcat_client.send(:"edit_#{ctype}_external_tool", cid, existing_installs[0][:id], conf) : bearcat_client.send(:"create_#{ctype}_external_tool", cid, conf) @new_lti_installation = api_result elsif version == 'v1p3' ekey = _ensure_lti_v1p3_key(exists: exists, host: host) existing_installs = _find_existing_installs(context, exists: exists) do |lti| # TODO This may not be correct when actually inheriting LTI keys lti[:developer_key_id] == (ekey[:id] % 10_000_000_000_000) end scope_tool = existing_installs[0] # Don't need to do an update if it already exists - Settings are stored on the LTI Key instead of the installation unless scope_tool 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? end @new_lti_installation = scope_tool else raise "Unrecognized LTI Version #{version}" end end save! end |
#_parse_lti_context(context) ⇒ Object
69 70 71 72 73 74 75 76 |
# File 'app/models/panda_pal/platform/canvas.rb', line 69 def _parse_lti_context(context) context = context.to_s context = 'account/self' if context == 'root_account' cid, ctype = context.split(/[\.\/]/).reverse ctype ||= 'account' [ctype, cid] end |
#bearcat_client ⇒ Object
388 389 390 391 392 393 394 395 396 |
# File 'app/models/panda_pal/platform/canvas.rb', line 388 def bearcat_client # Less than ideal, but `canvas_sync_client` has been the long-adopted tradition so we check for it so that we can continue to drop-in new versions of PandaPal return canvas_sync_client if defined?(canvas_sync_client) Bearcat::Client.new( prefix: canvas_url, token: canvas_api_token, ) end |
#canvas_api_token ⇒ Object
368 369 370 371 372 373 374 375 376 377 |
# File 'app/models/panda_pal/platform/canvas.rb', line 368 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_lti_launch_url(placement_or_context, context: nil, host: nil, params: {}, lti_id: nil) ⇒ Object
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'app/models/panda_pal/platform/canvas.rb', line 310 def canvas_lti_launch_url(placement_or_context, context: nil, host: nil, params: {}, lti_id: nil) if placement_or_context.is_a?(String) && placement_or_context.include?('/') context = placement_or_context placement = nil placement_or_context = nil elsif placement_or_context.is_a?(Symbol) || placement_or_context.is_a?(String) placement = placement_or_context.to_s elsif !context context = placement_or_context placement_or_context = nil else raise ArgumentError, "context seems to have been passed twice" end if context.is_a?(String) context_path = context elsif context context_name = context.class.name.downcase placement ||= context_name + "_navigation" context_path = "#{context_name}s/#{context.canvas_id}" else placement ||= "account_navigation" context_path = "accounts/#{current_organization.canvas_account_id}" end if %w[global account course user].include?(placement) placement = "#{placement}_navigation" end canvas_uri = URI.parse(self.canvas_url) lti_id ||= self.settings.dig(:canvas, :external_tool_id) canvas_uri.path = "/#{context_path}/external_tools/#{lti_id}" canvas_params = {} canvas_params[:launch_type] = placement if placement == "global_navigation" if params.present? host = "#{host.scheme}://#{host.host_with_port}" if host.is_a?(ActionDispatch::Request) canvas_params[:launch_url] = PandaPal::Engine.routes.url_helpers.v1p3_resource_link_request_url( host: host, **params, ).to_s end canvas_uri.query = URI.encode_www_form(canvas_params) canvas_uri end |
#canvas_url ⇒ Object
358 359 360 361 362 363 364 365 366 |
# File 'app/models/panda_pal/platform/canvas.rb', line 358 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(*args, **kwargs) ⇒ Object
145 146 147 148 149 |
# File 'app/models/panda_pal/platform/canvas.rb', line 145 def install_lti(*args, **kwargs) switch_tenant do _install_lti(*args, **kwargs) end end |
#lti_api_configuration(host: nil) ⇒ Object
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
# File 'app/models/panda_pal/platform/canvas.rb', line 261 def lti_api_configuration(host: nil) PandaPal::LaunchUrlHelpers.with_uri_host(host) do domain = PandaPal.lti_properties[:domain] || host.host launch_url = PandaPal.[:secure_launch_url] || "#{domain}#{PandaPal.lti_options[:secure_launch_path]}" || PandaPal.[:launch_url] || "#{domain}#{PandaPal.lti_options[:launch_path]}" || domain lti_json = { name: PandaPal.[:title], description: PandaPal.[: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.[:custom_fields] || []).each do |k, v| lti_json[:custom_fields][k] = v end PandaPal.lti_paths.each do |k, | = PandaPal::LaunchUrlHelpers.normalize_lti_launch_desc() [: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] = end lti_json end end |
#lti_installations(context = :root_account) ⇒ Object
306 307 308 |
# File 'app/models/panda_pal/platform/canvas.rb', line 306 def lti_installations(context= :root_account) _find_existing_installs(context) end |
#reinstall_lti!(*args, **kwargs) ⇒ Object
257 258 259 |
# File 'app/models/panda_pal/platform/canvas.rb', line 257 def reinstall_lti!(*args, **kwargs) install_lti(*args, exists: :replace, **kwargs) end |
#root_account_info ⇒ Object
379 380 381 382 383 384 385 |
# File 'app/models/panda_pal/platform/canvas.rb', line 379 def root_account_info Rails.cache.fetch("panda_pal/org:#{name}/root_account_info", expires_in: 24.hours) do response = bearcat_client.account("self") response = bearcat_client.account(response[:root_account_id]) if response[:root_account_id].present? response end end |