summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenId/RelyingParty/ServiceEndpoint.cs
blob: b5ede551f325eef06b87d503f9c793d12a5c33b5 (plain)
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
using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.IO;
using DotNetOpenId.Yadis;
using System.Diagnostics;
using DotNetOpenId.Extensions;
using System.Globalization;

namespace DotNetOpenId.RelyingParty {
	/// <summary>
	/// Represents information discovered about a user-supplied Identifier.
	/// </summary>
	[DebuggerDisplay("ClaimedIdentifier: {ClaimedIdentifier}, ProviderEndpoint: {ProviderEndpoint}, OpenId: {Protocol.Version}")]
	internal class ServiceEndpoint : IXrdsProviderEndpoint {
		/// <summary>
		/// The URL which accepts OpenID Authentication protocol messages.
		/// </summary>
		/// <remarks>
		/// Obtained by performing discovery on the User-Supplied Identifier. 
		/// This value MUST be an absolute HTTP or HTTPS URL.
		/// </remarks>
		public Uri ProviderEndpoint { get; private set; }
		/// <summary>
		/// Returns true if the <see cref="ProviderEndpoint"/> is using an encrypted channel.
		/// </summary>
		internal bool IsSecure {
			get { return string.Equals(ProviderEndpoint.Scheme, Uri.UriSchemeHttps, StringComparison.OrdinalIgnoreCase); }
		}
		Uri IProviderEndpoint.Uri { get { return ProviderEndpoint; } }
		/*
		/// <summary>
		/// An Identifier for an OpenID Provider.
		/// </summary>
		public Identifier ProviderIdentifier { get; private set; }
		*/
		/// <summary>
		/// An Identifier that was presented by the end user to the Relying Party, 
		/// or selected by the user at the OpenID Provider. 
		/// During the initiation phase of the protocol, an end user may enter 
		/// either their own Identifier or an OP Identifier. If an OP Identifier 
		/// is used, the OP may then assist the end user in selecting an Identifier 
		/// to share with the Relying Party.
		/// </summary>
		public Identifier UserSuppliedIdentifier { get; private set; }
		/// <summary>
		/// The Identifier that the end user claims to own.
		/// </summary>
		public Identifier ClaimedIdentifier { get; private set; }
		/// <summary>
		/// An alternate Identifier for an end user that is local to a 
		/// particular OP and thus not necessarily under the end user's 
		/// control.
		/// </summary>
		public Identifier ProviderLocalIdentifier { get; private set; }
		string friendlyIdentifierForDisplay;
		/// <summary>
		/// Supports the <see cref="IAuthenticationResponse.FriendlyIdentifierForDisplay"/> property.
		/// </summary>
		public string FriendlyIdentifierForDisplay {
			get {
				if (friendlyIdentifierForDisplay == null) {
					XriIdentifier xri = ClaimedIdentifier as XriIdentifier;
					UriIdentifier uri = ClaimedIdentifier as UriIdentifier;
					if (xri != null) {
						if (UserSuppliedIdentifier == null || String.Equals(UserSuppliedIdentifier, ClaimedIdentifier, StringComparison.OrdinalIgnoreCase)) {
							friendlyIdentifierForDisplay = ClaimedIdentifier;
						} else {
							friendlyIdentifierForDisplay = UserSuppliedIdentifier;
						}
					} else if (uri != null) {
						if (uri != Protocol.ClaimedIdentifierForOPIdentifier) {
							string displayUri = uri.Uri.Authority + uri.Uri.PathAndQuery;
							displayUri = displayUri.TrimEnd('/');
							// Multi-byte unicode characters get encoded by the Uri class for transit.
							// Since this is for display purposes, we want to reverse this and display a readable
							// representation of these foreign characters.  
							friendlyIdentifierForDisplay = Uri.UnescapeDataString(displayUri);
						}
					} else {
						Debug.Fail("Doh!  We never should have reached here.");
						friendlyIdentifierForDisplay = ClaimedIdentifier;
					}
				}
				return friendlyIdentifierForDisplay;
			}
		}
		/// <summary>
		/// Gets the list of services available at this OP Endpoint for the
		/// claimed Identifier.  May be null.
		/// </summary>
		public string[] ProviderSupportedServiceTypeUris { get; private set; }

		ServiceEndpoint(Identifier claimedIdentifier, Identifier userSuppliedIdentifier,
			Uri providerEndpoint, Identifier providerLocalIdentifier,
			string[] providerSupportedServiceTypeUris, int? servicePriority, int? uriPriority) {
			if (claimedIdentifier == null) throw new ArgumentNullException("claimedIdentifier");
			if (providerEndpoint == null) throw new ArgumentNullException("providerEndpoint");
			if (providerSupportedServiceTypeUris == null) throw new ArgumentNullException("providerSupportedServiceTypeUris");
			ClaimedIdentifier = claimedIdentifier;
			UserSuppliedIdentifier = userSuppliedIdentifier;
			ProviderEndpoint = providerEndpoint;
			ProviderLocalIdentifier = providerLocalIdentifier ?? claimedIdentifier;
			ProviderSupportedServiceTypeUris = providerSupportedServiceTypeUris;
			this.servicePriority = servicePriority;
			this.uriPriority = uriPriority;
		}
		/// <summary>
		/// Used for deserializing <see cref="ServiceEndpoint"/> from authentication responses.
		/// </summary>
		ServiceEndpoint(Identifier claimedIdentifier, Identifier userSuppliedIdentifier, 
			Uri providerEndpoint, Identifier providerLocalIdentifier, Protocol protocol) {
			ClaimedIdentifier = claimedIdentifier;
			UserSuppliedIdentifier = userSuppliedIdentifier;
			ProviderEndpoint = providerEndpoint;
			ProviderLocalIdentifier = providerLocalIdentifier ?? claimedIdentifier;
			this.protocol = protocol;
		}

		internal static ServiceEndpoint CreateForProviderIdentifier(
			Identifier providerIdentifier, Uri providerEndpoint,
			string[] providerSupportedServiceTypeUris, int? servicePriority, int? uriPriority) {

			Protocol protocol = Protocol.Detect(providerSupportedServiceTypeUris);

			return new ServiceEndpoint(protocol.ClaimedIdentifierForOPIdentifier, providerIdentifier,
				providerEndpoint, protocol.ClaimedIdentifierForOPIdentifier,
				providerSupportedServiceTypeUris, servicePriority, uriPriority);
		}

		internal static ServiceEndpoint CreateForClaimedIdentifier(
			Identifier claimedIdentifier, Identifier providerLocalIdentifier,
			Uri providerEndpoint,
			string[] providerSupportedServiceTypeUris, int? servicePriority, int? uriPriority) {

			return CreateForClaimedIdentifier(claimedIdentifier, null, providerLocalIdentifier, 
				providerEndpoint, providerSupportedServiceTypeUris, servicePriority, uriPriority);
		}

		internal static ServiceEndpoint CreateForClaimedIdentifier(
			Identifier claimedIdentifier, Identifier userSuppliedIdentifier, Identifier providerLocalIdentifier,
			Uri providerEndpoint,
			string[] providerSupportedServiceTypeUris, int? servicePriority, int? uriPriority) {

			return new ServiceEndpoint(claimedIdentifier, userSuppliedIdentifier, providerEndpoint,
				providerLocalIdentifier, providerSupportedServiceTypeUris, servicePriority, uriPriority);
		}

		Protocol protocol;
		/// <summary>
		/// Gets the OpenID protocol used by the Provider.
		/// </summary>
		public Protocol Protocol {
			get {
				if (protocol == null) {
					protocol = Protocol.Detect(ProviderSupportedServiceTypeUris);
				}
				if (protocol != null) return protocol;
				throw new InvalidOperationException("Unable to determine the version of OpenID the Provider supports.");
			}
		}

		public bool IsTypeUriPresent(string typeUri) {
			return IsExtensionSupported(typeUri);
		}

		public bool IsExtensionSupported(string extensionUri) {
			if (ProviderSupportedServiceTypeUris == null)
				throw new InvalidOperationException("Cannot lookup extension support on a rehydrated ServiceEndpoint.");
			return Array.IndexOf(ProviderSupportedServiceTypeUris, extensionUri) >= 0;
		}

		public bool IsExtensionSupported(IExtension extension) {
			if (extension == null) throw new ArgumentNullException("extension");

			// Consider the primary case.
			if (IsExtensionSupported(extension.TypeUri)) {
				return true;
			}
			// Consider the secondary cases.
			if (extension.AdditionalSupportedTypeUris != null) {
				foreach (string extensionTypeUri in extension.AdditionalSupportedTypeUris) {
					if (IsExtensionSupported(extensionTypeUri)) {
						return true;
					}
				}
			}
			return false;
		}

		public bool IsExtensionSupported<T>() where T : Extensions.IExtension, new() {
			T extension = new T();
			return IsExtensionSupported(extension);
		}

		public bool IsExtensionSupported(Type extensionType) {
			if (extensionType == null) throw new ArgumentNullException("extensionType");
			if (!typeof(Extensions.IExtension).IsAssignableFrom(extensionType))
				throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,
					Strings.TypeMustImplementX, typeof(Extensions.IExtension).FullName),
					"extensionType");
			var extension = (Extensions.IExtension)Activator.CreateInstance(extensionType);
			return IsExtensionSupported(extension);
		}

		Version IProviderEndpoint.Version { get { return Protocol.Version; } }

		/// <summary>
		/// Saves the discovered information about this endpoint
		/// for later comparison to validate assertions.
		/// </summary>
		internal void Serialize(TextWriter writer) {
			writer.WriteLine(ClaimedIdentifier);
			writer.WriteLine(ProviderLocalIdentifier);
			writer.WriteLine(UserSuppliedIdentifier);
			writer.WriteLine(ProviderEndpoint);
			writer.WriteLine(Protocol.Version);
			// No reason to serialize priority. We only needed priority to decide whether to use this endpoint.
		}

		/// <summary>
		/// Reads previously discovered information about an endpoint
		/// from a solicited authentication assertion for validation.
		/// </summary>
		/// <returns>
		/// A <see cref="ServiceEndpoint"/> object that has everything
		/// except the <see cref="ProviderSupportedServiceTypeUris"/>
		/// deserialized.
		/// </returns>
		internal static ServiceEndpoint Deserialize(TextReader reader) {
			var claimedIdentifier = Identifier.Parse(reader.ReadLine());
			var providerLocalIdentifier = Identifier.Parse(reader.ReadLine());
			string userSuppliedIdentifier = reader.ReadLine();
			if (userSuppliedIdentifier.Length == 0) userSuppliedIdentifier = null;
			var providerEndpoint = new Uri(reader.ReadLine());
			var protocol = Util.FindBestVersion(p => p.Version, new[] { new Version(reader.ReadLine()) });
			return new ServiceEndpoint(claimedIdentifier, userSuppliedIdentifier,
				providerEndpoint, providerLocalIdentifier, protocol);
		}
		internal static ServiceEndpoint ParseFromAuthResponse(IDictionary<string, string> query, Identifier userSuppliedIdentifier) {
			Protocol protocol = Protocol.Detect(query);
			Debug.Assert(protocol.openid.op_endpoint != null, "This method should only be called in OpenID 2.0 contexts.");
			return new ServiceEndpoint(
				Util.GetRequiredArg(query, protocol.openid.claimed_id),
				userSuppliedIdentifier,
				new Uri(Util.GetRequiredArg(query, protocol.openid.op_endpoint)),
				Util.GetRequiredArg(query, protocol.openid.identity),
				protocol);
		}

		public static bool operator ==(ServiceEndpoint se1, ServiceEndpoint se2) {
			if ((object)se1 == null ^ (object)se2 == null) return false;
			if ((object)se1 == null) return true;
			return se1.Equals(se2);
		}
		public static bool operator !=(ServiceEndpoint se1, ServiceEndpoint se2) {
			return !(se1 == se2);
		}
		public override bool Equals(object obj) {
			ServiceEndpoint other = obj as ServiceEndpoint;
			if (other == null) return false;
			// We specifically do not check our ProviderSupportedServiceTypeUris array
			// or the priority field
			// as that is not persisted in our tokens, and it is not part of the 
			// important assertion validation that is part of the spec.
			return
				this.ClaimedIdentifier == other.ClaimedIdentifier &&
				this.ProviderEndpoint == other.ProviderEndpoint &&
				this.ProviderLocalIdentifier == other.ProviderLocalIdentifier &&
				this.Protocol == other.Protocol;
		}
		public override int GetHashCode() {
			return ClaimedIdentifier.GetHashCode();
		}
		public override string ToString() {
			StringBuilder builder = new StringBuilder();
			builder.AppendLine("ClaimedIdentifier: " + ClaimedIdentifier);
			builder.AppendLine("ProviderLocalIdentifier: " + ProviderLocalIdentifier);
			builder.AppendLine("ProviderEndpoint: " + ProviderEndpoint.AbsoluteUri);
			builder.AppendLine("OpenID version: " + Protocol.Version);
			builder.AppendLine("Service Type URIs:");
			if (ProviderSupportedServiceTypeUris != null) {
				foreach (string serviceTypeUri in ProviderSupportedServiceTypeUris) {
					builder.Append("\t");
					var matchingExtension = Util.FirstOrDefault(ExtensionManager.RequestExtensions, ext => ext.Key.TypeUri == serviceTypeUri);
					if (matchingExtension.Key != null) {
						builder.AppendLine(string.Format(CultureInfo.CurrentCulture, "{0} ({1})", serviceTypeUri, matchingExtension.Value));
					} else {
						builder.AppendLine(serviceTypeUri);
					}
				}
			} else {
				builder.AppendLine("\t(unavailable)");
			}
			builder.Length -= Environment.NewLine.Length; // trim last newline
			return builder.ToString();
		}

		#region IXrdsProviderEndpoint Members

		private int? servicePriority;
		/// <summary>
		/// Gets the priority associated with this service that may have been given
		/// in the XRDS document.
		/// </summary>
		int? IXrdsProviderEndpoint.ServicePriority {
			get { return servicePriority; }
		}
		private int? uriPriority;
		/// <summary>
		/// Gets the priority associated with the service endpoint URL.
		/// </summary>
		int? IXrdsProviderEndpoint.UriPriority {
			get { return uriPriority; }
		}

		#endregion
	}
}