-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathwsse_spec.rb
365 lines (298 loc) · 11.9 KB
/
wsse_spec.rb
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
require 'spec_helper'
require 'base64'
require 'nokogiri'
describe Akami do
let(:wsse) { Akami.wsse }
it "contains the namespace for WS Security Secext" do
expect(Akami::WSSE::WSE_NAMESPACE).to eq(
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
)
end
it "contains the namespace for WS Security Utility" do
expect(Akami::WSSE::WSU_NAMESPACE).to eq(
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
)
end
it "contains the namespace for the PasswordText type" do
expect(Akami::WSSE::PASSWORD_TEXT_URI).to eq(
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"
)
end
it "contains the namespace for the PasswordDigest type" do
expect(Akami::WSSE::PASSWORD_DIGEST_URI).to eq(
"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest"
)
end
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
describe "#credentials" do
it "sets the username" do
wsse.credentials "username", "password"
expect(wsse.username).to eq("username")
end
it "sets the password" do
wsse.credentials "username", "password"
expect(wsse.password).to eq("password")
end
it "defaults to set digest to false" do
wsse.credentials "username", "password"
expect(wsse).not_to be_digest
end
it "sets digest to true if specified" do
wsse.credentials "username", "password", :digest
expect(wsse).to be_digest
end
end
describe "#username" do
it "sets the username" do
wsse.username = "username"
expect(wsse.username).to eq("username")
end
end
describe "#password" do
it "sets the password" do
wsse.password = "password"
expect(wsse.password).to eq("password")
end
end
describe "#digest" do
it "defaults to false" do
expect(wsse).not_to be_digest
end
it "specifies whether to use digest auth" do
wsse.digest = true
expect(wsse).to be_digest
end
end
describe "#to_xml" do
context "with no credentials" do
it "returns an empty String" do
expect(wsse.to_xml).to eq("")
end
end
context "with only a username" do
before { wsse.username = "username" }
it "returns an empty String" do
expect(wsse.to_xml).to eq("")
end
end
context "with only a password" do
before { wsse.password = "password" }
it "returns an empty String" do
expect(wsse.to_xml).to eq("")
end
end
context "with credentials" do
before { wsse.credentials "username", "password" }
it "contains a wsse:Security tag" do
namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
expect(wsse.to_xml).to include("<wsse:Security xmlns:wsse=\"#{namespace}\">")
end
it "contains a wsu:Id attribute" do
expect(wsse.to_xml).to include('<wsse:UsernameToken wsu:Id="UsernameToken-1"')
end
it "increments the wsu:Id attribute count" do
expect(wsse.to_xml).to include('<wsse:UsernameToken wsu:Id="UsernameToken-1"')
expect(wsse.to_xml).to include('<wsse:UsernameToken wsu:Id="UsernameToken-2"')
end
it "contains the WSE and WSU namespaces" do
expect(wsse.to_xml).to include(Akami::WSSE::WSE_NAMESPACE, Akami::WSSE::WSU_NAMESPACE)
end
it "contains the username and password" do
expect(wsse.to_xml).to include("username", "password")
end
it "does not contain a wsse:Nonce tag" do
expect(wsse.to_xml).not_to match(/<wsse:Nonce.*>.*<\/wsse:Nonce>/)
end
it "does not contain a wsu:Created tag" do
expect(wsse.to_xml).not_to match(/<wsu:Created>.*<\/wsu:Created>/)
end
it "contains the PasswordText type attribute" do
expect(wsse.to_xml).to include(Akami::WSSE::PASSWORD_TEXT_URI)
end
end
context "with credentials and digest auth" do
before { wsse.credentials "username", "password", :digest }
it "contains the WSE and WSU namespaces" do
expect(wsse.to_xml).to include(Akami::WSSE::WSE_NAMESPACE, Akami::WSSE::WSU_NAMESPACE)
end
it "contains the username" do
expect(wsse.to_xml).to include("username")
end
it "does not contain the (original) password" do
expect(wsse.to_xml).not_to include("password")
end
it "contains the Nonce base64 type attribute" do
expect(wsse.to_xml).to include(Akami::WSSE::BASE64_URI)
end
it "contains a wsu:Created tag" do
created_at = Time.now
Timecop.freeze created_at do
expect(wsse.to_xml).to include("<wsu:Created>#{created_at.utc.xmlschema}</wsu:Created>")
end
end
it "contains the PasswordDigest type attribute" do
expect(wsse.to_xml).to include(Akami::WSSE::PASSWORD_DIGEST_URI)
end
it "should reset the nonce every time" do
created_at = Time.now
Timecop.freeze created_at do
nonce_regexp = /<wsse:Nonce.*>([^<]+)<\/wsse:Nonce>/
nonce_first = Base64.decode64(nonce_regexp.match(wsse.to_xml)[1])
nonce_second = Base64.decode64(nonce_regexp.match(wsse.to_xml)[1])
expect(nonce_first).not_to eq(nonce_second)
end
end
it "has contains a properly hashed password" do
xml_header = Nokogiri::XML(wsse.to_xml)
xml_header.remove_namespaces!
nonce = Base64.decode64(xml_header.xpath('//Nonce').first.content)
created_at = xml_header.xpath('//Created').first.content
password_hash = Base64.decode64(xml_header.xpath('//Password').first.content)
expect(password_hash).to eq(Digest::SHA1.digest((nonce + created_at + "password")))
end
end
context "with #timestamp set to true" do
before { wsse.timestamp = true }
it "contains a wsse:Timestamp node" do
expect(wsse.to_xml).to include('<wsu:Timestamp wsu:Id="Timestamp-1" ' +
'xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">')
end
it "contains a wsu:Created node defaulting to Time.now" do
created_at = Time.now
Timecop.freeze created_at do
expect(wsse.to_xml).to include("<wsu:Created>#{created_at.utc.xmlschema}</wsu:Created>")
end
end
it "contains a wsu:Expires node defaulting to Time.now + 60 seconds" do
created_at = Time.now
Timecop.freeze created_at do
expect(wsse.to_xml).to include("<wsu:Expires>#{(created_at + 60).utc.xmlschema}</wsu:Expires>")
end
end
end
context "with #created_at" do
before { wsse.created_at = Time.now + 86400 }
it "contains a wsu:Created node with the given time" do
expect(wsse.to_xml).to include("<wsu:Created>#{wsse.created_at.utc.xmlschema}</wsu:Created>")
end
it "contains a wsu:Expires node set to #created_at + 60 seconds" do
expect(wsse.to_xml).to include("<wsu:Expires>#{(wsse.created_at + 60).utc.xmlschema}</wsu:Expires>")
end
end
context "with #expires_at" do
before { wsse.expires_at = Time.now + 86400 }
it "contains a wsu:Created node defaulting to Time.now" do
created_at = Time.now
Timecop.freeze created_at do
expect(wsse.to_xml).to include("<wsu:Created>#{created_at.utc.xmlschema}</wsu:Created>")
end
end
it "contains a wsu:Expires node set to the given time" do
expect(wsse.to_xml).to include("<wsu:Expires>#{wsse.expires_at.utc.xmlschema}</wsu:Expires>")
end
end
context "whith credentials and timestamp" do
before do
wsse.credentials "username", "password"
wsse.timestamp = true
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
it "contains the username and password" do
expect(wsse.to_xml).to include("username", "password")
end
end
context "whith signature and timestamp on it" do
let(:fixtures_path) do
File.join(Bundler.root, 'spec', 'fixtures', 'akami', 'wsse', 'signature' )
end
let(:cert_file_path) { File.join(fixtures_path, 'cert.pem') }
let(:private_key_path) { File.join(fixtures_path, 'private_key') }
before do
wsse.signature = Akami::WSSE::Signature.new(
Akami::WSSE::Certs.new(
cert_file: cert_file_path,
private_key_file: private_key_path),
timestamp: true)
# some stubs because we have no document body
wsse.signature.stub(:have_document?) { true }
wsse.signature.stub(:body_digest) { "stubbedBodyDigest" }
wsse.signature.stub(:timestamp_digest) { "stubbedTimestampDigest" }
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
it "contains an id on Timestamp" do
id = wsse.signature.timestamp_id
expect(wsse.to_xml).to include(%Q|<wsu:Timestamp wsu:Id="#{id}"|)
end
it "contains two references" do
body_id = wsse.signature.body_id
expect(wsse.to_xml).to include(%Q|<Reference URI="##{body_id}"|)
timestamp_id = wsse.signature.timestamp_id
expect(wsse.to_xml).to include(%Q|<Reference URI="##{timestamp_id}"|)
end
context "with #created_at" do
before do
wsse.signature = Akami::WSSE::Signature.new(
Akami::WSSE::Certs.new(
cert_file: cert_file_path,
private_key_file: private_key_path),
timestamp: true,
created_at: Time.now + 86400)
wsse.signature.stub(:have_document?) { true }
wsse.signature.stub(:body_digest) { "stubbedBodyDigest" }
wsse.signature.stub(:timestamp_digest) { "stubbedTimestampDigest" }
end
it "contains a wsu:Created node with the given time" do
expect(wsse.to_xml).to include("<wsu:Created>#{wsse.signature.created_at.utc.xmlschema}</wsu:Created>")
end
it "contains a wsu:Expires node set to #created_at + 60 seconds" do
expect(wsse.to_xml).to include("<wsu:Expires>#{(wsse.signature.created_at + 60).utc.xmlschema}</wsu:Expires>")
end
end
context "with #expires_at" do
before do
wsse.signature = Akami::WSSE::Signature.new(
Akami::WSSE::Certs.new(
cert_file: cert_file_path,
private_key_file: private_key_path),
{timestamp: true,
expires_at: (Time.now + 86400)})
wsse.signature.stub(:have_document?) { true }
wsse.signature.stub(:body_digest) { "stubbedBodyDigest" }
wsse.signature.stub(:timestamp_digest) { "stubbedTimestampDigest" }
end
it "contains a wsu:Created node defaulting to Time.now" do
created_at = Time.now
Timecop.freeze created_at do
expect(wsse.to_xml).to include("<wsu:Created>#{created_at.utc.xmlschema}</wsu:Created>")
end
end
it "contains a wsu:Expires node set to the given time" do
expect(wsse.to_xml).to include("<wsu:Expires>#{wsse.signature.expires_at.utc.xmlschema}</wsu:Expires>")
end
end
end
end
end