1
//-----------------------------------------------------------------------
2 // <copyright file="StandardProviderApplicationStore.cs" company="Andrew Arnott">
3 // Copyright (c) Andrew Arnott. All rights reserved.
5 //-----------------------------------------------------------------------
7 namespace DotNetOpenAuth
.OpenId
.Provider
{
9 using System
.Collections
.Generic
;
12 using DotNetOpenAuth
.Messaging
.Bindings
;
15 /// An in-memory store for Providers, suitable for single server, single process
16 /// ASP.NET web sites.
19 /// This class provides only a basic implementation that is likely to work
20 /// out of the box on most single-server web sites. It is highly recommended
21 /// that high traffic web sites consider using a database to store the information
22 /// used by an OpenID Provider and write a custom implementation of the
23 /// <see cref="IProviderApplicationStore"/> interface to use instead of this
26 public class StandardProviderApplicationStore
: IProviderApplicationStore
{
28 /// The nonce store to use.
30 private readonly INonceStore nonceStore
;
33 /// The association store to use.
35 private readonly IAssociationStore
<AssociationRelyingPartyType
> associationStore
;
38 /// Initializes a new instance of the <see cref="StandardProviderApplicationStore"/> class.
40 /// <param name="maximumMessageAge">The maximum message age that is allowed according to the
41 /// <see cref="StandardExpirationBindingElement.MaximumMessageAge"/>
43 public StandardProviderApplicationStore(TimeSpan maximumMessageAge
) {
44 this.nonceStore
= new NonceMemoryStore(maximumMessageAge
);
45 this.associationStore
= new AssociationMemoryStore
<AssociationRelyingPartyType
>();
48 #region IAssociationStore<AssociationRelyingPartyType> Members
51 /// Saves an <see cref="Association"/> for later recall.
53 /// <param name="distinguishingFactor">The Uri (for relying parties) or Smart/Dumb (for providers).</param>
54 /// <param name="association">The association to store.</param>
55 public void StoreAssociation(AssociationRelyingPartyType distinguishingFactor
, Association association
) {
56 this.associationStore
.StoreAssociation(distinguishingFactor
, association
);
60 /// Gets the best association (the one with the longest remaining life) for a given key.
62 /// <param name="distinguishingFactor">The Uri (for relying parties) or Smart/Dumb (for Providers).</param>
64 /// The requested association, or null if no unexpired <see cref="Association"/>s exist for the given key.
66 public Association
GetAssociation(AssociationRelyingPartyType distinguishingFactor
) {
67 return this.associationStore
.GetAssociation(distinguishingFactor
);
71 /// Gets the association for a given key and handle.
73 /// <param name="distinguishingFactor">The Uri (for relying parties) or Smart/Dumb (for Providers).</param>
74 /// <param name="handle">The handle of the specific association that must be recalled.</param>
76 /// The requested association, or null if no unexpired <see cref="Association"/>s exist for the given key and handle.
78 public Association
GetAssociation(AssociationRelyingPartyType distinguishingFactor
, string handle
) {
79 return this.associationStore
.GetAssociation(distinguishingFactor
, handle
);
83 /// Removes a specified handle that may exist in the store.
85 /// <param name="distinguishingFactor">The Uri (for relying parties) or Smart/Dumb (for Providers).</param>
86 /// <param name="handle">The handle of the specific association that must be deleted.</param>
88 /// True if the association existed in this store previous to this call.
91 /// No exception should be thrown if the association does not exist in the store
94 public bool RemoveAssociation(AssociationRelyingPartyType distinguishingFactor
, string handle
) {
95 return this.associationStore
.RemoveAssociation(distinguishingFactor
, handle
);
99 /// Clears all expired associations from the store.
102 /// If another algorithm is in place to periodically clear out expired associations,
103 /// this method call may be ignored.
104 /// This should be done frequently enough to avoid a memory leak, but sparingly enough
105 /// to not be a performance drain.
107 public void ClearExpiredAssociations() {
108 this.associationStore
.ClearExpiredAssociations();
113 #region INonceStore Members
116 /// Stores a given nonce and timestamp.
118 /// <param name="nonce">A series of random characters.</param>
119 /// <param name="timestamp">The timestamp that together with the nonce string make it unique.
120 /// The timestamp may also be used by the data store to clear out old nonces.</param>
122 /// True if the nonce+timestamp (combination) was not previously in the database.
123 /// False if the nonce was stored previously with the same timestamp.
126 /// The nonce must be stored for no less than the maximum time window a message may
127 /// be processed within before being discarded as an expired message.
128 /// If the binding element is applicable to your channel, this expiration window
129 /// is retrieved or set using the
130 /// <see cref="StandardExpirationBindingElement.MaximumMessageAge"/> property.
132 public bool StoreNonce(string nonce
, DateTime timestamp
) {
133 return this.nonceStore
.StoreNonce(nonce
, timestamp
);