| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Runtime.Serialization; |
| | | 7 | | using System.Xml; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Represents a serializable version of a token that can be attached to a <see cref="System.Security.Claims.ClaimsI |
| | | 13 | | /// original token that was used to create <see cref="System.Security.Claims.ClaimsIdentity"/> |
| | | 14 | | /// </summary> |
| | | 15 | | [Serializable] |
| | | 16 | | public class BootstrapContext : ISerializable |
| | | 17 | | { |
| | | 18 | | private readonly SecurityToken _token; |
| | | 19 | | private readonly string _tokenString; |
| | | 20 | | private readonly byte[] _tokenBytes; |
| | | 21 | | private readonly SecurityTokenHandler _tokenHandler; |
| | | 22 | | private const string TokenTypeKey = "K"; |
| | | 23 | | private const string TokenKey = "T"; |
| | | 24 | | private const char SecurityTokenType = 'T'; |
| | | 25 | | private const char StringTokenType = 'S'; |
| | | 26 | | private const char ByteTokenType = 'B'; |
| | | 27 | | |
| | 0 | 28 | | protected BootstrapContext(SerializationInfo info, StreamingContext context) |
| | | 29 | | { |
| | 0 | 30 | | if (info == null) |
| | 0 | 31 | | return; |
| | | 32 | | |
| | 0 | 33 | | switch (info.GetChar(TokenTypeKey)) |
| | | 34 | | { |
| | | 35 | | case SecurityTokenType: |
| | | 36 | | { |
| | 0 | 37 | | if (context.Context is SecurityTokenHandler sth) |
| | | 38 | | { |
| | 0 | 39 | | using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(Convert.FromBase64S |
| | | 40 | | { |
| | 0 | 41 | | reader.MoveToContent(); |
| | 0 | 42 | | if (sth.CanReadToken(reader)) |
| | | 43 | | { |
| | 0 | 44 | | SecurityToken token = sth.ReadToken(reader); |
| | 0 | 45 | | if (token == null) |
| | | 46 | | { |
| | 0 | 47 | | _tokenString = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(info |
| | | 48 | | } |
| | | 49 | | else |
| | | 50 | | { |
| | 0 | 51 | | _token = token; |
| | | 52 | | } |
| | | 53 | | } |
| | 0 | 54 | | } |
| | | 55 | | } |
| | | 56 | | else |
| | | 57 | | { |
| | 0 | 58 | | _tokenString = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(info.GetString(T |
| | | 59 | | } |
| | | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | break; |
| | | 63 | | |
| | | 64 | | case StringTokenType: |
| | | 65 | | { |
| | 0 | 66 | | _tokenString = info.GetString(TokenKey); |
| | | 67 | | } |
| | 0 | 68 | | break; |
| | | 69 | | |
| | | 70 | | case ByteTokenType: |
| | | 71 | | { |
| | 0 | 72 | | _tokenBytes = (byte[])info.GetValue(TokenKey, typeof(byte[])); |
| | | 73 | | } |
| | | 74 | | break; |
| | | 75 | | |
| | | 76 | | default: |
| | | 77 | | break; |
| | | 78 | | } |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// A SecurityToken and a SecurityTokenHandler that can serialize the token. |
| | | 83 | | /// </summary> |
| | | 84 | | /// <param name=nameof(token)><see cref="SecurityToken"/> that can be serialized. Cannot be null.</param> |
| | | 85 | | /// <param name="tokenHandler"><see cref="SecurityTokenHandler"/> that is responsible for serializing the token. |
| | | 86 | | /// <exception cref="ArgumentNullException"> thrown if 'token' or 'tokenHandler' is null.</exception> |
| | | 87 | | /// <remarks>The <see cref="SecurityTokenHandler"/> is used not used to deserialize the token as it cannot be as |
| | 28 | 88 | | public BootstrapContext(SecurityToken token, SecurityTokenHandler tokenHandler) |
| | | 89 | | { |
| | 28 | 90 | | _token = token ?? throw new ArgumentNullException(nameof(token)); |
| | 28 | 91 | | _tokenHandler = tokenHandler ?? throw new ArgumentNullException(nameof(tokenHandler)); |
| | 28 | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// String that represents a SecurityToken. |
| | | 96 | | /// </summary> |
| | | 97 | | /// <param name=nameof(token)>string that represents a token. Can not be null.</param> |
| | | 98 | | /// <exception cref="ArgumentNullException"> thrown if 'token' is null.</exception> |
| | 0 | 99 | | public BootstrapContext(string token) |
| | | 100 | | { |
| | 0 | 101 | | _tokenString = token ?? throw new ArgumentNullException(nameof(token)); |
| | 0 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <summary> |
| | | 105 | | /// String that represents a SecurityToken. |
| | | 106 | | /// </summary> |
| | | 107 | | /// <param name=nameof(token)>string that represents a token. Can not be null.</param> |
| | | 108 | | /// <exception cref="ArgumentNullException"> thrown if 'token' is null.</exception> |
| | 0 | 109 | | public BootstrapContext(byte[] token) |
| | | 110 | | { |
| | 0 | 111 | | _tokenBytes = token ?? throw new ArgumentNullException(nameof(token)); |
| | 0 | 112 | | } |
| | | 113 | | |
| | | 114 | | #region ISerializable Members |
| | | 115 | | /// <summary> |
| | | 116 | | /// Called to serialize this context. |
| | | 117 | | /// </summary> |
| | | 118 | | /// <param name="info"><see cref="SerializationInfo"/> container for storing data. Cannot be null.</param> |
| | | 119 | | /// <param name="context"><see cref="StreamingContext"/> contains the context for streaming and optionally addit |
| | | 120 | | /// <exception cref="ArgumentNullException"> thrown if 'info' is null.</exception> |
| | | 121 | | public virtual void GetObjectData(SerializationInfo info, StreamingContext context) |
| | | 122 | | { |
| | 0 | 123 | | if (_tokenBytes != null) |
| | | 124 | | { |
| | 0 | 125 | | info.AddValue(TokenTypeKey, ByteTokenType); |
| | 0 | 126 | | info.AddValue(TokenKey, _tokenBytes); |
| | | 127 | | } |
| | 0 | 128 | | else if (_tokenString != null) |
| | | 129 | | { |
| | 0 | 130 | | info.AddValue(TokenTypeKey, StringTokenType); |
| | 0 | 131 | | info.AddValue(TokenKey, _tokenString); |
| | | 132 | | } |
| | 0 | 133 | | else if (_token != null && _tokenHandler != null) |
| | | 134 | | { |
| | 0 | 135 | | using (MemoryStream ms = new MemoryStream()) |
| | | 136 | | { |
| | 0 | 137 | | info.AddValue(TokenTypeKey, SecurityTokenType); |
| | 0 | 138 | | using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(ms, System.Text.Encoding.UT |
| | | 139 | | { |
| | 0 | 140 | | _tokenHandler.WriteToken(writer, _token); |
| | 0 | 141 | | writer.Flush(); |
| | 0 | 142 | | info.AddValue(TokenKey, Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length)); |
| | 0 | 143 | | } |
| | | 144 | | } |
| | | 145 | | } |
| | 0 | 146 | | } |
| | | 147 | | |
| | | 148 | | #endregion |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Gets the string that was passed in constructor. If a different constructor was used, will be null. |
| | | 152 | | /// </summary> |
| | | 153 | | public byte[] TokenBytes |
| | | 154 | | { |
| | 0 | 155 | | get { return _tokenBytes; } |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// Gets the string that was passed in constructor. If a different constructor was used, will be null. |
| | | 160 | | /// </summary> |
| | | 161 | | public string Token |
| | | 162 | | { |
| | 0 | 163 | | get { return _tokenString; } |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | /// <summary> |
| | | 167 | | /// Gets the SecurityToken that was passed in constructor. If a different constructor was used, will be null. |
| | | 168 | | /// </summary> |
| | | 169 | | public SecurityToken SecurityToken |
| | | 170 | | { |
| | 0 | 171 | | get { return _token; } |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// Gets the SecurityTokenHandler that was passed in constructor. If a different constructor was used, will be n |
| | | 176 | | /// </summary> |
| | | 177 | | public SecurityTokenHandler SecurityTokenHandler |
| | | 178 | | { |
| | 0 | 179 | | get { return _tokenHandler; } |
| | | 180 | | } |
| | | 181 | | } |
| | | 182 | | } |