canvas-embed 0.1.4 → 0.1.5

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 (45) 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 +5 -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 +10 -2667
  28. data/example/tmp/cache/bootsnap/compile-cache-iseq/01/2f863f8d7ff70d +0 -0
  29. data/example/tmp/cache/bootsnap/compile-cache-iseq/0a/707c6e76401f50 +0 -0
  30. data/example/tmp/cache/bootsnap/compile-cache-iseq/24/4134f174d68291 +0 -0
  31. data/example/tmp/cache/bootsnap/compile-cache-iseq/3b/64ba0c52596b87 +0 -0
  32. data/example/tmp/cache/bootsnap/compile-cache-iseq/4c/837c8a08879a20 +0 -0
  33. data/example/tmp/cache/bootsnap/compile-cache-iseq/68/a73620fd7d4284 +0 -0
  34. data/example/tmp/cache/bootsnap/compile-cache-iseq/79/1a2ebed1814cfa +0 -0
  35. data/example/tmp/cache/bootsnap/compile-cache-iseq/ee/02ad963145723a +0 -0
  36. data/example/tmp/cache/bootsnap/compile-cache-iseq/fb/4f5abf058e6979 +0 -0
  37. data/example/tmp/cache/bootsnap/load-path-cache +0 -0
  38. data/example/tmp/pids/server.pid +1 -0
  39. data/lib/canvas/embed/version.rb +1 -1
  40. data/lib/canvas/embed.rb +27 -11
  41. data/spec/canvas/embed_spec.rb +44 -7
  42. data/spec/spec_helper.rb +2 -2
  43. metadata +12 -4
  44. data/pkg/canvas-embed-0.1.2.gem +0 -0
  45. data/pkg/canvas-embed-0.1.3.gem +0 -0
@@ -0,0 +1 @@
1
+ 32082
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Canvas
4
4
  module Embed
5
- VERSION = "0.1.4"
5
+ VERSION = '0.1.5'
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
- module Embed
10
- extend self
11
- def generate_token(private_key, scopes)
9
+ class InvalidScopeError < StandardError
10
+ end
11
+ module Embeds
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
@@ -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::Embeds.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::Embeds.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::Embeds.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.5
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
@@ -177,6 +177,7 @@ files:
177
177
  - example/tmp/cache/bootsnap/compile-cache-iseq/0a/469b451850b921
178
178
  - example/tmp/cache/bootsnap/compile-cache-iseq/0a/4bda7fdbdf4107
179
179
  - example/tmp/cache/bootsnap/compile-cache-iseq/0a/6390c610b26fd1
180
+ - example/tmp/cache/bootsnap/compile-cache-iseq/0a/707c6e76401f50
180
181
  - example/tmp/cache/bootsnap/compile-cache-iseq/0a/e615070628fb80
181
182
  - example/tmp/cache/bootsnap/compile-cache-iseq/0a/ee6e39a5d73ccf
182
183
  - example/tmp/cache/bootsnap/compile-cache-iseq/0b/0c487dfdaa3d16
@@ -366,6 +367,7 @@ files:
366
367
  - example/tmp/cache/bootsnap/compile-cache-iseq/23/cff797bfa51532
367
368
  - example/tmp/cache/bootsnap/compile-cache-iseq/23/d026b0f79546cb
368
369
  - example/tmp/cache/bootsnap/compile-cache-iseq/24/1c7af6e85f5ccd
370
+ - example/tmp/cache/bootsnap/compile-cache-iseq/24/4134f174d68291
369
371
  - example/tmp/cache/bootsnap/compile-cache-iseq/24/b6fd6db603b55d
370
372
  - example/tmp/cache/bootsnap/compile-cache-iseq/24/bb7c22318dc3d8
371
373
  - example/tmp/cache/bootsnap/compile-cache-iseq/24/bfa8386af50833
@@ -544,6 +546,7 @@ files:
544
546
  - example/tmp/cache/bootsnap/compile-cache-iseq/3b/10260e9f0aef7e
545
547
  - example/tmp/cache/bootsnap/compile-cache-iseq/3b/1e1db1be0438eb
546
548
  - example/tmp/cache/bootsnap/compile-cache-iseq/3b/285adc4394d621
549
+ - example/tmp/cache/bootsnap/compile-cache-iseq/3b/64ba0c52596b87
547
550
  - example/tmp/cache/bootsnap/compile-cache-iseq/3b/7bf86a11746de3
548
551
  - example/tmp/cache/bootsnap/compile-cache-iseq/3b/8087c8f02e8681
549
552
  - example/tmp/cache/bootsnap/compile-cache-iseq/3b/b54befdbc84d77
@@ -667,6 +670,7 @@ files:
667
670
  - example/tmp/cache/bootsnap/compile-cache-iseq/4c/383a38512e4570
668
671
  - example/tmp/cache/bootsnap/compile-cache-iseq/4c/43df3e4a240376
669
672
  - example/tmp/cache/bootsnap/compile-cache-iseq/4c/55eb24b65029b7
673
+ - example/tmp/cache/bootsnap/compile-cache-iseq/4c/837c8a08879a20
670
674
  - example/tmp/cache/bootsnap/compile-cache-iseq/4c/907dbc57f42021
671
675
  - example/tmp/cache/bootsnap/compile-cache-iseq/4c/97a9746f58838d
672
676
  - example/tmp/cache/bootsnap/compile-cache-iseq/4c/c54dd9153acccd
@@ -866,6 +870,7 @@ files:
866
870
  - example/tmp/cache/bootsnap/compile-cache-iseq/68/26225d919a195a
867
871
  - example/tmp/cache/bootsnap/compile-cache-iseq/68/6e8af278257638
868
872
  - example/tmp/cache/bootsnap/compile-cache-iseq/68/9dcbc323ffed61
873
+ - example/tmp/cache/bootsnap/compile-cache-iseq/68/a73620fd7d4284
869
874
  - example/tmp/cache/bootsnap/compile-cache-iseq/68/f1cdd7e9091e18
870
875
  - example/tmp/cache/bootsnap/compile-cache-iseq/68/fbf396ca9e546a
871
876
  - example/tmp/cache/bootsnap/compile-cache-iseq/69/520a93dbc5821a
@@ -973,6 +978,7 @@ files:
973
978
  - example/tmp/cache/bootsnap/compile-cache-iseq/78/c5842b4437cdde
974
979
  - example/tmp/cache/bootsnap/compile-cache-iseq/78/d30c508cda8b38
975
980
  - example/tmp/cache/bootsnap/compile-cache-iseq/78/f61c15c553c783
981
+ - example/tmp/cache/bootsnap/compile-cache-iseq/79/1a2ebed1814cfa
976
982
  - example/tmp/cache/bootsnap/compile-cache-iseq/79/342a608d5d1515
977
983
  - example/tmp/cache/bootsnap/compile-cache-iseq/79/4ede59d3f0b51a
978
984
  - example/tmp/cache/bootsnap/compile-cache-iseq/79/73a9af77a7eca3
@@ -1858,6 +1864,7 @@ files:
1858
1864
  - example/tmp/cache/bootsnap/compile-cache-iseq/ed/cf94cbf291244d
1859
1865
  - example/tmp/cache/bootsnap/compile-cache-iseq/ed/fdc17978ec173c
1860
1866
  - example/tmp/cache/bootsnap/compile-cache-iseq/ee/013dff52fec7ae
1867
+ - example/tmp/cache/bootsnap/compile-cache-iseq/ee/02ad963145723a
1861
1868
  - example/tmp/cache/bootsnap/compile-cache-iseq/ee/79e36bad363cee
1862
1869
  - example/tmp/cache/bootsnap/compile-cache-iseq/ee/7def47930ad4ae
1863
1870
  - example/tmp/cache/bootsnap/compile-cache-iseq/ee/b7c1a394a98742
@@ -1960,6 +1967,7 @@ files:
1960
1967
  - example/tmp/cache/bootsnap/compile-cache-iseq/fb/12c8ce722adb93
1961
1968
  - example/tmp/cache/bootsnap/compile-cache-iseq/fb/379879ab6ff445
1962
1969
  - example/tmp/cache/bootsnap/compile-cache-iseq/fb/4da182e3c59f07
1970
+ - example/tmp/cache/bootsnap/compile-cache-iseq/fb/4f5abf058e6979
1963
1971
  - example/tmp/cache/bootsnap/compile-cache-iseq/fb/783d0d4c99556c
1964
1972
  - example/tmp/cache/bootsnap/compile-cache-iseq/fb/85b0151933145c
1965
1973
  - example/tmp/cache/bootsnap/compile-cache-iseq/fb/bd0386cf2acdec
@@ -1998,11 +2006,10 @@ files:
1998
2006
  - example/tmp/cache/bootsnap/compile-cache-yaml/dc/10ca6b40828fd3
1999
2007
  - example/tmp/cache/bootsnap/load-path-cache
2000
2008
  - example/tmp/local_secret.txt
2009
+ - example/tmp/pids/server.pid
2001
2010
  - example/tmp/restart.txt
2002
2011
  - lib/canvas/embed.rb
2003
2012
  - lib/canvas/embed/version.rb
2004
- - pkg/canvas-embed-0.1.2.gem
2005
- - pkg/canvas-embed-0.1.3.gem
2006
2013
  - sig/canvas/embed.rbs
2007
2014
  - spec/canvas/embed_spec.rb
2008
2015
  - spec/spec_helper.rb
@@ -2012,6 +2019,7 @@ licenses:
2012
2019
  metadata:
2013
2020
  homepage_uri: https://canvasapp.com
2014
2021
  source_code_uri: https://github.com/canvas/embeds
2022
+ bug_tracker_uri: https://github.com/canvas/embeds/issues
2015
2023
  post_install_message:
2016
2024
  rdoc_options: []
2017
2025
  require_paths:
Binary file
Binary file