qalam_oauth_engine 3.0.8 → 3.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a94aa9f960fad372ccd1ce843c502f7f1f8011f010e29dd095224a0d9a5c701f
4
- data.tar.gz: a1f11ff92ae13c52c42271180623616cbf47cd42780e34e3f029a30c6143fe44
3
+ metadata.gz: c39bb4366b4792b9a2b0746a9073f28d410f181d82147c266939ae96489d5d5f
4
+ data.tar.gz: 97dd0420a00015b587b5a2ece17cad01fbfe0a78d95a1e8941980d4b8d076a4d
5
5
  SHA512:
6
- metadata.gz: 8540835b1a123aae6f271195d80443a7627649dc2156c1ec2a6f0e6be6295d99f0669815b5e11250b7b0c7f70c7811d901158107ed1e2424934a0de3ba2c3d7b
7
- data.tar.gz: f01b816cc088416ffa80ddfd5908ae40bded8ac8e11f8c784ead5317fd82a3d4039b424aa29cfff82aad4756c5776bbcdc291ec8116ebec48776dd2cb0a3b5a1
6
+ metadata.gz: 4346ca322ffd60dbb3e78e0106832255d8950e0850f1297669737c3a3ac9867e2bd95c15f4c88543dcf39625df63971341fe2524d40ddc5378d807e7551a491d
7
+ data.tar.gz: 4b7e0be12099a9312f1e125de7e6eae8d676bde404929979bbb7cc4e274cfb008d8e7e3d3d893cf5d668b75af1d7c6ffed170269343efcc2b7691fabffff8c67
@@ -7,7 +7,7 @@ module CanvasOauth
7
7
  if token = canvas.get_access_token(params[:code])
8
8
  set_root_account
9
9
  refresh_token = canvas.refresh_token
10
- if CanvasOauth::Authorization.cache_token(token, refresh_token, user_id, @root_account_id, tool_consumer_instance_guid)
10
+ if CanvasOauth::Authorization.cache_token(token, refresh_token, user_id, @root_account_id, tool_consumer_instance_guid, canvas_url)
11
11
  redirect_to main_app.root_path
12
12
  else
13
13
  render plain: "Error: unable to save token"
@@ -24,12 +24,5 @@ module CanvasOauth
24
24
  callback_state.present? && callback_state == session.delete(:oauth2_state)
25
25
  end
26
26
 
27
- def set_root_account
28
- if session[:account_id]
29
- @root_account_id = canvas.root_account_id(session[:account_id])
30
- elsif session[:course_id]
31
- @root_account_id = canvas.course_root_account_id(session[:course_id])
32
- end
33
- end
34
27
  end
35
28
  end
@@ -2,17 +2,35 @@ 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, account_id, tool_consumer_instance_guid)
5
+ def self.cache_token(token, refresh_token, user_id, account_id, tool_consumer_instance_guid, canvas_url)
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
+ t.canvas_root_account_id = account_id if CanvasOauth::Authorization.column_names.include?('canvas_root_account_id')
11
+ t.canvas_url = canvas_url if canvas_url && CanvasOauth::Authorization.column_names.include?('canvas_url')
11
12
  t.tool_consumer_instance_guid = tool_consumer_instance_guid
12
13
  t.last_used_at = Time.now
13
14
  end
14
15
  end
15
16
 
17
+ def self.fetch_canvas_auth(user_id, tool_consumer_instance_guid, canvas_url)
18
+ user_canvas_auths = where(canvas_user_id: user_id, tool_consumer_instance_guid: tool_consumer_instance_guid).order("created_at DESC")
19
+ user_canvas_auths = user_canvas_auths.where('lower(canvas_url) LIKE :q', q: "%#{canvas_url.downcase}%") if canvas_url && CanvasOauth::Authorization.column_names.include?('canvas_url')
20
+ if canvas_auth = user_canvas_auths.first
21
+ canvas_auth.update_attribute(:last_used_at, Time.now)
22
+ return canvas_auth
23
+ end
24
+ end
25
+
26
+ def self.fetch_id(user_id, tool_consumer_instance_guid)
27
+ user_accounts = where(canvas_user_id: user_id, tool_consumer_instance_guid: tool_consumer_instance_guid).order("created_at DESC")
28
+ if canvas_auth = user_accounts.first
29
+ canvas_auth.update_attribute(:last_used_at, Time.now)
30
+ return canvas_auth.id
31
+ end
32
+ end
33
+
16
34
  def self.fetch_account(user_id, tool_consumer_instance_guid)
17
35
  user_accounts = where(canvas_user_id: user_id, tool_consumer_instance_guid: tool_consumer_instance_guid).order("created_at DESC")
18
36
  if canvas_auth = user_accounts.first
@@ -6,6 +6,7 @@ class CreateCanvasOauthAuthorizations < ActiveRecord::Migration[4.2]
6
6
  t.string "tool_consumer_instance_guid", :null => false
7
7
  t.string "token"
8
8
  t.string "refresh_token"
9
+ t.string "canvas_url"
9
10
  t.datetime "last_used_at"
10
11
  t.datetime "created_at", :null => false
11
12
  t.datetime "updated_at", :null => false
@@ -4,13 +4,14 @@ module CanvasOauth
4
4
  PER_PAGE = 50
5
5
 
6
6
  attr_accessor :token, :refresh_token, :key, :secret
7
- attr_reader :canvas_url, :canvas_user_id, :canvas_root_account_id
7
+ attr_reader :id, :canvas_url, :canvas_user_id, :canvas_root_account_id
8
8
 
9
- def initialize(canvas_url, canvas_user_id, canvas_root_account_id, token, refresh_token, key, secret)
9
+ def initialize(id, 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
13
-
13
+
14
+ self.id = id
14
15
  self.refresh_token = refresh_token
15
16
  self.canvas_url = canvas_url
16
17
  self.canvas_user_id = canvas_user_id
@@ -140,6 +141,10 @@ module CanvasOauth
140
141
  def canvas_root_account_id=(value)
141
142
  @canvas_root_account_id = value
142
143
  end
144
+
145
+ def id=(value)
146
+ @id = value
147
+ end
143
148
 
144
149
  def hex_sis_id(name, value)
145
150
  hex = value.unpack("H*")[0]
@@ -1,12 +1,22 @@
1
1
  module CanvasOauth
2
2
  class CanvasApiExtensions
3
- def self.build(canvas_url, user_id, tool_consumer_instance_guid)
4
- account_id = CanvasOauth::Authorization.fetch_account(user_id, tool_consumer_instance_guid)
5
- token = CanvasOauth::Authorization.fetch_token(user_id, tool_consumer_instance_guid)
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)
3
+ def self.build(user_id, tool_consumer_instance_guid, canvas_url)
4
+ canvas_auth = CanvasOauth::Authorization.fetch_canvas_auth(user_id, tool_consumer_instance_guid, canvas_url)
5
+
6
+ id = canvas_auth&.id
7
+ account_id = canvas_auth&.canvas_root_account_id
8
+ token = canvas_auth&.token
9
+ refresh_token = canvas_auth&.refresh_token
10
+
11
+ if canvas_url && CanvasLtiKey.table_exists?
12
+ canvas_lti_key = CanvasLtiKey.where('lower(canvas_url) LIKE :q', q: "%#{canvas_url.downcase}%").first
13
+ canvas_key = canvas_lti_key&.key or CanvasConfig.key
14
+ canvas_secret = canvas_lti_key&.secret or CanvasConfig.secret
15
+ else
16
+ canvas_key = CanvasConfig.key
17
+ canvas_secret = CanvasConfig.secret
18
+ end
19
+ CanvasApi.new(id, canvas_url, user_id, account_id, token, refresh_token, canvas_key, canvas_secret)
10
20
  end
11
21
  end
12
22
  end
@@ -16,7 +16,7 @@ module CanvasOauth
16
16
 
17
17
  protected
18
18
  def initialize_canvas
19
- @canvas = ::CanvasOauth::CanvasApiExtensions.build(canvas_url, user_id, tool_consumer_instance_guid)
19
+ @canvas = ::CanvasOauth::CanvasApiExtensions.build(user_id, tool_consumer_instance_guid, canvas_url)
20
20
  end
21
21
 
22
22
  def canvas
@@ -58,6 +58,14 @@ module CanvasOauth
58
58
  # be overridden or the session data needs to be set up by the time the
59
59
  # oauth filter runs (like with the lti_provider_engine)
60
60
 
61
+ def set_root_account
62
+ if session[:account_id]
63
+ @root_account_id = canvas.root_account_id(session[:account_id])
64
+ elsif session[:course_id]
65
+ @root_account_id = canvas.course_root_account_id(session[:course_id])
66
+ end
67
+ end
68
+
61
69
  def canvas_url
62
70
  session[:canvas_url]
63
71
  end
@@ -13,12 +13,12 @@ module CanvasOauth
13
13
 
14
14
  def self.setup!
15
15
  if File.exists?(config_file)
16
- Rails.logger.info "Initializing Qalam using configuration in #{config_file}"
16
+ Rails.logger.info "Initializing Qalam using configuration in #{config_file}".green
17
17
  config = load_config
18
18
  self.key = config['key']
19
19
  self.secret = config['secret']
20
20
  elsif ENV['CANVAS_KEY'].present? && ENV['CANVAS_SECRET'].present?
21
- Rails.logger.info "Initializing Qalam using environment vars CANVAS_KEY and CANVAS_SECRET"
21
+ Rails.logger.info "Initializing Qalam using environment vars CANVAS_KEY and CANVAS_SECRET".green
22
22
  self.key = ENV['CANVAS_KEY']
23
23
  self.secret = ENV['CANVAS_SECRET']
24
24
  else
@@ -1,3 +1,3 @@
1
1
  module CanvasOauth
2
- VERSION = "3.0.8"
2
+ VERSION = "3.0.9"
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.8
4
+ version: 3.0.9
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-29 00:00:00.000000000 Z
14
+ date: 2021-08-30 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: httparty
@@ -61,6 +61,20 @@ dependencies:
61
61
  - - "<"
62
62
  - !ruby/object:Gem::Version
63
63
  version: '6.1'
64
+ - !ruby/object:Gem::Dependency
65
+ name: colorize
66
+ requirement: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ type: :runtime
72
+ prerelease: false
73
+ version_requirements: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
64
78
  - !ruby/object:Gem::Dependency
65
79
  name: byebug
66
80
  requirement: !ruby/object:Gem::Requirement