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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
|
//-----------------------------------------------------------------------
// <copyright file="Token.cs" company="Andrew Arnott, Microsoft Corporation">
// Copyright (c) Andrew Arnott, Microsoft Corporation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.InfoCard {
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.IdentityModel.Claims;
using System.IdentityModel.Policy;
using System.IdentityModel.Tokens;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using DotNetOpenAuth.Messaging;
/// <summary>
/// The decrypted token that was submitted as an Information Card.
/// </summary>
[ContractVerification(true)]
public class Token {
/// <summary>
/// Backing field for the <see cref="Claims"/> property.
/// </summary>
private IDictionary<string, string> claims;
/// <summary>
/// Backing field for the <see cref="UniqueId"/> property.
/// </summary>
private string uniqueId;
/// <summary>
/// Initializes a new instance of the <see cref="Token"/> class.
/// </summary>
/// <param name="tokenXml">Xml token, which may be encrypted.</param>
/// <param name="audience">The audience. May be <c>null</c> to avoid audience checking.</param>
/// <param name="decryptor">The decryptor to use to decrypt the token, if necessary..</param>
/// <exception cref="InformationCardException">Thrown for any problem decoding or decrypting the token.</exception>
[SuppressMessage("Microsoft.Usage", "CA2202:Do not dispose objects multiple times", Justification = "Not a problem for this type."), SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "False positive")]
private Token(string tokenXml, Uri audience, TokenDecryptor decryptor) {
Requires.NotNullOrEmpty(tokenXml, "tokenXml");
Requires.True(decryptor != null || !IsEncrypted(tokenXml), null);
Contract.Ensures(this.AuthorizationContext != null);
byte[] decryptedBytes;
string decryptedString;
using (StringReader xmlReader = new StringReader(tokenXml)) {
using (XmlReader tokenReader = XmlReader.Create(xmlReader)) {
Contract.Assume(tokenReader != null); // BCL contract should say XmlReader.Create result != null
if (IsEncrypted(tokenReader)) {
Logger.InfoCard.DebugFormat("Incoming SAML token, before decryption: {0}", tokenXml);
decryptedBytes = decryptor.DecryptToken(tokenReader);
decryptedString = Encoding.UTF8.GetString(decryptedBytes);
Contract.Assume(decryptedString != null); // BCL contracts should be enhanced here
} else {
decryptedBytes = Encoding.UTF8.GetBytes(tokenXml);
decryptedString = tokenXml;
}
}
}
var stringReader = new StringReader(decryptedString);
try {
this.Xml = new XPathDocument(stringReader).CreateNavigator();
} catch {
stringReader.Dispose();
throw;
}
Logger.InfoCard.DebugFormat("Incoming SAML token, after any decryption: {0}", this.Xml.InnerXml);
this.AuthorizationContext = TokenUtility.AuthenticateToken(this.Xml.ReadSubtree(), audience);
}
/// <summary>
/// Gets the AuthorizationContext behind this token.
/// </summary>
public AuthorizationContext AuthorizationContext { get; private set; }
/// <summary>
/// Gets the the decrypted token XML.
/// </summary>
public XPathNavigator Xml { get; private set; }
/// <summary>
/// Gets the UniqueID of this token, usable as a stable username that the user
/// has already verified belongs to him/her.
/// </summary>
/// <remarks>
/// By default, this uses the PPID and the Issuer's Public Key and hashes them
/// together to generate a UniqueID.
/// </remarks>
public string UniqueId {
get {
if (string.IsNullOrEmpty(this.uniqueId)) {
this.uniqueId = TokenUtility.GetUniqueName(this.AuthorizationContext);
}
return this.uniqueId;
}
}
/// <summary>
/// Gets the hash of the card issuer's public key.
/// </summary>
public string IssuerPubKeyHash {
get { return TokenUtility.GetIssuerPubKeyHash(this.AuthorizationContext); }
}
/// <summary>
/// Gets the Site Specific ID that the user sees in the Identity Selector.
/// </summary>
public string SiteSpecificId {
get {
Requires.ValidState(this.Claims.ContainsKey(ClaimTypes.PPID) && !string.IsNullOrEmpty(this.Claims[ClaimTypes.PPID]));
string ppidValue;
ErrorUtilities.VerifyOperation(this.Claims.TryGetValue(ClaimTypes.PPID, out ppidValue) && ppidValue != null, InfoCardStrings.PpidClaimRequired);
return TokenUtility.CalculateSiteSpecificID(ppidValue);
}
}
/// <summary>
/// Gets the claims in all the claimsets as a dictionary of strings.
/// </summary>
public IDictionary<string, string> Claims {
get {
if (this.claims == null) {
this.claims = this.GetFlattenedClaims();
}
return this.claims;
}
}
/// <summary>
/// Deserializes an XML document into a token.
/// </summary>
/// <param name="tokenXml">The token XML.</param>
/// <returns>The deserialized token.</returns>
public static Token Read(string tokenXml) {
Requires.NotNullOrEmpty(tokenXml, "tokenXml");
return Read(tokenXml, (Uri)null);
}
/// <summary>
/// Deserializes an XML document into a token.
/// </summary>
/// <param name="tokenXml">The token XML.</param>
/// <param name="audience">The URI that this token must have been crafted to be sent to. Use <c>null</c> to accept any intended audience.</param>
/// <returns>The deserialized token.</returns>
public static Token Read(string tokenXml, Uri audience) {
Requires.NotNullOrEmpty(tokenXml, "tokenXml");
return Read(tokenXml, audience, Enumerable.Empty<SecurityToken>());
}
/// <summary>
/// Deserializes an XML document into a token.
/// </summary>
/// <param name="tokenXml">The token XML.</param>
/// <param name="decryptionTokens">Any X.509 certificates that may be used to decrypt the token, if necessary.</param>
/// <returns>The deserialized token.</returns>
public static Token Read(string tokenXml, IEnumerable<SecurityToken> decryptionTokens) {
Requires.NotNullOrEmpty(tokenXml, "tokenXml");
Requires.NotNull(decryptionTokens, "decryptionTokens");
return Read(tokenXml, null, decryptionTokens);
}
/// <summary>
/// Deserializes an XML document into a token.
/// </summary>
/// <param name="tokenXml">The token XML.</param>
/// <param name="audience">The URI that this token must have been crafted to be sent to. Use <c>null</c> to accept any intended audience.</param>
/// <param name="decryptionTokens">Any X.509 certificates that may be used to decrypt the token, if necessary.</param>
/// <returns>The deserialized token.</returns>
public static Token Read(string tokenXml, Uri audience, IEnumerable<SecurityToken> decryptionTokens) {
Requires.NotNullOrEmpty(tokenXml, "tokenXml");
Requires.NotNull(decryptionTokens, "decryptionTokens");
Contract.Ensures(Contract.Result<Token>() != null);
TokenDecryptor decryptor = null;
if (IsEncrypted(tokenXml)) {
decryptor = new TokenDecryptor();
decryptor.Tokens.AddRange(decryptionTokens);
}
return new Token(tokenXml, audience, decryptor);
}
/// <summary>
/// Determines whether the specified token XML is encrypted.
/// </summary>
/// <param name="tokenXml">The token XML.</param>
/// <returns>
/// <c>true</c> if the specified token XML is encrypted; otherwise, <c>false</c>.
/// </returns>
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "False positive"), Pure]
internal static bool IsEncrypted(string tokenXml) {
Requires.NotNull(tokenXml, "tokenXml");
var stringReader = new StringReader(tokenXml);
XmlReader tokenReader;
try {
tokenReader = XmlReader.Create(stringReader);
} catch {
stringReader.Dispose();
throw;
}
try {
Contract.Assume(tokenReader != null); // CC missing for XmlReader.Create
return IsEncrypted(tokenReader);
} catch {
IDisposable disposableReader = tokenReader;
disposableReader.Dispose();
throw;
}
}
/// <summary>
/// Determines whether the specified token XML is encrypted.
/// </summary>
/// <param name="tokenXmlReader">The token XML.</param>
/// <returns>
/// <c>true</c> if the specified token XML is encrypted; otherwise, <c>false</c>.
/// </returns>
private static bool IsEncrypted(XmlReader tokenXmlReader) {
Requires.NotNull(tokenXmlReader, "tokenXmlReader");
return tokenXmlReader.IsStartElement(TokenDecryptor.XmlEncryptionStrings.EncryptedData, TokenDecryptor.XmlEncryptionStrings.Namespace);
}
#if CONTRACTS_FULL
/// <summary>
/// Verifies conditions that should be true for any valid state of this object.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification = "Called by code contracts.")]
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Called by code contracts.")]
[ContractInvariantMethod]
private void ObjectInvariant() {
Contract.Invariant(this.AuthorizationContext != null);
}
#endif
/// <summary>
/// Flattens the claims into a dictionary
/// </summary>
/// <returns>A dictionary of claim type URIs and claim values.</returns>
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Expensive call.")]
[Pure]
private IDictionary<string, string> GetFlattenedClaims() {
var flattenedClaims = new Dictionary<string, string>();
foreach (ClaimSet set in this.AuthorizationContext.ClaimSets) {
foreach (Claim claim in set) {
if (claim.Right == Rights.PossessProperty) {
flattenedClaims.Add(claim.ClaimType, TokenUtility.GetResourceValue(claim));
}
}
}
return flattenedClaims;
}
}
}
|