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