blob: a8ac41ebe0d2f01d552298e9c67ec8cb46d3d90b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
//-----------------------------------------------------------------------
// <copyright file="AssociationDataBag.cs" company="Andrew Arnott">
// Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.OpenId.Provider {
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Text;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.Messaging.Bindings;
/// <summary>
/// A signed and encrypted serialization of an association.
/// </summary>
internal class AssociationDataBag : DataBag, IStreamSerializingDataBag {
/// <summary>
/// Initializes a new instance of the <see cref="AssociationDataBag"/> class.
/// </summary>
public AssociationDataBag() {
}
/// <summary>
/// Gets or sets the association secret.
/// </summary>
[MessagePart(IsRequired = true)]
internal byte[] Secret { get; set; }
/// <summary>
/// Gets or sets the UTC time that this association expires.
/// </summary>
[MessagePart(IsRequired = true)]
internal DateTime ExpiresUtc { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this instance is for "dumb" mode RPs.
/// </summary>
/// <value>
/// <c>true</c> if this instance is private association; otherwise, <c>false</c>.
/// </value>
[MessagePart(IsRequired = true)]
internal bool IsPrivateAssociation {
get { return this.AssociationType == AssociationRelyingPartyType.Dumb; }
set { this.AssociationType = value ? AssociationRelyingPartyType.Dumb : AssociationRelyingPartyType.Smart; }
}
/// <summary>
/// Gets or sets the type of the association (shared or private, a.k.a. smart or dumb).
/// </summary>
internal AssociationRelyingPartyType AssociationType { get; set; }
/// <summary>
/// Serializes the instance to the specified stream.
/// </summary>
/// <param name="stream">The stream.</param>
public void Serialize(Stream stream) {
var writer = new BinaryWriter(stream);
writer.Write(this.IsPrivateAssociation);
writer.WriteBuffer(this.Secret);
writer.Write((int)(this.ExpiresUtc - TimestampEncoder.Epoch).TotalSeconds);
writer.Flush();
}
/// <summary>
/// Initializes the fields on this instance from the specified stream.
/// </summary>
/// <param name="stream">The stream.</param>
public void Deserialize(Stream stream) {
var reader = new BinaryReader(stream);
this.IsPrivateAssociation = reader.ReadBoolean();
this.Secret = reader.ReadBuffer();
this.ExpiresUtc = TimestampEncoder.Epoch + TimeSpan.FromSeconds(reader.ReadInt32());
}
/// <summary>
/// Creates the formatter used for serialization of this type.
/// </summary>
/// <param name="cryptoKeyStore">The crypto key store used when signing or encrypting.</param>
/// <param name="bucket">The bucket in which symmetric keys are stored for signing/encrypting data.</param>
/// <param name="minimumAge">The minimum age.</param>
/// <returns>
/// A formatter for serialization.
/// </returns>
internal static IDataBagFormatter<AssociationDataBag> CreateFormatter(ICryptoKeyStore cryptoKeyStore, string bucket, TimeSpan? minimumAge = null) {
Contract.Requires<ArgumentNullException>(cryptoKeyStore != null);
Contract.Requires<ArgumentException>(!String.IsNullOrEmpty(bucket));
Contract.Ensures(Contract.Result<IDataBagFormatter<AssociationDataBag>>() != null);
return new BinaryDataBagFormatter<AssociationDataBag>(cryptoKeyStore, bucket, signed: true, encrypted: true, minimumAge: minimumAge);
}
}
}
|