| | | 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.Collections.Generic; |
| | | 6 | | using System.Xml; |
| | | 7 | | using CoreWCF.IdentityModel.Tokens; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.IdentityModel.Selectors |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// SecurityTokenSerializer is responsible for writing and reading SecurityKeyIdentifiers, SecurityKeyIdentifierClau |
| | | 13 | | /// In order to read SecurityTokens the SecurityTokenSerializer may need to resolve token references using the Secur |
| | | 14 | | /// The SecurityTokenSerializer is stateless |
| | | 15 | | /// Exceptions: XmlException, SecurityTokenException, NotSupportedException, InvalidOperationException, ArgumentExce |
| | | 16 | | /// </summary> |
| | | 17 | | public abstract class SecurityTokenSerializer |
| | | 18 | | { |
| | | 19 | | // public methods |
| | | 20 | | public bool CanReadToken(XmlReader reader) |
| | | 21 | | { |
| | 0 | 22 | | if (reader == null) |
| | | 23 | | { |
| | 0 | 24 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 25 | | } |
| | 0 | 26 | | return CanReadTokenCore(reader); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | public bool CanWriteToken(SecurityToken token) |
| | | 30 | | { |
| | 0 | 31 | | if (token == null) |
| | | 32 | | { |
| | 0 | 33 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token)); |
| | | 34 | | } |
| | 0 | 35 | | return CanWriteTokenCore(token); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | public bool CanReadKeyIdentifier(XmlReader reader) |
| | | 39 | | { |
| | 0 | 40 | | if (reader == null) |
| | | 41 | | { |
| | 0 | 42 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 43 | | } |
| | 0 | 44 | | return CanReadKeyIdentifierCore(reader); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | public bool CanWriteKeyIdentifier(SecurityKeyIdentifier keyIdentifier) |
| | | 48 | | { |
| | 0 | 49 | | if (keyIdentifier == null) |
| | | 50 | | { |
| | 0 | 51 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifier)); |
| | | 52 | | } |
| | 0 | 53 | | return CanWriteKeyIdentifierCore(keyIdentifier); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | public bool CanReadKeyIdentifierClause(XmlReader reader) |
| | | 57 | | { |
| | 12 | 58 | | if (reader == null) |
| | | 59 | | { |
| | 0 | 60 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 61 | | } |
| | 12 | 62 | | return CanReadKeyIdentifierClauseCore(reader); |
| | | 63 | | } |
| | | 64 | | |
| | | 65 | | public bool CanWriteKeyIdentifierClause(SecurityKeyIdentifierClause keyIdentifierClause) |
| | | 66 | | { |
| | 0 | 67 | | if (keyIdentifierClause == null) |
| | | 68 | | { |
| | 0 | 69 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause)); |
| | | 70 | | } |
| | 0 | 71 | | return CanWriteKeyIdentifierClauseCore(keyIdentifierClause); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | |
| | | 75 | | public SecurityToken ReadToken(XmlReader reader, SecurityTokenResolver tokenResolver) |
| | | 76 | | { |
| | 104 | 77 | | if (reader == null) |
| | | 78 | | { |
| | 0 | 79 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 80 | | } |
| | 104 | 81 | | return ReadTokenCore(reader, tokenResolver); |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | public void WriteToken(XmlWriter writer, SecurityToken token) |
| | | 85 | | { |
| | 20 | 86 | | if (writer == null) |
| | | 87 | | { |
| | 0 | 88 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer)); |
| | | 89 | | } |
| | 20 | 90 | | if (token == null) |
| | | 91 | | { |
| | 0 | 92 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(token)); |
| | | 93 | | } |
| | 20 | 94 | | WriteTokenCore(writer, token); |
| | 20 | 95 | | } |
| | | 96 | | |
| | | 97 | | public SecurityKeyIdentifier ReadKeyIdentifier(XmlReader reader) |
| | | 98 | | { |
| | 46 | 99 | | if (reader == null) |
| | | 100 | | { |
| | 0 | 101 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 102 | | } |
| | 46 | 103 | | return ReadKeyIdentifierCore(reader); |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | public void WriteKeyIdentifier(XmlWriter writer, SecurityKeyIdentifier keyIdentifier) |
| | | 107 | | { |
| | 0 | 108 | | if (writer == null) |
| | | 109 | | { |
| | 0 | 110 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer)); |
| | | 111 | | } |
| | 0 | 112 | | if (keyIdentifier == null) |
| | | 113 | | { |
| | 0 | 114 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifier)); |
| | | 115 | | } |
| | 0 | 116 | | WriteKeyIdentifierCore(writer, keyIdentifier); |
| | 0 | 117 | | } |
| | | 118 | | |
| | | 119 | | public SecurityKeyIdentifierClause ReadKeyIdentifierClause(XmlReader reader) |
| | | 120 | | { |
| | 78 | 121 | | if (reader == null) |
| | | 122 | | { |
| | 0 | 123 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 124 | | } |
| | 78 | 125 | | return ReadKeyIdentifierClauseCore(reader); |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | public void WriteKeyIdentifierClause(XmlWriter writer, SecurityKeyIdentifierClause keyIdentifierClause) |
| | | 129 | | { |
| | 40 | 130 | | if (writer == null) |
| | | 131 | | { |
| | 0 | 132 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer)); |
| | | 133 | | } |
| | 40 | 134 | | if (keyIdentifierClause == null) |
| | | 135 | | { |
| | 0 | 136 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause)); |
| | | 137 | | } |
| | 40 | 138 | | WriteKeyIdentifierClauseCore(writer, keyIdentifierClause); |
| | 40 | 139 | | } |
| | | 140 | | |
| | | 141 | | |
| | | 142 | | // protected abstract methods |
| | | 143 | | protected abstract bool CanReadTokenCore(XmlReader reader); |
| | | 144 | | protected abstract bool CanWriteTokenCore(SecurityToken token); |
| | | 145 | | protected abstract bool CanReadKeyIdentifierCore(XmlReader reader); |
| | | 146 | | protected abstract bool CanWriteKeyIdentifierCore(SecurityKeyIdentifier keyIdentifier); |
| | | 147 | | protected abstract bool CanReadKeyIdentifierClauseCore(XmlReader reader); |
| | | 148 | | protected abstract bool CanWriteKeyIdentifierClauseCore(SecurityKeyIdentifierClause keyIdentifierClause); |
| | | 149 | | |
| | | 150 | | protected abstract SecurityToken ReadTokenCore(XmlReader reader, SecurityTokenResolver tokenResolver); |
| | | 151 | | protected abstract void WriteTokenCore(XmlWriter writer, SecurityToken token); |
| | | 152 | | protected abstract SecurityKeyIdentifier ReadKeyIdentifierCore(XmlReader reader); |
| | | 153 | | protected abstract void WriteKeyIdentifierCore(XmlWriter writer, SecurityKeyIdentifier keyIdentifier); |
| | | 154 | | protected abstract SecurityKeyIdentifierClause ReadKeyIdentifierClauseCore(XmlReader reader); |
| | | 155 | | protected abstract void WriteKeyIdentifierClauseCore(XmlWriter writer, SecurityKeyIdentifierClause keyIdentifier |
| | | 156 | | |
| | | 157 | | internal abstract class KeyIdentifierClauseEntry |
| | | 158 | | { |
| | | 159 | | protected abstract XmlDictionaryString LocalName { get; } |
| | | 160 | | protected abstract XmlDictionaryString NamespaceUri { get; } |
| | | 161 | | |
| | | 162 | | public virtual bool CanReadKeyIdentifierClauseCore(XmlDictionaryReader reader) |
| | | 163 | | { |
| | 90 | 164 | | return reader.IsStartElement(LocalName, NamespaceUri); |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | public abstract SecurityKeyIdentifierClause ReadKeyIdentifierClauseCore(XmlDictionaryReader reader); |
| | | 168 | | |
| | | 169 | | public abstract bool SupportsCore(SecurityKeyIdentifierClause keyIdentifierClause); |
| | | 170 | | |
| | | 171 | | public abstract void WriteKeyIdentifierClauseCore(XmlDictionaryWriter writer, SecurityKeyIdentifierClause ke |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | internal abstract class StrEntry |
| | | 175 | | { |
| | | 176 | | public abstract string GetTokenTypeUri(); |
| | | 177 | | public abstract Type GetTokenType(SecurityKeyIdentifierClause clause); |
| | | 178 | | public abstract bool CanReadClause(XmlDictionaryReader reader, string tokenType); |
| | | 179 | | public abstract SecurityKeyIdentifierClause ReadClause(XmlDictionaryReader reader, byte[] derivationNonce, i |
| | | 180 | | public abstract bool SupportsCore(SecurityKeyIdentifierClause clause); |
| | | 181 | | public abstract void WriteContent(XmlDictionaryWriter writer, SecurityKeyIdentifierClause clause); |
| | | 182 | | } |
| | | 183 | | |
| | | 184 | | internal abstract class SerializerEntries |
| | | 185 | | { |
| | 134 | 186 | | public virtual void PopulateTokenEntries(IList<TokenEntry> tokenEntries) { } |
| | 400 | 187 | | public virtual void PopulateKeyIdentifierEntries(IList<KeyIdentifierEntry> keyIdentifierEntries) { } |
| | 132 | 188 | | public virtual void PopulateKeyIdentifierClauseEntries(IList<KeyIdentifierClauseEntry> keyIdentifierClauseEn |
| | 268 | 189 | | public virtual void PopulateStrEntries(IList<StrEntry> strEntries) { } |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | internal abstract class KeyIdentifierEntry |
| | | 193 | | { |
| | | 194 | | protected abstract XmlDictionaryString LocalName { get; } |
| | | 195 | | protected abstract XmlDictionaryString NamespaceUri { get; } |
| | | 196 | | |
| | | 197 | | public virtual bool CanReadKeyIdentifierCore(XmlDictionaryReader reader) |
| | | 198 | | { |
| | 0 | 199 | | return reader.IsStartElement(LocalName, NamespaceUri); |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | public abstract SecurityKeyIdentifier ReadKeyIdentifierCore(XmlDictionaryReader reader); |
| | | 203 | | |
| | | 204 | | public abstract bool SupportsCore(SecurityKeyIdentifier keyIdentifier); |
| | | 205 | | |
| | | 206 | | public abstract void WriteKeyIdentifierCore(XmlDictionaryWriter writer, SecurityKeyIdentifier keyIdentifier) |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | internal abstract class TokenEntry |
| | | 210 | | { |
| | | 211 | | private Type[] _tokenTypes = null; |
| | | 212 | | |
| | | 213 | | protected abstract XmlDictionaryString LocalName { get; } |
| | | 214 | | protected abstract XmlDictionaryString NamespaceUri { get; } |
| | 0 | 215 | | public Type TokenType { get { return GetTokenTypes()[0]; } } |
| | | 216 | | public abstract string TokenTypeUri { get; } |
| | | 217 | | protected abstract string ValueTypeUri { get; } |
| | | 218 | | |
| | | 219 | | public bool SupportsCore(Type tokenType) |
| | | 220 | | { |
| | 112 | 221 | | Type[] tokenTypes = GetTokenTypes(); |
| | 416 | 222 | | for (int i = 0; i < tokenTypes.Length; ++i) |
| | | 223 | | { |
| | 112 | 224 | | if (tokenTypes[i].IsAssignableFrom(tokenType)) |
| | | 225 | | { |
| | 16 | 226 | | return true; |
| | | 227 | | } |
| | | 228 | | } |
| | 96 | 229 | | return false; |
| | | 230 | | } |
| | | 231 | | |
| | | 232 | | protected abstract Type[] GetTokenTypesCore(); |
| | | 233 | | |
| | | 234 | | public Type[] GetTokenTypes() |
| | | 235 | | { |
| | 131 | 236 | | if (_tokenTypes == null) |
| | | 237 | | { |
| | 67 | 238 | | _tokenTypes = GetTokenTypesCore(); |
| | | 239 | | } |
| | | 240 | | |
| | 131 | 241 | | return _tokenTypes; |
| | | 242 | | } |
| | | 243 | | |
| | | 244 | | public virtual bool SupportsTokenTypeUri(string tokenTypeUri) |
| | | 245 | | { |
| | 105 | 246 | | return (TokenTypeUri == tokenTypeUri); |
| | | 247 | | } |
| | | 248 | | } |
| | | 249 | | } |
| | | 250 | | } |