| | | 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.Text; |
| | | 6 | | |
| | | 7 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 8 | | { |
| | | 9 | | /// <summary> |
| | | 10 | | /// When caching an <see cref="SessionSecurityToken"/> there are two indexes required. One is the ContextId |
| | | 11 | | /// that is unique across all <see cref="SessionSecurityToken"/> and the next is KeyGeneration which is |
| | | 12 | | /// unique within a session. When an <see cref="SessionSecurityToken"/> is issued it has only a ContextId. When |
| | | 13 | | /// the <see cref="SessionSecurityToken"/> is renewed the KeyGeneration is added as an second index to the |
| | | 14 | | /// <see cref="SessionSecurityToken"/>. Now the renewed <see cref="SessionSecurityToken"/> is uniquely identifiable |
| | | 15 | | /// KeyGeneration. |
| | | 16 | | /// The class <see cref="SessionSecurityTokenCacheKey"/> is used as the index |
| | | 17 | | /// to the <see cref="SessionSecurityToken"/> cache. This index will always have a valid ContextId specified |
| | | 18 | | /// but the KeyGeneration may be null. There is also an optional EndpointId |
| | | 19 | | /// which gives the endpoint to which the token is scoped. |
| | | 20 | | /// </summary> |
| | | 21 | | public class SessionSecurityTokenCacheKey |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Creates an instance of <see cref="SessionSecurityTokenCacheKey"/> which |
| | | 25 | | /// is used as an index while caching <see cref="SessionSecurityToken"/>. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="endpointId">The endpoint Id to which the <see cref="SessionSecurityToken"/> is scoped.</param> |
| | | 28 | | /// <param name="contextId">UniqueId of the <see cref="SessionSecurityToken"/>.</param> |
| | | 29 | | /// <param name="keyGeneration">UniqueId which is available when the <see cref="SessionSecurityToken"/> is renew |
| | | 30 | | /// null when caching a new <see cref="SessionSecurityToken"/>.</param> |
| | 0 | 31 | | public SessionSecurityTokenCacheKey(string endpointId, System.Xml.UniqueId contextId, System.Xml.UniqueId keyGen |
| | | 32 | | { |
| | 0 | 33 | | EndpointId = endpointId ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(endpointI |
| | 0 | 34 | | ContextId = contextId ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(contextId)) |
| | 0 | 35 | | KeyGeneration = keyGeneration; |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets or sets a value indicating whether KeyGeneration can be ignored |
| | | 40 | | /// while doing index comparison. |
| | | 41 | | /// </summary> |
| | 0 | 42 | | public bool IgnoreKeyGeneration { get; set; } |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Gets the ContextId of the <see cref="SessionSecurityToken"/> |
| | | 46 | | /// </summary> |
| | 0 | 47 | | public System.Xml.UniqueId ContextId { get; } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Gets the EndpointId to which this cache entry is scoped. |
| | | 51 | | /// </summary> |
| | 0 | 52 | | public string EndpointId { get; } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Gets the KeyGeneration of the <see cref="SessionSecurityToken"/> |
| | | 56 | | /// </summary> |
| | 0 | 57 | | public System.Xml.UniqueId KeyGeneration { get; } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Implements the equality operator for <see cref="SessionSecurityTokenCacheKey"/>. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <param name="first">First object to compare.</param> |
| | | 63 | | /// <param name="second">Second object to compare.</param> |
| | | 64 | | /// <returns>'true' if both objects are equal.</returns> |
| | | 65 | | public static bool operator ==(SessionSecurityTokenCacheKey first, SessionSecurityTokenCacheKey second) |
| | | 66 | | { |
| | 0 | 67 | | if (ReferenceEquals(first, null)) |
| | | 68 | | { |
| | 0 | 69 | | return ReferenceEquals(second, null); |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | return first.Equals(second); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Implements the inequality operator for <see cref="SessionSecurityTokenCacheKey"/>. |
| | | 77 | | /// </summary> |
| | | 78 | | /// <param name="first">First object to compare.</param> |
| | | 79 | | /// <param name="second">Second object to compare.</param> |
| | | 80 | | /// <returns>'true' if both the objects are different.</returns> |
| | | 81 | | public static bool operator !=(SessionSecurityTokenCacheKey first, SessionSecurityTokenCacheKey second) |
| | | 82 | | { |
| | 0 | 83 | | return !(first == second); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Checks if the given object is the same as the current object. |
| | | 88 | | /// </summary> |
| | | 89 | | /// <param name="obj">The object to be compared.</param> |
| | | 90 | | /// <returns>'true' if both are the same object else false.</returns> |
| | | 91 | | public override bool Equals(object obj) |
| | | 92 | | { |
| | 0 | 93 | | if (obj is SessionSecurityTokenCacheKey) |
| | | 94 | | { |
| | 0 | 95 | | SessionSecurityTokenCacheKey key2 = obj as SessionSecurityTokenCacheKey; |
| | 0 | 96 | | if (key2.ContextId != ContextId) |
| | | 97 | | { |
| | 0 | 98 | | return false; |
| | | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | if (!StringComparer.Ordinal.Equals(key2.EndpointId, EndpointId)) |
| | | 102 | | { |
| | 0 | 103 | | return false; |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | // If KeyGeneration can be ignored on either one of them then we |
| | | 107 | | // don't do KeyGeneration comparison. |
| | 0 | 108 | | if (!IgnoreKeyGeneration && !key2.IgnoreKeyGeneration) |
| | | 109 | | { |
| | 0 | 110 | | return key2.KeyGeneration == KeyGeneration; |
| | | 111 | | } |
| | | 112 | | |
| | 0 | 113 | | return true; |
| | | 114 | | } |
| | | 115 | | |
| | 0 | 116 | | return false; |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Returns a Hash code for this object. |
| | | 121 | | /// </summary> |
| | | 122 | | /// <returns>Hash code for the object as a Integer.</returns> |
| | | 123 | | public override int GetHashCode() |
| | | 124 | | { |
| | 0 | 125 | | if (KeyGeneration == null) |
| | | 126 | | { |
| | 0 | 127 | | return ContextId.GetHashCode(); |
| | | 128 | | } |
| | | 129 | | else |
| | | 130 | | { |
| | 0 | 131 | | return ContextId.GetHashCode() ^ KeyGeneration.GetHashCode(); |
| | | 132 | | } |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | /// <summary> |
| | | 136 | | /// Implements ToString() to provide a unique identifier. |
| | | 137 | | /// </summary> |
| | | 138 | | /// <returns>This key, in string form.</returns> |
| | | 139 | | public override string ToString() |
| | | 140 | | { |
| | 0 | 141 | | StringBuilder sb = new StringBuilder(); |
| | 0 | 142 | | sb.Append(EndpointId); |
| | 0 | 143 | | sb.Append(';'); |
| | 0 | 144 | | sb.Append(ContextId.ToString()); |
| | 0 | 145 | | sb.Append(';'); |
| | 0 | 146 | | if (!IgnoreKeyGeneration && KeyGeneration != null) |
| | | 147 | | { |
| | 0 | 148 | | sb.Append(KeyGeneration.ToString()); |
| | | 149 | | } |
| | | 150 | | |
| | 0 | 151 | | return sb.ToString(); |
| | | 152 | | } |
| | | 153 | | } |
| | | 154 | | } |