File: with_nimbus_jose_spec.rb

package info (click to toggle)
ruby-json-jwt 1.6.2-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 292 kB
  • sloc: ruby: 2,120; makefile: 3
file content (99 lines) | stat: -rw-r--r-- 3,105 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require 'spec_helper'

describe 'interop' do
  describe 'with Nimbus JOSE' do
    if NimbusSpecHelper.nimbus_available?
      context 'JWE' do
        let(:shared_key) { SecureRandom.hex 16 } # default shared key is too short
        let(:private_key_path) { der_file_path 'rsa/private_key' }

        describe 'encrypt!' do
          shared_examples_for :gcm_encryption do
            context 'when enc=A128GCM' do
              before { jwe.enc = :A128GCM }

              it 'should decryptable by Nimbus JOSE JWT' do
                jwe.encrypt! key
                NimbusJWE.decrypt(jwe, private_key_path).should == plain_text
              end
            end

            context 'when enc=A256GCM' do
              before { jwe.enc = :A256GCM }

              it 'should decryptable by Nimbus JOSE JWT' do
                jwe.encrypt! key
                NimbusJWE.decrypt(jwe, private_key_path).should == plain_text
              end
            end
          end

          shared_examples_for :cbc_encryption do
            context 'when enc=A128CBC-HS256' do
              before { jwe.enc = :'A128CBC-HS256' }

              it 'should decryptable by Nimbus JOSE JWT' do
                jwe.encrypt! key
                NimbusJWE.decrypt(jwe, private_key_path).should == plain_text
              end
            end

            context 'when enc=A256CBC-HS512' do
              before { jwe.enc = :'A256CBC-HS512' }

              it 'should decryptable by Nimbus JOSE JWT' do
                jwe.encrypt! key
                NimbusJWE.decrypt(jwe, private_key_path).should == plain_text
              end
            end
          end

          context 'when plaintext given' do
            let(:plain_text) { 'Hello World' }
            let(:jwe) { JSON::JWE.new plain_text }

            context 'when alg=RSA1_5' do
              let(:key) { public_key }
              before { jwe.alg = :'RSA1_5' }

              it_behaves_like :gcm_encryption if gcm_supported?
              it_behaves_like :cbc_encryption
            end

            context 'when alg=RSA-OAEP' do
              let(:key) { public_key }
              before { jwe.alg = :'RSA-OAEP' }

              it_behaves_like :gcm_encryption if gcm_supported?
              it_behaves_like :cbc_encryption
            end
          end

          context 'when jwt given' do
            let(:plain_text) { jwt.to_s }
            let(:jwt) { JSON::JWT.new(foo: :bar) }
            let(:jwe) { JSON::JWE.new jwt }

            context 'when alg=RSA-OAEP' do
              let(:key) { public_key }
              before { jwe.alg = :'RSA1_5' }

              it_behaves_like :gcm_encryption if gcm_supported?
              it_behaves_like :cbc_encryption
            end

            context 'when alg=RSA-OAEP' do
              let(:key) { public_key }
              before { jwe.alg = :'RSA-OAEP' }

              it_behaves_like :gcm_encryption if gcm_supported?
              it_behaves_like :cbc_encryption
            end
          end
        end
      end
    else
      skip 'Nimbus JOSE unavailable'
    end
  end
end