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
|
//-----------------------------------------------------------------------
// <copyright file="UriDiscoveryServiceTests.cs" company="Outercurve Foundation">
// Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.Test.OpenId.DiscoveryServices {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration;
using DotNetOpenAuth.OpenId.RelyingParty;
using DotNetOpenAuth.Test.Mocks;
using NUnit.Framework;
[TestFixture]
public class UriDiscoveryServiceTests : OpenIdTestBase {
[Test]
public async Task DiscoveryWithRedirects() {
Identifier claimedId = this.GetMockIdentifier(ProtocolVersion.V20, false);
// Add a couple of chained redirect pages that lead to the claimedId.
Uri userSuppliedUri = new Uri("https://localhost/someSecurePage");
Uri insecureMidpointUri = new Uri("http://localhost/insecureStop");
this.RegisterMockRedirect(userSuppliedUri, insecureMidpointUri);
this.RegisterMockRedirect(insecureMidpointUri, new Uri(claimedId.ToString()));
// don't require secure SSL discovery for this test.
Identifier userSuppliedIdentifier = new UriIdentifier(userSuppliedUri, false);
var discoveryResult = await this.DiscoverAsync(userSuppliedIdentifier);
Assert.AreEqual(1, discoveryResult.Count());
}
[Test]
public async Task DiscoverRequireSslWithSecureRedirects() {
Identifier claimedId = this.GetMockIdentifier(ProtocolVersion.V20, true);
// Add a couple of chained redirect pages that lead to the claimedId.
// All redirects should be secure.
Uri userSuppliedUri = new Uri("https://localhost/someSecurePage");
Uri secureMidpointUri = new Uri("https://localhost/secureStop");
this.RegisterMockRedirect(userSuppliedUri, secureMidpointUri);
this.RegisterMockRedirect(secureMidpointUri, new Uri(claimedId.ToString()));
Identifier userSuppliedIdentifier = new UriIdentifier(userSuppliedUri, true);
var discoveryResult = await this.DiscoverAsync(userSuppliedIdentifier);
Assert.AreEqual(1, discoveryResult.Count());
}
[Test, ExpectedException(typeof(ProtocolException))]
public async Task DiscoverRequireSslWithInsecureRedirect() {
Identifier claimedId = this.GetMockIdentifier(ProtocolVersion.V20, true);
// Add a couple of chained redirect pages that lead to the claimedId.
// Include an insecure HTTP jump in those redirects to verify that
// the ultimate endpoint is never found as a result of high security profile.
Uri userSuppliedUri = new Uri("https://localhost/someSecurePage");
Uri insecureMidpointUri = new Uri("http://localhost/insecureStop");
this.RegisterMockRedirect(userSuppliedUri, insecureMidpointUri);
this.RegisterMockRedirect(insecureMidpointUri, new Uri(claimedId.ToString()));
Identifier userSuppliedIdentifier = new UriIdentifier(userSuppliedUri, true);
await this.DiscoverAsync(userSuppliedIdentifier);
}
[Test]
public async Task DiscoveryRequireSslWithInsecureXrdsInSecureHtmlHead() {
var insecureXrdsSource = this.GetMockIdentifier(ProtocolVersion.V20, false);
Uri secureClaimedUri = new Uri("https://localhost/secureId");
string html = string.Format("<html><head><meta http-equiv='X-XRDS-Location' content='{0}'/></head><body></body></html>", insecureXrdsSource);
this.RegisterMockResponse(secureClaimedUri, "text/html", html);
Identifier userSuppliedIdentifier = new UriIdentifier(secureClaimedUri, true);
var discoveryResult = await this.DiscoverAsync(userSuppliedIdentifier);
Assert.AreEqual(0, discoveryResult.Count());
}
[Test]
public async Task DiscoveryRequireSslWithInsecureXrdsInSecureHttpHeader() {
var insecureXrdsSource = this.GetMockIdentifier(ProtocolVersion.V20, false);
string html = "<html><head></head><body></body></html>";
WebHeaderCollection headers = new WebHeaderCollection {
{ "X-XRDS-Location", insecureXrdsSource }
};
this.RegisterMockResponse(VanityUriSsl, VanityUriSsl, "text/html", headers, html);
Identifier userSuppliedIdentifier = new UriIdentifier(VanityUriSsl, true);
var discoveryResult = await this.DiscoverAsync(userSuppliedIdentifier);
Assert.AreEqual(0, discoveryResult.Count());
}
[Test]
public async Task DiscoveryRequireSslWithInsecureXrdsButSecureLinkTags() {
var insecureXrdsSource = this.GetMockIdentifier(ProtocolVersion.V20, false);
string html = string.Format(
@"
<html><head>
<meta http-equiv='X-XRDS-Location' content='{0}'/> <!-- this one will be insecure and ignored -->
<link rel='openid2.provider' href='{1}' />
<link rel='openid2.local_id' href='{2}' />
</head><body></body></html>",
HttpUtility.HtmlEncode(insecureXrdsSource),
HttpUtility.HtmlEncode(OPUriSsl.AbsoluteUri),
HttpUtility.HtmlEncode(OPLocalIdentifiersSsl[1].AbsoluteUri));
this.Handle(VanityUriSsl).By(html, "text/html");
Identifier userSuppliedIdentifier = new UriIdentifier(VanityUriSsl, true);
// We verify that the XRDS was ignored and the LINK tags were used
// because the XRDS OP-LocalIdentifier uses different local identifiers.
var discoveryResult = await this.DiscoverAsync(userSuppliedIdentifier);
Assert.AreEqual(OPLocalIdentifiersSsl[1].AbsoluteUri, discoveryResult.Single().ProviderLocalIdentifier.ToString());
}
[Test]
public async Task DiscoveryRequiresSslIgnoresInsecureEndpointsInXrds() {
var insecureEndpoint = GetServiceEndpoint(0, ProtocolVersion.V20, 10, false);
var secureEndpoint = GetServiceEndpoint(1, ProtocolVersion.V20, 20, true);
UriIdentifier secureClaimedId = new UriIdentifier(VanityUriSsl, true);
this.RegisterMockXrdsResponse(secureClaimedId, new[] { insecureEndpoint, secureEndpoint });
var discoverResult = await this.DiscoverAsync(secureClaimedId);
Assert.AreEqual(secureEndpoint.ProviderLocalIdentifier, discoverResult.Single().ProviderLocalIdentifier);
}
[Test]
public async Task XrdsDirectDiscovery_10() {
await this.FailDiscoverXrdsAsync("xrds-irrelevant");
await this.DiscoverXrdsAsync("xrds10", ProtocolVersion.V10, null, "http://a/b");
await this.DiscoverXrdsAsync("xrds11", ProtocolVersion.V11, null, "http://a/b");
await this.DiscoverXrdsAsync("xrds1020", ProtocolVersion.V10, null, "http://a/b");
}
[Test]
public async Task XrdsDirectDiscovery_20() {
await this.DiscoverXrdsAsync("xrds20", ProtocolVersion.V20, null, "http://a/b");
await this.DiscoverXrdsAsync("xrds2010a", ProtocolVersion.V20, null, "http://a/b");
await this.DiscoverXrdsAsync("xrds2010b", ProtocolVersion.V20, null, "http://a/b");
}
[Test]
public async Task HtmlDiscover_11() {
await this.DiscoverHtmlAsync("html10prov", ProtocolVersion.V11, null, "http://a/b");
await this.DiscoverHtmlAsync("html10both", ProtocolVersion.V11, "http://c/d", "http://a/b");
await this.FailDiscoverHtmlAsync("html10del");
// Verify that HTML discovery generates the 1.x endpoints when appropriate
await this.DiscoverHtmlAsync("html2010", ProtocolVersion.V11, "http://g/h", "http://e/f");
await this.DiscoverHtmlAsync("html1020", ProtocolVersion.V11, "http://g/h", "http://e/f");
await this.DiscoverHtmlAsync("html2010combinedA", ProtocolVersion.V11, "http://c/d", "http://a/b");
await this.DiscoverHtmlAsync("html2010combinedB", ProtocolVersion.V11, "http://c/d", "http://a/b");
await this.DiscoverHtmlAsync("html2010combinedC", ProtocolVersion.V11, "http://c/d", "http://a/b");
}
[Test]
public async Task HtmlDiscover_20() {
await this.DiscoverHtmlAsync("html20prov", ProtocolVersion.V20, null, "http://a/b");
await this.DiscoverHtmlAsync("html20both", ProtocolVersion.V20, "http://c/d", "http://a/b");
await this.FailDiscoverHtmlAsync("html20del");
await this.DiscoverHtmlAsync("html2010", ProtocolVersion.V20, "http://c/d", "http://a/b");
await this.DiscoverHtmlAsync("html1020", ProtocolVersion.V20, "http://c/d", "http://a/b");
await this.DiscoverHtmlAsync("html2010combinedA", ProtocolVersion.V20, "http://c/d", "http://a/b");
await this.DiscoverHtmlAsync("html2010combinedB", ProtocolVersion.V20, "http://c/d", "http://a/b");
await this.DiscoverHtmlAsync("html2010combinedC", ProtocolVersion.V20, "http://c/d", "http://a/b");
await this.FailDiscoverHtmlAsync("html20relative");
}
[Test]
public async Task XrdsDiscoveryFromHead() {
this.RegisterMockResponse(
new Uri("http://localhost/xrds1020.xml"),
"application/xrds+xml",
LoadEmbeddedFile("/Discovery/xrdsdiscovery/xrds1020.xml"));
await this.DiscoverXrdsAsync("XrdsReferencedInHead.html", ProtocolVersion.V10, null, "http://a/b");
}
[Test]
public async Task XrdsDiscoveryFromHttpHeader() {
WebHeaderCollection headers = new WebHeaderCollection();
headers.Add("X-XRDS-Location", new Uri("http://localhost/xrds1020.xml").AbsoluteUri);
this.RegisterMockResponse(new Uri("http://localhost/xrds1020.xml"), "application/xrds+xml", LoadEmbeddedFile("/Discovery/xrdsdiscovery/xrds1020.xml"));
await this.DiscoverXrdsAsync("XrdsReferencedInHttpHeader.html", ProtocolVersion.V10, null, "http://a/b", headers);
}
/// <summary>
/// Verifies HTML discovery proceeds if an XRDS document is referenced that doesn't contain OpenID endpoints.
/// </summary>
[Test]
public async Task HtmlDiscoveryProceedsIfXrdsIsEmpty() {
this.RegisterMockResponse(new Uri("http://localhost/xrds-irrelevant.xml"), "application/xrds+xml", LoadEmbeddedFile("/Discovery/xrdsdiscovery/xrds-irrelevant.xml"));
await this.DiscoverHtmlAsync("html20provWithEmptyXrds", ProtocolVersion.V20, null, "http://a/b");
}
/// <summary>
/// Verifies HTML discovery proceeds if the XRDS that is referenced cannot be found.
/// </summary>
[Test]
public async Task HtmlDiscoveryProceedsIfXrdsIsBadOrMissing() {
await this.DiscoverHtmlAsync("html20provWithBadXrds", ProtocolVersion.V20, null, "http://a/b");
}
/// <summary>
/// Verifies that a dual identifier yields only one service endpoint by default.
/// </summary>
[Test]
public async Task DualIdentifierOffByDefault() {
this.RegisterMockResponse(VanityUri, "application/xrds+xml", LoadEmbeddedFile("/Discovery/xrdsdiscovery/xrds20dual.xml"));
var results = (await this.DiscoverAsync(VanityUri)).ToList();
Assert.AreEqual(1, results.Count(r => r.ClaimedIdentifier == r.Protocol.ClaimedIdentifierForOPIdentifier), "OP Identifier missing from discovery results.");
Assert.AreEqual(1, results.Count, "Unexpected additional services discovered.");
}
/// <summary>
/// Verifies that a dual identifier yields two service endpoints when that feature is turned on.
/// </summary>
[Test]
public async Task DualIdentifier() {
this.RegisterMockResponse(VanityUri, "application/xrds+xml", LoadEmbeddedFile("/Discovery/xrdsdiscovery/xrds20dual.xml"));
var rp = this.CreateRelyingParty(true);
rp.SecuritySettings.AllowDualPurposeIdentifiers = true;
var results = (await rp.DiscoverAsync(VanityUri, CancellationToken.None)).ToList();
Assert.AreEqual(1, results.Count(r => r.ClaimedIdentifier == r.Protocol.ClaimedIdentifierForOPIdentifier), "OP Identifier missing from discovery results.");
Assert.AreEqual(1, results.Count(r => r.ClaimedIdentifier == VanityUri), "Claimed identifier missing from discovery results.");
Assert.AreEqual(2, results.Count, "Unexpected additional services discovered.");
}
private async Task DiscoverAsync(string url, ProtocolVersion version, Identifier expectedLocalId, string providerEndpoint, bool expectSreg, bool useRedirect) {
await this.DiscoverAsync(url, version, expectedLocalId, providerEndpoint, expectSreg, useRedirect, null);
}
private string RegisterDiscoveryRedirector(Uri baseUrl) {
var redirectorUrl = new Uri(baseUrl, "Discovery/htmldiscovery/redirect.aspx");
this.Handle(redirectorUrl).By(req => {
string redirectTarget = HttpUtility.ParseQueryString(req.RequestUri.Query)["target"];
var response = new HttpResponseMessage(HttpStatusCode.Redirect);
response.Headers.Location = new Uri(redirectTarget, UriKind.RelativeOrAbsolute);
response.RequestMessage = req;
return response;
});
return redirectorUrl.AbsoluteUri + "?target=";
}
private async Task DiscoverAsync(string url, ProtocolVersion version, Identifier expectedLocalId, string providerEndpoint, bool expectSreg, bool useRedirect, WebHeaderCollection headers) {
Protocol protocol = Protocol.Lookup(version);
Uri baseUrl = new Uri("http://localhost/");
string redirectBase = this.RegisterDiscoveryRedirector(baseUrl);
UriIdentifier claimedId = new Uri(baseUrl, url);
UriIdentifier userSuppliedIdentifier = new Uri(redirectBase + Uri.EscapeDataString(url));
if (expectedLocalId == null) {
expectedLocalId = claimedId;
}
Identifier idToDiscover = useRedirect ? userSuppliedIdentifier : claimedId;
string contentType;
if (url.EndsWith("html")) {
contentType = "text/html";
} else if (url.EndsWith("xml")) {
contentType = "application/xrds+xml";
} else {
throw new InvalidOperationException();
}
this.RegisterMockResponse(claimedId, claimedId, contentType, headers ?? new WebHeaderCollection(), LoadEmbeddedFile(url));
IdentifierDiscoveryResult expected = IdentifierDiscoveryResult.CreateForClaimedIdentifier(
claimedId,
expectedLocalId,
new ProviderEndpointDescription(new Uri(providerEndpoint), new string[] { protocol.ClaimedIdentifierServiceTypeURI }), // services aren't checked by Equals
null,
null);
var discoveryResult = await this.DiscoverAsync(idToDiscover);
IdentifierDiscoveryResult se = discoveryResult.FirstOrDefault(ep => ep.Equals(expected));
Assert.IsNotNull(se, url + " failed to be discovered.");
// Do extra checking of service type URIs, which aren't included in
// the ServiceEndpoint.Equals method.
Assert.AreEqual(expectSreg ? 2 : 1, se.Capabilities.Count);
Assert.IsTrue(se.Capabilities.Contains(protocol.ClaimedIdentifierServiceTypeURI));
Assert.AreEqual(expectSreg, se.IsExtensionSupported<ClaimsRequest>());
}
private async Task DiscoverXrdsAsync(string page, ProtocolVersion version, Identifier expectedLocalId, string providerEndpoint) {
await this.DiscoverXrdsAsync(page, version, expectedLocalId, providerEndpoint, null);
}
private async Task DiscoverXrdsAsync(string page, ProtocolVersion version, Identifier expectedLocalId, string providerEndpoint, WebHeaderCollection headers) {
if (!page.Contains(".")) {
page += ".xml";
}
await this.DiscoverAsync("/Discovery/xrdsdiscovery/" + page, version, expectedLocalId, providerEndpoint, true, false, headers);
await this.DiscoverAsync("/Discovery/xrdsdiscovery/" + page, version, expectedLocalId, providerEndpoint, true, true, headers);
}
private async Task DiscoverHtmlAsync(string page, ProtocolVersion version, Identifier expectedLocalId, string providerEndpoint, bool useRedirect) {
await this.DiscoverAsync("/Discovery/htmldiscovery/" + page, version, expectedLocalId, providerEndpoint, false, useRedirect);
}
private async Task DiscoverHtmlAsync(string scenario, ProtocolVersion version, Identifier expectedLocalId, string providerEndpoint) {
string page = scenario + ".html";
await this.DiscoverHtmlAsync(page, version, expectedLocalId, providerEndpoint, false);
await this.DiscoverHtmlAsync(page, version, expectedLocalId, providerEndpoint, true);
}
private async Task FailDiscoverAsync(string url) {
UriIdentifier userSuppliedId = new Uri(new Uri("http://localhost"), url);
this.RegisterMockResponse(new Uri(userSuppliedId), userSuppliedId, "text/html", LoadEmbeddedFile(url));
var discoveryResult = await this.DiscoverAsync(userSuppliedId);
Assert.AreEqual(0, discoveryResult.Count()); // ... but that no endpoint info is discoverable
}
private async Task FailDiscoverHtmlAsync(string scenario) {
await this.FailDiscoverAsync("/Discovery/htmldiscovery/" + scenario + ".html");
}
private async Task FailDiscoverXrdsAsync(string scenario) {
await this.FailDiscoverAsync("/Discovery/xrdsdiscovery/" + scenario + ".xml");
}
}
}
|