| | | 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.Collections.Generic; |
| | | 5 | | using System.Collections.ObjectModel; |
| | | 6 | | using System.Security.Claims; |
| | | 7 | | using System.Security.Principal; |
| | | 8 | | using CoreWCF.Channels; |
| | | 9 | | using CoreWCF.IdentityModel.Policy; |
| | | 10 | | using CoreWCF.Security; |
| | | 11 | | using Claim = CoreWCF.IdentityModel.Claims.Claim; |
| | | 12 | | |
| | | 13 | | namespace CoreWCF |
| | | 14 | | { |
| | | 15 | | public class ServiceSecurityContext |
| | | 16 | | { |
| | | 17 | | private static ServiceSecurityContext s_anonymous; |
| | | 18 | | private AuthorizationContext _authorizationContext; |
| | | 19 | | private IIdentity _primaryIdentity; |
| | | 20 | | private Claim _identityClaim; |
| | | 21 | | private WindowsIdentity _windowsIdentity; |
| | | 22 | | |
| | | 23 | | // Perf: delay created authorizationContext using forward chain. |
| | 87 | 24 | | public ServiceSecurityContext(ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies) |
| | | 25 | | { |
| | 87 | 26 | | _authorizationContext = null; |
| | 87 | 27 | | AuthorizationPolicies = authorizationPolicies ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumen |
| | 87 | 28 | | } |
| | | 29 | | |
| | | 30 | | public ServiceSecurityContext(AuthorizationContext authorizationContext) |
| | 56 | 31 | | : this(authorizationContext, EmptyReadOnlyCollection<IAuthorizationPolicy>.Instance) |
| | | 32 | | { |
| | 56 | 33 | | } |
| | | 34 | | |
| | 58 | 35 | | public ServiceSecurityContext(AuthorizationContext authorizationContext, ReadOnlyCollection<IAuthorizationPolicy |
| | | 36 | | { |
| | 58 | 37 | | _authorizationContext = authorizationContext ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument |
| | 58 | 38 | | AuthorizationPolicies = authorizationPolicies ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumen |
| | 58 | 39 | | } |
| | | 40 | | |
| | | 41 | | public static ServiceSecurityContext Anonymous |
| | | 42 | | { |
| | | 43 | | get |
| | | 44 | | { |
| | 206 | 45 | | if (s_anonymous == null) |
| | | 46 | | { |
| | 4 | 47 | | s_anonymous = new ServiceSecurityContext(EmptyReadOnlyCollection<IAuthorizationPolicy>.Instance); |
| | | 48 | | } |
| | 206 | 49 | | return s_anonymous; |
| | | 50 | | } |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | public static ServiceSecurityContext Current |
| | | 54 | | { |
| | | 55 | | get |
| | | 56 | | { |
| | 0 | 57 | | ServiceSecurityContext result = null; |
| | | 58 | | |
| | 0 | 59 | | OperationContext operationContext = OperationContext.Current; |
| | 0 | 60 | | if (operationContext != null) |
| | | 61 | | { |
| | 0 | 62 | | MessageProperties properties = operationContext.IncomingMessageProperties; |
| | 0 | 63 | | if (properties != null) |
| | | 64 | | { |
| | 0 | 65 | | SecurityMessageProperty security = properties.Security; |
| | 0 | 66 | | if (security != null) |
| | | 67 | | { |
| | 0 | 68 | | result = security.ServiceSecurityContext; |
| | | 69 | | } |
| | | 70 | | } |
| | | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | return result; |
| | | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | public bool IsAnonymous |
| | | 78 | | { |
| | | 79 | | get |
| | | 80 | | { |
| | 0 | 81 | | return this == Anonymous || IdentityClaim == null; |
| | | 82 | | } |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | // TODO: Claim is from IdentityModel but I needed to expose it. This needs to be resolved. |
| | | 86 | | public Claim IdentityClaim |
| | | 87 | | { |
| | | 88 | | get |
| | | 89 | | { |
| | 0 | 90 | | if (_identityClaim == null) |
| | | 91 | | { |
| | 0 | 92 | | _identityClaim = SecurityUtils.GetPrimaryIdentityClaim(AuthorizationContext); |
| | | 93 | | } |
| | 0 | 94 | | return _identityClaim; |
| | | 95 | | } |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | public IIdentity PrimaryIdentity |
| | | 99 | | { |
| | | 100 | | get |
| | | 101 | | { |
| | 228 | 102 | | if (_primaryIdentity == null) |
| | | 103 | | { |
| | 76 | 104 | | IIdentity primaryIdentity = null; |
| | 76 | 105 | | IList<IIdentity> identities = GetIdentities(); |
| | | 106 | | // Multiple Identities is treated as anonymous |
| | 76 | 107 | | if (identities != null && identities.Count == 1) |
| | | 108 | | { |
| | 17 | 109 | | primaryIdentity = identities[0]; |
| | | 110 | | } |
| | | 111 | | |
| | 76 | 112 | | _primaryIdentity = primaryIdentity ?? SecurityUtils.AnonymousIdentity; |
| | | 113 | | } |
| | 228 | 114 | | return _primaryIdentity; |
| | | 115 | | } |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | public WindowsIdentity WindowsIdentity |
| | | 119 | | { |
| | | 120 | | get |
| | | 121 | | { |
| | 0 | 122 | | if (_windowsIdentity == null) |
| | | 123 | | { |
| | 0 | 124 | | WindowsIdentity windowsIdentity = null; |
| | 0 | 125 | | IList<IIdentity> identities = GetIdentities(); |
| | 0 | 126 | | if (identities != null) |
| | | 127 | | { |
| | 0 | 128 | | for (int i = 0; i < identities.Count; ++i) |
| | | 129 | | { |
| | 0 | 130 | | if (identities[i] is WindowsIdentity identity) |
| | | 131 | | { |
| | | 132 | | // Multiple Identities is treated as anonymous |
| | 0 | 133 | | if (windowsIdentity != null) |
| | | 134 | | { |
| | 0 | 135 | | windowsIdentity = WindowsIdentity.GetAnonymous(); |
| | 0 | 136 | | break; |
| | | 137 | | } |
| | 0 | 138 | | windowsIdentity = identity; |
| | | 139 | | } |
| | | 140 | | } |
| | | 141 | | } |
| | | 142 | | |
| | 0 | 143 | | _windowsIdentity = windowsIdentity ?? WindowsIdentity.GetAnonymous(); |
| | | 144 | | } |
| | 0 | 145 | | return _windowsIdentity; |
| | | 146 | | } |
| | | 147 | | } |
| | | 148 | | |
| | 245 | 149 | | internal ReadOnlyCollection<IAuthorizationPolicy> AuthorizationPolicies { get; set; } |
| | | 150 | | |
| | | 151 | | public AuthorizationContext AuthorizationContext |
| | | 152 | | { |
| | | 153 | | get |
| | | 154 | | { |
| | 186 | 155 | | if (_authorizationContext == null) |
| | | 156 | | { |
| | 63 | 157 | | _authorizationContext = AuthorizationContext.CreateDefaultAuthorizationContext(AuthorizationPolicies |
| | | 158 | | } |
| | 186 | 159 | | return _authorizationContext; |
| | | 160 | | } |
| | | 161 | | } |
| | | 162 | | |
| | | 163 | | public IList<IIdentity> GetIdentities() |
| | | 164 | | { |
| | 78 | 165 | | AuthorizationContext authContext = AuthorizationContext; |
| | 78 | 166 | | if (authContext != null && authContext.Properties.TryGetValue(SecurityUtils.Identities, out object identitie |
| | | 167 | | { |
| | 46 | 168 | | return identities as IList<IIdentity>; |
| | | 169 | | } |
| | 32 | 170 | | return null; |
| | | 171 | | } |
| | | 172 | | } |
| | | 173 | | } |