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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
//-----------------------------------------------------------------------
// <copyright file="TestSupport.cs" company="Andrew Arnott">
// Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.Test.OpenId {
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using DotNetOAuth.Test.OpenId.UI;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.RelyingParty;
using DotNetOpenAuth.Test.Mocks;
////using DotNetOpenAuth.Test.UI;
using log4net;
public class TestSupport {
public const string HostTestPage = "HostTest.aspx";
public const string ProviderPage = "ProviderEndpoint.aspx";
public const string DirectedProviderEndpoint = "DirectedProviderEndpoint.aspx";
public const string MobileConsumerPage = "RelyingPartyMobile.aspx";
public const string ConsumerPage = "RelyingParty.aspx";
public const string OPDefaultPage = "OPDefault.aspx";
public static readonly ILog Logger = LogManager.GetLogger("DotNetOpenId.Test");
public static readonly string TestWebDirectory = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), @"..\..\src\DotNetOpenId.TestWeb"));
private const string IdentityPage = "IdentityEndpoint.aspx";
private const string DirectedIdentityPage = "DirectedIdentityEndpoint.aspx";
public enum Scenarios {
// Authentication test scenarios
AutoApproval,
AutoApprovalAddFragment,
ApproveOnSetup,
AlwaysDeny,
/* Extension test scenarios */
/// <summary>
/// Provides all required and requested fields.
/// </summary>
ExtensionFullCooperation,
/// <summary>
/// Provides only those fields marked as required.
/// </summary>
ExtensionPartialCooperation,
}
public static Uri ReturnTo {
get { return TestSupport.GetFullUrl(TestSupport.ConsumerPage); }
}
public static Realm Realm {
get { return new Realm(TestSupport.GetFullUrl(TestSupport.ConsumerPage).AbsoluteUri); }
}
public static Identifier GetDelegateUrl(Scenarios scenario) {
return GetDelegateUrl(scenario, false);
}
public static Identifier GetDelegateUrl(Scenarios scenario, bool useSsl) {
return new UriIdentifier(GetFullUrl("/" + scenario, null, useSsl));
}
public static Uri GetFullUrl(string url) {
return GetFullUrl(url, null, false);
}
public static Uri GetFullUrl(string url, string key, object value) {
var dictionary = new Dictionary<string, string> {
{ key, value.ToString() },
};
return GetFullUrl(url, dictionary, false);
}
public static Uri GetFullUrl(string url, IDictionary<string, string> args, bool useSsl) {
Uri defaultUriBase = new Uri(useSsl ? "https://localhost/" : "http://localhost/");
Uri baseUri = UITestSupport.Host != null ? UITestSupport.Host.BaseUri : defaultUriBase;
UriBuilder builder = new UriBuilder(new Uri(baseUri, url));
MessagingUtilities.AppendQueryArgs(builder, args);
return builder.Uri;
}
/// <summary>
/// Returns the content of a given embedded resource.
/// </summary>
/// <param name="path">The path of the file as it appears within the project,
/// where the leading / marks the root directory of the project.</param>
/// <returns>The content of the requested resource.</returns>
internal static string LoadEmbeddedFile(string path) {
if (!path.StartsWith("/")) {
path = "/" + path;
}
path = "DotNetOpenAuth.Test.OpenId" + path.Replace('/', '.');
Stream resource = Assembly.GetExecutingAssembly().GetManifestResourceStream(path);
if (resource == null) {
throw new ArgumentException();
}
using (StreamReader sr = new StreamReader(resource)) {
return sr.ReadToEnd();
}
}
////internal static UriIdentifier GetOPIdentityUrl(Scenarios scenario, bool useSsl) {
//// var args = new Dictionary<string, string> {
//// { "user", scenario.ToString() },
////};
//// return new UriIdentifier(GetFullUrl("/" + OPDefaultPage, args, useSsl));
////}
internal static UriIdentifier GetIdentityUrl(Scenarios scenario, ProtocolVersion providerVersion) {
return GetIdentityUrl(scenario, providerVersion, false);
}
internal static UriIdentifier GetIdentityUrl(Scenarios scenario, ProtocolVersion providerVersion, bool useSsl) {
var dictionary = new Dictionary<string, string> {
{ "user", scenario.ToString() },
{ "version", providerVersion.ToString() },
};
return new UriIdentifier(GetFullUrl("/" + IdentityPage, dictionary, useSsl));
}
internal static MockIdentifier GetMockIdentifier(Scenarios scenario, MockHttpRequest mockRequest, ProtocolVersion providerVersion) {
return GetMockIdentifier(scenario, mockRequest, providerVersion, false);
}
internal static MockIdentifier GetMockIdentifier(Scenarios scenario, MockHttpRequest mockRequest, ProtocolVersion providerVersion, bool useSsl) {
ServiceEndpoint se = GetServiceEndpoint(scenario, providerVersion, 10, useSsl);
return new MockIdentifier(GetIdentityUrl(scenario, providerVersion, useSsl), mockRequest, new ServiceEndpoint[] { se });
}
internal static ServiceEndpoint GetServiceEndpoint(Scenarios scenario, ProtocolVersion providerVersion, int servicePriority, bool useSsl) {
var providerEndpoint = new ProviderEndpointDescription(GetFullUrl("/" + ProviderPage, null, useSsl), new string[] { Protocol.Lookup(providerVersion).ClaimedIdentifierServiceTypeURI });
return ServiceEndpoint.CreateForClaimedIdentifier(
GetIdentityUrl(scenario, providerVersion, useSsl),
GetDelegateUrl(scenario, useSsl),
providerEndpoint,
servicePriority,
10);
}
////internal static UriIdentifier GetDirectedIdentityUrl(Scenarios scenario, ProtocolVersion providerVersion) {
//// return GetDirectedIdentityUrl(scenario, providerVersion, false);
////}
////internal static UriIdentifier GetDirectedIdentityUrl(Scenarios scenario, ProtocolVersion providerVersion, bool useSsl) {
//// return new UriIdentifier(GetFullUrl("/" + directedIdentityPage, new Dictionary<string, string> {
//// { "user", scenario.ToString() },
//// { "version", providerVersion.ToString() },
////}, useSsl));
////}
////internal static IRelyingPartyApplicationStore RelyingPartyStore;
////internal static IProviderAssociationStore ProviderStore;
/////// <summary>
/////// Generates a new, stateful <see cref="OpenIdRelyingParty"/> whose direct messages
/////// will be automatically handled by an internal <see cref="OpenIdProvider"/>
/////// that uses the shared <see cref="ProviderStore"/>.
/////// </summary>
////internal static OpenIdRelyingParty CreateRelyingParty(NameValueCollection fields) {
//// return CreateRelyingParty(RelyingPartyStore, null, fields);
////}
////internal static OpenIdRelyingParty CreateRelyingParty(IRelyingPartyApplicationStore store, NameValueCollection fields) {
//// return CreateRelyingParty(store, null, fields);
////}
/////// <summary>
/////// Generates a new <see cref="OpenIdRelyingParty"/> whose direct messages
/////// will be automatically handled by an internal <see cref="OpenIdProvider"/>
/////// that uses the shared <see cref="ProviderStore"/>.
/////// </summary>
////internal static OpenIdRelyingParty CreateRelyingParty(IRelyingPartyApplicationStore store, Uri requestUrl, NameValueCollection fields) {
//// var rp = new OpenIdRelyingParty(store, requestUrl ?? GetFullUrl(ConsumerPage), fields ?? new NameValueCollection());
//// if (fields == null || fields.Count == 0) {
//// Assert.IsNull(rp.Response);
//// }
//// rp.DirectMessageChannel = new DirectMessageTestRedirector(ProviderStore);
//// return rp;
////}
////internal static DotNetOpenId.RelyingParty.IAuthenticationRequest CreateRelyingPartyRequest(bool stateless, Scenarios scenario, ProtocolVersion version, bool useSsl) {
//// // Publish RP discovery information
//// MockHttpRequest.RegisterMockRPDiscovery();
//// var rp = TestSupport.CreateRelyingParty(stateless ? null : RelyingPartyStore, null);
//// var rpReq = rp.CreateRequest(TestSupport.GetMockIdentifier(scenario, version, useSsl), Realm, ReturnTo);
//// // Sidetrack: verify URLs and other default properties
//// {
//// Assert.AreEqual(AuthenticationRequestMode.Setup, rpReq.Mode);
//// Assert.AreEqual(Realm, rpReq.Realm);
//// Assert.AreEqual(ReturnTo, rpReq.ReturnToUrl);
//// }
//// return rpReq;
////}
/////// <summary>
/////// Generates a new <see cref="OpenIdRelyingParty"/> ready to process a
/////// response from an <see cref="OpenIdProvider"/>.
/////// </summary>
////internal static IAuthenticationResponse CreateRelyingPartyResponse(IRelyingPartyApplicationStore store, IResponse providerResponse) {
//// return CreateRelyingPartyResponse(store, providerResponse, false);
////}
////internal static IAuthenticationResponse CreateRelyingPartyResponse(IRelyingPartyApplicationStore store, IResponse providerResponse, bool requireSsl) {
//// if (providerResponse == null) throw new ArgumentNullException("providerResponse");
//// var opAuthWebResponse = (Response)providerResponse;
//// var opAuthResponse = (EncodableResponse)opAuthWebResponse.EncodableMessage;
//// var rp = CreateRelyingParty(store, opAuthResponse.RedirectUrl,
//// opAuthResponse.EncodedFields.ToNameValueCollection());
//// rp.Settings.RequireSsl = requireSsl;
//// // Get the response now, before trying the replay attack. The Response
//// // property is lazily-evaluated, so the replay attack can be evaluated first
//// // and pass, while this one that SUPPOSED to pass fails, if we don't force it now.
//// var response = rp.Response;
//// // Side-track to test for replay attack while we're at it.
//// // This simulates a network sniffing user who caught the
//// // authenticating query en route to either the user agent or
//// // the consumer, and tries the same query to the consumer in an
//// // attempt to spoof the identity of the authenticating user.
//// try {
//// Logger.Info("Attempting replay attack...");
//// var replayRP = CreateRelyingParty(store, opAuthResponse.RedirectUrl,
//// opAuthResponse.EncodedFields.ToNameValueCollection());
//// replayRP.Settings.RequireSsl = requireSsl;
//// Assert.AreNotEqual(AuthenticationStatus.Authenticated, replayRP.Response.Status, "Replay attack succeeded!");
//// } catch (OpenIdException) { // nonce already used
//// // another way to pass
//// }
//// // Return the result of the initial response (not the replay attack one).
//// return response;
////}
/////// <summary>
/////// Generates a new <see cref="OpenIdProvider"/> that uses the shared
/////// store in <see cref="ProviderStore"/>.
/////// </summary>
////internal static OpenIdProvider CreateProvider(NameValueCollection fields) {
//// return CreateProvider(fields, false);
////}
////internal static OpenIdProvider CreateProvider(NameValueCollection fields, bool useSsl) {
//// Protocol protocol = fields != null ? Protocol.Detect(fields.ToDictionary()) : Protocol.V20;
//// Uri opEndpoint = GetFullUrl(ProviderPage, null, useSsl);
//// var provider = new OpenIdProvider(ProviderStore, opEndpoint, opEndpoint, fields ?? new NameValueCollection());
//// return provider;
////}
////internal static OpenIdProvider CreateProviderForRequest(DotNetOpenId.RelyingParty.IAuthenticationRequest request) {
//// IResponse relyingPartyAuthenticationRequest = request.RedirectingResponse;
//// var rpWebMessageToOP = (Response)relyingPartyAuthenticationRequest;
//// var rpMessageToOP = (IndirectMessageRequest)rpWebMessageToOP.EncodableMessage;
//// var opEndpoint = (ServiceEndpoint)request.Provider;
//// var provider = new OpenIdProvider(ProviderStore, opEndpoint.ProviderEndpoint,
//// opEndpoint.ProviderEndpoint, rpMessageToOP.EncodedFields.ToNameValueCollection());
//// return provider;
////}
////internal static IResponse CreateProviderResponseToRequest(
//// DotNetOpenId.RelyingParty.IAuthenticationRequest request,
//// Action<DotNetOpenId.Provider.IAuthenticationRequest> prepareProviderResponse) {
//// {
//// // Sidetrack: Verify the return_to and realm URLs
//// var consumerToProviderQuery = HttpUtility.ParseQueryString(request.RedirectingResponse.ExtractUrl().Query);
//// Protocol protocol = Protocol.Detect(consumerToProviderQuery.ToDictionary());
//// Assert.IsTrue(consumerToProviderQuery[protocol.openid.return_to].StartsWith(request.ReturnToUrl.AbsoluteUri, StringComparison.Ordinal));
//// Assert.AreEqual(request.Realm.ToString(), consumerToProviderQuery[protocol.openid.Realm]);
//// }
//// var op = TestSupport.CreateProviderForRequest(request);
//// var opReq = (DotNetOpenId.Provider.IAuthenticationRequest)op.Request;
//// prepareProviderResponse(opReq);
//// Assert.IsTrue(opReq.IsResponseReady);
//// return opReq.Response;
////}
////internal static IAuthenticationResponse CreateRelyingPartyResponseThroughProvider(
//// DotNetOpenId.RelyingParty.IAuthenticationRequest request,
//// Action<DotNetOpenId.Provider.IAuthenticationRequest> providerAction) {
//// var rpReq = (AuthenticationRequest)request;
//// var opResponse = CreateProviderResponseToRequest(rpReq, providerAction);
//// // Be careful to use whatever store the original RP was using.
//// var rp = CreateRelyingPartyResponse(rpReq.RelyingParty.Store, opResponse,
//// ((AuthenticationRequest)request).RelyingParty.Settings.RequireSsl);
//// Assert.IsNotNull(rp);
//// return rp;
////}
////[SetUp]
////public void SetUp() {
//// log4net.Config.XmlConfigurator.Configure(Assembly.GetExecutingAssembly().GetManifestResourceStream("DotNetOpenId.Test.Logging.config"));
//// ResetStores();
////}
////[TearDown]
////public void TearDown() {
//// log4net.LogManager.Shutdown();
////}
////internal static void ResetStores() {
//// RelyingPartyStore = new ApplicationMemoryStore();
//// ProviderStore = new ProviderMemoryStore();
////}
////internal static void SetAuthenticationFromScenario(Scenarios scenario, DotNetOpenId.Provider.IAuthenticationRequest request) {
//// Assert.IsTrue(request.IsReturnUrlDiscoverable);
//// switch (scenario) {
//// case TestSupport.Scenarios.ExtensionFullCooperation:
//// case TestSupport.Scenarios.ExtensionPartialCooperation:
//// case TestSupport.Scenarios.AutoApproval:
//// // immediately approve
//// request.IsAuthenticated = true;
//// break;
//// case TestSupport.Scenarios.AutoApprovalAddFragment:
//// request.SetClaimedIdentifierFragment("frag");
//// request.IsAuthenticated = true;
//// break;
//// case TestSupport.Scenarios.ApproveOnSetup:
//// request.IsAuthenticated = !request.Immediate;
//// break;
//// case TestSupport.Scenarios.AlwaysDeny:
//// request.IsAuthenticated = false;
//// break;
//// default:
//// throw new InvalidOperationException("Unrecognized scenario");
//// }
////}
/////// <summary>
/////// Uses an RPs stored association to resign an altered message from a Provider,
/////// to simulate a Provider that deliberately sent a bad message in an attempt
/////// to thwart RP security.
/////// </summary>
////internal static void Resign(NameValueCollection nvc, IRelyingPartyApplicationStore store) {
//// Debug.Assert(nvc != null);
//// Debug.Assert(store != null);
//// var dict = Util.NameValueCollectionToDictionary(nvc);
//// Protocol protocol = Protocol.Detect(dict);
//// Uri providerEndpoint = new Uri(nvc[protocol.openid.op_endpoint]);
//// string assoc_handle = nvc[protocol.openid.assoc_handle];
//// Association assoc = store.GetAssociation(providerEndpoint, assoc_handle);
//// Debug.Assert(assoc != null, "Association not found in RP's store. Maybe you're communicating with a hosted OP instead of the TestSupport one?");
//// IList<string> signed = nvc[protocol.openid.signed].Split(',');
//// var subsetDictionary = new Dictionary<string, string>();
//// foreach (string signedKey in signed) {
//// string keyName = protocol.openid.Prefix + signedKey;
//// subsetDictionary.Add(signedKey, dict[keyName]);
//// }
//// nvc[protocol.openid.sig] = Convert.ToBase64String(assoc.Sign(subsetDictionary, signed));
////}
////public static IAssociationStore<AssociationRelyingPartyType> ProviderStoreContext {
//// get {
//// return DotNetOpenId.Provider.OpenIdProvider.HttpApplicationStore;
//// }
////}
////internal static MockIdentifier GetMockOPIdentifier(Scenarios scenario, UriIdentifier expectedClaimedId) {
//// return GetMockOPIdentifier(scenario, expectedClaimedId, false, false);
////}
////internal static MockIdentifier GetMockOPIdentifier(Scenarios scenario, UriIdentifier expectedClaimedId, bool useSslOpIdentifier, bool useSslProviderEndpoint) {
//// var fields = new Dictionary<string, string> {
//// { "user", scenario.ToString() },
////};
//// Uri opEndpoint = GetFullUrl(DirectedProviderEndpoint, fields, useSslProviderEndpoint);
//// Uri opIdentifier = GetOPIdentityUrl(scenario, useSslOpIdentifier);
//// ServiceEndpoint se = ServiceEndpoint.CreateForProviderIdentifier(
//// opIdentifier,
//// opEndpoint,
//// new string[] { Protocol.V20.OPIdentifierServiceTypeURI },
//// 10,
//// 10);
//// // Register the Claimed Identifier that directed identity will choose so that RP
//// // discovery on that identifier can be mocked up.
//// MockHttpRequest.RegisterMockXrdsResponse(expectedClaimedId, se);
//// return new MockIdentifier(opIdentifier, new ServiceEndpoint[] { se });
////}
}
}
|