canvas-embed 0.1.4 → 0.1.6

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.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +5 -5
  3. data/Gemfile.lock +3 -3
  4. data/{LICENSE.txt → LICENSE} +1 -1
  5. data/README.md +0 -12
  6. data/Rakefile +3 -3
  7. data/bin/console +3 -3
  8. data/canvas-embed.gemspec +15 -14
  9. data/example/Gemfile +20 -19
  10. data/example/Gemfile.lock +2 -2
  11. data/example/README.md +16 -3
  12. data/example/Rakefile +3 -1
  13. data/example/app/controllers/application_controller.rb +7 -5
  14. data/example/bin/bundle +22 -18
  15. data/example/bin/importmap +3 -2
  16. data/example/bin/rails +5 -3
  17. data/example/bin/rake +4 -2
  18. data/example/bin/setup +10 -8
  19. data/example/config/application.rb +5 -3
  20. data/example/config/boot.rb +5 -3
  21. data/example/config/environment.rb +3 -1
  22. data/example/config/environments/development.rb +5 -3
  23. data/example/config/initializers/cors.rb +10 -8
  24. data/example/config/puma.rb +11 -9
  25. data/example/config/routes.rb +4 -2
  26. data/example/config.ru +3 -1
  27. data/example/log/development.log +188 -2565
  28. data/example/storage/development.sqlite3-shm +0 -0
  29. data/example/storage/development.sqlite3-wal +0 -0
  30. data/example/tmp/cache/bootsnap/compile-cache-iseq/01/2f863f8d7ff70d +0 -0
  31. data/example/tmp/cache/bootsnap/compile-cache-iseq/0a/707c6e76401f50 +0 -0
  32. data/example/tmp/cache/bootsnap/compile-cache-iseq/24/4134f174d68291 +0 -0
  33. data/example/tmp/cache/bootsnap/compile-cache-iseq/3b/64ba0c52596b87 +0 -0
  34. data/example/tmp/cache/bootsnap/compile-cache-iseq/4c/837c8a08879a20 +0 -0
  35. data/example/tmp/cache/bootsnap/compile-cache-iseq/68/a73620fd7d4284 +0 -0
  36. data/example/tmp/cache/bootsnap/compile-cache-iseq/79/1a2ebed1814cfa +0 -0
  37. data/example/tmp/cache/bootsnap/compile-cache-iseq/b2/d451a148d6d303 +0 -0
  38. data/example/tmp/cache/bootsnap/compile-cache-iseq/d8/fff5874ac0039e +0 -0
  39. data/example/tmp/cache/bootsnap/compile-cache-iseq/ee/02ad963145723a +0 -0
  40. data/example/tmp/cache/bootsnap/compile-cache-iseq/fb/4f5abf058e6979 +0 -0
  41. data/example/tmp/cache/bootsnap/load-path-cache +0 -0
  42. data/example/tmp/pids/server.pid +1 -0
  43. data/lib/canvas/embed/version.rb +1 -1
  44. data/lib/canvas/embed.rb +26 -10
  45. data/pkg/canvas-embed-0.1.5.gem +0 -0
  46. data/spec/canvas/embed_spec.rb +44 -7
  47. data/spec/spec_helper.rb +2 -2
  48. metadata +17 -4
  49. data/pkg/canvas-embed-0.1.2.gem +0 -0
  50. data/pkg/canvas-embed-0.1.3.gem +0 -0
File without changes
@@ -0,0 +1 @@
1
+ 56214
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Canvas
4
4
  module Embed
5
- VERSION = "0.1.4"
5
+ VERSION = '0.1.6'
6
6
  end
7
7
  end
data/lib/canvas/embed.rb CHANGED
@@ -1,26 +1,42 @@
1
- # frozen_string_literal: true
1
+ # Uses your signing key from Canvas to generate a token granting access to scoped Canvas data
2
2
 
3
- require_relative "embed/version"
3
+ require_relative 'embed/version'
4
4
  require 'json'
5
5
  require 'rbnacl'
6
- require "base64"
6
+ require 'base64'
7
7
 
8
8
  module Canvas
9
+ class InvalidScopeError < StandardError
10
+ end
9
11
  module Embed
10
- extend self
11
- def generate_token(private_key, scopes)
12
+ module_function
13
+ # Public: Generate a token granting access to a scoped set of your data in Canvas
14
+ #
15
+ # private_key - String, signing key obtained from Canvas
16
+ # scopes - Hash, containing the scopes to grant for this token
17
+ # expiration_seconds - Optional Integer, how long the token should be valid for
18
+ # user_id - Optional String, identifier for the user, used in logging in Canvas.
19
+ #
20
+ def generate_token(private_key, scopes, expiration_seconds = 3600, user_id = nil)
21
+ if !scopes.is_a?(Hash)
22
+ raise InvalidScopeError.new("Invalid scope #{scopes} type #{scopes.class}")
23
+ end
12
24
  # token consists of an id and the signing key
13
25
  key_id, key = private_key.split('.')
14
26
  # transform signing key hex into bytes
15
27
  key_bytes = [key].pack('H*')
16
28
  secret_box = RbNaCl::SecretBox.new(key_bytes)
17
29
  nonce = RbNaCl::Random.random_bytes(secret_box.nonce_bytes)
18
- message = { "scopes" => scopes }.to_json
19
- ciphertext = secret_box.encrypt(nonce, message)
30
+ exp = Time.now.to_i + expiration_seconds
31
+ message = { 'scopes' => scopes, 'exp' => exp }
32
+ if user_id != nil
33
+ message['userId'] = user_id
34
+ end
35
+ ciphertext = secret_box.encrypt(nonce, message.to_json)
20
36
  # transform bytes into hex
21
- unpacked_message = ciphertext.unpack('H*').first
22
- unpacked_nonce = nonce.unpack('H*').first
23
- token = { "message" => unpacked_message, "nonce" => unpacked_nonce, "keyId" => key_id }.to_json;
37
+ unpacked_message = ciphertext.unpack1('H*')
38
+ unpacked_nonce = nonce.unpack1('H*')
39
+ token = { 'message' => unpacked_message, 'nonce' => unpacked_nonce, 'keyId' => key_id }.to_json
24
40
  # strict for no newlines
25
41
  Base64.strict_encode64(token)
26
42
  end
Binary file
@@ -1,19 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "canvas/embed"
3
+ # require 'rails_helper'
4
+ require 'rbnacl'
5
+ require 'canvas/embed'
4
6
 
5
7
  RSpec.describe Canvas::Embed do
6
- it "has a version number" do
8
+ subject do
9
+ self.extend(Canvas::Embed)
10
+ end
11
+
12
+ it 'has a version number' do
7
13
  expect(Canvas::Embed::VERSION).not_to be nil
8
14
  end
9
15
 
10
- it "generates valid encrypted token" do
16
+ it 'generates valid encrypted token' do
11
17
  key = RbNaCl::Random.random_bytes(RbNaCl::SecretBox.key_bytes)
12
- unpacked_key = "emk_ZRzQbE9d." + key.unpack('H*').first
18
+ unpacked_key = "emk_ZRzQbE9d.#{key.unpack1('H*')}"
13
19
 
14
- original_message = { "team" => "canvas"};
20
+ original_message = { 'team' => 'canvas' }
15
21
 
16
- token = Canvas::Embed::generate_token(unpacked_key, original_message)
22
+ token = Canvas::Embed.generate_token(unpacked_key, original_message)
17
23
  expect(token).not_to be nil
18
24
 
19
25
  decoded = JSON.parse(Base64.decode64(token))
@@ -29,6 +35,37 @@ RSpec.describe Canvas::Embed do
29
35
  decrypted_message = secret_box.decrypt(nonce, message)
30
36
  context = JSON.parse(decrypted_message)
31
37
 
32
- expect(context["scopes"]["team"]).to eq("canvas")
38
+ expect(context['scopes']['team']).to eq('canvas')
39
+ expect(context['exp']).to eq(Time.now.to_i + 3600)
40
+ expect(context['userId']).to eq(nil)
41
+ end
42
+
43
+ it 'rejects non-hash' do
44
+ key = RbNaCl::Random.random_bytes(RbNaCl::SecretBox.key_bytes)
45
+ unpacked_key = "emk_ZRzQbE9d.#{key.unpack1('H*')}"
46
+ original_message = "'team': 'canvas'"
47
+ expect { Canvas::Embed.generate_token(unpacked_key, original_message) }.to raise_error(Canvas::InvalidScopeError)
48
+ end
49
+
50
+ it 'accepts custom expiration and userId' do
51
+ key = RbNaCl::Random.random_bytes(RbNaCl::SecretBox.key_bytes)
52
+ unpacked_key = "emk_ZRzQbE9d.#{key.unpack1('H*')}"
53
+ original_message = { 'team' => 'canvas' }
54
+ token = Canvas::Embed.generate_token(unpacked_key, original_message, 7200, "cus_abc123")
55
+ expect(token).not_to be nil
56
+ decoded = JSON.parse(Base64.decode64(token))
57
+ expect(decoded['keyId']).to eq('emk_ZRzQbE9d')
58
+
59
+ message_hex = decoded['message']
60
+ nonce_hex = decoded['nonce']
61
+ nonce = [nonce_hex].pack('H*')
62
+ message = [message_hex].pack('H*')
63
+
64
+ secret_box = RbNaCl::SecretBox.new(key)
65
+ decrypted_message = secret_box.decrypt(nonce, message)
66
+ context = JSON.parse(decrypted_message)
67
+
68
+ expect(context['exp']).to eq(Time.now.to_i + 7200)
69
+ expect(context['userId']).to eq("cus_abc123")
33
70
  end
34
71
  end
data/spec/spec_helper.rb CHANGED
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "canvas/embed"
3
+ # require 'canvas-embed'
4
4
 
5
5
  RSpec.configure do |config|
6
6
  # Enable flags like --only-failures and --next-failure
7
- config.example_status_persistence_file_path = ".rspec_status"
7
+ config.example_status_persistence_file_path = '.rspec_status'
8
8
 
9
9
  # Disable RSpec exposing methods globally on `Module` and `main`
10
10
  config.disable_monkey_patching!
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: canvas-embed
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Will Pride
@@ -63,7 +63,7 @@ extra_rdoc_files: []
63
63
  files:
64
64
  - Gemfile
65
65
  - Gemfile.lock
66
- - LICENSE.txt
66
+ - LICENSE
67
67
  - README.md
68
68
  - Rakefile
69
69
  - bin/console
@@ -93,6 +93,8 @@ files:
93
93
  - example/config/storage.yml
94
94
  - example/log/development.log
95
95
  - example/storage/development.sqlite3
96
+ - example/storage/development.sqlite3-shm
97
+ - example/storage/development.sqlite3-wal
96
98
  - example/tmp/cache/bootsnap/compile-cache-iseq/00/47f4f1a378e4f3
97
99
  - example/tmp/cache/bootsnap/compile-cache-iseq/00/adaa3c79791768
98
100
  - example/tmp/cache/bootsnap/compile-cache-iseq/00/aea96502258cc3
@@ -177,6 +179,7 @@ files:
177
179
  - example/tmp/cache/bootsnap/compile-cache-iseq/0a/469b451850b921
178
180
  - example/tmp/cache/bootsnap/compile-cache-iseq/0a/4bda7fdbdf4107
179
181
  - example/tmp/cache/bootsnap/compile-cache-iseq/0a/6390c610b26fd1
182
+ - example/tmp/cache/bootsnap/compile-cache-iseq/0a/707c6e76401f50
180
183
  - example/tmp/cache/bootsnap/compile-cache-iseq/0a/e615070628fb80
181
184
  - example/tmp/cache/bootsnap/compile-cache-iseq/0a/ee6e39a5d73ccf
182
185
  - example/tmp/cache/bootsnap/compile-cache-iseq/0b/0c487dfdaa3d16
@@ -366,6 +369,7 @@ files:
366
369
  - example/tmp/cache/bootsnap/compile-cache-iseq/23/cff797bfa51532
367
370
  - example/tmp/cache/bootsnap/compile-cache-iseq/23/d026b0f79546cb
368
371
  - example/tmp/cache/bootsnap/compile-cache-iseq/24/1c7af6e85f5ccd
372
+ - example/tmp/cache/bootsnap/compile-cache-iseq/24/4134f174d68291
369
373
  - example/tmp/cache/bootsnap/compile-cache-iseq/24/b6fd6db603b55d
370
374
  - example/tmp/cache/bootsnap/compile-cache-iseq/24/bb7c22318dc3d8
371
375
  - example/tmp/cache/bootsnap/compile-cache-iseq/24/bfa8386af50833
@@ -544,6 +548,7 @@ files:
544
548
  - example/tmp/cache/bootsnap/compile-cache-iseq/3b/10260e9f0aef7e
545
549
  - example/tmp/cache/bootsnap/compile-cache-iseq/3b/1e1db1be0438eb
546
550
  - example/tmp/cache/bootsnap/compile-cache-iseq/3b/285adc4394d621
551
+ - example/tmp/cache/bootsnap/compile-cache-iseq/3b/64ba0c52596b87
547
552
  - example/tmp/cache/bootsnap/compile-cache-iseq/3b/7bf86a11746de3
548
553
  - example/tmp/cache/bootsnap/compile-cache-iseq/3b/8087c8f02e8681
549
554
  - example/tmp/cache/bootsnap/compile-cache-iseq/3b/b54befdbc84d77
@@ -667,6 +672,7 @@ files:
667
672
  - example/tmp/cache/bootsnap/compile-cache-iseq/4c/383a38512e4570
668
673
  - example/tmp/cache/bootsnap/compile-cache-iseq/4c/43df3e4a240376
669
674
  - example/tmp/cache/bootsnap/compile-cache-iseq/4c/55eb24b65029b7
675
+ - example/tmp/cache/bootsnap/compile-cache-iseq/4c/837c8a08879a20
670
676
  - example/tmp/cache/bootsnap/compile-cache-iseq/4c/907dbc57f42021
671
677
  - example/tmp/cache/bootsnap/compile-cache-iseq/4c/97a9746f58838d
672
678
  - example/tmp/cache/bootsnap/compile-cache-iseq/4c/c54dd9153acccd
@@ -866,6 +872,7 @@ files:
866
872
  - example/tmp/cache/bootsnap/compile-cache-iseq/68/26225d919a195a
867
873
  - example/tmp/cache/bootsnap/compile-cache-iseq/68/6e8af278257638
868
874
  - example/tmp/cache/bootsnap/compile-cache-iseq/68/9dcbc323ffed61
875
+ - example/tmp/cache/bootsnap/compile-cache-iseq/68/a73620fd7d4284
869
876
  - example/tmp/cache/bootsnap/compile-cache-iseq/68/f1cdd7e9091e18
870
877
  - example/tmp/cache/bootsnap/compile-cache-iseq/68/fbf396ca9e546a
871
878
  - example/tmp/cache/bootsnap/compile-cache-iseq/69/520a93dbc5821a
@@ -973,6 +980,7 @@ files:
973
980
  - example/tmp/cache/bootsnap/compile-cache-iseq/78/c5842b4437cdde
974
981
  - example/tmp/cache/bootsnap/compile-cache-iseq/78/d30c508cda8b38
975
982
  - example/tmp/cache/bootsnap/compile-cache-iseq/78/f61c15c553c783
983
+ - example/tmp/cache/bootsnap/compile-cache-iseq/79/1a2ebed1814cfa
976
984
  - example/tmp/cache/bootsnap/compile-cache-iseq/79/342a608d5d1515
977
985
  - example/tmp/cache/bootsnap/compile-cache-iseq/79/4ede59d3f0b51a
978
986
  - example/tmp/cache/bootsnap/compile-cache-iseq/79/73a9af77a7eca3
@@ -1408,6 +1416,7 @@ files:
1408
1416
  - example/tmp/cache/bootsnap/compile-cache-iseq/b2/7702d3d8709e10
1409
1417
  - example/tmp/cache/bootsnap/compile-cache-iseq/b2/bcfc7147918ee9
1410
1418
  - example/tmp/cache/bootsnap/compile-cache-iseq/b2/c3b52c4862931e
1419
+ - example/tmp/cache/bootsnap/compile-cache-iseq/b2/d451a148d6d303
1411
1420
  - example/tmp/cache/bootsnap/compile-cache-iseq/b3/08f99db6a2f454
1412
1421
  - example/tmp/cache/bootsnap/compile-cache-iseq/b3/2928ee05dee54a
1413
1422
  - example/tmp/cache/bootsnap/compile-cache-iseq/b3/5e2f2005224107
@@ -1683,6 +1692,7 @@ files:
1683
1692
  - example/tmp/cache/bootsnap/compile-cache-iseq/d8/7cf9cd6b97ce3e
1684
1693
  - example/tmp/cache/bootsnap/compile-cache-iseq/d8/8e9b55d3c27632
1685
1694
  - example/tmp/cache/bootsnap/compile-cache-iseq/d8/d4be612eb65cc7
1695
+ - example/tmp/cache/bootsnap/compile-cache-iseq/d8/fff5874ac0039e
1686
1696
  - example/tmp/cache/bootsnap/compile-cache-iseq/d9/00dae66024922b
1687
1697
  - example/tmp/cache/bootsnap/compile-cache-iseq/d9/4752982ff62455
1688
1698
  - example/tmp/cache/bootsnap/compile-cache-iseq/d9/57d00f31fb9cc2
@@ -1858,6 +1868,7 @@ files:
1858
1868
  - example/tmp/cache/bootsnap/compile-cache-iseq/ed/cf94cbf291244d
1859
1869
  - example/tmp/cache/bootsnap/compile-cache-iseq/ed/fdc17978ec173c
1860
1870
  - example/tmp/cache/bootsnap/compile-cache-iseq/ee/013dff52fec7ae
1871
+ - example/tmp/cache/bootsnap/compile-cache-iseq/ee/02ad963145723a
1861
1872
  - example/tmp/cache/bootsnap/compile-cache-iseq/ee/79e36bad363cee
1862
1873
  - example/tmp/cache/bootsnap/compile-cache-iseq/ee/7def47930ad4ae
1863
1874
  - example/tmp/cache/bootsnap/compile-cache-iseq/ee/b7c1a394a98742
@@ -1960,6 +1971,7 @@ files:
1960
1971
  - example/tmp/cache/bootsnap/compile-cache-iseq/fb/12c8ce722adb93
1961
1972
  - example/tmp/cache/bootsnap/compile-cache-iseq/fb/379879ab6ff445
1962
1973
  - example/tmp/cache/bootsnap/compile-cache-iseq/fb/4da182e3c59f07
1974
+ - example/tmp/cache/bootsnap/compile-cache-iseq/fb/4f5abf058e6979
1963
1975
  - example/tmp/cache/bootsnap/compile-cache-iseq/fb/783d0d4c99556c
1964
1976
  - example/tmp/cache/bootsnap/compile-cache-iseq/fb/85b0151933145c
1965
1977
  - example/tmp/cache/bootsnap/compile-cache-iseq/fb/bd0386cf2acdec
@@ -1998,11 +2010,11 @@ files:
1998
2010
  - example/tmp/cache/bootsnap/compile-cache-yaml/dc/10ca6b40828fd3
1999
2011
  - example/tmp/cache/bootsnap/load-path-cache
2000
2012
  - example/tmp/local_secret.txt
2013
+ - example/tmp/pids/server.pid
2001
2014
  - example/tmp/restart.txt
2002
2015
  - lib/canvas/embed.rb
2003
2016
  - lib/canvas/embed/version.rb
2004
- - pkg/canvas-embed-0.1.2.gem
2005
- - pkg/canvas-embed-0.1.3.gem
2017
+ - pkg/canvas-embed-0.1.5.gem
2006
2018
  - sig/canvas/embed.rbs
2007
2019
  - spec/canvas/embed_spec.rb
2008
2020
  - spec/spec_helper.rb
@@ -2012,6 +2024,7 @@ licenses:
2012
2024
  metadata:
2013
2025
  homepage_uri: https://canvasapp.com
2014
2026
  source_code_uri: https://github.com/canvas/embeds
2027
+ bug_tracker_uri: https://github.com/canvas/embeds/issues
2015
2028
  post_install_message:
2016
2029
  rdoc_options: []
2017
2030
  require_paths:
Binary file
Binary file