1
//-----------------------------------------------------------------------
2 // <copyright file="OpenIdProvider.cs" company="Andrew Arnott">
3 // Copyright (c) Andrew Arnott. All rights reserved.
5 //-----------------------------------------------------------------------
7 namespace DotNetOpenAuth
.OpenId
.Provider
{
9 using DotNetOpenAuth
.Configuration
;
10 using DotNetOpenAuth
.Messaging
;
11 using DotNetOpenAuth
.Messaging
.Bindings
;
12 using DotNetOpenAuth
.OpenId
.ChannelElements
;
13 using DotNetOpenAuth
.OpenId
.Messages
;
16 /// Offers services for a web page that is acting as an OpenID identity server.
18 public sealed class OpenIdProvider
{
20 /// Backing field for the <see cref="SecuritySettings"/> property.
22 private ProviderSecuritySettings securitySettings
;
25 /// Initializes a new instance of the <see cref="OpenIdProvider"/> class.
27 /// <param name="associationStore">The association store to use. Cannot be null.</param>
28 /// <param name="nonceStore">The nonce store to use. Cannot be null.</param>
29 public OpenIdProvider(IAssociationStore
<AssociationRelyingPartyType
> associationStore
, INonceStore nonceStore
) {
30 ErrorUtilities
.VerifyArgumentNotNull(associationStore
, "associationStore");
31 ErrorUtilities
.VerifyArgumentNotNull(nonceStore
, "nonceStore");
33 this.Channel
= new OpenIdChannel(associationStore
, nonceStore
);
34 this.AssociationStore
= associationStore
;
35 this.SecuritySettings
= ProviderSection
.Configuration
.SecuritySettings
.CreateSecuritySettings();
39 /// Gets the channel to use for sending/receiving messages.
41 public Channel Channel { get; internal set; }
44 /// Gets the security settings used by this Provider.
46 public ProviderSecuritySettings SecuritySettings
{
48 return this.securitySettings
;
53 throw new ArgumentNullException("value");
56 this.securitySettings
= value;
61 /// Gets the association store.
63 internal IAssociationStore
<AssociationRelyingPartyType
> AssociationStore { get; private set; }
66 /// Gets the web request handler to use for discovery and the part of
67 /// authentication where direct messages are sent to an untrusted remote party.
69 internal IDirectSslWebRequestHandler WebRequestHandler
{
70 // TODO: Since the OpenIdChannel.WebRequestHandler might be set to a non-SSL
71 // implementation, we should consider altering the consumers of this property
72 // to handle either case.
73 get { return this.Channel.WebRequestHandler as IDirectSslWebRequestHandler; }
77 /// Gets the incoming OpenID request if there is one, or null if none was detected.
79 /// <returns>The request that the hosting Provider should possibly process and then transmit the response for.</returns>
81 /// Requests may be infrastructural to OpenID and allow auto-responses, or they may
82 /// be authentication requests where the Provider site has to make decisions based
83 /// on its own user database and policies.
85 public IRequest
GetRequest() {
86 return this.GetRequest(this.Channel
.GetRequestFromContext());
90 /// Gets the incoming OpenID request if there is one, or null if none was detected.
92 /// <param name="httpRequestInfo">The incoming HTTP request to extract the message from.</param>
93 /// <returns>The request that the hosting Provider should possibly process and then transmit the response for.</returns>
95 /// Requests may be infrastructural to OpenID and allow auto-responses, or they may
96 /// be authentication requests where the Provider site has to make decisions based
97 /// on its own user database and policies.
99 public IRequest
GetRequest(HttpRequestInfo httpRequestInfo
) {
100 IDirectedProtocolMessage incomingMessage
= this.Channel
.ReadFromRequest(httpRequestInfo
);
101 if (incomingMessage
== null) {
105 var checkIdMessage
= incomingMessage
as CheckIdRequest
;
106 if (checkIdMessage
!= null) {
107 return new AuthenticationRequest(this, checkIdMessage
);
110 var checkAuthMessage
= incomingMessage
as CheckAuthenticationRequest
;
111 if (checkAuthMessage
!= null) {
112 return new AutoResponsiveRequest(this, incomingMessage
, new CheckAuthenticationResponse(checkAuthMessage
));
115 var associateMessage
= incomingMessage
as AssociateRequest
;
116 if (associateMessage
!= null) {
117 return new AutoResponsiveRequest(this, incomingMessage
, associateMessage
.CreateResponse(this.AssociationStore
));
120 throw ErrorUtilities
.ThrowProtocol(MessagingStrings
.UnexpectedMessageReceivedOfMany
);