xmlenc 0.3.0 → 0.4.0

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
  SHA1:
3
- metadata.gz: 869074b32ca2144be9a0c5b4344e2a962b067096
4
- data.tar.gz: e33dd060461d348bec6c2160ae83c7997341e572
3
+ metadata.gz: 8dc37fb9c2a756e30492c8713cadbbd5757db889
4
+ data.tar.gz: 2217523b1fca27126ee8eb4cee15d95343c3c27f
5
5
  SHA512:
6
- metadata.gz: 3216e9fef6a70ed78735a8cc6587dd22e1a1c8ced0d6ef21ec125203e0778f2379374a3bbfa9a2f9b4ed962cb49146fd71d319f42ceda0f2e4319e2284b83cc6
7
- data.tar.gz: 7e80bcaaa65527a1bea1b932ee159bcd225c5335bb58601239e8de4aa64ed6993c90987a9c884d60fc90292253f98da1cebf979fc45c36a9a7d1cd130e2d26b6
6
+ metadata.gz: 5e3395a8f9d927b54334c5ce9fc981e54956708fff72f5b6ce05cfd0ed46b481eef7753bbd0e12ab56e74828ce8b55d3084e11206abe015c211fdee81fcdb4e7
7
+ data.tar.gz: 84b10748b719e2f0a844d83249cfe4da5cbb59304daeb523e27bfa8a5035c698dcea445a946e26d76d5f442937f5ac4678fc06e729de785bac9791230750f36b
@@ -27,6 +27,7 @@ module Xmlenc
27
27
  autoload :DigestMethod, 'xmlenc/builder/digest_method'
28
28
  autoload :ReferenceList, 'xmlenc/builder/reference_list'
29
29
  autoload :DataReference, 'xmlenc/builder/data_reference'
30
+ autoload :RetrievalMethod, 'xmlenc/builder/retrieval_method'
30
31
 
31
32
  module ComplexTypes
32
33
  autoload :EncryptedType, 'xmlenc/builder/complex_types/encrypted_type'
@@ -23,21 +23,33 @@ module Xmlenc
23
23
  'http://www.w3.org/2001/04/xmlenc#Element'
24
24
  end
25
25
 
26
- def initialize(attributes = {})
27
- super
28
- self.id = SecureRandom.hex(5)
26
+ def initialize(*args)
27
+ options = args.extract_options!
28
+ if options.key?(:id)
29
+ self.id = options.delete(:id)
30
+ else
31
+ self.id = SecureRandom.hex(5)
32
+ end
33
+ super(*(args << options))
29
34
  end
30
35
 
31
- def encrypt(data)
36
+ def encrypt(data, key_options = {})
32
37
  encryptor = algorithm.setup
33
38
  encrypted = encryptor.encrypt(data, :node => encryption_method)
34
39
  cipher_data.cipher_value = Base64.encode64(encrypted)
35
-
36
- encrypted_key = EncryptedKey.new(:data => encryptor.key)
40
+ key_params = { :data => encryptor.key }
41
+ encrypted_key = EncryptedKey.new(key_params.merge(key_options))
37
42
  encrypted_key.add_data_reference(id)
38
43
  encrypted_key
39
44
  end
40
45
 
46
+ def set_key_retrieval_method(retrieval_method)
47
+ if retrieval_method
48
+ self.key_info ||= KeyInfo.new
49
+ self.key_info.retrieval_method = retrieval_method
50
+ end
51
+ end
52
+
41
53
  private
42
54
 
43
55
  def algorithm
@@ -11,6 +11,9 @@ module Xmlenc
11
11
  tag "EncryptedKey"
12
12
  namespace "xenc"
13
13
 
14
+ attribute :id, String, tag: 'Id'
15
+ attribute :recipient, String, tag: 'Recipient'
16
+
14
17
  has_one :reference_list, Xmlenc::Builder::ReferenceList, :xpath => "./"
15
18
 
16
19
  attr_accessor :data
@@ -26,6 +29,13 @@ module Xmlenc
26
29
  self.reference_list.add_data_reference(data_id)
27
30
  end
28
31
 
32
+ def initialize(*args)
33
+ options = args.extract_options!
34
+ @recipient = options.delete(:recipient)
35
+ @id = options.delete(:id)
36
+ super(*(args << options))
37
+ end
38
+
29
39
  private
30
40
 
31
41
  def algorithm
@@ -15,7 +15,9 @@ module Xmlenc
15
15
 
16
16
  def initialize(attributes = {})
17
17
  digest_method_algorithm = attributes.delete(:digest_method_algorithm)
18
- attributes[:digest_method] = Xmlenc::Builder::DigestMethod.new(:algorithm => digest_method_algorithm)
18
+ if digest_method_algorithm
19
+ attributes[:digest_method] = Xmlenc::Builder::DigestMethod.new(:algorithm => digest_method_algorithm)
20
+ end
19
21
  super
20
22
  end
21
23
  end
@@ -9,6 +9,7 @@ module Xmlenc
9
9
  namespace "ds"
10
10
 
11
11
  element :key_name, String, :namespace => "ds", :tag => "KeyName"
12
+ has_many :retrieval_method, Xmlenc::Builder::RetrievalMethod, :tag => "RetrievalMethod"
12
13
 
13
14
  has_one :encrypted_key, Xmlenc::Builder::EncryptedKey, :xpath => "./"
14
15
  end
@@ -0,0 +1,17 @@
1
+ module Xmlenc
2
+ module Builder
3
+ class RetrievalMethod
4
+ include Xmlenc::Builder::Base
5
+
6
+ tag "RetrievalMethod"
7
+
8
+ attribute :type, String, :tag => "Type"
9
+ attribute :uri, String, :tag => "URI"
10
+
11
+ def type
12
+ 'http://www.w3.org/2001/04/xmlenc#EncryptedKey'
13
+ end
14
+ end
15
+ end
16
+ end
17
+
@@ -1,3 +1,3 @@
1
1
  module Xmlenc
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -71,4 +71,57 @@ describe Xmlenc::Builder::EncryptedData do
71
71
  end
72
72
  end
73
73
  end
74
+
75
+ describe "#initialize" do
76
+ it 'sets a default #id' do
77
+ expect(described_class.new().id).to be_a String
78
+ end
79
+
80
+ it 'sets #id to specified id' do
81
+ expect(described_class.new(id: 'TEST').id).to eq 'TEST'
82
+ end
83
+ end
84
+
85
+ describe "#encrypt" do
86
+ subject { described_class.new() }
87
+
88
+ before { subject.set_encryption_method(algorithm: 'http://www.w3.org/2001/04/xmlenc#aes256-cbc') }
89
+
90
+ it 'returns an EncryptedKey' do
91
+ expect(subject.encrypt('TEST')).to be_a Xmlenc::Builder::EncryptedKey
92
+ end
93
+
94
+ context "extra key_options are passed" do
95
+ let(:key_options) { { :id => '_SOME_ID', :recipient => 'SOME_RECIPIENT' } }
96
+
97
+ before do
98
+ subject.set_encryption_method(algorithm: 'http://www.w3.org/2001/04/xmlenc#aes256-cbc')
99
+ allow_message_expectations_on_nil
100
+ allow(nil).to receive(:add_data_reference)
101
+ end
102
+
103
+ it 'and then used to create the EncryptedKey' do
104
+ expect(Xmlenc::Builder::EncryptedKey).to receive(:new).with(hash_including(key_options))
105
+ subject.encrypt('TEST', key_options)
106
+ end
107
+ end
108
+ end
109
+
110
+ describe "#set_key_retrieval_method" do
111
+ it "sets the key info with the key name" do
112
+ subject.set_key_retrieval_method 'retrieval_method'
113
+ expect(subject.key_info.retrieval_method).to eq "retrieval_method"
114
+ end
115
+
116
+ it "does not override old key info data" do
117
+ subject.set_key_retrieval_method("key retrieval_method")
118
+ expect(subject.key_info.encrypted_key).not_to be_nil
119
+ end
120
+
121
+ it "does not set the key info element if the key retrieval_method is nil" do
122
+ subject.key_info = nil
123
+ subject.set_key_retrieval_method(nil)
124
+ expect(subject.key_info).to be_nil
125
+ end
126
+ end
74
127
  end
@@ -6,19 +6,21 @@ describe Xmlenc::Builder::EncryptedKey do
6
6
  subject { described_class.parse(xml, :single => true) }
7
7
 
8
8
  describe "required fields" do
9
- it "should have the cipher data field" do
10
- expect(subject).to respond_to :cipher_data
11
- end
9
+ [:cipher_data].each do |field|
10
+ it "should have the #{field} field" do
11
+ expect(subject).to respond_to :cipher_data
12
+ end
12
13
 
13
- it "should check the presence of cipher data" do
14
- subject.cipher_data = nil
15
- expect(subject).to_not be_valid
16
- expect(subject.errors[:cipher_data].size).to eq(1)
14
+ it "should check the presence of #{field}" do
15
+ subject.cipher_data = nil
16
+ expect(subject).to_not be_valid
17
+ expect(subject.errors[:cipher_data].size).to eq(1)
18
+ end
17
19
  end
18
20
  end
19
21
 
20
22
  describe "optional fields" do
21
- [:encryption_method, :key_info].each do |field|
23
+ [:id, :recipient, :encryption_method, :key_info].each do |field|
22
24
  it "should have the #{field} field" do
23
25
  expect(subject).to respond_to field
24
26
  end
@@ -72,6 +74,12 @@ describe Xmlenc::Builder::EncryptedKey do
72
74
  end
73
75
  end
74
76
 
77
+ describe "#encrypt" do
78
+ it "has method" do
79
+ expect(subject).to respond_to :encrypt
80
+ end
81
+ end
82
+
75
83
  describe "#add_data_reference" do
76
84
  it "has method" do
77
85
  expect(subject).to respond_to :add_data_reference
@@ -87,9 +95,18 @@ describe Xmlenc::Builder::EncryptedKey do
87
95
  end
88
96
  end
89
97
 
90
- describe "#encrypt" do
91
- it "has method" do
92
- expect(subject).to respond_to :encrypt
98
+ describe "#initialize" do
99
+ it 'initializes an EncryptedKey' do
100
+ expect(described_class.new()).to be_a described_class
101
+ end
102
+
103
+ context 'with extra options' do
104
+ subject { described_class.new(id: 'AN_ID', recipient: 'A_RECIPIENT') }
105
+
106
+ it 'sets @recipient and @id' do
107
+ expect(subject.id).to eq 'AN_ID'
108
+ expect(subject.recipient).to eq 'A_RECIPIENT'
109
+ end
93
110
  end
94
111
  end
95
112
  end
@@ -27,4 +27,20 @@ describe Xmlenc::Builder::EncryptionMethod do
27
27
  end
28
28
  end
29
29
 
30
+ describe "#digest_method" do
31
+ subject { described_class.new() }
32
+
33
+ it 'has an empty digest_method' do
34
+ expect(subject.digest_method).to eq nil
35
+ end
36
+
37
+ context "digest_method_algorithm given" do
38
+ subject { described_class.new(digest_method_algorithm: 'ALGO') }
39
+
40
+ it 'has no empty digest_method' do
41
+ expect(subject.digest_method).not_to eq nil
42
+ end
43
+ end
44
+ end
45
+
30
46
  end
@@ -5,6 +5,21 @@ describe Xmlenc::Builder::KeyInfo do
5
5
  let(:xml) { File.read File.join("spec", "fixtures", "encrypted_document.xml") }
6
6
  subject { described_class.parse(xml) }
7
7
 
8
+ describe "optional fields" do
9
+ subject { described_class.new }
10
+
11
+ [:key_name, :retrieval_method, :encrypted_key].each do |field|
12
+ it "should have the #{field} field" do
13
+ expect(subject).to respond_to field
14
+ end
15
+
16
+ it "should allow #{field} to be blank" do
17
+ subject.send("#{field}=", nil)
18
+ expect(subject).to be_valid
19
+ end
20
+ end
21
+ end
22
+
8
23
  describe "#parse" do
9
24
  it "should create two KeyInfo elements" do
10
25
  subject.each do |element|
@@ -0,0 +1,22 @@
1
+ require "spec_helper"
2
+
3
+ describe Xmlenc::Builder::RetrievalMethod do
4
+
5
+ let(:xml) { File.read File.join("spec", "fixtures", "encrypted_document.xml") }
6
+ subject { described_class.parse(xml) }
7
+
8
+ describe "optional fields" do
9
+ subject { described_class.new }
10
+
11
+ [:type, :uri].each do |field|
12
+ it "should have the #{field} field" do
13
+ expect(subject).to respond_to field
14
+ end
15
+
16
+ it "should allow #{field} to be blank" do
17
+ subject.send("#{field}=", nil)
18
+ expect(subject).to be_valid
19
+ end
20
+ end
21
+ end
22
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xmlenc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benoist
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-10-12 00:00:00.000000000 Z
11
+ date: 2015-10-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -136,6 +136,7 @@ files:
136
136
  - lib/xmlenc/builder/encryption_method.rb
137
137
  - lib/xmlenc/builder/key_info.rb
138
138
  - lib/xmlenc/builder/reference_list.rb
139
+ - lib/xmlenc/builder/retrieval_method.rb
139
140
  - lib/xmlenc/encrypted_data.rb
140
141
  - lib/xmlenc/encrypted_document.rb
141
142
  - lib/xmlenc/encrypted_key.rb
@@ -169,6 +170,7 @@ files:
169
170
  - spec/lib/xmlenc/builder/encryption_method_spec.rb
170
171
  - spec/lib/xmlenc/builder/key_info_spec.rb
171
172
  - spec/lib/xmlenc/builder/reference_list_spec.rb
173
+ - spec/lib/xmlenc/builder/retrieval_method_spec.rb
172
174
  - spec/lib/xmlenc/encrypted_data_spec.rb
173
175
  - spec/lib/xmlenc/encrypted_document_spec.rb
174
176
  - spec/lib/xmlenc/encrypted_key_spec.rb
@@ -230,6 +232,7 @@ test_files:
230
232
  - spec/lib/xmlenc/builder/encryption_method_spec.rb
231
233
  - spec/lib/xmlenc/builder/key_info_spec.rb
232
234
  - spec/lib/xmlenc/builder/reference_list_spec.rb
235
+ - spec/lib/xmlenc/builder/retrieval_method_spec.rb
233
236
  - spec/lib/xmlenc/encrypted_data_spec.rb
234
237
  - spec/lib/xmlenc/encrypted_document_spec.rb
235
238
  - spec/lib/xmlenc/encrypted_key_spec.rb