qalam_oauth_engine 3.0.2 → 3.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 670853e86a63843215ca1dcfbf8ae5e8ebe7665e39c484794c433ef7d7086c6e
4
- data.tar.gz: 31bb746f4fcc1df2cac7d0dfff30262c91b782aa1b0006b0d0c2eed9cd293320
3
+ metadata.gz: fedb8888333b8e5593967fd75d34041dabd73bd19af4cc017b77f3ee4125d6b9
4
+ data.tar.gz: 6bc18fbcb7d0cae6641da7418dad7835bf5890dcc28502b06a42e9737bb35c68
5
5
  SHA512:
6
- metadata.gz: d24cc4150b92f9f0330a53d1afd32fe1cf50bbff59bf7b2e3fe08334a93021e3ca4ee4d8ccf0fde4fd2079b80339e5883bf23c5a359f67bb4ea7389d337dde21
7
- data.tar.gz: 669a149ee5bdc12e2459a2c4c773017f2a5e8a28704d93361e8ad1a45d0618c4cf8155c702a6bbe5accaf433591f2f11c9a1126daca86f5b1f987979e081c0c4
6
+ metadata.gz: 8cc4173cba1f1fefd30015c358b0e123ab34f329fad944559996a88c14f9d5ca0e36ce4077d7de064a80989c0c9bd7a2893fff621f9644e73168b3670dcb86a0
7
+ data.tar.gz: 16dd53a5746b3cb1d2166057874a77faeccb53a7fa154dec30cd1804be8db76443d8cf5139ad0f864aeb1f9b5d6a5ec0ebd746b4ed84d35c43ee08e9f808df0f
@@ -5,8 +5,10 @@ module CanvasOauth
5
5
  def oauth
6
6
  if verify_oauth2_state(params[:state]) && params[:code]
7
7
  if token = canvas.get_access_token(params[:code])
8
+ set_root_account
9
+ puts @root_account_id.inspect.green
8
10
  refresh_token = canvas.refresh_token
9
- if CanvasOauth::Authorization.cache_token(token, refresh_token, user_id, tool_consumer_instance_guid)
11
+ if CanvasOauth::Authorization.cache_token(token, refresh_token, user_id, @root_account_id, tool_consumer_instance_guid)
10
12
  redirect_to main_app.root_path
11
13
  else
12
14
  render plain: "Error: unable to save token"
@@ -22,5 +24,13 @@ module CanvasOauth
22
24
  def verify_oauth2_state(callback_state)
23
25
  callback_state.present? && callback_state == session.delete(:oauth2_state)
24
26
  end
27
+
28
+ def set_root_account
29
+ if session[:account_id]
30
+ @root_account_id = canvas.root_account_id(session[:account_id])
31
+ elsif session[:course_id]
32
+ @root_account_id = canvas.course_root_account_id(session[:course_id])
33
+ end
34
+ end
25
35
  end
26
36
  end
@@ -0,0 +1,4 @@
1
+ class CanvasLtiKey < ApplicationRecord
2
+ validates :canvas_url, :secret, presence: true
3
+ validates :key, presence: true, :uniqueness => {:scope=>:canvas_url}
4
+ end
@@ -2,16 +2,25 @@ module CanvasOauth
2
2
  class Authorization < ActiveRecord::Base
3
3
  validates :canvas_user_id, :token, :refresh_token, :last_used_at, presence: true
4
4
 
5
- def self.cache_token(token, refresh_token, user_id, tool_consumer_instance_guid)
5
+ def self.cache_token(token, refresh_token, user_id, account_id, tool_consumer_instance_guid)
6
6
  create do |t|
7
7
  t.token = token
8
8
  t.refresh_token = refresh_token
9
9
  t.canvas_user_id = user_id
10
+ t.canvas_root_account_id = account_id
10
11
  t.tool_consumer_instance_guid = tool_consumer_instance_guid
11
12
  t.last_used_at = Time.now
12
13
  end
13
14
  end
14
15
 
16
+ def self.fetch_account(user_id, tool_consumer_instance_guid)
17
+ user_accounts = where(canvas_user_id: user_id, tool_consumer_instance_guid: tool_consumer_instance_guid).order("created_at DESC")
18
+ if canvas_auth = user_accounts.first
19
+ canvas_auth.update_attribute(:last_used_at, Time.now)
20
+ return canvas_auth.canvas_root_account_id
21
+ end
22
+ end
23
+
15
24
  def self.fetch_token(user_id, tool_consumer_instance_guid)
16
25
  user_tokens = where(canvas_user_id: user_id, tool_consumer_instance_guid: tool_consumer_instance_guid).order("created_at DESC")
17
26
  if canvas_auth = user_tokens.first
@@ -2,6 +2,7 @@ class CreateCanvasOauthAuthorizations < ActiveRecord::Migration[4.2]
2
2
  def change
3
3
  create_table "canvas_oauth_authorizations", :force => true do |t|
4
4
  t.integer "canvas_user_id", :limit => 8
5
+ t.integer "canvas_root_account_id", :limit => 8
5
6
  t.string "tool_consumer_instance_guid", :null => false
6
7
  t.string "token"
7
8
  t.string "refresh_token"
@@ -0,0 +1,11 @@
1
+ class CreateCanvasLtiKeys < ActiveRecord::Migration[6.0]
2
+ def change
3
+ create_table :canvas_lti_keys do |t|
4
+ t.string :key
5
+ t.string :secret
6
+ t.string :canvas_url
7
+
8
+ t.timestamps
9
+ end
10
+ end
11
+ end
@@ -6,7 +6,7 @@ module CanvasOauth
6
6
  attr_accessor :token, :refresh_token, :key, :secret
7
7
  attr_reader :canvas_url, :canvas_user_id
8
8
 
9
- def initialize(canvas_url, canvas_user_id, token, refresh_token, key, secret)
9
+ def initialize(canvas_url, canvas_user_id, canvas_root_account_id, token, refresh_token, key, secret)
10
10
  unless [key, secret].all?(&:present?)
11
11
  raise "Invalid Qalam oAuth configuration"
12
12
  end
@@ -14,6 +14,7 @@ module CanvasOauth
14
14
  self.refresh_token = refresh_token
15
15
  self.canvas_url = canvas_url
16
16
  self.canvas_user_id = canvas_user_id
17
+ self.canvas_root_account_id = canvas_root_account_id
17
18
  self.token = token
18
19
  self.key = key
19
20
  self.secret = secret
@@ -130,6 +131,10 @@ module CanvasOauth
130
131
  def canvas_user_id=(value)
131
132
  @canvas_user_id = value
132
133
  end
134
+
135
+ def canvas_root_account_id=(value)
136
+ @canvas_root_account_id = value
137
+ end
133
138
 
134
139
  def hex_sis_id(name, value)
135
140
  hex = value.unpack("H*")[0]
@@ -1,9 +1,12 @@
1
1
  module CanvasOauth
2
2
  class CanvasApiExtensions
3
3
  def self.build(canvas_url, user_id, tool_consumer_instance_guid)
4
- refresh_token = CanvasOauth::Authorization.fetch_refresh_token(user_id, tool_consumer_instance_guid)
4
+ account_id = CanvasOauth::Authorization.fetch_account(user_id, tool_consumer_instance_guid)
5
5
  token = CanvasOauth::Authorization.fetch_token(user_id, tool_consumer_instance_guid)
6
- CanvasApi.new(canvas_url, user_id, token, refresh_token, CanvasConfig.key, CanvasConfig.secret)
6
+ refresh_token = CanvasOauth::Authorization.fetch_refresh_token(user_id, tool_consumer_instance_guid)
7
+ canvas_key = ((CanvasLtiKey.table_exists? && CanvasLtiKey.find_by(canvas_url: canvas_url)&.key) or CanvasConfig.key)
8
+ canvas_secret = ((CanvasLtiKey.table_exists? && CanvasLtiKey.find_by(key: canvas_key, canvas_url: canvas_url)&.secret) or CanvasConfig.secret)
9
+ CanvasApi.new(canvas_url, user_id, account_id, token, refresh_token, canvas_key, canvas_secret)
7
10
  end
8
11
  end
9
12
  end
@@ -17,15 +17,10 @@ module CanvasOauth
17
17
  config = load_config
18
18
  self.key = config['key']
19
19
  self.secret = config['secret']
20
-
21
- Rails.logger.info "\n> Initializing Key #{config['key']} - Secret #{config['secret']}\n".green
22
-
23
20
  elsif ENV['CANVAS_KEY'].present? && ENV['CANVAS_SECRET'].present?
24
21
  Rails.logger.info "Initializing Qalam using environment vars CANVAS_KEY and CANVAS_SECRET"
25
22
  self.key = ENV['CANVAS_KEY']
26
23
  self.secret = ENV['CANVAS_SECRET']
27
-
28
- Rails.logger.info "\n> Initializing Key #{ENV['CANVAS_KEY']} - Secret #{ENV['CANVAS_SECRET']}\n".green
29
24
  else
30
25
  warn "Warning: Qalam key and secret not configured (RAILS_ENV = #{ENV['RAILS_ENV']})."
31
26
  end
@@ -1,3 +1,3 @@
1
1
  module CanvasOauth
2
- VERSION = "3.0.2"
2
+ VERSION = "3.0.6"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qalam_oauth_engine
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.2
4
+ version: 3.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dave Donahue
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2021-08-01 00:00:00.000000000 Z
14
+ date: 2021-08-23 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: httparty
@@ -257,10 +257,12 @@ files:
257
257
  - Rakefile
258
258
  - app/controllers/canvas_oauth/canvas_controller.rb
259
259
  - app/controllers/canvas_oauth/oauth_application_controller.rb
260
+ - app/models/canvas_lti_key.rb
260
261
  - app/models/canvas_oauth/authorization.rb
261
262
  - config/canvas.yml.example
262
263
  - config/routes.rb
263
264
  - db/migrate/20121121005358_create_canvas_oauth_authorizations.rb
265
+ - db/migrate/20210809092919_create_canvas_lti_keys.rb
264
266
  - lib/canvas_oauth.rb
265
267
  - lib/canvas_oauth/canvas_api.rb
266
268
  - lib/canvas_oauth/canvas_api_extensions.rb
@@ -330,7 +332,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
330
332
  - !ruby/object:Gem::Version
331
333
  version: '0'
332
334
  requirements: []
333
- rubygems_version: 3.2.21
335
+ rubygems_version: 3.2.22
334
336
  signing_key:
335
337
  specification_version: 4
336
338
  summary: CanvasOauth is a mountable engine for handling the oauth workflow with canvas