| | | 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.Collections.ObjectModel; |
| | | 7 | | using System.Runtime.Serialization; |
| | | 8 | | using SamlAuthorityBinding = Microsoft.IdentityModel.Tokens.Saml.SamlAuthorityBinding; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 11 | | { |
| | | 12 | | [DataContract(Namespace = "http://schemas.datacontract.org/2004/07/System.IdentityModel.Tokens")] |
| | | 13 | | public class SamlAuthenticationClaimResource |
| | | 14 | | { |
| | | 15 | | [DataMember] |
| | | 16 | | private DateTime authenticationInstant; |
| | | 17 | | [DataMember] |
| | | 18 | | private string authenticationMethod; |
| | | 19 | | [DataMember] |
| | | 20 | | private string dnsAddress; |
| | | 21 | | [DataMember] |
| | | 22 | | private string ipAddress; |
| | | 23 | | |
| | | 24 | | [OnDeserialized] |
| | | 25 | | private void OnDeserialized(StreamingContext ctx) |
| | | 26 | | { |
| | 0 | 27 | | if (string.IsNullOrEmpty(authenticationMethod)) |
| | 0 | 28 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(authenticationMethod)); |
| | 0 | 29 | | if (AuthorityBindings == null) |
| | 0 | 30 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(AuthorityBindings)); |
| | 0 | 31 | | } |
| | | 32 | | |
| | 0 | 33 | | public SamlAuthenticationClaimResource( |
| | 0 | 34 | | DateTime authenticationInstant, |
| | 0 | 35 | | string authenticationMethod, |
| | 0 | 36 | | string dnsAddress, |
| | 0 | 37 | | string ipAddress |
| | 0 | 38 | | ) |
| | | 39 | | { |
| | 0 | 40 | | if (string.IsNullOrEmpty(authenticationMethod)) |
| | 0 | 41 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(authenticationMethod)); |
| | | 42 | | |
| | 0 | 43 | | this.authenticationInstant = authenticationInstant; |
| | 0 | 44 | | this.authenticationMethod = authenticationMethod; |
| | 0 | 45 | | this.dnsAddress = dnsAddress; |
| | 0 | 46 | | this.ipAddress = ipAddress; |
| | 0 | 47 | | AuthorityBindings = (new List<SamlAuthorityBinding>()).AsReadOnly(); |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | public SamlAuthenticationClaimResource( |
| | | 51 | | DateTime authenticationInstant, |
| | | 52 | | string authenticationMethod, |
| | | 53 | | string dnsAddress, |
| | | 54 | | string ipAddress, |
| | | 55 | | IEnumerable<SamlAuthorityBinding> authorityBindings |
| | | 56 | | ) |
| | 0 | 57 | | : this(authenticationInstant, authenticationMethod, dnsAddress, ipAddress) |
| | | 58 | | { |
| | 0 | 59 | | if (authorityBindings == null) |
| | 0 | 60 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(authorityBind |
| | | 61 | | |
| | 0 | 62 | | List<SamlAuthorityBinding> tempList = new List<SamlAuthorityBinding>(); |
| | 0 | 63 | | foreach (SamlAuthorityBinding authorityBinding in authorityBindings) |
| | | 64 | | { |
| | 0 | 65 | | if (authorityBinding != null) |
| | 0 | 66 | | tempList.Add(authorityBinding); |
| | | 67 | | } |
| | 0 | 68 | | AuthorityBindings = tempList.AsReadOnly(); |
| | | 69 | | |
| | 0 | 70 | | } |
| | | 71 | | |
| | | 72 | | public SamlAuthenticationClaimResource( |
| | | 73 | | DateTime authenticationInstant, |
| | | 74 | | string authenticationMethod, |
| | | 75 | | string dnsAddress, |
| | | 76 | | string ipAddress, |
| | | 77 | | ReadOnlyCollection<SamlAuthorityBinding> authorityBindings |
| | | 78 | | ) |
| | 0 | 79 | | : this(authenticationInstant, authenticationMethod, dnsAddress, ipAddress) |
| | | 80 | | { |
| | 0 | 81 | | AuthorityBindings = authorityBindings ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Argum |
| | | 82 | | |
| | 0 | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | public DateTime AuthenticationInstant => authenticationInstant; |
| | | 86 | | |
| | 0 | 87 | | public string AuthenticationMethod => authenticationMethod; |
| | | 88 | | |
| | 0 | 89 | | public ReadOnlyCollection<SamlAuthorityBinding> AuthorityBindings { get; private set; } |
| | | 90 | | |
| | | 91 | | [DataMember] |
| | | 92 | | private List<SamlAuthorityBinding> SamlAuthorityBindings |
| | | 93 | | { |
| | | 94 | | get |
| | | 95 | | { |
| | 0 | 96 | | List<SamlAuthorityBinding> sab = new List<SamlAuthorityBinding>(); |
| | 0 | 97 | | for (int i = 0; i < AuthorityBindings.Count; ++i) |
| | | 98 | | { |
| | 0 | 99 | | sab.Add(AuthorityBindings[i]); |
| | | 100 | | } |
| | 0 | 101 | | return sab; |
| | | 102 | | } |
| | | 103 | | set |
| | | 104 | | { |
| | 0 | 105 | | if (value != null) |
| | 0 | 106 | | AuthorityBindings = value.AsReadOnly(); |
| | 0 | 107 | | } |
| | | 108 | | } |
| | | 109 | | |
| | 0 | 110 | | public string IPAddress => ipAddress; |
| | | 111 | | |
| | 0 | 112 | | public string DnsAddress => dnsAddress; |
| | | 113 | | |
| | | 114 | | public override bool Equals(object obj) |
| | | 115 | | { |
| | 0 | 116 | | if (obj == null) |
| | 0 | 117 | | return false; |
| | | 118 | | |
| | 0 | 119 | | if (ReferenceEquals(this, obj)) |
| | 0 | 120 | | return true; |
| | | 121 | | |
| | 0 | 122 | | if (!(obj is SamlAuthenticationClaimResource rhs)) |
| | 0 | 123 | | return false; |
| | | 124 | | |
| | 0 | 125 | | if ((AuthenticationInstant != rhs.AuthenticationInstant) || |
| | 0 | 126 | | (AuthenticationMethod != rhs.AuthenticationMethod) || |
| | 0 | 127 | | (AuthorityBindings.Count != rhs.AuthorityBindings.Count) || |
| | 0 | 128 | | (IPAddress != rhs.IPAddress) || |
| | 0 | 129 | | (DnsAddress != rhs.DnsAddress)) |
| | 0 | 130 | | return false; |
| | | 131 | | |
| | | 132 | | int i; |
| | 0 | 133 | | for (i = 0; i < AuthorityBindings.Count; ++i) |
| | | 134 | | { |
| | 0 | 135 | | bool matched = false; |
| | 0 | 136 | | for (int j = 0; j < rhs.AuthorityBindings.Count; ++j) |
| | | 137 | | { |
| | 0 | 138 | | if ((AuthorityBindings[i].AuthorityKind == rhs.AuthorityBindings[j].AuthorityKind) && |
| | 0 | 139 | | (AuthorityBindings[i].Binding == rhs.AuthorityBindings[j].Binding) && |
| | 0 | 140 | | (AuthorityBindings[i].Location == rhs.AuthorityBindings[j].Location)) |
| | | 141 | | { |
| | 0 | 142 | | matched = true; |
| | 0 | 143 | | break; |
| | | 144 | | } |
| | | 145 | | } |
| | | 146 | | |
| | 0 | 147 | | if (!matched) |
| | 0 | 148 | | return false; |
| | | 149 | | } |
| | | 150 | | |
| | 0 | 151 | | return true; |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | public override int GetHashCode() |
| | | 155 | | { |
| | 0 | 156 | | return (authenticationInstant.GetHashCode() ^ authenticationMethod.GetHashCode()); |
| | | 157 | | } |
| | | 158 | | } |
| | | 159 | | } |