Added support for dumb mode in the Relying Party.
[dotnetoauth.git] / src / DotNetOpenAuth / OpenId / RelyingParty / StandardRelyingPartyApplicationStore.cs
blob1b2a89235d0b5956df01b13e31354df12ca82599
1 //-----------------------------------------------------------------------
2 // <copyright file="StandardRelyingPartyApplicationStore.cs" company="Andrew Arnott">
3 // Copyright (c) Andrew Arnott. All rights reserved.
4 // </copyright>
5 //-----------------------------------------------------------------------
7 namespace DotNetOpenAuth.OpenId.RelyingParty {
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 Relying Parties, suitable for single server, single process
16 /// ASP.NET web sites.
17 /// </summary>
18 internal class StandardRelyingPartyApplicationStore : IRelyingPartyApplicationStore {
19 /// <summary>
20 /// The nonce store to use.
21 /// </summary>
22 private readonly NonceMemoryStore nonceStore;
24 /// <summary>
25 /// The association store to use.
26 /// </summary>
27 private readonly AssociationMemoryStore<Uri> associationStore;
29 /// <summary>
30 /// The private secret store to use.
31 /// </summary>
32 private readonly PrivateSecretMemoryStore privateSecretStore;
34 /// <summary>
35 /// Initializes a new instance of the <see cref="StandardRelyingPartyApplicationStore"/> class.
36 /// </summary>
37 /// <param name="maximumMessageAge">
38 /// The maximum message age that is allowed according to the
39 /// <see cref="StandardExpirationBindingElement.MaximumMessageAge"/>
40 /// property.
41 /// </param>
42 internal StandardRelyingPartyApplicationStore(TimeSpan maximumMessageAge) {
43 this.nonceStore = new NonceMemoryStore(maximumMessageAge);
44 this.associationStore = new AssociationMemoryStore<Uri>();
45 this.privateSecretStore = new PrivateSecretMemoryStore();
48 #region IPrivateSecretStore Members
50 /// <summary>
51 /// Gets or sets a secret key that can be used for signing.
52 /// </summary>
53 /// <value>A 64-byte binary value, which may contain null bytes.</value>
54 public byte[] PrivateSecret {
55 get { return this.privateSecretStore.PrivateSecret; }
56 set { this.privateSecretStore.PrivateSecret = value; }
59 #endregion
61 #region IAssociationStore<Uri> Members
63 /// <summary>
64 /// Saves an <see cref="Association"/> for later recall.
65 /// </summary>
66 /// <param name="distinguishingFactor">The Uri (for relying parties) or Smart/Dumb (for providers).</param>
67 /// <param name="association">The association to store.</param>
68 public void StoreAssociation(Uri distinguishingFactor, Association association) {
69 this.associationStore.StoreAssociation(distinguishingFactor, association);
72 /// <summary>
73 /// Gets the best association (the one with the longest remaining life) for a given key.
74 /// </summary>
75 /// <param name="distinguishingFactor">The Uri (for relying parties) or Smart/Dumb (for Providers).</param>
76 /// <returns>
77 /// The requested association, or null if no unexpired <see cref="Association"/>s exist for the given key.
78 /// </returns>
79 public Association GetAssociation(Uri distinguishingFactor) {
80 return this.associationStore.GetAssociation(distinguishingFactor);
83 /// <summary>
84 /// Gets the association for a given key and handle.
85 /// </summary>
86 /// <param name="distinguishingFactor">The Uri (for relying parties) or Smart/Dumb (for Providers).</param>
87 /// <param name="handle">The handle of the specific association that must be recalled.</param>
88 /// <returns>
89 /// The requested association, or null if no unexpired <see cref="Association"/>s exist for the given key and handle.
90 /// </returns>
91 public Association GetAssociation(Uri distinguishingFactor, string handle) {
92 return this.associationStore.GetAssociation(distinguishingFactor, handle);
95 /// <summary>
96 /// Removes a specified handle that may exist in the store.
97 /// </summary>
98 /// <param name="distinguishingFactor">The Uri (for relying parties) or Smart/Dumb (for Providers).</param>
99 /// <param name="handle">The handle of the specific association that must be deleted.</param>
100 /// <returns>
101 /// True if the association existed in this store previous to this call.
102 /// </returns>
103 /// <remarks>
104 /// No exception should be thrown if the association does not exist in the store
105 /// before this call.
106 /// </remarks>
107 public bool RemoveAssociation(Uri distinguishingFactor, string handle) {
108 return this.associationStore.RemoveAssociation(distinguishingFactor, handle);
111 /// <summary>
112 /// Clears all expired associations from the store.
113 /// </summary>
114 /// <remarks>
115 /// If another algorithm is in place to periodically clear out expired associations,
116 /// this method call may be ignored.
117 /// This should be done frequently enough to avoid a memory leak, but sparingly enough
118 /// to not be a performance drain.
119 /// </remarks>
120 public void ClearExpiredAssociations() {
121 this.associationStore.ClearExpiredAssociations();
124 #endregion
126 #region INonceStore Members
128 /// <summary>
129 /// Stores a given nonce and timestamp.
130 /// </summary>
131 /// <param name="nonce">A series of random characters.</param>
132 /// <param name="timestamp">The timestamp that together with the nonce string make it unique.
133 /// The timestamp may also be used by the data store to clear out old nonces.</param>
134 /// <returns>
135 /// True if the nonce+timestamp (combination) was not previously in the database.
136 /// False if the nonce was stored previously with the same timestamp.
137 /// </returns>
138 /// <remarks>
139 /// The nonce must be stored for no less than the maximum time window a message may
140 /// be processed within before being discarded as an expired message.
141 /// If the binding element is applicable to your channel, this expiration window
142 /// is retrieved or set using the
143 /// <see cref="StandardExpirationBindingElement.MaximumMessageAge"/> property.
144 /// </remarks>
145 public bool StoreNonce(string nonce, DateTime timestamp) {
146 return this.nonceStore.StoreNonce(nonce, timestamp);
149 #endregion