Added StandardProviderApplicationStore and several OpenIdProvider unit tests.
[dotnetoauth.git] / src / DotNetOpenAuth / OpenId / Provider / StandardProviderApplicationStore.cs
blob6951e5a0cf733f73c45a6693449d87b11fbd7ccd
1 //-----------------------------------------------------------------------
2 // <copyright file="StandardProviderApplicationStore.cs" company="Andrew Arnott">
3 // Copyright (c) Andrew Arnott. All rights reserved.
4 // </copyright>
5 //-----------------------------------------------------------------------
7 namespace DotNetOpenAuth.OpenId.Provider {
8 using System;
9 using System.Collections.Generic;
10 using System.Linq;
11 using System.Text;
12 using DotNetOpenAuth.Messaging.Bindings;
14 /// <summary>
15 /// An in-memory store for Providers, suitable for single server, single process
16 /// ASP.NET web sites.
17 /// </summary>
18 /// <remarks>
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
24 /// class.
25 /// </remarks>
26 public class StandardProviderApplicationStore : IProviderApplicationStore {
27 /// <summary>
28 /// The nonce store to use.
29 /// </summary>
30 private readonly INonceStore nonceStore;
32 /// <summary>
33 /// The association store to use.
34 /// </summary>
35 private readonly IAssociationStore<AssociationRelyingPartyType> associationStore;
37 /// <summary>
38 /// Initializes a new instance of the <see cref="StandardProviderApplicationStore"/> class.
39 /// </summary>
40 /// <param name="maximumMessageAge">The maximum message age that is allowed according to the
41 /// <see cref="StandardExpirationBindingElement.MaximumMessageAge"/>
42 /// property.</param>
43 public StandardProviderApplicationStore(TimeSpan maximumMessageAge) {
44 this.nonceStore = new NonceMemoryStore(maximumMessageAge);
45 this.associationStore = new AssociationMemoryStore<AssociationRelyingPartyType>();
48 #region IAssociationStore<AssociationRelyingPartyType> Members
50 /// <summary>
51 /// Saves an <see cref="Association"/> for later recall.
52 /// </summary>
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);
59 /// <summary>
60 /// Gets the best association (the one with the longest remaining life) for a given key.
61 /// </summary>
62 /// <param name="distinguishingFactor">The Uri (for relying parties) or Smart/Dumb (for Providers).</param>
63 /// <returns>
64 /// The requested association, or null if no unexpired <see cref="Association"/>s exist for the given key.
65 /// </returns>
66 public Association GetAssociation(AssociationRelyingPartyType distinguishingFactor) {
67 return this.associationStore.GetAssociation(distinguishingFactor);
70 /// <summary>
71 /// Gets the association for a given key and handle.
72 /// </summary>
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>
75 /// <returns>
76 /// The requested association, or null if no unexpired <see cref="Association"/>s exist for the given key and handle.
77 /// </returns>
78 public Association GetAssociation(AssociationRelyingPartyType distinguishingFactor, string handle) {
79 return this.associationStore.GetAssociation(distinguishingFactor, handle);
82 /// <summary>
83 /// Removes a specified handle that may exist in the store.
84 /// </summary>
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>
87 /// <returns>
88 /// True if the association existed in this store previous to this call.
89 /// </returns>
90 /// <remarks>
91 /// No exception should be thrown if the association does not exist in the store
92 /// before this call.
93 /// </remarks>
94 public bool RemoveAssociation(AssociationRelyingPartyType distinguishingFactor, string handle) {
95 return this.associationStore.RemoveAssociation(distinguishingFactor, handle);
98 /// <summary>
99 /// Clears all expired associations from the store.
100 /// </summary>
101 /// <remarks>
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.
106 /// </remarks>
107 public void ClearExpiredAssociations() {
108 this.associationStore.ClearExpiredAssociations();
111 #endregion
113 #region INonceStore Members
115 /// <summary>
116 /// Stores a given nonce and timestamp.
117 /// </summary>
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>
121 /// <returns>
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.
124 /// </returns>
125 /// <remarks>
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.
131 /// </remarks>
132 public bool StoreNonce(string nonce, DateTime timestamp) {
133 return this.nonceStore.StoreNonce(nonce, timestamp);
136 #endregion