| | | 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; |
| | | 6 | | using System.Collections.Generic; |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.IO; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 11 | | { |
| | | 12 | | public class SecurityKeyIdentifier : IEnumerable<SecurityKeyIdentifierClause> |
| | | 13 | | { |
| | | 14 | | private const int InitialSize = 2; |
| | | 15 | | private readonly List<SecurityKeyIdentifierClause> _clauses; |
| | | 16 | | |
| | 30 | 17 | | public SecurityKeyIdentifier() |
| | | 18 | | { |
| | 30 | 19 | | _clauses = new List<SecurityKeyIdentifierClause>(InitialSize); |
| | 30 | 20 | | } |
| | | 21 | | |
| | 50 | 22 | | public SecurityKeyIdentifier(params SecurityKeyIdentifierClause[] clauses) |
| | | 23 | | { |
| | 50 | 24 | | if (clauses == null) |
| | | 25 | | { |
| | 0 | 26 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(clauses)); |
| | | 27 | | } |
| | 50 | 28 | | _clauses = new List<SecurityKeyIdentifierClause>(clauses.Length); |
| | 200 | 29 | | for (int i = 0; i < clauses.Length; i++) |
| | | 30 | | { |
| | 50 | 31 | | Add(clauses[i]); |
| | | 32 | | } |
| | 50 | 33 | | } |
| | | 34 | | |
| | | 35 | | public SecurityKeyIdentifierClause this[int index] |
| | | 36 | | { |
| | 78 | 37 | | get { return _clauses[index]; } |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | public bool CanCreateKey |
| | | 41 | | { |
| | | 42 | | get |
| | | 43 | | { |
| | 0 | 44 | | for (int i = 0; i < Count; i++) |
| | | 45 | | { |
| | 0 | 46 | | if (this[i].CanCreateKey) |
| | | 47 | | { |
| | 0 | 48 | | return true; |
| | | 49 | | } |
| | | 50 | | } |
| | 0 | 51 | | return false; |
| | | 52 | | } |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | public int Count |
| | | 56 | | { |
| | 109 | 57 | | get { return _clauses.Count; } |
| | | 58 | | } |
| | | 59 | | |
| | 79 | 60 | | public bool IsReadOnly { get; private set; } |
| | | 61 | | |
| | | 62 | | public void Add(SecurityKeyIdentifierClause clause) |
| | | 63 | | { |
| | 79 | 64 | | if (IsReadOnly) |
| | | 65 | | { |
| | 0 | 66 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.ObjectIsReadO |
| | | 67 | | } |
| | 79 | 68 | | if (clause == null) |
| | | 69 | | { |
| | 0 | 70 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(clause))); |
| | | 71 | | } |
| | 79 | 72 | | _clauses.Add(clause); |
| | 79 | 73 | | } |
| | | 74 | | |
| | | 75 | | public SecurityKey CreateKey() |
| | | 76 | | { |
| | 0 | 77 | | for (int i = 0; i < Count; i++) |
| | | 78 | | { |
| | 0 | 79 | | if (this[i].CanCreateKey) |
| | | 80 | | { |
| | 0 | 81 | | return this[i].CreateKey(); |
| | | 82 | | } |
| | | 83 | | } |
| | 0 | 84 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.KeyIdentifierCann |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | public TClause Find<TClause>() where TClause : SecurityKeyIdentifierClause |
| | | 88 | | { |
| | 0 | 89 | | if (!TryFind<TClause>(out TClause clause)) |
| | | 90 | | { |
| | 0 | 91 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.NoKeyIdenti |
| | | 92 | | } |
| | 0 | 93 | | return clause; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | public IEnumerator<SecurityKeyIdentifierClause> GetEnumerator() |
| | | 97 | | { |
| | 0 | 98 | | return _clauses.GetEnumerator(); |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | public void MakeReadOnly() |
| | | 102 | | { |
| | 0 | 103 | | IsReadOnly = true; |
| | 0 | 104 | | } |
| | | 105 | | |
| | | 106 | | public override string ToString() |
| | | 107 | | { |
| | 0 | 108 | | using (StringWriter writer = new StringWriter(CultureInfo.InvariantCulture)) |
| | | 109 | | { |
| | 0 | 110 | | writer.WriteLine("SecurityKeyIdentifier"); |
| | 0 | 111 | | writer.WriteLine(" ("); |
| | 0 | 112 | | writer.WriteLine(" IsReadOnly = {0},", IsReadOnly); |
| | 0 | 113 | | writer.WriteLine(" Count = {0}{1}", Count, Count > 0 ? "," : ""); |
| | 0 | 114 | | for (int i = 0; i < Count; i++) |
| | | 115 | | { |
| | 0 | 116 | | writer.WriteLine(" Clause[{0}] = {1}{2}", i, this[i], i < Count - 1 ? "," : ""); |
| | | 117 | | } |
| | 0 | 118 | | writer.WriteLine(" )"); |
| | 0 | 119 | | return writer.ToString(); |
| | | 120 | | } |
| | 0 | 121 | | } |
| | | 122 | | |
| | | 123 | | public bool TryFind<TClause>(out TClause clause) where TClause : SecurityKeyIdentifierClause |
| | | 124 | | { |
| | 0 | 125 | | for (int i = 0; i < _clauses.Count; i++) |
| | | 126 | | { |
| | 0 | 127 | | if (_clauses[i] is TClause c) |
| | | 128 | | { |
| | 0 | 129 | | clause = c; |
| | 0 | 130 | | return true; |
| | | 131 | | } |
| | | 132 | | } |
| | 0 | 133 | | clause = null; |
| | 0 | 134 | | return false; |
| | | 135 | | } |
| | | 136 | | |
| | | 137 | | IEnumerator IEnumerable.GetEnumerator() |
| | | 138 | | { |
| | 0 | 139 | | return GetEnumerator(); |
| | | 140 | | } |
| | | 141 | | } |
| | | 142 | | } |