1
//-----------------------------------------------------------------------
2 // <copyright file="OpenIdRelyingParty.cs" company="Andrew Arnott">
3 // Copyright (c) Andrew Arnott. All rights reserved.
5 //-----------------------------------------------------------------------
7 namespace DotNetOpenAuth
.OpenId
.RelyingParty
{
9 using System
.Collections
.Generic
;
10 using System
.Collections
.Specialized
;
11 using System
.ComponentModel
;
14 using DotNetOpenAuth
.Configuration
;
15 using DotNetOpenAuth
.Messaging
;
16 using DotNetOpenAuth
.Messaging
.Bindings
;
17 using DotNetOpenAuth
.OpenId
.ChannelElements
;
18 using DotNetOpenAuth
.OpenId
.Messages
;
21 /// A delegate that decides whether a given OpenID Provider endpoint may be
22 /// considered for authenticating a user.
24 /// <param name="endpoint">The endpoint for consideration.</param>
26 /// <c>True</c> if the endpoint should be considered.
27 /// <c>False</c> to remove it from the pool of acceptable providers.
29 public delegate bool EndpointSelector(IXrdsProviderEndpoint endpoint
);
32 /// Provides the programmatic facilities to act as an OpenId consumer.
34 public sealed class OpenIdRelyingParty
{
36 /// The name of the key to use in the HttpApplication cache to store the
37 /// instance of <see cref="StandardRelyingPartyApplicationStore"/> to use.
39 private const string ApplicationStoreKey
= "DotNetOpenAuth.OpenId.RelyingParty.OpenIdRelyingParty.ApplicationStore";
42 /// Backing field for the <see cref="SecuritySettings"/> property.
44 private RelyingPartySecuritySettings securitySettings
;
47 /// Backing store for the <see cref="EndpointOrder"/> property.
49 private Comparison
<IXrdsProviderEndpoint
> endpointOrder
= DefaultEndpointOrder
;
52 /// Initializes a new instance of the <see cref="OpenIdRelyingParty"/> class.
54 public OpenIdRelyingParty()
55 : this(DotNetOpenAuth
.Configuration
.RelyingPartySection
.Configuration
.ApplicationStore
.CreateInstance(HttpApplicationStore
)) {
59 /// Initializes a new instance of the <see cref="OpenIdRelyingParty"/> class.
61 /// <param name="applicationStore">The application store. If null, the relying party will always operate in "dumb mode".</param>
62 public OpenIdRelyingParty(IRelyingPartyApplicationStore applicationStore
)
63 : this(applicationStore
, applicationStore
, applicationStore
) {
67 /// Initializes a new instance of the <see cref="OpenIdRelyingParty"/> class.
69 /// <param name="associationStore">The association store. If null, the relying party will always operate in "dumb mode".</param>
70 /// <param name="nonceStore">The nonce store to use. If null, the relying party will always operate in "dumb mode".</param>
71 /// <param name="secretStore">The secret store to use. If null, the relying party will always operate in "dumb mode".</param>
72 private OpenIdRelyingParty(IAssociationStore
<Uri
> associationStore
, INonceStore nonceStore
, IPrivateSecretStore secretStore
) {
73 // If we are a smart-mode RP (supporting associations), then we MUST also be
74 // capable of storing nonces to prevent replay attacks.
75 // If we're a dumb-mode RP, then 2.0 OPs are responsible for preventing replays.
76 ErrorUtilities
.VerifyArgument(associationStore
== null || nonceStore
!= null, OpenIdStrings
.AssociationStoreRequiresNonceStore
);
78 this.Channel
= new OpenIdChannel(associationStore
, nonceStore
, secretStore
);
79 this.AssociationStore
= associationStore
;
80 this.SecuritySettings
= RelyingPartySection
.Configuration
.SecuritySettings
.CreateSecuritySettings();
82 // Without a nonce store, we must rely on the Provider to protect against
83 // replay attacks. But only 2.0+ Providers can be expected to provide
85 if (nonceStore
== null) {
86 this.SecuritySettings
.MinimumRequiredOpenIdVersion
= ProtocolVersion
.V20
;
91 /// Gets an XRDS sorting routine that uses the XRDS Service/@Priority
92 /// attribute to determine order.
95 /// Endpoints lacking any priority value are sorted to the end of the list.
97 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
98 public static Comparison
<IXrdsProviderEndpoint
> DefaultEndpointOrder
{
100 // Sort first by service type (OpenID 2.0, 1.1, 1.0),
101 // then by Service/@priority, then by Service/Uri/@priority
102 return (se1
, se2
) => {
103 int result
= GetEndpointPrecedenceOrderByServiceType(se1
).CompareTo(GetEndpointPrecedenceOrderByServiceType(se2
));
107 if (se1
.ServicePriority
.HasValue
&& se2
.ServicePriority
.HasValue
) {
108 result
= se1
.ServicePriority
.Value
.CompareTo(se2
.ServicePriority
.Value
);
112 if (se1
.UriPriority
.HasValue
&& se2
.UriPriority
.HasValue
) {
113 return se1
.UriPriority
.Value
.CompareTo(se2
.UriPriority
.Value
);
114 } else if (se1
.UriPriority
.HasValue
) {
116 } else if (se2
.UriPriority
.HasValue
) {
122 if (se1
.ServicePriority
.HasValue
) {
124 } else if (se2
.ServicePriority
.HasValue
) {
127 // neither service defines a priority, so base ordering by uri priority.
128 if (se1
.UriPriority
.HasValue
&& se2
.UriPriority
.HasValue
) {
129 return se1
.UriPriority
.Value
.CompareTo(se2
.UriPriority
.Value
);
130 } else if (se1
.UriPriority
.HasValue
) {
132 } else if (se2
.UriPriority
.HasValue
) {
144 /// Gets the standard state storage mechanism that uses ASP.NET's
145 /// HttpApplication state dictionary to store associations and nonces.
147 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
148 public static IRelyingPartyApplicationStore HttpApplicationStore
{
150 HttpContext context
= HttpContext
.Current
;
151 ErrorUtilities
.VerifyOperation(context
!= null, OpenIdStrings
.StoreRequiredWhenNoHttpContextAvailable
, typeof(IRelyingPartyApplicationStore
).Name
);
152 var store
= (IRelyingPartyApplicationStore
)context
.Application
[ApplicationStoreKey
];
154 context
.Application
.Lock();
156 if ((store
= (IRelyingPartyApplicationStore
)context
.Application
[ApplicationStoreKey
]) == null) {
157 context
.Application
[ApplicationStoreKey
] = store
= new StandardRelyingPartyApplicationStore();
160 context
.Application
.UnLock();
169 /// Gets the channel to use for sending/receiving messages.
171 public Channel Channel { get; internal set; }
174 /// Gets the security settings used by this Relying Party.
176 public RelyingPartySecuritySettings SecuritySettings
{
178 return this.securitySettings
;
183 throw new ArgumentNullException("value");
186 this.securitySettings
= value;
191 /// Gets or sets the optional Provider Endpoint filter to use.
194 /// Provides a way to optionally filter the providers that may be used in authenticating a user.
195 /// If provided, the delegate should return true to accept an endpoint, and false to reject it.
196 /// If null, all identity providers will be accepted. This is the default.
198 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
199 public EndpointSelector EndpointFilter { get; set; }
202 /// Gets or sets the ordering routine that will determine which XRDS
203 /// Service element to try first
205 /// <value>Default is <see cref="DefaultEndpointOrder"/>.</value>
207 /// This may never be null. To reset to default behavior this property
208 /// can be set to the value of <see cref="DefaultEndpointOrder"/>.
210 [EditorBrowsable(EditorBrowsableState
.Advanced
)]
211 public Comparison
<IXrdsProviderEndpoint
> EndpointOrder
{
213 return this.endpointOrder
;
217 ErrorUtilities
.VerifyArgumentNotNull(value, "value");
218 this.endpointOrder
= value;
223 /// Gets the association store.
225 internal IAssociationStore
<Uri
> AssociationStore { get; private set; }
228 /// Gets a value indicating whether this Relying Party can sign its return_to
229 /// parameter in outgoing authentication requests.
231 internal bool CanSignCallbackArguments
{
232 get { return this.Channel.BindingElements.OfType<ReturnToSignatureBindingElement>().Any(); }
236 /// Gets the web request handler to use for discovery and the part of
237 /// authentication where direct messages are sent to an untrusted remote party.
239 internal IDirectWebRequestHandler WebRequestHandler
{
240 get { return this.Channel.WebRequestHandler; }
244 /// Creates an authentication request to verify that a user controls
245 /// some given Identifier.
247 /// <param name="userSuppliedIdentifier">
248 /// The Identifier supplied by the user. This may be a URL, an XRI or i-name.
250 /// <param name="realm">
251 /// The shorest URL that describes this relying party web site's address.
252 /// For example, if your login page is found at https://www.example.com/login.aspx,
253 /// your realm would typically be https://www.example.com/.
255 /// <param name="returnToUrl">
256 /// The URL of the login page, or the page prepared to receive authentication
257 /// responses from the OpenID Provider.
260 /// An authentication request object that describes the HTTP response to
261 /// send to the user agent to initiate the authentication.
263 /// <exception cref="ProtocolException">Thrown if no OpenID endpoint could be found.</exception>
264 public IAuthenticationRequest
CreateRequest(Identifier userSuppliedIdentifier
, Realm realm
, Uri returnToUrl
) {
266 return this.CreateRequests(userSuppliedIdentifier
, realm
, returnToUrl
).First();
267 } catch (InvalidOperationException ex
) {
268 throw ErrorUtilities
.Wrap(ex
, OpenIdStrings
.OpenIdEndpointNotFound
);
273 /// Creates an authentication request to verify that a user controls
274 /// some given Identifier.
276 /// <param name="userSuppliedIdentifier">
277 /// The Identifier supplied by the user. This may be a URL, an XRI or i-name.
279 /// <param name="realm">
280 /// The shorest URL that describes this relying party web site's address.
281 /// For example, if your login page is found at https://www.example.com/login.aspx,
282 /// your realm would typically be https://www.example.com/.
285 /// An authentication request object that describes the HTTP response to
286 /// send to the user agent to initiate the authentication.
289 /// <para>Requires an <see cref="HttpContext.Current">HttpContext.Current</see> context.</para>
291 /// <exception cref="ProtocolException">Thrown if no OpenID endpoint could be found.</exception>
292 /// <exception cref="InvalidOperationException">Thrown if <see cref="HttpContext.Current">HttpContext.Current</see> == <c>null</c>.</exception>
293 public IAuthenticationRequest
CreateRequest(Identifier userSuppliedIdentifier
, Realm realm
) {
295 return this.CreateRequests(userSuppliedIdentifier
, realm
).First();
296 } catch (InvalidOperationException ex
) {
297 throw ErrorUtilities
.Wrap(ex
, OpenIdStrings
.OpenIdEndpointNotFound
);
302 /// Creates an authentication request to verify that a user controls
303 /// some given Identifier.
305 /// <param name="userSuppliedIdentifier">
306 /// The Identifier supplied by the user. This may be a URL, an XRI or i-name.
309 /// An authentication request object that describes the HTTP response to
310 /// send to the user agent to initiate the authentication.
313 /// <para>Requires an <see cref="HttpContext.Current">HttpContext.Current</see> context.</para>
315 /// <exception cref="ProtocolException">Thrown if no OpenID endpoint could be found.</exception>
316 /// <exception cref="InvalidOperationException">Thrown if <see cref="HttpContext.Current">HttpContext.Current</see> == <c>null</c>.</exception>
317 public IAuthenticationRequest
CreateRequest(Identifier userSuppliedIdentifier
) {
319 return this.CreateRequests(userSuppliedIdentifier
).First();
320 } catch (InvalidOperationException ex
) {
321 throw ErrorUtilities
.Wrap(ex
, OpenIdStrings
.OpenIdEndpointNotFound
);
326 /// Gets an authentication response from a Provider.
328 /// <returns>The processed authentication response if there is any; <c>null</c> otherwise.</returns>
330 /// <para>Requires an <see cref="HttpContext.Current">HttpContext.Current</see> context.</para>
332 public IAuthenticationResponse
GetResponse() {
333 return this.GetResponse(this.Channel
.GetRequestFromContext());
337 /// Gets an authentication response from a Provider.
339 /// <param name="httpRequestInfo">The HTTP request that may be carrying an authentication response from the Provider.</param>
340 /// <returns>The processed authentication response if there is any; <c>null</c> otherwise.</returns>
341 public IAuthenticationResponse
GetResponse(HttpRequestInfo httpRequestInfo
) {
343 var message
= this.Channel
.ReadFromRequest();
344 PositiveAssertionResponse positiveAssertion
;
345 NegativeAssertionResponse negativeAssertion
;
346 if ((positiveAssertion
= message
as PositiveAssertionResponse
) != null) {
347 return new PositiveAuthenticationResponse(positiveAssertion
, this);
348 } else if ((negativeAssertion
= message
as NegativeAssertionResponse
) != null) {
349 return new NegativeAuthenticationResponse(negativeAssertion
);
350 } else if (message
!= null) {
351 Logger
.WarnFormat("Received unexpected message type {0} when expecting an assertion message.", message
.GetType().Name
);
355 } catch (ProtocolException ex
) {
356 return new FailedAuthenticationResponse(ex
);
361 /// Determines whether some parameter name belongs to OpenID or this library
362 /// as a protocol or internal parameter name.
364 /// <param name="parameterName">Name of the parameter.</param>
366 /// <c>true</c> if the named parameter is a library- or protocol-specific parameter; otherwise, <c>false</c>.
368 internal static bool IsOpenIdSupportingParameter(string parameterName
) {
369 Protocol protocol
= Protocol
.Default
;
370 return parameterName
.StartsWith(protocol
.openid
.Prefix
, StringComparison
.OrdinalIgnoreCase
)
371 || parameterName
.StartsWith("dnoi.", StringComparison
.Ordinal
);
375 /// Generates the authentication requests that can satisfy the requirements of some OpenID Identifier.
377 /// <param name="userSuppliedIdentifier">
378 /// The Identifier supplied by the user. This may be a URL, an XRI or i-name.
380 /// <param name="realm">
381 /// The shorest URL that describes this relying party web site's address.
382 /// For example, if your login page is found at https://www.example.com/login.aspx,
383 /// your realm would typically be https://www.example.com/.
385 /// <param name="returnToUrl">
386 /// The URL of the login page, or the page prepared to receive authentication
387 /// responses from the OpenID Provider.
390 /// An authentication request object that describes the HTTP response to
391 /// send to the user agent to initiate the authentication.
394 /// <para>Any individual generated request can satisfy the authentication.
395 /// The generated requests are sorted in preferred order.
396 /// Each request is generated as it is enumerated to. Associations are created only as
397 /// <see cref="IAuthenticationRequest.RedirectingResponse"/> is called.</para>
398 /// <para>No exception is thrown if no OpenID endpoints were discovered.
399 /// An empty enumerable is returned instead.</para>
401 internal IEnumerable
<IAuthenticationRequest
> CreateRequests(Identifier userSuppliedIdentifier
, Realm realm
, Uri returnToUrl
) {
402 ErrorUtilities
.VerifyArgumentNotNull(realm
, "realm");
403 ErrorUtilities
.VerifyArgumentNotNull(returnToUrl
, "returnToUrl");
405 return AuthenticationRequest
.Create(userSuppliedIdentifier
, this, realm
, returnToUrl
, true).Cast
<IAuthenticationRequest
>();
409 /// Generates the authentication requests that can satisfy the requirements of some OpenID Identifier.
411 /// <param name="userSuppliedIdentifier">
412 /// The Identifier supplied by the user. This may be a URL, an XRI or i-name.
414 /// <param name="realm">
415 /// The shorest URL that describes this relying party web site's address.
416 /// For example, if your login page is found at https://www.example.com/login.aspx,
417 /// your realm would typically be https://www.example.com/.
420 /// An authentication request object that describes the HTTP response to
421 /// send to the user agent to initiate the authentication.
424 /// <para>Any individual generated request can satisfy the authentication.
425 /// The generated requests are sorted in preferred order.
426 /// Each request is generated as it is enumerated to. Associations are created only as
427 /// <see cref="IAuthenticationRequest.RedirectingResponse"/> is called.</para>
428 /// <para>No exception is thrown if no OpenID endpoints were discovered.
429 /// An empty enumerable is returned instead.</para>
430 /// <para>Requires an <see cref="HttpContext.Current">HttpContext.Current</see> context.</para>
432 /// <exception cref="InvalidOperationException">Thrown if <see cref="HttpContext.Current">HttpContext.Current</see> == <c>null</c>.</exception>
433 internal IEnumerable
<IAuthenticationRequest
> CreateRequests(Identifier userSuppliedIdentifier
, Realm realm
) {
434 ErrorUtilities
.VerifyHttpContext();
436 // Build the return_to URL
437 UriBuilder returnTo
= new UriBuilder(MessagingUtilities
.GetRequestUrlFromContext());
439 // Trim off any parameters with an "openid." prefix, and a few known others
440 // to avoid carrying state from a prior login attempt.
441 returnTo
.Query
= string.Empty
;
442 NameValueCollection queryParams
= MessagingUtilities
.GetQueryFromContextNVC();
443 var returnToParams
= new Dictionary
<string, string>(queryParams
.Count
);
444 foreach (string key
in queryParams
) {
445 if (!IsOpenIdSupportingParameter(key
)) {
446 returnToParams
.Add(key
, queryParams
[key
]);
449 returnTo
.AppendQueryArgs(returnToParams
);
451 return this.CreateRequests(userSuppliedIdentifier
, realm
, returnTo
.Uri
);
455 /// Generates the authentication requests that can satisfy the requirements of some OpenID Identifier.
457 /// <param name="userSuppliedIdentifier">
458 /// The Identifier supplied by the user. This may be a URL, an XRI or i-name.
461 /// An authentication request object that describes the HTTP response to
462 /// send to the user agent to initiate the authentication.
465 /// <para>Any individual generated request can satisfy the authentication.
466 /// The generated requests are sorted in preferred order.
467 /// Each request is generated as it is enumerated to. Associations are created only as
468 /// <see cref="IAuthenticationRequest.RedirectingResponse"/> is called.</para>
469 /// <para>No exception is thrown if no OpenID endpoints were discovered.
470 /// An empty enumerable is returned instead.</para>
471 /// <para>Requires an <see cref="HttpContext.Current">HttpContext.Current</see> context.</para>
473 /// <exception cref="InvalidOperationException">Thrown if <see cref="HttpContext.Current">HttpContext.Current</see> == <c>null</c>.</exception>
474 internal IEnumerable
<IAuthenticationRequest
> CreateRequests(Identifier userSuppliedIdentifier
) {
475 ErrorUtilities
.VerifyHttpContext();
477 // Build the realm URL
478 UriBuilder realmUrl
= new UriBuilder(MessagingUtilities
.GetRequestUrlFromContext());
479 realmUrl
.Path
= HttpContext
.Current
.Request
.ApplicationPath
;
480 realmUrl
.Query
= null;
481 realmUrl
.Fragment
= null;
483 // For RP discovery, the realm url MUST NOT redirect. To prevent this for
484 // virtual directory hosted apps, we need to make sure that the realm path ends
485 // in a slash (since our calculation above guarantees it doesn't end in a specific
486 // page like default.aspx).
487 if (!realmUrl
.Path
.EndsWith("/", StringComparison
.Ordinal
)) {
488 realmUrl
.Path
+= "/";
491 return this.CreateRequests(userSuppliedIdentifier
, new Realm(realmUrl
.Uri
));
495 /// Gets an association between this Relying Party and a given Provider
496 /// if it already exists in the association store.
498 /// <param name="provider">The provider to create an association with.</param>
499 /// <returns>The association if one exists and has useful life remaining. Otherwise <c>null</c>.</returns>
500 internal Association
GetExistingAssociation(ProviderEndpointDescription provider
) {
501 ErrorUtilities
.VerifyArgumentNotNull(provider
, "provider");
503 Protocol protocol
= Protocol
.Lookup(provider
.ProtocolVersion
);
505 // If the RP has no application store for associations, there's no point in creating one.
506 if (this.AssociationStore
== null) {
510 // TODO: we need a way to lookup an association that fulfills a given set of security
511 // requirements. We may have a SHA-1 association and a SHA-256 association that need
512 // to be called for specifically. (a bizzare scenario, admittedly, making this low priority).
513 Association association
= this.AssociationStore
.GetAssociation(provider
.Endpoint
);
515 // If the returned association does not fulfill security requirements, ignore it.
516 if (association
!= null && !this.SecuritySettings
.IsAssociationInPermittedRange(protocol
, association
.GetAssociationType(protocol
))) {
520 if (association
!= null && !association
.HasUsefulLifeRemaining
) {
528 /// Gets an existing association with the specified Provider, or attempts to create
529 /// a new association of one does not already exist.
531 /// <param name="provider">The provider to get an association for.</param>
532 /// <returns>The existing or new association; <c>null</c> if none existed and one could not be created.</returns>
533 internal Association
GetOrCreateAssociation(ProviderEndpointDescription provider
) {
534 return this.GetExistingAssociation(provider
) ?? this.CreateNewAssociation(provider
);
538 /// Gets the priority rating for a given type of endpoint, allowing a
539 /// priority sorting of endpoints.
541 /// <param name="endpoint">The endpoint to prioritize.</param>
542 /// <returns>An arbitary integer, which may be used for sorting against other returned values from this method.</returns>
543 private static double GetEndpointPrecedenceOrderByServiceType(IXrdsProviderEndpoint endpoint
) {
544 // The numbers returned from this method only need to compare against other numbers
545 // from this method, which makes them arbitrary but relational to only others here.
546 if (endpoint
.IsTypeUriPresent(Protocol
.V20
.OPIdentifierServiceTypeURI
)) {
549 if (endpoint
.IsTypeUriPresent(Protocol
.V20
.ClaimedIdentifierServiceTypeURI
)) {
552 if (endpoint
.IsTypeUriPresent(Protocol
.V11
.ClaimedIdentifierServiceTypeURI
)) {
555 if (endpoint
.IsTypeUriPresent(Protocol
.V10
.ClaimedIdentifierServiceTypeURI
)) {
562 /// Creates a new association with a given Provider.
564 /// <param name="provider">The provider to create an association with.</param>
566 /// The newly created association, or null if no association can be created with
567 /// the given Provider given the current security settings.
570 /// A new association is created and returned even if one already exists in the
571 /// association store.
572 /// Any new association is automatically added to the <see cref="AssociationStore"/>.
574 private Association
CreateNewAssociation(ProviderEndpointDescription provider
) {
575 ErrorUtilities
.VerifyArgumentNotNull(provider
, "provider");
577 // If there is no association store, there is no point in creating an association.
578 if (this.AssociationStore
== null) {
582 var associateRequest
= AssociateRequest
.Create(this.SecuritySettings
, provider
);
583 if (associateRequest
== null) {
584 // this can happen if security requirements and protocol conflict
585 // to where there are no association types to choose from.
589 var associateResponse
= this.Channel
.Request(associateRequest
);
590 var associateSuccessfulResponse
= associateResponse
as AssociateSuccessfulResponse
;
591 var associateUnsuccessfulResponse
= associateResponse
as AssociateUnsuccessfulResponse
;
592 if (associateSuccessfulResponse
!= null) {
593 Association association
= associateSuccessfulResponse
.CreateAssociation(associateRequest
);
594 this.AssociationStore
.StoreAssociation(provider
.Endpoint
, association
);
596 } else if (associateUnsuccessfulResponse
!= null) {
598 throw new NotImplementedException();
600 throw new ProtocolException(MessagingStrings
.UnexpectedMessageReceivedOfMany
);