Merge pull request #4621 from alexanderkyte/strdup_env
[mono.git] / mcs / class / System.IdentityModel / System.IdentityModel.Tokens / BootstrapContext.cs
1 //
2 // BootstrapContext.cs
3 //
4 // Author:
5 //   Robert J. van der Boon (rjvdboon@gmail.com)
6 //
7 // Copyright (C) 2014 Robert J. van der Boon
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28 using System;
29 using System.IO;
30 using System.Runtime.Serialization;
31 using System.Text;
32 using System.Xml;
33
34 namespace System.IdentityModel.Tokens {
35         [Serializable]
36         public class BootstrapContext : ISerializable {
37                 /// <summary>Gets the string that was used to initialize the context.</summary>
38                 public string Token { get; private set; }
39                 /// <summary>Gets the array that was used to initialize the context.</summary>
40                 public byte [] TokenBytes { get; private set; }
41                 /// <summary>Gets the security token that was used to initialize the context.</summary>
42                 public SecurityToken SecurityToken { get; private set; }
43                 /// <summary>Gets the token handler that was used to initialize the context.</summary>
44                 public SecurityTokenHandler SecurityTokenHandler { get; private set; }
45
46                 /// <summary>Initializes a new instance of the <see cref="BootstrapContext"/> class by using the specified string.</summary>
47                 public BootstrapContext (string token)
48                 {
49                         if (token == null)
50                                 throw new ArgumentNullException ("token");
51                         Token = token;
52                 }
53
54                 /// <summary>Initializes a new instance of the <see cref="BootstrapContext"/> class by using the specified array.</summary>
55                 public BootstrapContext (byte [] token)
56                 {
57                         if (token == null)
58                                 throw new ArgumentNullException ("token");
59                         TokenBytes = token;
60                 }
61
62                 /// <summary>Initializes a new instance of the <see cref="BootstrapContext"/> class by using the specified security token and token handler.</summary>
63                 public BootstrapContext (SecurityToken token, SecurityTokenHandler tokenHandler)
64                 {
65                         if (token == null)
66                                 throw new ArgumentNullException ("token");
67                         if (tokenHandler == null)
68                                 throw new ArgumentNullException ("tokenHandler");
69                         SecurityToken = token;
70                         SecurityTokenHandler = tokenHandler;
71                 }
72
73                 /// <summary>Initializes a new instance of the <see cref="BootstrapContext"/> class from a stream.</summary>
74                 protected BootstrapContext (SerializationInfo info, StreamingContext context)
75                 {
76                         if (info == null)
77                                 throw new ArgumentNullException ("info");
78                         char type = info.GetChar ("K");
79                         switch (type) {
80                         case 'S':
81                                 Token = info.GetString ("T");
82                                 break;
83                         case 'B':
84                                 TokenBytes = (byte [])info.GetValue ("T", typeof (byte []));
85                                 break;
86                         case 'T':
87                                 Token = Encoding.UTF8.GetString (Convert.FromBase64String (info.GetString ("T")));
88                                 break;
89                         }
90                 }
91
92                 /// <summary>Populates the <see cref="SerializationInfo"/> with data needed to serialize the current <see cref="BootstrapContext"/> object.</summary>
93                 public void GetObjectData (SerializationInfo info, StreamingContext context)
94                 {
95                         if (info == null)
96                                 throw new ArgumentNullException ("info");
97                         if (Token != null) {
98                                 info.AddValue ("K", 'S');
99                                 info.AddValue ("T", Token);
100                         } else if (TokenBytes != null) {
101                                 info.AddValue ("K", 'B');
102                                 info.AddValue ("T", TokenBytes);
103                         } else if (SecurityToken != null && SecurityTokenHandler != null) {
104                                 info.AddValue ("K", 'T');
105                                 using (var ms = new MemoryStream ())
106                                 using (var streamWriter = new StreamWriter (ms, new UTF8Encoding (false)))
107                                 using (var writer = XmlWriter.Create (streamWriter, new XmlWriterSettings { OmitXmlDeclaration = true })) {
108                                         SecurityTokenHandler.WriteToken (writer, SecurityToken);
109                                         writer.Flush ();
110                                         info.AddValue ("T", Convert.ToBase64String (ms.ToArray ()));
111                                 }
112                         }
113                 }
114         }
115 }