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 Dec 2, 2014
1 parent 49554c8 commit 1daacf9
Show file tree
Hide file tree
Showing 4 changed files with 114 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>
37 changes: 37 additions & 0 deletions spec/fixtures/akami/wsse/signature/cert.pem
Original file line number Diff line number Diff line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JHz8jRDeA+
jnvaC9Y//AoRr/WGKKU3FY40J7pQKcQNczGUzCS+ag0IO64agTs=
-----END CERTIFICATE-----
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,A0BF079E41C9A4C1

rEO5mWOWbnr8uyjtfIRwW9wRo2/y1wMON2c605BkgaNeAKH/++1UC9qEhNtHfE2Q
omQkXiHfV6yt3l3dWPcnA9ToT9PRNLyoRMMC+Ds5EuEk4ypihQ8SS2xt3wqCRkwm
3/AyrRZXUnBTVzZ9D35lz1HCFfJ61UpOMN9XOZy4yUqDZVG/ybMhXilfNKgTr8xJ
StTb7ruwV/8xTiEgmcxGKJbOcDKqwsd/mW3VbDN6zOqYC0TlFc/eiCbJxMd35l6a
oRfHWHbabumcfZplqoDcOwYpRb8ZiDbu74RrDogKnpH7nnJrp8jSYueCiXGDWGFG
ZXB6f2FhsbrLEM87L8cxJO6Hk6+c39qgiP2aRICeKjUNKPOfSiPr4yP4/g3tFY2z
sI9xur3nAXoqxXBrEkEdvXiH35UfJpqOTxbAN+Ozxnj1QeLWDDE8nkMW/XRRxdCk
jeUwR5PS3sM2S/fIn7rThQSKRMxznK//cEUsMwz514HekxnKyj8rZUETcFFySxmz
lmOzDOaPNejDOgNSrlT+IDqE9YlMxLObv5l960QOjk0qBaO8e33HkgfBqENzq7Uv
Wrj3FyBlNz4vfhCZHBY0+rSgdZHM64DV0HybE7yc+mMUwrV7ish1bGb/Bek/4JF+
lrpRuxTEQFGNTGFYa0C5zDpz9UtiSUs2X4EX74GfxCUmn7kNND+4OVrndZiEONPz
/6QffS7YG1UtahDpDzI1wOCYD9Iwg/HnmTWMcXj2Yw7jRvlG0sArw/5dNJkrmuJY
6PvFoEeZSF4qrUCDlw/BUJMaGDlhSCyTWulZZU+wWuJRTWCiDxYO1g==
-----END RSA PRIVATE KEY-----

0 comments on commit 1daacf9

Please sign in to comment.