| | | 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.Security.Principal; |
| | | 7 | | using CoreWCF.Security; |
| | | 8 | | |
| | | 9 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 10 | | { |
| | | 11 | | public class WindowsSecurityToken : SecurityToken, IDisposable |
| | | 12 | | { |
| | | 13 | | private string _id; |
| | | 14 | | private DateTime _effectiveTime; |
| | | 15 | | private DateTime _expirationTime; |
| | | 16 | | private WindowsIdentity _windowsIdentity; |
| | | 17 | | private bool _disposed = false; |
| | | 18 | | |
| | | 19 | | public WindowsSecurityToken(WindowsIdentity windowsIdentity) |
| | 0 | 20 | | : this(windowsIdentity, SecurityUniqueId.Create().Value) |
| | | 21 | | { |
| | 0 | 22 | | } |
| | | 23 | | |
| | | 24 | | public WindowsSecurityToken(WindowsIdentity windowsIdentity, string id) |
| | 0 | 25 | | : this(windowsIdentity, id, null) |
| | | 26 | | { |
| | 0 | 27 | | } |
| | | 28 | | |
| | 0 | 29 | | public WindowsSecurityToken(WindowsIdentity windowsIdentity, string id, string authenticationType) |
| | | 30 | | { |
| | 0 | 31 | | DateTime effectiveTime = DateTime.UtcNow; |
| | 0 | 32 | | Initialize(id, authenticationType, effectiveTime, DateTime.UtcNow.AddHours(10), windowsIdentity, true); |
| | 0 | 33 | | } |
| | | 34 | | |
| | 0 | 35 | | protected WindowsSecurityToken() |
| | | 36 | | { |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | protected void Initialize(string id, DateTime effectiveTime, DateTime expirationTime, WindowsIdentity windowsIde |
| | | 40 | | { |
| | 0 | 41 | | Initialize(id, null, effectiveTime, expirationTime, windowsIdentity, clone); |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | protected void Initialize(string id, string authenticationType, DateTime effectiveTime, DateTime expirationTime, |
| | | 45 | | { |
| | 0 | 46 | | if (windowsIdentity == null) |
| | | 47 | | { |
| | 0 | 48 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(windowsIdentity)); |
| | | 49 | | } |
| | | 50 | | |
| | 0 | 51 | | _id = id ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(id)); |
| | 0 | 52 | | AuthenticationType = authenticationType; |
| | 0 | 53 | | _effectiveTime = effectiveTime; |
| | 0 | 54 | | _expirationTime = expirationTime; |
| | 0 | 55 | | _windowsIdentity = clone ? SecurityUtils.CloneWindowsIdentityIfNecessary(windowsIdentity, authenticationType |
| | 0 | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | public override string Id => _id; |
| | | 59 | | |
| | 0 | 60 | | public string AuthenticationType { get; private set; } |
| | | 61 | | |
| | 0 | 62 | | public override DateTime ValidFrom => _effectiveTime; |
| | | 63 | | |
| | 0 | 64 | | public override DateTime ValidTo => _expirationTime; |
| | | 65 | | |
| | | 66 | | public virtual WindowsIdentity WindowsIdentity |
| | | 67 | | { |
| | | 68 | | get |
| | | 69 | | { |
| | 0 | 70 | | ThrowIfDisposed(); |
| | 0 | 71 | | return _windowsIdentity; |
| | | 72 | | } |
| | | 73 | | } |
| | | 74 | | |
| | 0 | 75 | | public override ReadOnlyCollection<SecurityKey> SecurityKeys => EmptyReadOnlyCollection<SecurityKey>.Instance; |
| | | 76 | | |
| | | 77 | | public virtual void Dispose() |
| | | 78 | | { |
| | 0 | 79 | | if (!_disposed) |
| | | 80 | | { |
| | 0 | 81 | | _disposed = true; |
| | 0 | 82 | | if (_windowsIdentity != null) |
| | | 83 | | { |
| | 0 | 84 | | _windowsIdentity.Dispose(); |
| | 0 | 85 | | _windowsIdentity = null; |
| | | 86 | | } |
| | | 87 | | } |
| | 0 | 88 | | } |
| | | 89 | | |
| | | 90 | | protected void ThrowIfDisposed() |
| | | 91 | | { |
| | 0 | 92 | | if (_disposed) |
| | | 93 | | { |
| | 0 | 94 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ObjectDisposedException(GetType().FullName |
| | | 95 | | } |
| | 0 | 96 | | } |
| | | 97 | | } |
| | | 98 | | } |