summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenId/Provider/CheckIdRequest.cs
blob: fa172006ada39820372c9643d2e441b0f9c2cf21 (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
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Collections.Generic;
using System.Globalization;
using System.Web;
using System.Net;

namespace DotNetOpenId.Provider {
	/// <summary>
	/// A request to confirm the identity of a user.
	/// </summary>
	/// <remarks>
	/// This class handles requests for openid modes checkid_immediate and checkid_setup.
	/// </remarks>
	[DebuggerDisplay("Mode: {Mode}, IsAuthenticated: {IsAuthenticated}, LocalIdentifier: {LocalIdentifier}, OpenId: {Protocol.Version}")]
	class CheckIdRequest : AssociatedRequest, IAuthenticationRequest {
		bool? isAuthenticated;
		/// <summary>
		/// Gets/sets whether the provider has determined that the 
		/// <see cref="ClaimedIdentifier"/> belongs to the currently logged in user
		/// and wishes to share this information with the consumer.
		/// </summary>
		public bool? IsAuthenticated {
			get { return isAuthenticated; }
			set {
				isAuthenticated = value;
				InvalidateResponse();
			}
		}

		/// <summary>
		/// Whether the consumer demands an immediate response.
		/// If false, the consumer is willing to wait for the identity provider
		/// to authenticate the user.
		/// </summary>
		public bool Immediate { get; private set; }
		/// <summary>
		/// The URL the consumer site claims to use as its 'base' address.
		/// </summary>
		public Realm Realm { get; private set; }
		bool? isReturnUrlDiscoverable;
		/// <summary>
		/// Whether verification of the return URL claimed by the Relying Party
		/// succeeded.
		/// </summary>
		/// <remarks>
		/// This property will never throw a WebException or OpenIdException.  Any failures
		/// occuring during return URL verification results in a false value being returned.
		/// Details regarding failure may be found in the trace log.
		/// </remarks>
		public bool IsReturnUrlDiscoverable {
			get {
				Debug.Assert(Realm != null);
				if (!isReturnUrlDiscoverable.HasValue) {
					isReturnUrlDiscoverable = false; // assume not until we succeed
					try {
						foreach (var returnUrl in Realm.Discover(false)) {
							Realm discoveredReturnToUrl = returnUrl.RelyingPartyEndpoint;
							// The spec requires that the return_to URLs given in an RPs XRDS doc
							// do not contain wildcards.
							if (discoveredReturnToUrl.DomainWildcard) {
								Logger.WarnFormat("Realm {0} contained return_to URL {1} which contains a wildcard, which is not allowed.",
										Realm, discoveredReturnToUrl);
								continue;
							}
							// Use the same rules as return_to/realm matching to check whether this
							// URL fits the return_to URL we were given.
							if (discoveredReturnToUrl.Contains(ReturnTo)) {
								isReturnUrlDiscoverable = true;
								break; // no need to keep looking after we find a match
							}
						}
					} catch (OpenIdException ex) {
						Logger.InfoFormat("Relying party discovery at URL {0} failed.  {1}",
								Realm, ex);
						// Don't do anything else.  We quietly fail at return_to verification and return false.
					} catch (WebException ex) {
						Logger.InfoFormat("Relying party discovery at URL {0} failed.  {1}",
								Realm, ex);
						// Don't do anything else.  We quietly fail at return_to verification and return false.
					}
				}
				return isReturnUrlDiscoverable.Value;
			}
		}
		/// <summary>
		/// Whether the Provider should help the user select a Claimed Identifier
		/// to send back to the relying party.
		/// </summary>
		public bool IsDirectedIdentity { get; private set; }
		/// <summary>
		/// A value indicating whether the requesting Relying Party is using a delegated URL.
		/// </summary>
		/// <remarks>
		/// When delegated identifiers are used, the <see cref="ClaimedIdentifier"/> should not
		/// be changed at the Provider during authentication.
		/// Delegation is only detectable on requests originating from OpenID 2.0 relying parties.
		/// A relying party implementing only OpenID 1.x may use delegation and this property will
		/// return false anyway.
		/// </remarks>
		public bool IsDelegatedIdentifier { get; private set; }
		Identifier localIdentifier;
		/// <summary>
		/// The user identifier used by this particular provider.
		/// </summary>
		public Identifier LocalIdentifier {
			get { return localIdentifier; }
			set {
				// Keep LocalIdentifier and ClaimedIdentifier in sync for directed identity.
				if (IsDirectedIdentity) {
					if (ClaimedIdentifier != null && ClaimedIdentifier != value) {
						throw new InvalidOperationException(Strings.IdentifierSelectRequiresMatchingIdentifiers);
					}

					claimedIdentifier = value;
				}

				localIdentifier = value;
			}
		}
		Identifier claimedIdentifier;
		/// <summary>
		/// The identifier this user is claiming to control.  
		/// </summary>
		public Identifier ClaimedIdentifier {
			get { return claimedIdentifier; }
			set {
				// Keep LocalIdentifier and ClaimedIdentifier in sync for directed identity.
				if (IsDirectedIdentity) {
					if (LocalIdentifier != null && LocalIdentifier != value) {
						throw new InvalidOperationException(Strings.IdentifierSelectRequiresMatchingIdentifiers);
					}

					localIdentifier = value;
				}

				if (IsDelegatedIdentifier) {
					throw new InvalidOperationException(Strings.ClaimedIdentifierCannotBeSetOnDelegatedAuthentication);
				}
				
				claimedIdentifier = value;
			}
		}

		/// <summary>
		/// Adds an optional fragment (#fragment) portion to a URI ClaimedIdentifier.
		/// Useful for identifier recycling.
		/// </summary>
		/// <param name="fragment">
		/// Should not include the # prefix character as that will be added internally.
		/// May be null or the empty string to clear a previously set fragment.
		/// </param>
		/// <remarks>
		/// <para>Unlike the <see cref="ClaimedIdentifier"/> property, which can only be set if
		/// using directed identity, this method can be called on any URI claimed identifier.</para>
		/// <para>Because XRI claimed identifiers (the canonical IDs) are never recycled,
		/// this method should<i>not</i> be called for XRIs.</para>
		/// </remarks>
		/// <exception cref="InvalidOperationException">
		/// Thrown when this method is called on an XRI, or on a directed identity request
		/// before the <see cref="ClaimedIdentifier"/> property is set.</exception>
		public void SetClaimedIdentifierFragment(string fragment) {
			if (IsDirectedIdentity && ClaimedIdentifier == null) {
				throw new InvalidOperationException(Strings.ClaimedIdentifierMustBeSetFirst);
			}
			if (ClaimedIdentifier is XriIdentifier) {
				throw new InvalidOperationException(Strings.FragmentNotAllowedOnXRIs);
			}

			UriBuilder builder = new UriBuilder(ClaimedIdentifier);
			builder.Fragment = fragment;
			claimedIdentifier = builder.Uri;
		}

		/// <summary>
		/// The URL to redirect the user agent to after the authentication attempt.
		/// This must fall "under" the realm URL.
		/// </summary>
		internal Uri ReturnTo { get; private set; }
		internal override string Mode {
			get { return Immediate ? Protocol.Args.Mode.checkid_immediate : Protocol.Args.Mode.checkid_setup; }
		}
		/// <summary>
		/// Indicates whether this request has all the information necessary to formulate a response.
		/// </summary>
		public override bool IsResponseReady {
			get { 
				// The null checks on the identifiers is to make sure that an identifier_select
				// has been resolved to actual identifiers.
				return IsAuthenticated.HasValue &&
					(!IsAuthenticated.Value || !IsDirectedIdentity || (LocalIdentifier != null && ClaimedIdentifier != null));
			}
		}

		[SuppressMessage("Microsoft.Performance", "CA1805:DoNotInitializeUnnecessarily")]
		internal CheckIdRequest(OpenIdProvider provider) : base(provider) {
			// handle the mandatory protocol fields
			string mode = Util.GetRequiredArg(Query, Protocol.openid.mode);
			if (Protocol.Args.Mode.checkid_immediate.Equals(mode, StringComparison.Ordinal)) {
				Immediate = true;
			} else if (Protocol.Args.Mode.checkid_setup.Equals(mode, StringComparison.Ordinal)) {
				Immediate = false; // implied
			} else {
				throw new OpenIdException(string.Format(CultureInfo.CurrentCulture,
					Strings.InvalidOpenIdQueryParameterValue, Protocol.openid.mode, mode), Query);
			}

			// The spec says claimed_id and identity can both be either present or
			// absent.  But for now we don't have or support extensions that don't
			// use these parameters, so we require them.  In the future that may change.
			if (Protocol.Version.Major >= 2) {
				claimedIdentifier = Util.GetRequiredIdentifierArg(Query, Protocol.openid.claimed_id);
			}
			localIdentifier = Util.GetRequiredIdentifierArg(Query, Protocol.openid.identity);
			// The spec says return_to is optional, but what good is authenticating
			// a user if the user won't be sent back?
			ReturnTo = Util.GetRequiredUriArg(Query, Protocol.openid.return_to);
			Realm = Util.GetOptionalRealmArg(Query, Protocol.openid.Realm) ?? ReturnTo;
			AssociationHandle = Util.GetOptionalArg(Query, Protocol.openid.assoc_handle);

			if (!Realm.Contains(ReturnTo)) {
				throw new OpenIdException(string.Format(CultureInfo.CurrentCulture,
					Strings.ReturnToNotUnderRealm, ReturnTo.AbsoluteUri, Realm), Query);
			}

			if (Protocol.Version.Major >= 2) {
				if (LocalIdentifier == Protocol.ClaimedIdentifierForOPIdentifier ^
					ClaimedIdentifier == Protocol.ClaimedIdentifierForOPIdentifier) {
					throw new OpenIdException(string.Format(CultureInfo.CurrentCulture,
						Strings.MatchingArgumentsExpected, Protocol.openid.claimed_id,
						Protocol.openid.identity, Protocol.ClaimedIdentifierForOPIdentifier),
						Query);
				}
			}

			if (ClaimedIdentifier == Protocol.ClaimedIdentifierForOPIdentifier &&
				Protocol.ClaimedIdentifierForOPIdentifier != null) {
				// Force the OP to deal with identifier_select by nulling out the two identifiers.
				IsDirectedIdentity = true;
				claimedIdentifier = null;
				localIdentifier = null;
			}

			// URL delegation is only detectable from 2.0 RPs, since openid.claimed_id isn't included from 1.0 RPs.
			// If the openid.claimed_id is present, and if it's different than the openid.identity argument, then
			// the RP has discovered a claimed identifier that has delegated authentication to this Provider.
			IsDelegatedIdentifier = ClaimedIdentifier != null && ClaimedIdentifier != LocalIdentifier;
		}

		protected override IEncodable CreateResponse() {
			Debug.Assert(IsAuthenticated.HasValue, "This should be checked internally before CreateResponse is called.");
			return AssertionMessage.CreateAssertion(this);
		}

		/// <summary>
		/// Encode this request as a URL to GET.
		/// Only used in response to immediate auth requests from OpenID 1.x RPs.
		/// </summary>
		internal Uri SetupUrl {
			get {
				if (Protocol.Version.Major >= 2) {
					Debug.Fail("This property only applicable to OpenID 1.x RPs.");
					throw new InvalidOperationException();
				}
				Debug.Assert(Provider.Endpoint != null, "The OpenIdProvider should have guaranteed this.");
				var q = new Dictionary<string, string>();

				q.Add(Protocol.openid.mode, Protocol.Args.Mode.checkid_setup);
				q.Add(Protocol.openid.identity, LocalIdentifier.ToString());
				q.Add(Protocol.openid.return_to, ReturnTo.AbsoluteUri);

				if (Realm != null)
					q.Add(Protocol.openid.Realm, Realm);

				if (this.AssociationHandle != null)
					q.Add(Protocol.openid.assoc_handle, this.AssociationHandle);

				UriBuilder builder = new UriBuilder(Provider.Endpoint);
				UriUtil.AppendQueryArgs(builder, q);

				return builder.Uri;
			}
		}

		public override string ToString() {
			string returnString = @"
CheckIdRequest.Immediate = '{0}'
CheckIdRequest.Realm = '{1}'
CheckIdRequest.Identity = '{2}' 
CheckIdRequest._mode = '{3}' 
CheckIdRequest.ReturnTo = '{4}' 
";

			return base.ToString() + string.Format(CultureInfo.CurrentCulture,
				returnString, Immediate, Realm, LocalIdentifier, Mode, ReturnTo);
		}
	}
}