1
//-----------------------------------------------------------------------
2 // <copyright file="StandardRelyingPartyApplicationStore.cs" company="Andrew Arnott">
3 // Copyright (c) Andrew Arnott. All rights reserved.
5 //-----------------------------------------------------------------------
7 namespace DotNetOpenAuth
.OpenId
.RelyingParty
{
9 using System
.Collections
.Generic
;
12 using DotNetOpenAuth
.Messaging
.Bindings
;
13 using DotNetOpenAuth
.OpenId
.ChannelElements
;
16 /// An in-memory store for Relying Parties, suitable for single server, single process
17 /// ASP.NET web sites.
19 internal class StandardRelyingPartyApplicationStore
: IRelyingPartyApplicationStore
{
21 /// The nonce store to use.
23 private readonly INonceStore nonceStore
;
26 /// The association store to use.
28 private readonly IAssociationStore
<Uri
> associationStore
;
31 /// The private secret store to use.
33 private readonly IPrivateSecretStore privateSecretStore
;
36 /// Initializes a new instance of the <see cref="StandardRelyingPartyApplicationStore"/> class.
38 internal StandardRelyingPartyApplicationStore() {
39 this.nonceStore
= new NonceMemoryStore(Configuration
.MaximumUserAgentAuthenticationTime
);
40 this.associationStore
= new AssociationMemoryStore
<Uri
>();
41 this.privateSecretStore
= new PrivateSecretMemoryStore();
44 #region IPrivateSecretStore Members
47 /// Gets or sets a secret key that can be used for signing.
49 /// <value>A 64-byte binary value, which may contain null bytes.</value>
50 public byte[] PrivateSecret
{
51 get { return this.privateSecretStore.PrivateSecret; }
52 set { this.privateSecretStore.PrivateSecret = value; }
57 #region IAssociationStore<Uri> Members
60 /// Saves an <see cref="Association"/> for later recall.
62 /// <param name="distinguishingFactor">The Uri (for relying parties) or Smart/Dumb (for providers).</param>
63 /// <param name="association">The association to store.</param>
64 public void StoreAssociation(Uri distinguishingFactor
, Association association
) {
65 this.associationStore
.StoreAssociation(distinguishingFactor
, association
);
69 /// Gets the best association (the one with the longest remaining life) for a given key.
71 /// <param name="distinguishingFactor">The Uri (for relying parties) or Smart/Dumb (for Providers).</param>
73 /// The requested association, or null if no unexpired <see cref="Association"/>s exist for the given key.
75 public Association
GetAssociation(Uri distinguishingFactor
) {
76 return this.associationStore
.GetAssociation(distinguishingFactor
);
80 /// Gets the association for a given key and handle.
82 /// <param name="distinguishingFactor">The Uri (for relying parties) or Smart/Dumb (for Providers).</param>
83 /// <param name="handle">The handle of the specific association that must be recalled.</param>
85 /// The requested association, or null if no unexpired <see cref="Association"/>s exist for the given key and handle.
87 public Association
GetAssociation(Uri distinguishingFactor
, string handle
) {
88 return this.associationStore
.GetAssociation(distinguishingFactor
, handle
);
92 /// Removes a specified handle that may exist in the store.
94 /// <param name="distinguishingFactor">The Uri (for relying parties) or Smart/Dumb (for Providers).</param>
95 /// <param name="handle">The handle of the specific association that must be deleted.</param>
97 /// True if the association existed in this store previous to this call.
100 /// No exception should be thrown if the association does not exist in the store
101 /// before this call.
103 public bool RemoveAssociation(Uri distinguishingFactor
, string handle
) {
104 return this.associationStore
.RemoveAssociation(distinguishingFactor
, handle
);
108 /// Clears all expired associations from the store.
111 /// If another algorithm is in place to periodically clear out expired associations,
112 /// this method call may be ignored.
113 /// This should be done frequently enough to avoid a memory leak, but sparingly enough
114 /// to not be a performance drain.
116 public void ClearExpiredAssociations() {
117 this.associationStore
.ClearExpiredAssociations();
122 #region INonceStore Members
125 /// Stores a given nonce and timestamp.
127 /// <param name="nonce">A series of random characters.</param>
128 /// <param name="timestamp">The timestamp that together with the nonce string make it unique.
129 /// The timestamp may also be used by the data store to clear out old nonces.</param>
131 /// True if the nonce+timestamp (combination) was not previously in the database.
132 /// False if the nonce was stored previously with the same timestamp.
135 /// The nonce must be stored for no less than the maximum time window a message may
136 /// be processed within before being discarded as an expired message.
137 /// If the binding element is applicable to your channel, this expiration window
138 /// is retrieved or set using the
139 /// <see cref="StandardExpirationBindingElement.MaximumMessageAge"/> property.
141 public bool StoreNonce(string nonce
, DateTime timestamp
) {
142 return this.nonceStore
.StoreNonce(nonce
, timestamp
);