Skip to content

Commit

Permalink
Refactor Akami::WSSE to_xml.
Browse files Browse the repository at this point in the history
* Clean up ugly if-statement in to_xml.
* Ensure certificate and timestamp headers are not mutually exclusive.
* Add basic signature and signature with timestamp specs.
  • Loading branch information
drn committed Jan 20, 2016
1 parent cc5ab24 commit 36b26a3
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 16 deletions.
32 changes: 19 additions & 13 deletions lib/akami/wsse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,29 @@ def body_attributes

# Returns the XML for a WSSE header.
def to_xml
xml = hash

if signature? and signature.have_document?
Gyoku.xml wsse_signature.merge!(hash)
elsif username_token? && timestamp?
Gyoku.xml wsse_username_token.merge!(wsu_timestamp) {
|key, v1, v2| v1.merge!(v2) {
|key, v1, v2| v1.merge!(v2)
}
}
xml.merge!(wsse_signature)
elsif username_token?
Gyoku.xml wsse_username_token.merge!(hash)
elsif timestamp?
Gyoku.xml wsu_timestamp.merge!(hash)
else
""
xml.merge!(wsse_username_token)
end
end

if timestamp?
xml.merge!(wsu_timestamp) do |key, v1, v2|
v1.merge!(v2) do |key, v1, v2|
if v1.is_a?(Hash) && v2.is_a?(Hash)
v1.merge!(v2)
end
end
end
unless xml['wsse:Security'][:order!].nil?
xml['wsse:Security'][:order!] << 'wsu:Timestamp'
end
end

Gyoku.xml xml
end
private

# Returns a Hash containing wsse:UsernameToken details.
Expand Down
60 changes: 57 additions & 3 deletions spec/akami/wsse_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
)
end

it "contains the namespace for Base64 Encoding type" do
expect(Akami::WSSE::BASE64_URI).to eq(
it "contains the namespace for Base64 Encoding type" do
expect(Akami::WSSE::BASE64_URI).to eq(
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary"
)
end
Expand Down Expand Up @@ -243,7 +243,7 @@
end
end

context "whith credentials and timestamp" do
context "with credentials and timestamp" do
before do
wsse.credentials "username", "password"
wsse.timestamp = true
Expand All @@ -261,6 +261,60 @@
expect(wsse.to_xml).to include("username", "password")
end
end

context 'with signature' do
let(:fixtures_path) {
File.join(Bundler.root, 'spec', 'fixtures', 'akami', 'wsse', 'signature')
}
let(:xml) { fixture('akami/wsse/sample.xml') }
let(:cert_path) { File.join(fixtures_path, 'cert.pem') }
let(:signature) {
Akami::WSSE::Signature.new(
Akami::WSSE::Certs.new(
cert_file: cert_path,
private_key_file: cert_path,
private_key_password: 'password'
)
)
}

before do
signature.document = xml
wsse.signature = signature
end

it 'contains SignedInfo node' do
expect(wsse.to_xml).to include('SignedInfo')
end
it 'contains SignatureValue node' do
expect(wsse.to_xml).to include('SignatureValue')
end
it 'contains KeyInfo node' do
expect(wsse.to_xml).to include('KeyInfo')
end

context 'with timestamp' do
before do
wsse.timestamp = true
end
it 'contains SignedInfo node' do
expect(wsse.to_xml).to include('SignedInfo')
end
it 'contains SignatureValue node' do
expect(wsse.to_xml).to include('SignatureValue')
end
it 'contains KeyInfo node' do
expect(wsse.to_xml).to include('KeyInfo')
end
it "contains a wsu:Created node" do
expect(wsse.to_xml).to include("<wsu:Created>")
end
it "contains a wsu:Expires node" do
expect(wsse.to_xml).to include("<wsu:Expires>")
end
end
end

end

end
1 change: 1 addition & 0 deletions spec/fixtures/akami/wsse/sample.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version="1.0" encoding="UTF-8"?><env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="https://api.example.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><env:Body xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="Body-69182b6fd3cdbaa69e00e0bca6c58f6aee1e0c27"><tns:echo>Example</tns:echo></env:Body></env:Envelope>

0 comments on commit 36b26a3

Please sign in to comment.