summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenId/RelyingParty/ServiceEndpoint.cs
blob: cbc5c0782d67f27bdde149f4fb34878ecb9a9e24 (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
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 : IProviderEndpoint {
		/// <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; }
		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; }
		/// <summary>
		/// Gets the list of services available at this OP Endpoint for the
		/// claimed Identifier.
		/// </summary>
		public string[] ProviderSupportedServiceTypeUris { get; private set; }

		internal ServiceEndpoint(Identifier claimedIdentifier, Uri providerEndpoint,
			Identifier providerLocalIdentifier, string[] providerSupportedServiceTypeUris) {
			if (claimedIdentifier == null) throw new ArgumentNullException("claimedIdentifier");
			if (providerEndpoint == null) throw new ArgumentNullException("providerEndpoint");
			if (providerSupportedServiceTypeUris == null) throw new ArgumentNullException("providerSupportedServiceTypeUris");
			ClaimedIdentifier = claimedIdentifier;
			ProviderEndpoint = providerEndpoint;
			ProviderLocalIdentifier = providerLocalIdentifier ?? claimedIdentifier;
			ProviderSupportedServiceTypeUris = providerSupportedServiceTypeUris;
		}
		ServiceEndpoint(Identifier claimedIdentifier, Uri providerEndpoint,
			Identifier providerLocalIdentifier, Protocol protocol) {
			ClaimedIdentifier = claimedIdentifier;
			ProviderEndpoint = providerEndpoint;
			ProviderLocalIdentifier = providerLocalIdentifier ?? claimedIdentifier;
			this.protocol = protocol;
		}

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

		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(ProviderEndpoint);
			writer.WriteLine(Protocol.Version);
		}

		/// <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());
			var providerEndpoint = new Uri(reader.ReadLine());
			var protocol = Util.FindBestVersion(p => p.Version, new[] { new Version(reader.ReadLine()) });
			return new ServiceEndpoint(claimedIdentifier, providerEndpoint,
				providerLocalIdentifier, protocol);
		}
		internal static ServiceEndpoint ParseFromAuthResponse(IDictionary<string, string> query) {
			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),
				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
			// 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() {
			return ProviderEndpoint.AbsoluteUri;
		}
	}
}