forked from k2n/saml20-clj
-
Notifications
You must be signed in to change notification settings - Fork 11
/
response_test.clj
308 lines (286 loc) · 16.6 KB
/
response_test.clj
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
(ns saml20-clj.sp.response-test
(:require [clojure.test :refer :all]
[java-time.api :as t]
[saml20-clj.coerce :as coerce]
[saml20-clj.sp.response :as response]
[saml20-clj.test :as test])
(:import org.opensaml.saml.saml2.core.Response))
(deftest response-status-test
(doseq [{:keys [response], :as response-map} (test/responses)]
(testing (test/describe-response-map response-map)
(is (= {:in-response-to "ONELOGIN_4fee3b046395c4e751011e97f8900b5273d56685"
:status "urn:oasis:names:tc:SAML:2.0:status:Success"
:success? true
:version "2.0"
:issue-instant (t/instant "2014-07-17T01:01:48.000Z")
:destination "http://sp.example.com/demo1/index.php?acs"}
(response/response-status response))))))
(deftest assertions-test
(doseq [{:keys [response], :as response-map} (test/responses)
:when (not test/invalid-confirmation-data?)]
(testing (test/describe-response-map response-map)
(is (= [{:attrs {"uid" ["test"]
"mail" ["test@example.com"]
"eduPersonAffiliation" ["users" "examplerole1"]}
:audiences ["sp.example.com"]
:name-id {:value "_ce3d2948b4cf20146dee0a0b3dd6f69b6cf86f62d7"
:format "urn:oasis:names:tc:SAML:2.0:nameid-format:transient"}
:confirmation {:in-response-to "ONELOGIN_4fee3b046395c4e751011e97f8900b5273d56685"
:not-before nil
:not-on-or-after (t/instant "2024-01-18T06:21:48.000Z")
:address nil
:recipient "http://sp.example.com/demo1/index.php?acs"}}]
(response/assertions response test/sp-private-key))))))
;; • Verify any signatures present on the assertion(s) or the response
(deftest decrypt-response-test
(testing "Should be able to decrypt a response"
(doseq [{:keys [response], :as response-map} (test/responses)
:when (test/signed-and-encrypted-assertion? response-map)]
(let [original (coerce/->Response response)
xml-before-decryption (coerce/->xml-string original)
decrypted (response/decrypt-response original test/sp-private-key)]
(testing (test/describe-response-map response-map)
(testing (str "\noriginal =\n" (coerce/->xml-string original))
(testing (str "decrypted =\n" (coerce/->xml-string decrypted))
(prn :original (coerce/->xml-string original))
(prn :decrypted (coerce/->xml-string decrypted))
(is (= 0
(count (.getEncryptedAssertions decrypted))))
(is (= 1
(count (.getAssertions decrypted))))
(testing "\noriginal object should not be modified"
(is (= xml-before-decryption
(coerce/->xml-string original)))
(is (= 0
(count (.getAssertions original))))
(is (= 1
(count (.getEncryptedAssertions original))))))))))))
(deftest assert-valid-signatures-test
(testing "unsigned responses should fail\n"
(doseq [{:keys [response], :as response-map} (test/responses)
:when (and (not (test/signed? response-map))
(not (test/malicious-signature? response-map)))]
(testing (test/describe-response-map response-map)
(is (thrown-with-msg?
java.lang.AssertionError
#"Neither response nor assertion\(s\) are signed"
(response/validate response test/idp-cert test/sp-private-key {:response-validators [:signature :require-signature]
:assertion-validators [:signature]}))))))
(testing "valid signed responses should pass\n"
(doseq [{:keys [response], :as response-map} (test/responses)
:when (test/signed? response-map)]
(testing (test/describe-response-map response-map)
(testing "\nsignature should be valid when checking against IdP cert"
(is (instance? Response
(response/validate response test/idp-cert test/sp-private-key {:response-validators [:signature :require-signature]
:assertion-validators [:signature]}))))
(testing "\nsignature should be invalid when checking against the wrong cert"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Invalid <(?:Response)|(?:Assertion)> signature"
;; using SP cert for both instead
(response/validate
response
test/sp-cert
{:filename test/keystore-filename
:password test/keystore-password
:alias "sp"}
{:response-validators [:signature :require-signature]
:assertion-validators [:signature]}))))))))
(deftest assert-encrypted-assertions-test
(testing "unencrypted assertions should fail"
(doseq [{:keys [response], :as response-map} (test/responses)
:when (not (test/assertions-encrypted? response-map))]
(testing (test/describe-response-map response-map)
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Unencrypted assertions present in response body"
(response/validate response test/idp-cert test/sp-private-key {:assertion-validators [:require-encryption]})))))
(doseq [{:keys [response], :as response-map} (test/responses)
:when (test/assertions-encrypted? response-map)]
(testing (test/describe-response-map response-map)
(is (instance? Response
(response/validate response test/idp-cert test/sp-private-key {:response-validators []
:assertion-validators [:require-encryption]})))))))
(deftest test-move-validator-config-helper
(is (= {:foo true :response-validators []}
(#'response/move-validator-config {:response-validators [:foo]} :response-validators :foo)))
(is (= {:foo true :response-validators [:bar]}
(#'response/move-validator-config {:response-validators [:foo :bar]} :response-validators :foo)))
(is (= {:response-validators [:bar]}
(#'response/move-validator-config {:response-validators [:bar]} :response-validators :foo))))
;;
;; Subject Confirmation Data Verifications
;;
;; TODO hardcoding these is brittle, we should pull them out of the
;; respective XML
(def acs-url "http://sp.example.com/demo1/index.php?acs")
(def auth-req-id "ONELOGIN_4fee3b046395c4e751011e97f8900b5273d56685")
;; TODO there's no test for an SubjectConfirmationData field is missing (which is valid), but it seems like a small
;; case to create a whole new document for
(defn- validate-assertions [validator options]
(let [response (test/response {:valid-confirmation-data? true})]
(response/validate response nil nil (merge {:response-validators nil
:assertion-validators [validator]}
options))
:valid))
(defn- validate-assertions-bad-data [validator options]
(let [response (test/response {:invalid-confirmation-data? true})]
(response/validate response nil nil (merge {:response-validators nil
:assertion-validators [validator]}
options))
:valid))
(deftest validate-assertions-not-on-or-after-test
(t/with-clock (t/mock-clock (t/instant "2020-09-24T00:00:00.000Z"))
(is (= :valid
(validate-assertions :not-on-or-after nil))))
(t/with-clock (t/mock-clock (t/instant "2025-01-01T00:00:00.000Z"))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"NotOnOrAfter has passed"
(validate-assertions :not-on-or-after nil)))
(testing "should respect clock skew"
;; one year of clock skew :(
(is (= :valid
(validate-assertions :not-on-or-after {:allowable-clock-skew-seconds (* 60 60 24 365)}))))
(testing "should notice if NotOnOrAfter is missing"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"does not contain NotOnOrAfter"
(validate-assertions-bad-data :not-on-or-after nil))))))
(deftest validate-assertions-not-before-test
(t/with-clock (t/mock-clock (t/instant "2020-09-24T00:00:00.000Z"))
(is (= :valid
(validate-assertions :not-before nil))))
(testing "should respect clock skew"
(is (= :valid
(validate-assertions :not-before {:allowable-clock-skew-seconds (* 60 60 24 365)}))))
(t/with-clock (t/mock-clock (t/instant "2010-09-24T00:00:00.000Z"))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"NotBefore is in the future"
(validate-assertions :not-before nil)))))
(deftest validate-assertions-recipient-test
(is (= :valid
(validate-assertions :recipient {:acs-url "http://sp.example.com/demo1/index.php?acs"})))
(testing "wrong ACS URL"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Recipient does not match assertion consumer service URL"
(validate-assertions :recipient {:acs-url "http://this.is.the.wrong.url"}))))
(testing "missing recipient"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"does not contain a Recipient"
(validate-assertions-bad-data :recipient {:acs-url "foobar"}))))
(testing "contains a recipient, but not checking against URL as required by spec"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"contains a Recipient but an acs-url was not passed to validate against"
(validate-assertions :recipient nil)))))
(deftest validate-assertions-in-response-to-test
(testing "\nchecking in-response-to attribute (solicited)"
(is (= :valid
(validate-assertions :in-response-to {:request-id auth-req-id, :solicited? true})))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"<SubjectConfirmationData> InResponseTo does not match request-id"
(validate-assertions :in-response-to {:request-id "bad-request-id", :solicited? true})))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"<SubjectConfirmationData> does not contain InResponseTo"
(validate-assertions-bad-data :in-response-to {:request-id auth-req-id, :solicited? true}))))
(testing "\nchecking in-response-to attribute (unsolicited)"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"<SubjectConfirmationData> InResponseTo should not be present for an unsolicited request"
(validate-assertions :in-response-to {:request-id "bad-request-id", :solicited? false})))
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"<SubjectConfirmationData> InResponseTo should not be present for an unsolicited request"
(validate-assertions :in-response-to {:solicited? false})))))
(deftest validate-assertions-address-test
(testing "correct user agent address"
(is (= :valid
(validate-assertions :address {:user-agent-address "192.168.1.1"}))))
(testing "bad user agent address"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Address does not match user-agent-address"
(validate-assertions :address {:user-agent-address "im.a.bad.man"})))))
(deftest validate-assertions-response-issuer-test
(let [normal-response (test/response {})
response-no-issuer (test/response {:no-issuer-information? true})]
(testing "If response has <Issuer>, it should be validated against `:issuer` (if provided)"
(letfn [(validate [response issuer]
(response/validate response nil nil {:response-validators [:issuer]
:assertion-validators nil
:issuer issuer}))]
(testing "Correct :issuer should succeed"
(is (instance? Response (validate normal-response "idp.example.com"))))
(testing "If :issuer is not passed, validator should no-op"
(is (instance? Response (validate normal-response nil))))
(is (thrown-with-msg?
AssertionError
#"Expected :issuer to be a String"
(validate normal-response 123)))
(testing "Wrong :issuer should fail"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Incorrect Response <Issuer>"
(validate normal-response "wrong.idp.issuer.com"))))
(testing "<Response> should be allowed to have no <Issuer>"
(is (instance? Response (validate response-no-issuer "idp.example.com"))))))
(testing "Assertion <Issuer> should be validated against `:issuer` (if provided)"
(letfn [(validate [response issuer]
(response/validate response nil nil {:response-validators []
:assertion-validators [:issuer]
:issuer issuer}))]
(testing "Correct :issuer should succeed"
(is (instance? Response (validate normal-response "idp.example.com"))))
(testing "If :issuer is not passed, validator should no-op"
(is (instance? Response (validate normal-response nil))))
(is (thrown-with-msg?
AssertionError
#"Expected :issuer to be a String"
(validate normal-response 123)))
(testing "Wrong :issuer should fail"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Incorrect Assertion <Issuer>"
(validate normal-response "wrong.idp.issuer.com"))))
(testing "Validation should fail if Assertion does not have an <Issuer> but :issuer is passed"
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
#"Assertion is missing required <Issuer> element"
(validate response-no-issuer "idp.example.com"))))))
(testing "Responses with no <Issuer> information should be allowed for now if :issuer is not passed"
(is (instance? Response (response/validate response-no-issuer nil nil {:response-validators [:issuer]
:assertion-validators [:issuer]}))))))
(deftest Assertion->map-test
(testing "basic checks on Assertions->map conversions"
(doseq [{:keys [response], :as response-map} (test/responses)
:when (test/valid-confirmation-data? response-map)]
(is (= {:audiences '("sp.example.com")
:attrs {"uid" '("test")
"mail" '("test@example.com")
"eduPersonAffiliation" '("users" "examplerole1")}
:name-id {:value "_ce3d2948b4cf20146dee0a0b3dd6f69b6cf86f62d7",
:format "urn:oasis:names:tc:SAML:2.0:nameid-format:transient"},
:confirmation {:in-response-to "ONELOGIN_4fee3b046395c4e751011e97f8900b5273d56685",
:not-before (t/instant "2019-01-18T06:21:48Z"),
:not-on-or-after (t/instant "2024-01-18T06:21:48Z"),
:address "192.168.1.1",
:recipient "http://sp.example.com/demo1/index.php?acs"}}
(response/Assertion->map
(first (response/opensaml-assertions (coerce/->Response response))))))))
(testing "Attribute Nodes sharing a Name will collect all of their contained Attribute Value Nodes."
(let [response (test/response {})]
(is (= {"uid" '("test")
"mail" '("test@example.com")
;; this key comes from an Attribute Node with two AttributeValue nodes inside
"eduPersonAffiliation" '("users" "examplerole1")
;; this key is from two Attribute nodes with the same Name
"member_of" '("test-group1" "test-group2" "test-group3")}
(:attrs (response/Assertion->map
(first (response/opensaml-assertions (coerce/->Response response))))))))))