| | | 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.ObjectModel; |
| | | 6 | | using System.Xml; |
| | | 7 | | using CoreWCF.IdentityModel; |
| | | 8 | | using CoreWCF.IdentityModel.Selectors; |
| | | 9 | | using CoreWCF.IdentityModel.Tokens; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Security.Tokens |
| | | 12 | | { |
| | | 13 | | public class SecurityContextSecurityTokenResolver : SecurityTokenResolver, ISecurityContextSecurityTokenCache |
| | | 14 | | { |
| | | 15 | | private readonly SecurityContextTokenCache _tokenCache; |
| | 23 | 16 | | private TimeSpan _clockSkew = SecurityProtocolFactory.defaultMaxClockSkew; |
| | | 17 | | |
| | | 18 | | public SecurityContextSecurityTokenResolver(int securityContextCacheCapacity, bool removeOldestTokensOnCacheFull |
| | 23 | 19 | | : this(securityContextCacheCapacity, removeOldestTokensOnCacheFull, SecurityProtocolFactory.defaultMaxClockS |
| | | 20 | | { |
| | 23 | 21 | | } |
| | | 22 | | |
| | 23 | 23 | | public SecurityContextSecurityTokenResolver(int securityContextCacheCapacity, bool removeOldestTokensOnCacheFull |
| | | 24 | | { |
| | 23 | 25 | | if (securityContextCacheCapacity <= 0) |
| | | 26 | | { |
| | 0 | 27 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(securit |
| | | 28 | | } |
| | | 29 | | |
| | 23 | 30 | | if (clockSkew < TimeSpan.Zero) |
| | | 31 | | { |
| | 0 | 32 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(clockSk |
| | | 33 | | } |
| | | 34 | | |
| | 23 | 35 | | SecurityContextTokenCacheCapacity = securityContextCacheCapacity; |
| | 23 | 36 | | RemoveOldestTokensOnCacheFull = removeOldestTokensOnCacheFull; |
| | 23 | 37 | | _clockSkew = clockSkew; |
| | 23 | 38 | | _tokenCache = new SecurityContextTokenCache(SecurityContextTokenCacheCapacity, RemoveOldestTokensOnCacheFull |
| | 23 | 39 | | } |
| | | 40 | | |
| | 23 | 41 | | public int SecurityContextTokenCacheCapacity { get; } |
| | | 42 | | |
| | | 43 | | public TimeSpan ClockSkew |
| | | 44 | | { |
| | | 45 | | get |
| | | 46 | | { |
| | 0 | 47 | | return _clockSkew; |
| | | 48 | | } |
| | | 49 | | } |
| | | 50 | | |
| | 23 | 51 | | public bool RemoveOldestTokensOnCacheFull { get; } |
| | | 52 | | |
| | | 53 | | public void AddContext(SecurityContextSecurityToken token) |
| | | 54 | | { |
| | 10 | 55 | | _tokenCache.AddContext(token); |
| | 10 | 56 | | } |
| | | 57 | | |
| | | 58 | | public bool TryAddContext(SecurityContextSecurityToken token) |
| | | 59 | | { |
| | 0 | 60 | | return _tokenCache.TryAddContext(token); |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | |
| | | 64 | | public void ClearContexts() |
| | | 65 | | { |
| | 0 | 66 | | _tokenCache.ClearContexts(); |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | public void RemoveContext(UniqueId contextId, UniqueId generation) |
| | | 70 | | { |
| | 0 | 71 | | _tokenCache.RemoveContext(contextId, generation, false); |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | public void RemoveAllContexts(UniqueId contextId) |
| | | 75 | | { |
| | 10 | 76 | | _tokenCache.RemoveAllContexts(contextId); |
| | 10 | 77 | | } |
| | | 78 | | |
| | | 79 | | public SecurityContextSecurityToken GetContext(UniqueId contextId, UniqueId generation) |
| | | 80 | | { |
| | 20 | 81 | | return _tokenCache.GetContext(contextId, generation); |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | public Collection<SecurityContextSecurityToken> GetAllContexts(UniqueId contextId) |
| | | 85 | | { |
| | 0 | 86 | | return _tokenCache.GetAllContexts(contextId); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | public void UpdateContextCachingTime(SecurityContextSecurityToken context, DateTime expirationTime) |
| | | 90 | | { |
| | 0 | 91 | | _tokenCache.UpdateContextCachingTime(context, expirationTime); |
| | 0 | 92 | | } |
| | | 93 | | |
| | | 94 | | protected override bool TryResolveTokenCore(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityToken t |
| | | 95 | | { |
| | 0 | 96 | | if (keyIdentifierClause is SecurityContextKeyIdentifierClause sctSkiClause) |
| | | 97 | | { |
| | 0 | 98 | | token = _tokenCache.GetContext(sctSkiClause.ContextId, sctSkiClause.Generation); |
| | | 99 | | } |
| | | 100 | | else |
| | | 101 | | { |
| | 0 | 102 | | token = null; |
| | | 103 | | } |
| | 0 | 104 | | return (token != null); |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | protected override bool TryResolveSecurityKeyCore(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityK |
| | | 108 | | { |
| | 0 | 109 | | if (TryResolveTokenCore(keyIdentifierClause, out SecurityToken sct)) |
| | | 110 | | { |
| | 0 | 111 | | key = ((SecurityContextSecurityToken)sct).SecurityKeys[0]; |
| | 0 | 112 | | return true; |
| | | 113 | | } |
| | | 114 | | else |
| | | 115 | | { |
| | 0 | 116 | | key = null; |
| | 0 | 117 | | return false; |
| | | 118 | | } |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | protected override bool TryResolveTokenCore(SecurityKeyIdentifier keyIdentifier, out SecurityToken token) |
| | | 122 | | { |
| | 0 | 123 | | if (keyIdentifier.TryFind<SecurityContextKeyIdentifierClause>(out SecurityContextKeyIdentifierClause sctSkiC |
| | | 124 | | { |
| | 0 | 125 | | return TryResolveToken(sctSkiClause, out token); |
| | | 126 | | } |
| | | 127 | | else |
| | | 128 | | { |
| | 0 | 129 | | token = null; |
| | 0 | 130 | | return false; |
| | | 131 | | } |
| | | 132 | | } |
| | | 133 | | } |
| | | 134 | | } |