diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2011-05-15 19:25:30 -0700 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2011-05-15 19:25:30 -0700 |
commit | 9ba3d8e9f066132c68501fbc191acd50fba905f4 (patch) | |
tree | ba63fe3ca77ea47d2d8a28468454ba1aa6826541 /samples/OpenIdRelyingPartyWebForms/Code/CustomStore.cs | |
parent | f55ff22c0d35e6435ff983e0ecca8b44e2bc1449 (diff) | |
download | DotNetOpenAuth-9ba3d8e9f066132c68501fbc191acd50fba905f4.zip DotNetOpenAuth-9ba3d8e9f066132c68501fbc191acd50fba905f4.tar.gz DotNetOpenAuth-9ba3d8e9f066132c68501fbc191acd50fba905f4.tar.bz2 |
Updated samples and project template custom stores to use ICryptoKeyStore for RPs.
Diffstat (limited to 'samples/OpenIdRelyingPartyWebForms/Code/CustomStore.cs')
-rw-r--r-- | samples/OpenIdRelyingPartyWebForms/Code/CustomStore.cs | 72 |
1 files changed, 36 insertions, 36 deletions
diff --git a/samples/OpenIdRelyingPartyWebForms/Code/CustomStore.cs b/samples/OpenIdRelyingPartyWebForms/Code/CustomStore.cs index 325a5d0..d113c8b 100644 --- a/samples/OpenIdRelyingPartyWebForms/Code/CustomStore.cs +++ b/samples/OpenIdRelyingPartyWebForms/Code/CustomStore.cs @@ -1,8 +1,11 @@ namespace OpenIdRelyingPartyWebForms.Code { using System; + using System.Collections.Generic; using System.Data; using System.Globalization; using System.Security.Cryptography; + using DotNetOpenAuth; + using DotNetOpenAuth.Configuration; using DotNetOpenAuth.OpenId; using DotNetOpenAuth.OpenId.RelyingParty; @@ -29,7 +32,7 @@ /// The context SHOULD be treated as case-sensitive. /// The value will never be <c>null</c> but may be the empty string.</param> /// <param name="nonce">A series of random characters.</param> - /// <param name="timestamp">The timestamp that together with the nonce string make it unique. + /// <param name="timestampUtc">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.</param> /// <returns> /// True if the nonce+timestamp (combination) was not previously in the database. @@ -42,7 +45,7 @@ /// is retrieved or set using the /// <see cref="StandardExpirationBindingElement.MaximumMessageAge"/> property. /// </remarks> - public bool StoreNonce(string context, string nonce, DateTime timestamp) { + public bool StoreNonce(string context, string nonce, DateTime timestampUtc) { // IMPORTANT: If actually persisting to a database that can be reached from // different servers/instances of this class at once, it is vitally important // to protect against race condition attacks by one or more of these: @@ -54,76 +57,73 @@ // at you in the result of a race condition somewhere in your web site UI code // and display some message to have the user try to log in again, and possibly // warn them about a replay attack. - timestamp = timestamp.ToLocalTime(); lock (this) { - if (dataSet.Nonce.FindByIssuedCodeContext(timestamp, nonce, context) != null) { + if (dataSet.Nonce.FindByIssuedUtcCodeContext(timestampUtc, nonce, context) != null) { return false; } - TimeSpan maxMessageAge = DotNetOpenAuth.Configuration.DotNetOpenAuthSection.Configuration.Messaging.MaximumMessageLifetime; - dataSet.Nonce.AddNonceRow(context, nonce, timestamp, timestamp + maxMessageAge); + TimeSpan maxMessageAge = DotNetOpenAuthSection.Configuration.Messaging.MaximumMessageLifetime; + dataSet.Nonce.AddNonceRow(context, nonce, timestampUtc, timestampUtc + maxMessageAge); return true; } } public void ClearExpiredNonces() { - this.removeExpiredRows(dataSet.Nonce, dataSet.Nonce.ExpiresColumn.ColumnName); + this.removeExpiredRows(dataSet.Nonce, dataSet.Nonce.ExpiresUtcColumn.ColumnName); } #endregion - #region IRelyingPartyAssociationStore Members + #region ICryptoKeyStore Members - public void StoreAssociation(Uri providerEndpoint, Association assoc) { - var assocRow = dataSet.Association.NewAssociationRow(); - assocRow.DistinguishingFactor = providerEndpoint.AbsoluteUri; - assocRow.Handle = assoc.Handle; - assocRow.Expires = assoc.Expires.ToLocalTime(); - assocRow.PrivateData = assoc.SerializePrivateData(); - dataSet.Association.AddAssociationRow(assocRow); + public CryptoKey GetKey(string bucket, string handle) { + var assocRow = dataSet.CryptoKey.FindByBucketHandle(bucket, handle); + return new CryptoKey(assocRow.Secret, assocRow.ExpiresUtc); } - public Association GetAssociation(Uri providerEndpoint, SecuritySettings securitySettings) { - // TODO: properly consider the securitySettings when picking an association to return. + public IEnumerable<KeyValuePair<string, CryptoKey>> GetKeys(string bucket) { // properly escape the URL to prevent injection attacks. - string value = providerEndpoint.AbsoluteUri.Replace("'", "''"); + string value = bucket.Replace("'", "''"); string filter = string.Format( CultureInfo.InvariantCulture, "{0} = '{1}'", - dataSet.Association.DistinguishingFactorColumn.ColumnName, + dataSet.CryptoKey.BucketColumn.ColumnName, value); - string sort = dataSet.Association.ExpiresColumn.ColumnName + " DESC"; - DataView view = new DataView(dataSet.Association, filter, sort, DataViewRowState.CurrentRows); + string sort = dataSet.CryptoKey.ExpiresUtcColumn.ColumnName + " DESC"; + DataView view = new DataView(dataSet.CryptoKey, filter, sort, DataViewRowState.CurrentRows); if (view.Count == 0) { - return null; + yield break; + } + + foreach (CustomStoreDataSet.CryptoKeyRow row in view) { + yield return new KeyValuePair<string, CryptoKey>(row.Handle, new CryptoKey(row.Secret, row.ExpiresUtc)); } - var row = (CustomStoreDataSet.AssociationRow)view[0].Row; - return Association.Deserialize(row.Handle, row.Expires.ToUniversalTime(), row.PrivateData); } - public Association GetAssociation(Uri providerEndpoint, string handle) { - var assocRow = dataSet.Association.FindByDistinguishingFactorHandle(providerEndpoint.AbsoluteUri, handle); - return Association.Deserialize(assocRow.Handle, assocRow.Expires, assocRow.PrivateData); + public void StoreKey(string bucket, string handle, CryptoKey key) { + var cryptoKeyRow = dataSet.CryptoKey.NewCryptoKeyRow(); + cryptoKeyRow.Bucket = bucket; + cryptoKeyRow.Handle = handle; + cryptoKeyRow.ExpiresUtc = key.ExpiresUtc; + cryptoKeyRow.Secret = key.Key; + dataSet.CryptoKey.AddCryptoKeyRow(cryptoKeyRow); } - public bool RemoveAssociation(Uri providerEndpoint, string handle) { - var row = dataSet.Association.FindByDistinguishingFactorHandle(providerEndpoint.AbsoluteUri, handle); + public void RemoveKey(string bucket, string handle) { + var row = dataSet.CryptoKey.FindByBucketHandle(bucket, handle); if (row != null) { - dataSet.Association.RemoveAssociationRow(row); - return true; - } else { - return false; + dataSet.CryptoKey.RemoveCryptoKeyRow(row); } } #endregion - internal void ClearExpiredAssociations() { - this.removeExpiredRows(dataSet.Association, dataSet.Association.ExpiresColumn.ColumnName); + internal void ClearExpiredSecrets() { + this.removeExpiredRows(dataSet.CryptoKey, dataSet.CryptoKey.ExpiresUtcColumn.ColumnName); } private void removeExpiredRows(DataTable table, string expiredColumnName) { - string filter = string.Format(CultureInfo.InvariantCulture, "{0} < #{1}#", expiredColumnName, DateTime.Now); + string filter = string.Format(CultureInfo.InvariantCulture, "{0} < #{1}#", expiredColumnName, DateTime.UtcNow); DataView view = new DataView(table, filter, null, DataViewRowState.CurrentRows); for (int i = view.Count - 1; i >= 0; i--) { view.Delete(i); |