//-----------------------------------------------------------------------
//
// Copyright (c) Outercurve Foundation. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.Messaging.Bindings {
using System;
using System.Collections.Generic;
using DotNetOpenAuth.Configuration;
using DotNetOpenAuth.Messaging.Bindings;
///
/// An in-memory store for Providers, suitable for single server, single process
/// ASP.NET web sites.
///
///
/// This class provides only a basic implementation that is likely to work
/// out of the box on most single-server web sites. It is highly recommended
/// that high traffic web sites consider using a database to store the information
/// used by an OpenID Provider and write a custom implementation of the
/// interface to use instead of this
/// class.
///
public class MemoryCryptoKeyAndNonceStore : ICryptoKeyAndNonceStore {
///
/// The nonce store to use.
///
private readonly INonceStore nonceStore;
///
/// The crypto key store where symmetric keys are persisted.
///
private readonly ICryptoKeyStore cryptoKeyStore;
///
/// Initializes a new instance of the class
/// with a default max nonce lifetime of 5 minutes.
///
public MemoryCryptoKeyAndNonceStore()
: this(TimeSpan.FromMinutes(5)) {
}
///
/// Initializes a new instance of the class.
///
/// The maximum time to live of a message that might carry a nonce.
public MemoryCryptoKeyAndNonceStore(TimeSpan maximumMessageAge) {
this.nonceStore = new MemoryNonceStore(maximumMessageAge);
this.cryptoKeyStore = new MemoryCryptoKeyStore();
}
#region INonceStore Members
///
/// Stores a given nonce and timestamp.
///
/// The context, or namespace, within which the must be unique.
/// A series of random characters.
/// The timestamp that together with the nonce string make it unique.
/// The timestamp may also be used by the data store to clear out old nonces.
///
/// True if the nonce+timestamp (combination) was not previously in the database.
/// False if the nonce was stored previously with the same timestamp.
///
///
/// The nonce must be stored for no less than the maximum time window a message may
/// be processed within before being discarded as an expired message.
/// If the binding element is applicable to your channel, this expiration window
/// is retrieved or set using the
/// property.
///
public bool StoreNonce(string context, string nonce, DateTime timestampUtc) {
return this.nonceStore.StoreNonce(context, nonce, timestampUtc);
}
#endregion
#region ICryptoKeyStore
///
/// Gets the key in a given bucket and handle.
///
/// The bucket name. Case sensitive.
/// The key handle. Case sensitive.
///
/// The cryptographic key, or null if no matching key was found.
///
public CryptoKey GetKey(string bucket, string handle) {
return this.cryptoKeyStore.GetKey(bucket, handle);
}
///
/// Gets a sequence of existing keys within a given bucket.
///
/// The bucket name. Case sensitive.
///
/// A sequence of handles and keys, ordered by descending .
///
public IEnumerable> GetKeys(string bucket) {
return this.cryptoKeyStore.GetKeys(bucket);
}
///
/// Stores a cryptographic key.
///
/// The name of the bucket to store the key in. Case sensitive.
/// The handle to the key, unique within the bucket. Case sensitive.
/// The key to store.
/// Thrown in the event of a conflict with an existing key in the same bucket and with the same handle.
public void StoreKey(string bucket, string handle, CryptoKey key) {
this.cryptoKeyStore.StoreKey(bucket, handle, key);
}
///
/// Removes the key.
///
/// The bucket name. Case sensitive.
/// The key handle. Case sensitive.
public void RemoveKey(string bucket, string handle) {
this.cryptoKeyStore.RemoveKey(bucket, handle);
}
#endregion
}
}